diff options
author | David Hows <howsdav@gmail.com> | 2016-01-12 15:52:10 +1100 |
---|---|---|
committer | David Hows <howsdav@gmail.com> | 2016-01-13 10:54:17 +1100 |
commit | 312a3722a7e3774489f2b303b03e3c0d882dcc59 (patch) | |
tree | 59b393effa70970c19a869dca06f84dba6c9edc6 /bench | |
parent | e35c23e539095f6a1c39f10cba07ec6da52cd56c (diff) | |
download | mongo-312a3722a7e3774489f2b303b03e3c0d882dcc59.tar.gz |
WT-2326 - Add dcalloc, dmalloc and dstrdup to wtperf and replace all existing malloc, calloc and strncpy calls
Diffstat (limited to 'bench')
-rw-r--r-- | bench/wtperf/config.c | 67 | ||||
-rw-r--r-- | bench/wtperf/misc.c | 18 | ||||
-rw-r--r-- | bench/wtperf/wtperf.c | 108 | ||||
-rw-r--r-- | bench/wtperf/wtperf.h | 52 | ||||
-rw-r--r-- | bench/wtperf/wtperf_truncate.c | 29 |
5 files changed, 99 insertions, 175 deletions
diff --git a/bench/wtperf/config.c b/bench/wtperf/config.c index 8cb360bed29..306ea5bed14 100644 --- a/bench/wtperf/config.c +++ b/bench/wtperf/config.c @@ -62,9 +62,7 @@ config_assign(CONFIG *dest, const CONFIG *src) memcpy(dest, src, sizeof(CONFIG)); if (src->uris != NULL) { - dest->uris = calloc(src->table_count, sizeof(char *)); - if (dest->uris == NULL) - return (enomem(dest)); + dest->uris = dcalloc(src->table_count, sizeof(char *)); for (i = 0; i < src->table_count; i++) dest->uris[i] = strdup(src->uris[i]); } @@ -75,9 +73,7 @@ config_assign(CONFIG *dest, const CONFIG *src) if (src->base_uri != NULL) dest->base_uri = strdup(src->base_uri); if (src->workload != NULL) { - dest->workload = calloc(WORKLOAD_MAX, sizeof(WORKLOAD)); - if (dest->workload == NULL) - return (enomem(dest)); + dest->workload = dcalloc(WORKLOAD_MAX, sizeof(WORKLOAD)); memcpy(dest->workload, src->workload, WORKLOAD_MAX * sizeof(WORKLOAD)); } @@ -89,9 +85,8 @@ config_assign(CONFIG *dest, const CONFIG *src) ((u_char *)dest + config_opts[i].offset); if (*pstr != NULL) { len = strlen(*pstr) + 1; - if ((newstr = malloc(len)) == NULL) - return (enomem(src)); - strncpy(newstr, *pstr, len); + newstr = dmalloc(len); + newstr = dstrdup(*pstr); *pstr = newstr; } } @@ -102,11 +97,9 @@ config_assign(CONFIG *dest, const CONFIG *src) /* Clone the config string information into the new cfg object */ TAILQ_FOREACH(conf_line, &src->config_head, c) { len = strlen(conf_line->string); - if ((tmp_line = calloc(sizeof(CONFIG_QUEUE_ENTRY), 1)) == NULL) - return (enomem(src)); - if ((tmp_line->string = calloc(len + 1, 1)) == NULL) - return (enomem(src)); - strncpy(tmp_line->string, conf_line->string, len); + tmp_line = dcalloc(sizeof(CONFIG_QUEUE_ENTRY), 1); + tmp_line->string = dcalloc(len + 1, 1); + tmp_line->string = dstrdup(tmp_line->string); TAILQ_INSERT_TAIL(&dest->config_head, tmp_line, c); } return (0); @@ -212,8 +205,7 @@ config_threads(CONFIG *cfg, const char *config, size_t len) cfg->workers_cnt = 0; } /* Allocate the workload array. */ - if ((cfg->workload = calloc(WORKLOAD_MAX, sizeof(WORKLOAD))) == NULL) - return (enomem(cfg)); + cfg->workload = dcalloc(WORKLOAD_MAX, sizeof(WORKLOAD)); cfg->workload_cnt = 0; /* @@ -426,13 +418,11 @@ config_opt(CONFIG *cfg, WT_CONFIG_ITEM *k, WT_CONFIG_ITEM *v) strp = (char **)valueloc; newlen = v->len + 1; if (*strp == NULL) { - if ((newstr = calloc(newlen, sizeof(char))) == NULL) - return (enomem(cfg)); - strncpy(newstr, v->str, v->len); + newstr = dcalloc(newlen, sizeof(char)); + newstr = dstrdup(v->str); } else { newlen += (strlen(*strp) + 1); - if ((newstr = calloc(newlen, sizeof(char))) == NULL) - return (enomem(cfg)); + newstr = dcalloc(newlen, sizeof(char)); snprintf(newstr, newlen, "%s,%*s", *strp, (int)v->len, v->str); /* Free the old value now we've copied it. */ @@ -457,10 +447,8 @@ config_opt(CONFIG *cfg, WT_CONFIG_ITEM *k, WT_CONFIG_ITEM *v) } strp = (char **)valueloc; free(*strp); - if ((newstr = malloc(v->len + 1)) == NULL) - return (enomem(cfg)); - strncpy(newstr, v->str, v->len); - newstr[v->len] = '\0'; + newstr = dcalloc(v->len + 1, 1); + newstr = dstrdup(v->str); *strp = newstr; break; } @@ -495,11 +483,7 @@ config_opt_file(CONFIG *cfg, const char *filename) goto err; } buf_size = (size_t)sb.st_size; - file_buf = calloc(buf_size + 2, 1); - if (file_buf == NULL) { - ret = ENOMEM; - goto err; - } + file_buf = dcalloc(buf_size + 2, 1); read_size = read(fd, file_buf, buf_size); if (read_size == -1 #ifndef _WIN32 @@ -595,7 +579,6 @@ config_opt_line(CONFIG *cfg, const char *optstr) WT_CONFIG_PARSER *scan; size_t len; int ret, t_ret; - char *string_copy; len = strlen(optstr); if ((ret = wiredtiger_config_parser_open( @@ -610,14 +593,9 @@ config_opt_line(CONFIG *cfg, const char *optstr) * any parsed from the original config. We allocate len + 1 to allow for * a null byte to be added. */ - if ((string_copy = calloc(len + 1, 1)) == NULL) - return (enomem(cfg)); - - strncpy(string_copy, optstr, len); - if ((config_line = calloc(sizeof(CONFIG_QUEUE_ENTRY), 1)) == NULL) - return (enomem(cfg)); - - config_line->string = string_copy; + config_line = dcalloc(sizeof(CONFIG_QUEUE_ENTRY), 1); + config_line->string = dcalloc(len + 1, 1); + config_line->string = dstrdup(optstr); TAILQ_INSERT_TAIL(&cfg->config_head, config_line, c); while (ret == 0) { @@ -649,8 +627,7 @@ config_opt_str(CONFIG *cfg, const char *name, const char *value) char *optstr; /* name="value" */ - if ((optstr = malloc(strlen(name) + strlen(value) + 4)) == NULL) - return (enomem(cfg)); + optstr = dmalloc(strlen(name) + strlen(value) + 4); sprintf(optstr, "%s=\"%s\"", name, value); ret = config_opt_line(cfg, optstr); free(optstr); @@ -749,12 +726,8 @@ config_to_file(CONFIG *cfg) fp = NULL; /* Backup the config */ - req_len = strlen(cfg->home) + strlen("/CONFIG.wtperf") + 1; - if ((path = calloc(req_len, 1)) == NULL) { - (void)enomem(cfg); - goto err; - } - + req_len = strlen(cfg->home) + strlen("/CONFIG.wtperf") + 1 + path = dcalloc(req_len, 1); snprintf(path, req_len, "%s/CONFIG.wtperf", cfg->home); if ((fp = fopen(path, "w")) == NULL) { lprintf(cfg, errno, 0, "%s", path); diff --git a/bench/wtperf/misc.c b/bench/wtperf/misc.c index f683c0cc239..93c903b5ab7 100644 --- a/bench/wtperf/misc.c +++ b/bench/wtperf/misc.c @@ -28,19 +28,6 @@ #include "wtperf.h" -int -enomem(const CONFIG *cfg) -{ - const char *msg; - - msg = "Unable to allocate memory"; - if (cfg->logf == NULL) - fprintf(stderr, "%s\n", msg); - else - lprintf(cfg, ENOMEM, 0, "%s", msg); - return (ENOMEM); -} - /* Setup the logging output mechanism. */ int setup_log_file(CONFIG *cfg) @@ -53,9 +40,8 @@ setup_log_file(CONFIG *cfg) if (cfg->verbose < 1) return (0); - if ((fname = calloc(strlen(cfg->monitor_dir) + - strlen(cfg->table_name) + strlen(".stat") + 2, 1)) == NULL) - return (enomem(cfg)); + fname = calloc(strlen(cfg->monitor_dir) + + strlen(cfg->table_name) + strlen(".stat") + 2, 1); sprintf(fname, "%s/%s.stat", cfg->monitor_dir, cfg->table_name); cfg->logf = fopen(fname, "w"); diff --git a/bench/wtperf/wtperf.c b/bench/wtperf/wtperf.c index b004d59c52c..0b37c49a2dc 100644 --- a/bench/wtperf/wtperf.c +++ b/bench/wtperf/wtperf.c @@ -452,12 +452,7 @@ worker(void *arg) lprintf(cfg, ret, 0, "worker: WT_CONNECTION.open_session"); goto err; } - cursors = calloc(cfg->table_count, sizeof(WT_CURSOR *)); - if (cursors == NULL) { - lprintf(cfg, ENOMEM, 0, - "worker: couldn't allocate cursor array"); - goto err; - } + cursors = dcalloc(cfg->table_count, sizeof(WT_CURSOR *)); for (i = 0; i < cfg->table_count_idle; i++) { snprintf(buf, 512, "%s_idle%05d", cfg->uris[0], (int)i); if ((ret = session->open_cursor( @@ -917,12 +912,7 @@ populate_thread(void *arg) cursor_config = (cfg->populate_threads == 1 && !cfg->index) ? "bulk" : NULL; /* Create the cursors. */ - cursors = calloc(cfg->table_count, sizeof(WT_CURSOR *)); - if (cursors == NULL) { - lprintf(cfg, ENOMEM, 0, - "worker: couldn't allocate cursor array"); - goto err; - } + cursors = dcalloc(cfg->table_count, sizeof(WT_CURSOR *)); for (i = 0; i < cfg->table_count; i++) { if ((ret = session->open_cursor( session, cfg->uris[i], NULL, @@ -1168,10 +1158,7 @@ monitor(void *arg) /* Open the logging file. */ len = strlen(cfg->monitor_dir) + 100; - if ((path = malloc(len)) == NULL) { - (void)enomem(cfg); - goto err; - } + path = dmalloc(len); snprintf(path, len, "%s/monitor", cfg->monitor_dir); if ((fp = fopen(path, "w")) == NULL) { lprintf(cfg, errno, 0, "%s", path); @@ -1392,9 +1379,7 @@ execute_populate(CONFIG *cfg) cfg->insert_key = 0; - if ((cfg->popthreads = - calloc(cfg->populate_threads, sizeof(CONFIG_THREAD))) == NULL) - return (enomem(cfg)); + cfg->popthreads = dcalloc(cfg->populate_threads, sizeof(CONFIG_THREAD)); if (cfg->use_asyncops > 0) { lprintf(cfg, 0, 1, "Starting %" PRIu32 " async thread(s)", cfg->async_threads); @@ -1587,11 +1572,7 @@ execute_workload(CONFIG *cfg) cfg->in_warmup = 1; /* Allocate memory for the worker threads. */ - if ((cfg->workers = - calloc((size_t)cfg->workers_cnt, sizeof(CONFIG_THREAD))) == NULL) { - ret = enomem(cfg); - goto err; - } + cfg->workers = dcalloc((size_t)cfg->workers_cnt, sizeof(CONFIG_THREAD)); if (cfg->use_asyncops > 0) { lprintf(cfg, 0, 1, "Starting %" PRIu32 " async thread(s)", @@ -1773,17 +1754,9 @@ create_uris(CONFIG *cfg) ret = 0; base_uri_len = strlen(cfg->base_uri); - cfg->uris = calloc(cfg->table_count, sizeof(char *)); - if (cfg->uris == NULL) { - ret = ENOMEM; - goto err; - } + cfg->uris = dcalloc(cfg->table_count, sizeof(char *)); for (i = 0; i < cfg->table_count; i++) { - uri = cfg->uris[i] = calloc(base_uri_len + 5, 1); - if (uri == NULL) { - ret = ENOMEM; - goto err; - } + uri = cfg->uris[i] = dcalloc(base_uri_len + 5, 1); /* * If there is only one table, just use base name. */ @@ -1880,40 +1853,22 @@ start_all_runs(CONFIG *cfg) return (start_run(cfg)); /* Allocate an array to hold our config struct copies. */ - configs = calloc(cfg->database_count, sizeof(CONFIG *)); - if (configs == NULL) - return (ENOMEM); + configs = dcalloc(cfg->database_count, sizeof(CONFIG *)); /* Allocate an array to hold our thread IDs. */ - threads = calloc(cfg->database_count, sizeof(pthread_t)); - if (threads == NULL) { - ret = ENOMEM; - goto err; - } + threads = dcalloc(cfg->database_count, sizeof(pthread_t)); home_len = strlen(cfg->home); cmd_len = (home_len * 2) + 30; /* Add some slop. */ - cmd_buf = calloc(cmd_len, 1); - if (cmd_buf == NULL) { - ret = ENOMEM; - goto err; - } + cmd_buf = dcalloc(cmd_len, 1); for (i = 0; i < cfg->database_count; i++) { - next_cfg = calloc(1, sizeof(CONFIG)); - if (next_cfg == NULL) { - ret = ENOMEM; - goto err; - } + next_cfg = dcalloc(1, sizeof(CONFIG)); configs[i] = next_cfg; if ((ret = config_assign(next_cfg, cfg)) != 0) goto err; /* Setup a unique home directory for each database. */ - new_home = malloc(home_len + 5); - if (new_home == NULL) { - ret = ENOMEM; - goto err; - } + new_home = dmalloc(home_len + 5); snprintf(new_home, home_len + 5, "%s/D%02d", cfg->home, (int)i); next_cfg->home = new_home; @@ -2041,12 +1996,8 @@ start_run(CONFIG *cfg) lprintf(cfg, 0, 1, "Starting %" PRIu32 " checkpoint thread(s)", cfg->checkpoint_threads); - if ((cfg->ckptthreads = - calloc(cfg->checkpoint_threads, - sizeof(CONFIG_THREAD))) == NULL) { - ret = enomem(cfg); - goto err; - } + cfg->ckptthreads = dcalloc( + cfg->checkpoint_threads, sizeof(CONFIG_THREAD)); if (start_threads(cfg, NULL, cfg->ckptthreads, cfg->checkpoint_threads, checkpoint_worker) != 0) goto err; @@ -2255,10 +2206,7 @@ main(int argc, char *argv[]) * to 4096 if needed. */ req_len = strlen(",async=(enabled=true,threads=)") + 4; - if ((cfg->async_config = calloc(req_len, 1)) == NULL) { - ret = enomem(cfg); - goto err; - } + cfg->async_config = dcalloc(req_len, 1); snprintf(cfg->async_config, req_len, ",async=(enabled=true,threads=%d)", cfg->async_threads); @@ -2281,10 +2229,7 @@ main(int argc, char *argv[]) /* Build the URI from the table name. */ req_len = strlen("table:") + strlen(HELIUM_NAME) + strlen(cfg->table_name) + 2; - if ((cfg->base_uri = calloc(req_len, 1)) == NULL) { - ret = enomem(cfg); - goto err; - } + cfg->base_uri = dcalloc(req_len, 1); snprintf(cfg->base_uri, req_len, "table:%s%s%s", cfg->helium_mount == NULL ? "" : HELIUM_NAME, cfg->helium_mount == NULL ? "" : "/", @@ -2303,10 +2248,7 @@ main(int argc, char *argv[]) req_len += strlen(cfg->async_config); if (cfg->compress_ext != NULL) req_len += strlen(cfg->compress_ext); - if ((cc_buf = calloc(req_len, 1)) == NULL) { - ret = enomem(cfg); - goto err; - } + cc_buf = dcalloc(req_len, 1); /* * This is getting hard to parse. */ @@ -2331,10 +2273,7 @@ main(int argc, char *argv[]) req_len += strlen(cfg->compress_table); if (cfg->index) req_len += strlen(INDEX_COL_NAMES); - if ((tc_buf = calloc(req_len, 1)) == NULL) { - ret = enomem(cfg); - goto err; - } + tc_buf = dcalloc(req_len, 1); /* * This is getting hard to parse. */ @@ -2353,10 +2292,7 @@ main(int argc, char *argv[]) if (cfg->log_partial && cfg->table_count > 1) { req_len = strlen(cfg->table_config) + strlen(LOG_PARTIAL_CONFIG) + 1; - if ((cfg->partial_config = calloc(req_len, 1)) == NULL) { - ret = enomem(cfg); - goto err; - } + cfg->partial_config = dcalloc(req_len, 1); snprintf((char *)cfg->partial_config, req_len, "%s%s", (char *)cfg->table_config, LOG_PARTIAL_CONFIG); } @@ -2416,10 +2352,8 @@ start_threads(CONFIG *cfg, * don't, it's not enough memory to bother. These buffers hold * strings: trailing NUL is included in the size. */ - if ((thread->key_buf = calloc(cfg->key_sz, 1)) == NULL) - return (enomem(cfg)); - if ((thread->value_buf = calloc(cfg->value_sz, 1)) == NULL) - return (enomem(cfg)); + thread->key_buf = dcalloc(cfg->key_sz, 1); + thread->value_buf = dcalloc(cfg->value_sz, 1); /* * Initialize and then toss in a bit of random values if needed. */ diff --git a/bench/wtperf/wtperf.h b/bench/wtperf/wtperf.h index 8a9be7a0938..9351ca0285e 100644 --- a/bench/wtperf/wtperf.h +++ b/bench/wtperf/wtperf.h @@ -289,7 +289,6 @@ void latency_insert(CONFIG *, uint32_t *, uint32_t *, uint32_t *); void latency_read(CONFIG *, uint32_t *, uint32_t *, uint32_t *); void latency_update(CONFIG *, uint32_t *, uint32_t *, uint32_t *); void latency_print(CONFIG *); -int enomem(const CONFIG *); int run_truncate( CONFIG *, CONFIG_THREAD *, WT_CURSOR *, WT_SESSION *, int *); int setup_log_file(CONFIG *); @@ -323,4 +322,55 @@ extract_key(char *key_buf, uint64_t *keynop) sscanf(key_buf, "%" SCNu64, keynop); } +/* + * die -- + * Print message and exit on failure. + */ +static inline void +die(int e, const char *str) +{ + fprintf(stderr, "Call to %s failed: %s", str, wiredtiger_strerror(e)); + exit(EXIT_FAILURE); +} + +/* + * dmalloc -- + * Call malloc, dying on failure. + */ +static inline void * +dmalloc(size_t len) +{ + void *p; + + if ((p = malloc(len)) == NULL) + die(errno, "malloc"); + return (p); +} + +/* + * dstrdup -- + * Call strdup, dying on failure. + */ +static inline char * +dstrdup(const char *str) +{ + char *p; + + if ((p = strdup(str)) == NULL) + die(errno, "strdup"); + return (p); +} +/* + * dcalloc -- + * Call calloc, dying on failure. + */ +static inline void * +dcalloc(size_t num, size_t len) +{ + void *p; + + if ((p = calloc(len, num)) == NULL) + die(errno, "calloc"); + return (p); +} #endif diff --git a/bench/wtperf/wtperf_truncate.c b/bench/wtperf/wtperf_truncate.c index 7eecc81843e..53a40de6b5a 100644 --- a/bench/wtperf/wtperf_truncate.c +++ b/bench/wtperf/wtperf_truncate.c @@ -102,17 +102,9 @@ setup_truncate(CONFIG *cfg, CONFIG_THREAD *thread, WT_SESSION *session) { if (trunc_cfg->stone_gap != 0) { trunc_cfg->expected_total = (end_point - start_point); for (i = 1; i <= trunc_cfg->needed_stones; i++) { - truncate_key = calloc(cfg->key_sz, 1); - if (truncate_key == NULL) { - ret = enomem(cfg); - goto err; - } - truncate_item = calloc(sizeof(TRUNCATE_QUEUE_ENTRY), 1); - if (truncate_item == NULL) { - free(truncate_key); - ret = enomem(cfg); - goto err; - } + truncate_key = dcalloc(cfg->key_sz, 1); + truncate_item = + dcalloc(sizeof(TRUNCATE_QUEUE_ENTRY), 1); generate_key( cfg, truncate_key, trunc_cfg->stone_gap * i); truncate_item->key = truncate_key; @@ -178,19 +170,8 @@ run_truncate(CONFIG *cfg, CONFIG_THREAD *thread, while (trunc_cfg->num_stones < trunc_cfg->needed_stones) { trunc_cfg->last_key += used_stone_gap; - truncate_key = calloc(cfg->key_sz, 1); - if (truncate_key == NULL) { - lprintf(cfg, ENOMEM, 0, - "truncate: couldn't allocate key array"); - return (ENOMEM); - } - truncate_item = calloc(sizeof(TRUNCATE_QUEUE_ENTRY), 1); - if (truncate_item == NULL) { - free(truncate_key); - lprintf(cfg, ENOMEM, 0, - "truncate: couldn't allocate item"); - return (ENOMEM); - } + truncate_key = dcalloc(cfg->key_sz, 1); + truncate_item = dcalloc(sizeof(TRUNCATE_QUEUE_ENTRY), 1); generate_key(cfg, truncate_key, trunc_cfg->last_key); truncate_item->key = truncate_key; truncate_item->diff = used_stone_gap; |