From: Michael Wallner Date: Fri, 23 Oct 2020 11:47:42 +0000 (+0200) Subject: src/bin: apply clang-format X-Git-Tag: 1.1.0-beta1~173 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Flibmemcached;a=commitdiff_plain;h=7c2da91b9897c54f66e7fc634a675fcaeef32167 src/bin: apply clang-format --- diff --git a/src/bin/common/client_options.h b/src/bin/common/client_options.h index 16ce4bed..a3e38252 100644 --- a/src/bin/common/client_options.h +++ b/src/bin/common/client_options.h @@ -1,26 +1,30 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once typedef struct memcached_help_text_st memcached_help_text_st; enum memcached_options { - OPT_SERVERS= 's', - OPT_VERSION= 'V', - OPT_HELP= 'h', - OPT_VERBOSE= 'v', - OPT_DEBUG= 'd', - OPT_ANALYZE= 'a', - OPT_FLAG= 257, + OPT_SERVERS = 's', + OPT_VERSION = 'V', + OPT_HELP = 'h', + OPT_VERBOSE = 'v', + OPT_DEBUG = 'd', + OPT_ANALYZE = 'a', + OPT_FLAG = 257, OPT_EXPIRE, OPT_SET, OPT_REPLACE, @@ -41,5 +45,5 @@ enum memcached_options { OPT_STAT_ARGS, OPT_SERVER_VERSION, OPT_QUIET, - OPT_FILE= 'f' + OPT_FILE = 'f' }; diff --git a/src/bin/common/execute.cc b/src/bin/common/execute.cc index f0a5e47e..2e436af8 100644 --- a/src/bin/common/execute.cc +++ b/src/bin/common/execute.cc @@ -1,38 +1,31 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ - /* - Execute a memcached_set() a set of pairs. - Return the number of rows set. + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #include "mem_config.h" #include "execute.h" -unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of) -{ - uint32_t count= 0; - for (; count < number_of; ++count) - { - memcached_return_t rc= memcached_set(memc, pairs[count].key, pairs[count].key_length, - pairs[count].value, pairs[count].value_length, - 0, 0); - if (memcached_failed(rc)) - { - fprintf(stderr, "%s:%d Failure on %u insert (%s) of %.*s\n", - __FILE__, __LINE__, count, - memcached_last_error_message(memc), - (unsigned int)pairs[count].key_length, pairs[count].key); - +unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of) { + uint32_t count = 0; + for (; count < number_of; ++count) { + memcached_return_t rc = memcached_set(memc, pairs[count].key, pairs[count].key_length, + pairs[count].value, pairs[count].value_length, 0, 0); + if (memcached_failed(rc)) { + fprintf(stderr, "%s:%d Failure on %u insert (%s) of %.*s\n", __FILE__, __LINE__, count, + memcached_last_error_message(memc), (unsigned int) pairs[count].key_length, + pairs[count].key); + // We will try to reconnect and see if that fixes the issue memcached_quit(memc); @@ -47,32 +40,25 @@ unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int numbe Execute a memcached_get() on a set of pairs. Return the number of rows retrieved. */ -unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of) -{ +unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of) { unsigned int x; unsigned int retrieved; - - for (retrieved= 0,x= 0; x < number_of; x++) - { + for (retrieved = 0, x = 0; x < number_of; x++) { size_t value_length; uint32_t flags; - unsigned int fetch_key= (unsigned int)((unsigned int)random() % number_of); + unsigned int fetch_key = (unsigned int) ((unsigned int) random() % number_of); memcached_return_t rc; - char *value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length, - &value_length, &flags, &rc); - - if (memcached_failed(rc)) - { - fprintf(stderr, "%s:%d Failure on read(%s) of %.*s\n", - __FILE__, __LINE__, - memcached_last_error_message(memc), - (unsigned int)pairs[fetch_key].key_length, pairs[fetch_key].key); - } - else - { + char *value = memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length, + &value_length, &flags, &rc); + + if (memcached_failed(rc)) { + fprintf(stderr, "%s:%d Failure on read(%s) of %.*s\n", __FILE__, __LINE__, + memcached_last_error_message(memc), (unsigned int) pairs[fetch_key].key_length, + pairs[fetch_key].key); + } else { retrieved++; } @@ -85,14 +71,12 @@ unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int numbe /** * Callback function to count the number of results */ -static memcached_return_t callback_counter(const memcached_st *ptr, - memcached_result_st *result, - void *context) -{ - (void)ptr; - (void)result; - unsigned int *counter= (unsigned int *)context; - *counter= *counter + 1; +static memcached_return_t callback_counter(const memcached_st *ptr, memcached_result_st *result, + void *context) { + (void) ptr; + (void) result; + unsigned int *counter = (unsigned int *) context; + *counter = *counter + 1; return MEMCACHED_SUCCESS; } @@ -105,34 +89,25 @@ static memcached_return_t callback_counter(const memcached_st *ptr, * @param number_of the number of keys to try to get * @return the number of keys received */ -unsigned int execute_mget(memcached_st *memc, - const char * const *keys, - size_t *key_length, - unsigned int number_of) -{ - unsigned int retrieved= 0; - memcached_execute_fn callbacks[]= { callback_counter }; +unsigned int execute_mget(memcached_st *memc, const char *const *keys, size_t *key_length, + unsigned int number_of) { + unsigned int retrieved = 0; + memcached_execute_fn callbacks[] = {callback_counter}; memcached_return_t rc; - rc= memcached_mget_execute(memc, keys, key_length, - (size_t)number_of, callbacks, &retrieved, 1); + rc = memcached_mget_execute(memc, keys, key_length, (size_t) number_of, callbacks, &retrieved, 1); - if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND || - rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END) + if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_BUFFERED + || rc == MEMCACHED_END) { - rc= memcached_fetch_execute(memc, callbacks, (void *)&retrieved, 1); - if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END) - { - fprintf(stderr, "%s:%d Failed to execute mget: %s\n", - __FILE__, __LINE__, + rc = memcached_fetch_execute(memc, callbacks, (void *) &retrieved, 1); + if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END) { + fprintf(stderr, "%s:%d Failed to execute mget: %s\n", __FILE__, __LINE__, memcached_strerror(memc, rc)); memcached_quit(memc); return 0; } - } - else - { - fprintf(stderr, "%s:%d Failed to execute mget: %s\n", - __FILE__, __LINE__, + } else { + fprintf(stderr, "%s:%d Failed to execute mget: %s\n", __FILE__, __LINE__, memcached_strerror(memc, rc)); memcached_quit(memc); return 0; diff --git a/src/bin/common/execute.h b/src/bin/common/execute.h index c044c669..9b08d8ad 100644 --- a/src/bin/common/execute.h +++ b/src/bin/common/execute.h @@ -1,15 +1,19 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -#pragma once +#pragma once #include @@ -22,7 +26,7 @@ extern "C" { unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of); unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of); -unsigned int execute_mget(memcached_st *memc, const char * const *keys, size_t *key_length, +unsigned int execute_mget(memcached_st *memc, const char *const *keys, size_t *key_length, unsigned int number_of); #ifdef __cplusplus diff --git a/src/bin/common/generator.cc b/src/bin/common/generator.cc index 3de895d0..6b4c3f9f 100644 --- a/src/bin/common/generator.cc +++ b/src/bin/common/generator.cc @@ -1,14 +1,17 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -25,40 +28,32 @@ #define KEY_BYTES 20 /* Use this for string generation */ -static const char ALPHANUMERICS[]= - "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz"; +static const char ALPHANUMERICS[] = "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz"; -#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1) +#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS) - 1) -static size_t get_alpha_num(void) -{ - return (size_t)random() % ALPHANUMERICS_SIZE; +static size_t get_alpha_num(void) { + return (size_t) random() % ALPHANUMERICS_SIZE; } -void get_random_string(char *buffer, size_t size) -{ - char *buffer_ptr= buffer; +void get_random_string(char *buffer, size_t size) { + char *buffer_ptr = buffer; - while (--size) - { - *buffer_ptr++= ALPHANUMERICS[get_alpha_num()]; + while (--size) { + *buffer_ptr++ = ALPHANUMERICS[get_alpha_num()]; } - *buffer_ptr++= ALPHANUMERICS[get_alpha_num()]; + *buffer_ptr++ = ALPHANUMERICS[get_alpha_num()]; } -void pairs_free(pairs_st *pairs) -{ - if (pairs == NULL) - { +void pairs_free(pairs_st *pairs) { + if (pairs == NULL) { return; } /* We free until we hit the null pair we stores during creation */ - for (uint32_t x= 0; pairs[x].key; x++) - { + for (uint32_t x = 0; pairs[x].key; x++) { free(pairs[x].key); - if (pairs[x].value) - { + if (pairs[x].value) { free(pairs[x].value); } } @@ -66,39 +61,33 @@ void pairs_free(pairs_st *pairs) free(pairs); } -pairs_st *pairs_generate(uint64_t number_of, size_t value_length) -{ - pairs_st *pairs= (pairs_st*)calloc((size_t)number_of + 1, sizeof(pairs_st)); +pairs_st *pairs_generate(uint64_t number_of, size_t value_length) { + pairs_st *pairs = (pairs_st *) calloc((size_t) number_of + 1, sizeof(pairs_st)); - if (pairs == NULL) - { + if (pairs == NULL) { goto error; } - for (uint64_t x= 0; x < number_of; x++) - { - pairs[x].key= (char *)calloc(KEY_BYTES, sizeof(char)); + for (uint64_t x = 0; x < number_of; x++) { + pairs[x].key = (char *) calloc(KEY_BYTES, sizeof(char)); if (pairs[x].key == NULL) goto error; get_random_string(pairs[x].key, KEY_BYTES); - pairs[x].key_length= KEY_BYTES; + pairs[x].key_length = KEY_BYTES; - if (value_length) - { - pairs[x].value= (char *)calloc(value_length, sizeof(char)); + if (value_length) { + pairs[x].value = (char *) calloc(value_length, sizeof(char)); if (pairs[x].value == NULL) goto error; get_random_string(pairs[x].value, value_length); - pairs[x].value_length= value_length; - } - else - { - pairs[x].value= NULL; - pairs[x].value_length= 0; + pairs[x].value_length = value_length; + } else { + pairs[x].value = NULL; + pairs[x].value_length = 0; } } diff --git a/src/bin/common/generator.h b/src/bin/common/generator.h index e60bfb38..b6758d5a 100644 --- a/src/bin/common/generator.h +++ b/src/bin/common/generator.h @@ -1,16 +1,16 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ - /* - Code to generate data to be pushed into memcached + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ */ #pragma once diff --git a/src/bin/common/utilities.cc b/src/bin/common/utilities.cc index c1f34b8e..0e21d709 100644 --- a/src/bin/common/utilities.cc +++ b/src/bin/common/utilities.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include "utilities.h" @@ -23,90 +27,75 @@ #include #include - -long int timedif(struct timeval a, struct timeval b) -{ +long int timedif(struct timeval a, struct timeval b) { long us, s; - us = (int)(a.tv_usec - b.tv_usec); + us = (int) (a.tv_usec - b.tv_usec); us /= 1000; - s = (int)(a.tv_sec - b.tv_sec); + s = (int) (a.tv_sec - b.tv_sec); s *= 1000; return s + us; } -void version_command(const char *command_name) -{ +void version_command(const char *command_name) { printf("%s v%u.%u\n", command_name, 1U, 0U); exit(EXIT_SUCCESS); } -void close_stdio(void) -{ +void close_stdio(void) { int fd; - if ((fd = open("/dev/null", O_RDWR, 0)) < 0) - { + if ((fd = open("/dev/null", O_RDWR, 0)) < 0) { return; - } - else - { - if (dup2(fd, STDIN_FILENO) < 0) - { + } else { + if (dup2(fd, STDIN_FILENO) < 0) { return; } - if (dup2(fd, STDOUT_FILENO) < 0) - { + if (dup2(fd, STDOUT_FILENO) < 0) { return; } - if (dup2(fd, STDERR_FILENO) < 0) - { + if (dup2(fd, STDERR_FILENO) < 0) { return; } - if (fd > STDERR_FILENO) - { + if (fd > STDERR_FILENO) { close(fd); } } } - -static const char *lookup_help(memcached_options option) -{ - switch (option) - { - case OPT_SERVERS: return("List which servers you wish to connect to."); - case OPT_VERSION: return("Display the version of the application and then exit."); - case OPT_HELP: return("Display this message and then exit."); - case OPT_VERBOSE: return("Give more details on the progression of the application."); - case OPT_QUIET: return("stderr and stdin will be closed at application startup."); - case OPT_DEBUG: return("Provide output only useful for debugging."); - case OPT_FLAG: return("Provide flag information for storage operation."); - case OPT_EXPIRE: return("Set the expire option for the object."); - case OPT_SET: return("Use set command with memcached when storing."); - case OPT_REPLACE: return("Use replace command with memcached when storing."); - case OPT_ADD: return("Use add command with memcached when storing."); - case OPT_SLAP_EXECUTE_NUMBER: return("Number of times to execute the given test."); - case OPT_SLAP_INITIAL_LOAD: return("Number of key pairs to load before executing tests."); - case OPT_SLAP_TEST: return("Test to run (currently \"get\" or \"set\")."); - case OPT_SLAP_CONCURRENCY: return("Number of users to simulate with load."); - case OPT_SLAP_NON_BLOCK: return("Set TCP up to use non-blocking IO."); - case OPT_SLAP_TCP_NODELAY: return("Set TCP socket up to use nodelay."); - case OPT_FLUSH: return("Flush servers before running tests."); - case OPT_HASH: return("Select hash type."); - case OPT_BINARY: return("Switch to binary protocol."); - case OPT_ANALYZE: return("Analyze the provided servers."); - case OPT_UDP: return("Use UDP protocol when communicating with server."); - case OPT_BUFFER: return("Enable request buffering."); +static const char *lookup_help(memcached_options option) { + switch (option) { + case OPT_SERVERS: return ("List which servers you wish to connect to."); + case OPT_VERSION: return ("Display the version of the application and then exit."); + case OPT_HELP: return ("Display this message and then exit."); + case OPT_VERBOSE: return ("Give more details on the progression of the application."); + case OPT_QUIET: return ("stderr and stdin will be closed at application startup."); + case OPT_DEBUG: return ("Provide output only useful for debugging."); + case OPT_FLAG: return ("Provide flag information for storage operation."); + case OPT_EXPIRE: return ("Set the expire option for the object."); + case OPT_SET: return ("Use set command with memcached when storing."); + case OPT_REPLACE: return ("Use replace command with memcached when storing."); + case OPT_ADD: return ("Use add command with memcached when storing."); + case OPT_SLAP_EXECUTE_NUMBER: return ("Number of times to execute the given test."); + case OPT_SLAP_INITIAL_LOAD: return ("Number of key pairs to load before executing tests."); + case OPT_SLAP_TEST: return ("Test to run (currently \"get\" or \"set\")."); + case OPT_SLAP_CONCURRENCY: return ("Number of users to simulate with load."); + case OPT_SLAP_NON_BLOCK: return ("Set TCP up to use non-blocking IO."); + case OPT_SLAP_TCP_NODELAY: return ("Set TCP socket up to use nodelay."); + case OPT_FLUSH: return ("Flush servers before running tests."); + case OPT_HASH: return ("Select hash type."); + case OPT_BINARY: return ("Switch to binary protocol."); + case OPT_ANALYZE: return ("Analyze the provided servers."); + case OPT_UDP: return ("Use UDP protocol when communicating with server."); + case OPT_BUFFER: return ("Enable request buffering."); case OPT_USERNAME: return "Username to use for SASL authentication"; case OPT_PASSWD: return "Password to use for SASL authentication"; case OPT_FILE: return "Path to file in which to save result"; case OPT_STAT_ARGS: return "Argument for statistics"; case OPT_SERVER_VERSION: return "Memcached daemon software version"; - default: - break; + default: break; }; assert(0); @@ -114,23 +103,19 @@ static const char *lookup_help(memcached_options option) } void help_command(const char *command_name, const char *description, - const struct option *long_options, - memcached_programs_help_st *options) -{ + const struct option *long_options, memcached_programs_help_st *options) { unsigned int x; - (void)options; + (void) options; printf("%s v%u.%u\n\n", command_name, 1U, 0U); printf("\t%s\n\n", description); printf("Current options. A '=' means the option takes a value.\n\n"); - for (x= 0; long_options[x].name; x++) - { + for (x = 0; long_options[x].name; x++) { const char *help_message; - printf("\t --%s%c\n", long_options[x].name, - long_options[x].has_arg ? '=' : ' '); - if ((help_message= lookup_help(memcached_options(long_options[x].val)))) + printf("\t --%s%c\n", long_options[x].name, long_options[x].has_arg ? '=' : ' '); + if ((help_message = lookup_help(memcached_options(long_options[x].val)))) printf("\t\t%s\n", help_message); } @@ -138,58 +123,42 @@ void help_command(const char *command_name, const char *description, exit(EXIT_SUCCESS); } -void process_hash_option(memcached_st *memc, char *opt_hash) -{ +void process_hash_option(memcached_st *memc, char *opt_hash) { uint64_t set; memcached_return_t rc; - if (opt_hash == NULL) - { + if (opt_hash == NULL) { return; } - set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */ - if (!strcasecmp(opt_hash, "CRC")) - { - set= MEMCACHED_HASH_CRC; - } - else if (!strcasecmp(opt_hash, "FNV1_64")) - { - set= MEMCACHED_HASH_FNV1_64; - } - else if (!strcasecmp(opt_hash, "FNV1A_64")) - { - set= MEMCACHED_HASH_FNV1A_64; - } - else if (!strcasecmp(opt_hash, "FNV1_32")) - { - set= MEMCACHED_HASH_FNV1_32; - } - else if (!strcasecmp(opt_hash, "FNV1A_32")) - { - set= MEMCACHED_HASH_FNV1A_32; - } - else - { + set = MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */ + if (!strcasecmp(opt_hash, "CRC")) { + set = MEMCACHED_HASH_CRC; + } else if (!strcasecmp(opt_hash, "FNV1_64")) { + set = MEMCACHED_HASH_FNV1_64; + } else if (!strcasecmp(opt_hash, "FNV1A_64")) { + set = MEMCACHED_HASH_FNV1A_64; + } else if (!strcasecmp(opt_hash, "FNV1_32")) { + set = MEMCACHED_HASH_FNV1_32; + } else if (!strcasecmp(opt_hash, "FNV1A_32")) { + set = MEMCACHED_HASH_FNV1A_32; + } else { fprintf(stderr, "hash: type not recognized %s\n", opt_hash); exit(EXIT_FAILURE); } - rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set); - if (rc != MEMCACHED_SUCCESS) - { + rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set); + if (rc != MEMCACHED_SUCCESS) { fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc)); exit(EXIT_FAILURE); } } -void initialize_sockets(void) -{ +void initialize_sockets(void) { /* Define the function for all platforms to avoid #ifdefs in each program */ #if defined(_WIN32) WSADATA wsaData; - if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) - { + if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) { fprintf(stderr, "Socket Initialization Error. Program aborted\n"); exit(EXIT_FAILURE); } diff --git a/src/bin/common/utilities.h b/src/bin/common/utilities.h index 149bd1f4..4ac6e513 100644 --- a/src/bin/common/utilities.h +++ b/src/bin/common/utilities.h @@ -1,13 +1,17 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #pragma once @@ -16,27 +20,25 @@ #include "client_options.h" #if defined(HAVE_SYS_TIME_H) -# include +# include #endif #if defined(HAVE_TIME_H) -# include +# include #endif - #ifdef __sun - /* For some odd reason the option struct on solaris defines the argument - * as char* and not const char* - */ -#define OPTIONSTRING char* +/* For some odd reason the option struct on solaris defines the argument + * as char* and not const char* + */ +# define OPTIONSTRING char * #else -#define OPTIONSTRING const char* +# define OPTIONSTRING const char * #endif typedef struct memcached_programs_help_st memcached_programs_help_st; -struct memcached_programs_help_st -{ +struct memcached_programs_help_st { char *not_used_yet; }; @@ -47,10 +49,10 @@ extern "C" { char *strdup_cleanup(const char *str); void cleanup(void); long int timedif(struct timeval a, struct timeval b); -void version_command(const char *command_name) __attribute__ ((noreturn)); +void version_command(const char *command_name) __attribute__((noreturn)); void help_command(const char *command_name, const char *description, - const struct option *long_options, - memcached_programs_help_st *options) __attribute__ ((noreturn)); + const struct option *long_options, memcached_programs_help_st *options) + __attribute__((noreturn)); void process_hash_option(memcached_st *memc, char *opt_hash); bool initialize_sasl(memcached_st *memc, char *user, char *password); void shutdown_sasl(void); diff --git a/src/bin/memaslap.c b/src/bin/memaslap.c index 77f74b36..05919611 100644 --- a/src/bin/memaslap.c +++ b/src/bin/memaslap.c @@ -1,18 +1,18 @@ /* - * memaslap - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * All rights reserved. - * http://www.schoonerinfotech.com/ - * - * Use and distribution licensed under the BSD license. See - * the COPYING file for full text. - * - * Authors: - * Brian Aker - * Mingqiang Zhuang - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -20,14 +20,13 @@ #include #if defined(HAVE_SYS_TIME_H) -# include +# include #endif #if defined(HAVE_TIME_H) -# include +# include #endif - #include "ms_sigsegv.h" #include "ms_setting.h" #include "ms_thread.h" @@ -41,69 +40,44 @@ ms_stats_t ms_stats; /* global statistic structure */ ms_statistic_t ms_statistic; -#define PROGRAM_NAME "memaslap" -#define PROGRAM_DESCRIPTION \ - "Generates workload against memcached servers." +#define PROGRAM_NAME "memaslap" +#define PROGRAM_DESCRIPTION "Generates workload against memcached servers." #ifdef __sun - /* For some odd reason the option struct on solaris defines the argument - * as char* and not const char* - */ -#define OPTIONSTRING char* +/* For some odd reason the option struct on solaris defines the argument + * as char* and not const char* + */ +# define OPTIONSTRING char * #else -#define OPTIONSTRING const char* +# define OPTIONSTRING const char * #endif /* options */ -static struct option long_options[]= -{ - { (OPTIONSTRING)"servers", required_argument, NULL, - OPT_SERVERS }, - { (OPTIONSTRING)"threads", required_argument, NULL, - OPT_THREAD_NUMBER }, - { (OPTIONSTRING)"concurrency", required_argument, NULL, - OPT_CONCURRENCY }, - { (OPTIONSTRING)"conn_sock", required_argument, NULL, - OPT_SOCK_PER_CONN }, - { (OPTIONSTRING)"execute_number", required_argument, NULL, - OPT_EXECUTE_NUMBER }, - { (OPTIONSTRING)"time", required_argument, NULL, - OPT_TIME }, - { (OPTIONSTRING)"cfg_cmd", required_argument, NULL, - OPT_CONFIG_CMD }, - { (OPTIONSTRING)"win_size", required_argument, NULL, - OPT_WINDOW_SIZE }, - { (OPTIONSTRING)"fixed_size", required_argument, NULL, - OPT_FIXED_LTH }, - { (OPTIONSTRING)"verify", required_argument, NULL, - OPT_VERIFY }, - { (OPTIONSTRING)"division", required_argument, NULL, - OPT_GETS_DIVISION }, - { (OPTIONSTRING)"stat_freq", required_argument, NULL, - OPT_STAT_FREQ }, - { (OPTIONSTRING)"exp_verify", required_argument, NULL, - OPT_EXPIRE }, - { (OPTIONSTRING)"overwrite", required_argument, NULL, - OPT_OVERWRITE }, - { (OPTIONSTRING)"reconnect", no_argument, NULL, - OPT_RECONNECT }, - { (OPTIONSTRING)"udp", no_argument, NULL, - OPT_UDP }, - { (OPTIONSTRING)"facebook", no_argument, NULL, - OPT_FACEBOOK_TEST }, - { (OPTIONSTRING)"binary", no_argument, NULL, - OPT_BINARY_PROTOCOL }, - { (OPTIONSTRING)"tps", required_argument, NULL, - OPT_TPS }, - { (OPTIONSTRING)"rep_write", required_argument, NULL, - OPT_REP_WRITE_SRV }, - { (OPTIONSTRING)"verbose", no_argument, NULL, - OPT_VERBOSE }, - { (OPTIONSTRING)"help", no_argument, NULL, - OPT_HELP }, - { (OPTIONSTRING)"version", no_argument, NULL, - OPT_VERSION }, - { 0, 0, 0, 0 }, +static struct option long_options[] = { + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "threads", required_argument, NULL, OPT_THREAD_NUMBER}, + {(OPTIONSTRING) "concurrency", required_argument, NULL, OPT_CONCURRENCY}, + {(OPTIONSTRING) "conn_sock", required_argument, NULL, OPT_SOCK_PER_CONN}, + {(OPTIONSTRING) "execute_number", required_argument, NULL, OPT_EXECUTE_NUMBER}, + {(OPTIONSTRING) "time", required_argument, NULL, OPT_TIME}, + {(OPTIONSTRING) "cfg_cmd", required_argument, NULL, OPT_CONFIG_CMD}, + {(OPTIONSTRING) "win_size", required_argument, NULL, OPT_WINDOW_SIZE}, + {(OPTIONSTRING) "fixed_size", required_argument, NULL, OPT_FIXED_LTH}, + {(OPTIONSTRING) "verify", required_argument, NULL, OPT_VERIFY}, + {(OPTIONSTRING) "division", required_argument, NULL, OPT_GETS_DIVISION}, + {(OPTIONSTRING) "stat_freq", required_argument, NULL, OPT_STAT_FREQ}, + {(OPTIONSTRING) "exp_verify", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING) "overwrite", required_argument, NULL, OPT_OVERWRITE}, + {(OPTIONSTRING) "reconnect", no_argument, NULL, OPT_RECONNECT}, + {(OPTIONSTRING) "udp", no_argument, NULL, OPT_UDP}, + {(OPTIONSTRING) "facebook", no_argument, NULL, OPT_FACEBOOK_TEST}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY_PROTOCOL}, + {(OPTIONSTRING) "tps", required_argument, NULL, OPT_TPS}, + {(OPTIONSTRING) "rep_write", required_argument, NULL, OPT_REP_WRITE_SRV}, + {(OPTIONSTRING) "verbose", no_argument, NULL, OPT_VERBOSE}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {0, 0, 0, 0}, }; /* Prototypes */ @@ -120,8 +94,7 @@ static int ms_check_para(void); static void ms_statistic_init(void); static void ms_stats_init(void); static void ms_print_statistics(int in_time); -static void ms_print_memaslap_stats(struct timeval *start_time, - struct timeval *end_time); +static void ms_print_memaslap_stats(struct timeval *start_time, struct timeval *end_time); static void ms_monitor_slap_mode(void); /** @@ -131,44 +104,37 @@ static void ms_monitor_slap_mode(void); * @param description, description of this process * @param long_options, global options array */ -static void ms_help_command(const char *command_name, const char *description) -{ - char *help_message= NULL; +static void ms_help_command(const char *command_name, const char *description) { + char *help_message = NULL; printf("%s v%u.%u\n", command_name, 1U, 0U); printf(" %s\n\n", description); - printf( - "Usage:\n" - " memaslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n" - "Options:\n"); + printf("Usage:\n" + " memaslap -hV | -s servers [-F config_file] [-t time | -x exe_num] [...]\n\n" + "Options:\n"); - for (int x= 0; long_options[x].name; x++) - { + for (int x = 0; long_options[x].name; x++) { printf(" -%c, --%s%c\n", long_options[x].val, long_options[x].name, long_options[x].has_arg ? '=' : ' '); - if ((help_message= (char *)ms_lookup_help(long_options[x].val)) != NULL) - { + if ((help_message = (char *) ms_lookup_help(long_options[x].val)) != NULL) { printf(" %s\n", help_message); } } - printf( - "\nExamples:\n" - " memaslap -s 127.0.0.1:11211 -S 5s\n" - " memaslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n" - " memaslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n" - " memaslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n" - " memaslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n" - " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n" - " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n"); + printf("\nExamples:\n" + " memaslap -s 127.0.0.1:11211 -S 5s\n" + " memaslap -s 127.0.0.1:11211 -t 2m -v 0.2 -e 0.05 -b\n" + " memaslap -s 127.0.0.1:11211 -F config -t 2m -w 40k -S 20s -o 0.2\n" + " memaslap -s 127.0.0.1:11211 -F config -t 2m -T 4 -c 128 -d 20 -P 40k\n" + " memaslap -s 127.0.0.1:11211 -F config -t 2m -d 50 -a -n 40\n" + " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m\n" + " memaslap -s 127.0.0.1:11211,127.0.0.1:11212 -F config -t 2m -p 2\n\n"); } /* ms_help_command */ - /* initialize the global locks */ -static void ms_sync_lock_init() -{ - ms_global.init_lock.count= 0; +static void ms_sync_lock_init() { + ms_global.init_lock.count = 0; pthread_mutex_init(&ms_global.init_lock.lock, NULL); pthread_cond_init(&ms_global.init_lock.cond, NULL); @@ -176,7 +142,7 @@ static void ms_sync_lock_init() pthread_mutex_init(&ms_global.warmup_lock.lock, NULL); pthread_cond_init(&ms_global.warmup_lock.cond, NULL); - ms_global.run_lock.count= 0; + ms_global.run_lock.count = 0; pthread_mutex_init(&ms_global.run_lock.lock, NULL); pthread_cond_init(&ms_global.run_lock.cond, NULL); @@ -184,10 +150,8 @@ static void ms_sync_lock_init() pthread_mutex_init(&ms_global.seq_mutex, NULL); } /* ms_sync_lock_init */ - /* destroy the global locks */ -static void ms_sync_lock_destroy() -{ +static void ms_sync_lock_destroy() { pthread_mutex_destroy(&ms_global.init_lock.lock); pthread_cond_destroy(&ms_global.init_lock.cond); @@ -200,41 +164,33 @@ static void ms_sync_lock_destroy() pthread_mutex_destroy(&ms_global.quit_mutex); pthread_mutex_destroy(&ms_global.seq_mutex); - if (ms_setting.stat_freq > 0) - { + if (ms_setting.stat_freq > 0) { pthread_mutex_destroy(&ms_statistic.stat_mutex); } } /* ms_sync_lock_destroy */ - /* initialize the global structure */ -static void ms_global_struct_init() -{ +static void ms_global_struct_init() { ms_sync_lock_init(); - ms_global.finish_warmup= false; - ms_global.time_out= false; + ms_global.finish_warmup = false; + ms_global.time_out = false; } - /* destroy the global structure */ -static void ms_global_struct_destroy() -{ +static void ms_global_struct_destroy() { ms_sync_lock_destroy(); } - /** * output the version information * * @param command_name, the string of this process */ -static void ms_version_command(const char *command_name) -{ +static void ms_version_command(const char *command_name) { printf("%s v%u.%u\n", command_name, 1U, 0U); exit(0); } - /** * get the description of the option * @@ -242,244 +198,190 @@ static void ms_version_command(const char *command_name) * * @return char*, description of the command option */ -static const char *ms_lookup_help(ms_options_t option) -{ - switch (option) - { +static const char *ms_lookup_help(ms_options_t option) { + switch (option) { case OPT_SERVERS: - return - "List one or more servers to connect. Servers count must be less than\n" - " threads count. e.g.: --servers=localhost:1234,localhost:11211"; + return "List one or more servers to connect. Servers count must be less than\n" + " threads count. e.g.: --servers=localhost:1234,localhost:11211"; - case OPT_VERSION: - return "Display the version of the application and then exit."; + case OPT_VERSION: return "Display the version of the application and then exit."; - case OPT_HELP: - return "Display this message and then exit."; + case OPT_HELP: return "Display this message and then exit."; case OPT_EXECUTE_NUMBER: return "Number of operations(get and set) to execute for the\n" " given test. Default 1000000."; case OPT_THREAD_NUMBER: - return - "Number of threads to startup, better equal to CPU numbers. Default 8."; + return "Number of threads to startup, better equal to CPU numbers. Default 8."; - case OPT_CONCURRENCY: - return "Number of concurrency to simulate with load. Default 128."; + case OPT_CONCURRENCY: return "Number of concurrency to simulate with load. Default 128."; - case OPT_FIXED_LTH: - return "Fixed length of value."; + case OPT_FIXED_LTH: return "Fixed length of value."; - case OPT_VERIFY: - return "The proportion of date verification, e.g.: --verify=0.01"; + case OPT_VERIFY: return "The proportion of date verification, e.g.: --verify=0.01"; - case OPT_GETS_DIVISION: - return "Number of keys to multi-get once. Default 1, means single get."; + case OPT_GETS_DIVISION: return "Number of keys to multi-get once. Default 1, means single get."; case OPT_TIME: - return - "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n" - " d-days e.g.: --time=2h."; + return "How long the test to run, suffix: s-seconds, m-minutes, h-hours,\n" + " d-days e.g.: --time=2h."; case OPT_CONFIG_CMD: - return - "Load the configure file to get command,key and value distribution list."; + return "Load the configure file to get command,key and value distribution list."; case OPT_WINDOW_SIZE: - return - "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n" - " Default 10k."; + return "Task window size of each concurrency, suffix: K, M e.g.: --win_size=10k.\n" + " Default 10k."; case OPT_UDP: - return - "UDP support, by default memaslap uses TCP; TCP port and UDP port of\n" - " server must be same."; + return "UDP support, by default memaslap uses TCP; TCP port and UDP port of\n" + " server must be same."; case OPT_EXPIRE: - return - "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n" - " Default no object with expire time"; + return "The proportion of objects with expire time, e.g.: --exp_verify=0.01.\n" + " Default no object with expire time"; case OPT_OVERWRITE: - return - "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n" - " Default never overwrite object."; + return "The proportion of objects need overwrite, e.g.: --overwrite=0.01.\n" + " Default never overwrite object."; case OPT_STAT_FREQ: - return - "Frequency of dumping statistic information. suffix: s-seconds,\n" - " m-minutes, e.g.: --resp_freq=10s."; + return "Frequency of dumping statistic information. suffix: s-seconds,\n" + " m-minutes, e.g.: --resp_freq=10s."; - case OPT_SOCK_PER_CONN: - return "Number of TCP socks per concurrency. Default 1."; + case OPT_SOCK_PER_CONN: return "Number of TCP socks per concurrency. Default 1."; - case OPT_RECONNECT: - return - "Reconnect support, when connection is closed it will be reconnected."; + case OPT_RECONNECT: return "Reconnect support, when connection is closed it will be reconnected."; - case OPT_VERBOSE: - return - "Whether it outputs detailed information when verification fails."; + case OPT_VERBOSE: return "Whether it outputs detailed information when verification fails."; case OPT_FACEBOOK_TEST: - return - "Whether it enables facebook test feature, set with TCP and multi-get with UDP."; + return "Whether it enables facebook test feature, set with TCP and multi-get with UDP."; case OPT_BINARY_PROTOCOL: - return - "Whether it enables binary protocol. Default with ASCII protocol."; + return "Whether it enables binary protocol. Default with ASCII protocol."; - case OPT_TPS: - return "Expected throughput, suffix: K, e.g.: --tps=10k."; + case OPT_TPS: return "Expected throughput, suffix: K, e.g.: --tps=10k."; - case OPT_REP_WRITE_SRV: - return "The first nth servers can write data, e.g.: --rep_write=2."; + case OPT_REP_WRITE_SRV: return "The first nth servers can write data, e.g.: --rep_write=2."; - default: - return "Forgot to document this option :)"; + default: return "Forgot to document this option :)"; } /* switch */ } /* ms_lookup_help */ - /* used to parse the time string */ -static int64_t ms_parse_time() -{ - int64_t ret= 0; - char unit= optarg[strlen(optarg) - 1]; +static int64_t ms_parse_time() { + int64_t ret = 0; + char unit = optarg[strlen(optarg) - 1]; - optarg[strlen(optarg) - 1]= '\0'; - ret= atoi(optarg); + optarg[strlen(optarg) - 1] = '\0'; + ret = atoi(optarg); - switch (unit) - { + switch (unit) { case 'd': case 'D': - ret*= 24; + ret *= 24; /* fall through */ case 'h': case 'H': - ret*= 60; + ret *= 60; /* fall through */ case 'm': case 'M': - ret*= 60; + ret *= 60; /* fall through */ case 's': - case 'S': - break; + case 'S': break; - default: - ret= -1; - break; + default: ret = -1; break; } /* switch */ return ret; } /* ms_parse_time */ - /* used to parse the size string */ -static int64_t ms_parse_size() -{ - int64_t ret= -1; - char unit= optarg[strlen(optarg) - 1]; - - optarg[strlen(optarg) - 1]= '\0'; - errno= 0; - ret= strtoll(optarg, (char **)NULL, 10); - if (errno != 0) - { +static int64_t ms_parse_size() { + int64_t ret = -1; + char unit = optarg[strlen(optarg) - 1]; + + optarg[strlen(optarg) - 1] = '\0'; + errno = 0; + ret = strtoll(optarg, (char **) NULL, 10); + if (errno != 0) { fprintf(stderr, "strtoll(optarg,..): %s\n", strerror(errno)); exit(1); } - switch (unit) - { + switch (unit) { case 'k': - case 'K': - ret*= 1024; - break; + case 'K': ret *= 1024; break; case 'm': - case 'M': - ret*= 1024 * 1024; - break; + case 'M': ret *= 1024 * 1024; break; case 'g': - case 'G': - ret*= 1024 * 1024 * 1024; - break; + case 'G': ret *= 1024 * 1024 * 1024; break; - default: - ret= -1; - break; + default: ret = -1; break; } /* switch */ return ret; } /* ms_parse_size */ - /* used to parse the options of command line */ -static void ms_options_parse(int argc, char *argv[]) -{ - int option_index= 0; +static void ms_options_parse(int argc, char *argv[]) { + int option_index = 0; int option_rv; - while ((option_rv= getopt_long(argc, argv, "VhURbaBs:x:T:c:X:v:d:" - "t:S:F:w:e:o:n:P:p:", - long_options, &option_index)) != -1) + while ((option_rv = getopt_long(argc, argv, + "VhURbaBs:x:T:c:X:v:d:" + "t:S:F:w:e:o:n:P:p:", + long_options, &option_index)) + != -1) { - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_VERSION: /* --version or -V */ - ms_version_command(PROGRAM_NAME); - break; + case OPT_VERSION: /* --version or -V */ ms_version_command(PROGRAM_NAME); break; - case OPT_HELP: /* --help or -h */ + case OPT_HELP: /* --help or -h */ ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION); exit(0); break; - case OPT_SERVERS: /* --servers or -s */ - ms_setting.srv_str= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ ms_setting.srv_str = strdup(optarg); break; - case OPT_CONCURRENCY: /* --concurrency or -c */ - errno= 0; - ms_setting.nconns= (uint32_t)strtoul(optarg, (char **) NULL, 10); - if (ms_setting.nconns <= 0 || errno != 0) - { + case OPT_CONCURRENCY: /* --concurrency or -c */ + errno = 0; + ms_setting.nconns = (uint32_t) strtoul(optarg, (char **) NULL, 10); + if (ms_setting.nconns <= 0 || errno != 0) { fprintf(stderr, "Concurrency must be greater than 0.:-)\n"); exit(1); } break; - case OPT_EXECUTE_NUMBER: /* --execute_number or -x */ - errno= 0; - ms_setting.exec_num= (int)strtol(optarg, (char **) NULL, 10); - if (ms_setting.exec_num <= 0 || errno != 0) - { + case OPT_EXECUTE_NUMBER: /* --execute_number or -x */ + errno = 0; + ms_setting.exec_num = (int) strtol(optarg, (char **) NULL, 10); + if (ms_setting.exec_num <= 0 || errno != 0) { fprintf(stderr, "Execute number must be greater than 0.:-)\n"); exit(1); } break; - case OPT_THREAD_NUMBER: /* --threads or -T */ - errno= 0; - ms_setting.nthreads= (uint32_t)strtoul(optarg, (char **) NULL, 10); - if (ms_setting.nthreads <= 0 || errno != 0) - { + case OPT_THREAD_NUMBER: /* --threads or -T */ + errno = 0; + ms_setting.nthreads = (uint32_t) strtoul(optarg, (char **) NULL, 10); + if (ms_setting.nthreads <= 0 || errno != 0) { fprintf(stderr, "Threads number must be greater than 0.:-)\n"); exit(1); } break; - case OPT_FIXED_LTH: /* --fixed_size or -X */ - errno= 0; - ms_setting.fixed_value_size= (size_t)strtoull(optarg, (char **) NULL, 10); + case OPT_FIXED_LTH: /* --fixed_size or -X */ + errno = 0; + ms_setting.fixed_value_size = (size_t) strtoull(optarg, (char **) NULL, 10); if ((ms_setting.fixed_value_size <= 0 || errno != 0) || (ms_setting.fixed_value_size > MAX_VALUE_SIZE)) { @@ -488,135 +390,116 @@ static void ms_options_parse(int argc, char *argv[]) } break; - case OPT_VERIFY: /* --verify or -v */ - ms_setting.verify_percent= atof(optarg); - if ((ms_setting.verify_percent <= 0) - || (ms_setting.verify_percent > 1.0)) - { - fprintf(stderr, "Data verification rate must be " - "greater than 0 and less than 1.0. :-)\n"); + case OPT_VERIFY: /* --verify or -v */ + ms_setting.verify_percent = atof(optarg); + if ((ms_setting.verify_percent <= 0) || (ms_setting.verify_percent > 1.0)) { + fprintf(stderr, + "Data verification rate must be " + "greater than 0 and less than 1.0. :-)\n"); exit(1); } break; - case OPT_GETS_DIVISION: /* --division or -d */ - errno= 0; - ms_setting.mult_key_num= (int)strtol(optarg, (char **) NULL, 10); - if (ms_setting.mult_key_num <= 0 || errno != 0) - { + case OPT_GETS_DIVISION: /* --division or -d */ + errno = 0; + ms_setting.mult_key_num = (int) strtol(optarg, (char **) NULL, 10); + if (ms_setting.mult_key_num <= 0 || errno != 0) { fprintf(stderr, "Multi-get key number must be greater than 0.:-)\n"); exit(1); } break; - case OPT_TIME: /* --time or -t */ - ms_setting.run_time= (int)ms_parse_time(); - if (ms_setting.run_time == -1) - { - fprintf(stderr, "Please specify the run time. :-)\n" - "'s' for second, 'm' for minute, 'h' for hour, " - "'d' for day. e.g.: --time=24h (means 24 hours).\n"); + case OPT_TIME: /* --time or -t */ + ms_setting.run_time = (int) ms_parse_time(); + if (ms_setting.run_time == -1) { + fprintf(stderr, + "Please specify the run time. :-)\n" + "'s' for second, 'm' for minute, 'h' for hour, " + "'d' for day. e.g.: --time=24h (means 24 hours).\n"); exit(1); } - if (ms_setting.run_time == 0) - { + if (ms_setting.run_time == 0) { fprintf(stderr, "Running time can not be 0. :-)\n"); exit(1); } break; - case OPT_CONFIG_CMD: /* --cfg_cmd or -F */ - ms_setting.cfg_file= strdup(optarg); - break; + case OPT_CONFIG_CMD: /* --cfg_cmd or -F */ ms_setting.cfg_file = strdup(optarg); break; - case OPT_WINDOW_SIZE: /* --win_size or -w */ - ms_setting.win_size= (size_t)ms_parse_size(); - if (ms_setting.win_size == (size_t)-1) - { - fprintf( - stderr, - "Please specify the item window size. :-)\n" - "e.g.: --win_size=10k (means 10k task window size).\n"); + case OPT_WINDOW_SIZE: /* --win_size or -w */ + ms_setting.win_size = (size_t) ms_parse_size(); + if (ms_setting.win_size == (size_t) -1) { + fprintf(stderr, + "Please specify the item window size. :-)\n" + "e.g.: --win_size=10k (means 10k task window size).\n"); exit(1); } break; - case OPT_UDP: /* --udp or -U*/ - ms_setting.udp= true; - break; + case OPT_UDP: /* --udp or -U*/ ms_setting.udp = true; break; - case OPT_EXPIRE: /* --exp_verify or -e */ - ms_setting.exp_ver_per= atof(optarg); - if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0)) - { - fprintf(stderr, "Expire time verification rate must be " - "greater than 0 and less than 1.0. :-)\n"); + case OPT_EXPIRE: /* --exp_verify or -e */ + ms_setting.exp_ver_per = atof(optarg); + if ((ms_setting.exp_ver_per <= 0) || (ms_setting.exp_ver_per > 1.0)) { + fprintf(stderr, + "Expire time verification rate must be " + "greater than 0 and less than 1.0. :-)\n"); exit(1); } break; - case OPT_OVERWRITE: /* --overwrite or -o */ - ms_setting.overwrite_percent= atof(optarg); - if ((ms_setting.overwrite_percent <= 0) - || (ms_setting.overwrite_percent > 1.0)) - { - fprintf(stderr, "Objects overwrite rate must be " - "greater than 0 and less than 1.0. :-)\n"); + case OPT_OVERWRITE: /* --overwrite or -o */ + ms_setting.overwrite_percent = atof(optarg); + if ((ms_setting.overwrite_percent <= 0) || (ms_setting.overwrite_percent > 1.0)) { + fprintf(stderr, + "Objects overwrite rate must be " + "greater than 0 and less than 1.0. :-)\n"); exit(1); } break; - case OPT_STAT_FREQ: /* --stat_freq or -S */ - ms_setting.stat_freq= (int)ms_parse_time(); - if (ms_setting.stat_freq == -1) - { - fprintf(stderr, "Please specify the frequency of dumping " - "statistic information. :-)\n" - "'s' for second, 'm' for minute, 'h' for hour, " - "'d' for day. e.g.: --time=24h (means 24 hours).\n"); + case OPT_STAT_FREQ: /* --stat_freq or -S */ + ms_setting.stat_freq = (int) ms_parse_time(); + if (ms_setting.stat_freq == -1) { + fprintf(stderr, + "Please specify the frequency of dumping " + "statistic information. :-)\n" + "'s' for second, 'm' for minute, 'h' for hour, " + "'d' for day. e.g.: --time=24h (means 24 hours).\n"); exit(1); } - if (ms_setting.stat_freq == 0) - { - fprintf(stderr, "The frequency of dumping statistic information " - "can not be 0. :-)\n"); + if (ms_setting.stat_freq == 0) { + fprintf(stderr, + "The frequency of dumping statistic information " + "can not be 0. :-)\n"); exit(1); } break; - case OPT_SOCK_PER_CONN: /* --conn_sock or -n */ - errno= 0; - ms_setting.sock_per_conn= (uint32_t)strtoul(optarg, (char **) NULL, 10); - if (ms_setting.sock_per_conn <= 0 || errno != 0) - { - fprintf(stderr, "Number of socks of each concurrency " - "must be greater than 0.:-)\n"); + case OPT_SOCK_PER_CONN: /* --conn_sock or -n */ + errno = 0; + ms_setting.sock_per_conn = (uint32_t) strtoul(optarg, (char **) NULL, 10); + if (ms_setting.sock_per_conn <= 0 || errno != 0) { + fprintf(stderr, + "Number of socks of each concurrency " + "must be greater than 0.:-)\n"); exit(1); } break; - case OPT_RECONNECT: /* --reconnect or -R */ - ms_setting.reconnect= true; - break; + case OPT_RECONNECT: /* --reconnect or -R */ ms_setting.reconnect = true; break; - case OPT_VERBOSE: /* --verbose or -b */ - ms_setting.verbose= true; - break; + case OPT_VERBOSE: /* --verbose or -b */ ms_setting.verbose = true; break; - case OPT_FACEBOOK_TEST: /* --facebook or -a */ - ms_setting.facebook_test= true; - break; + case OPT_FACEBOOK_TEST: /* --facebook or -a */ ms_setting.facebook_test = true; break; - case OPT_BINARY_PROTOCOL: /* --binary or -B */ - ms_setting.binary_prot_= true; - break; + case OPT_BINARY_PROTOCOL: /* --binary or -B */ ms_setting.binary_prot_ = true; break; - case OPT_TPS: /* --tps or -P */ - ms_setting.expected_tps= (int)ms_parse_size(); - if (ms_setting.expected_tps == -1) - { + case OPT_TPS: /* --tps or -P */ + ms_setting.expected_tps = (int) ms_parse_size(); + if (ms_setting.expected_tps == -1) { fprintf(stderr, "Please specify the item expected throughput. :-)\n" "e.g.: --tps=10k (means 10k throughput).\n"); @@ -624,11 +507,10 @@ static void ms_options_parse(int argc, char *argv[]) } break; - case OPT_REP_WRITE_SRV: /* --rep_write or -p */ - errno= 0; - ms_setting.rep_write_srv= (uint32_t)strtoul(optarg, (char **) NULL, 10); - if (ms_setting.rep_write_srv <= 0 || errno != 0) - { + case OPT_REP_WRITE_SRV: /* --rep_write or -p */ + errno = 0; + ms_setting.rep_write_srv = (uint32_t) strtoul(optarg, (char **) NULL, 10); + if (ms_setting.rep_write_srv <= 0 || errno != 0) { fprintf(stderr, "Number of replication writing server must be greater " "than 0.:-)\n"); @@ -640,38 +522,29 @@ static void ms_options_parse(int argc, char *argv[]) /* getopt_long already printed an error message. */ exit(1); - default: - abort(); + default: abort(); } /* switch */ } } /* ms_options_parse */ - -static int ms_check_para() -{ - if (ms_setting.srv_str == NULL) - { +static int ms_check_para() { + if (ms_setting.srv_str == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - ms_setting.srv_str= strdup(temp); - } - else - { + if ((temp = getenv("MEMCACHED_SERVERS"))) { + ms_setting.srv_str = strdup(temp); + } else { fprintf(stderr, "No servers provided\n\n"); return -1; } } - if (ms_setting.nconns % (uint32_t)ms_setting.nthreads != 0) - { + if (ms_setting.nconns % (uint32_t) ms_setting.nthreads != 0) { fprintf(stderr, "Concurrency must be the multiples of threads count.\n"); return -1; } - if (ms_setting.win_size % UNIT_ITEMS_COUNT != 0) - { + if (ms_setting.win_size % UNIT_ITEMS_COUNT != 0) { fprintf(stderr, "Window size must be the multiples of 1024.\n\n"); return -1; } @@ -679,202 +552,142 @@ static int ms_check_para() return EXIT_SUCCESS; } /* ms_check_para */ - /* initialize the statistic structure */ -static void ms_statistic_init() -{ +static void ms_statistic_init() { pthread_mutex_init(&ms_statistic.stat_mutex, NULL); ms_init_stats(&ms_statistic.get_stat, "Get"); ms_init_stats(&ms_statistic.set_stat, "Set"); ms_init_stats(&ms_statistic.total_stat, "Total"); } /* ms_statistic_init */ - /* initialize the global state structure */ -static void ms_stats_init() -{ +static void ms_stats_init() { memset(&ms_stats, 0, sizeof(ms_stats_t)); - if (ms_setting.stat_freq > 0) - { + if (ms_setting.stat_freq > 0) { ms_statistic_init(); } } /* ms_stats_init */ - /* use to output the statistic */ -static void ms_print_statistics(int in_time) -{ - int obj_size= (int)(ms_setting.avg_key_size + ms_setting.avg_val_size); +static void ms_print_statistics(int in_time) { + int obj_size = (int) (ms_setting.avg_key_size + ms_setting.avg_val_size); printf("\033[1;1H\033[2J\n"); - ms_dump_format_stats(&ms_statistic.get_stat, in_time, - ms_setting.stat_freq, obj_size); - ms_dump_format_stats(&ms_statistic.set_stat, in_time, - ms_setting.stat_freq, obj_size); - ms_dump_format_stats(&ms_statistic.total_stat, in_time, - ms_setting.stat_freq, obj_size); + ms_dump_format_stats(&ms_statistic.get_stat, in_time, ms_setting.stat_freq, obj_size); + ms_dump_format_stats(&ms_statistic.set_stat, in_time, ms_setting.stat_freq, obj_size); + ms_dump_format_stats(&ms_statistic.total_stat, in_time, ms_setting.stat_freq, obj_size); } /* ms_print_statistics */ - /* used to print the states of memaslap */ -static void ms_print_memaslap_stats(struct timeval *start_time, - struct timeval *end_time) -{ +static void ms_print_memaslap_stats(struct timeval *start_time, struct timeval *end_time) { char buf[0x2000]; - char *pos= buf; - - pos+= snprintf(pos, - sizeof(buf), "cmd_get: %lu\n", - (unsigned long) ms_stats.cmd_get); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "cmd_set: %lu\n", - (unsigned long) ms_stats.cmd_set); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "get_misses: %lu\n", - (unsigned long) ms_stats.get_misses); - - if (ms_setting.verify_percent > 0) - { - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "verify_misses: %lu\n", - (unsigned long) ms_stats.vef_miss); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "verify_failed: %lu\n", - (unsigned long) ms_stats.vef_failed); + char *pos = buf; + + pos += snprintf(pos, sizeof(buf), "cmd_get: %lu\n", (unsigned long) ms_stats.cmd_get); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "cmd_set: %lu\n", + (unsigned long) ms_stats.cmd_set); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "get_misses: %lu\n", + (unsigned long) ms_stats.get_misses); + + if (ms_setting.verify_percent > 0) { + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_misses: %lu\n", + (unsigned long) ms_stats.vef_miss); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "verify_failed: %lu\n", + (unsigned long) ms_stats.vef_failed); } - if (ms_setting.exp_ver_per > 0) - { - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "expired_get: %lu\n", - (unsigned long) ms_stats.exp_get); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "unexpired_unget: %lu\n", - (unsigned long) ms_stats.unexp_unget); + if (ms_setting.exp_ver_per > 0) { + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "expired_get: %lu\n", + (unsigned long) ms_stats.exp_get); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "unexpired_unget: %lu\n", + (unsigned long) ms_stats.unexp_unget); } - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "written_bytes: %lu\n", - (unsigned long) ms_stats.bytes_written); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "read_bytes: %lu\n", - (unsigned long) ms_stats.bytes_read); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "object_bytes: %lu\n", - (unsigned long) ms_stats.obj_bytes); - - if (ms_setting.udp || ms_setting.facebook_test) - { - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "packet_disorder: %lu\n", - (unsigned long) ms_stats.pkt_disorder); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "packet_drop: %lu\n", - (unsigned long)ms_stats.pkt_drop); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "udp_timeout: %lu\n", - (unsigned long)ms_stats.udp_timeout); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "written_bytes: %lu\n", + (unsigned long) ms_stats.bytes_written); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "read_bytes: %lu\n", + (unsigned long) ms_stats.bytes_read); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "object_bytes: %lu\n", + (unsigned long) ms_stats.obj_bytes); + + if (ms_setting.udp || ms_setting.facebook_test) { + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_disorder: %lu\n", + (unsigned long) ms_stats.pkt_disorder); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "packet_drop: %lu\n", + (unsigned long) ms_stats.pkt_drop); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), "udp_timeout: %lu\n", + (unsigned long) ms_stats.udp_timeout); } - if (ms_setting.stat_freq > 0) - { + if (ms_setting.stat_freq > 0) { ms_dump_stats(&ms_statistic.get_stat); ms_dump_stats(&ms_statistic.set_stat); ms_dump_stats(&ms_statistic.total_stat); } - int64_t time_diff= ms_time_diff(start_time, end_time); - pos+= snprintf(pos, - sizeof(buf) - (size_t)(pos -buf), - "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n", - (double)time_diff / 1000000, - (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set), - (ms_stats.cmd_get - + ms_stats.cmd_set) / ((long double)time_diff / 1000000), - (double)( - ms_stats.bytes_written - + ms_stats.bytes_read) / 1024 / 1024 - / ((double)time_diff / 1000000)); + int64_t time_diff = ms_time_diff(start_time, end_time); + pos += snprintf(pos, sizeof(buf) - (size_t)(pos - buf), + "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n", + (double) time_diff / 1000000, + (unsigned long long) (ms_stats.cmd_get + ms_stats.cmd_set), + (ms_stats.cmd_get + ms_stats.cmd_set) / ((long double) time_diff / 1000000), + (double) (ms_stats.bytes_written + ms_stats.bytes_read) / 1024 / 1024 + / ((double) time_diff / 1000000)); assert(pos <= (buf + sizeof(buf))); fwrite(buf, 1, pos - buf, stdout); fflush(stdout); } /* ms_print_memaslap_stats */ - /* the loop of the main thread, wait the work threads to complete */ -static void ms_monitor_slap_mode() -{ +static void ms_monitor_slap_mode() { struct timeval start_time, end_time; /* Wait all the threads complete initialization. */ pthread_mutex_lock(&ms_global.init_lock.lock); - while (ms_global.init_lock.count < ms_setting.nthreads) - { - pthread_cond_wait(&ms_global.init_lock.cond, - &ms_global.init_lock.lock); + while (ms_global.init_lock.count < ms_setting.nthreads) { + pthread_cond_wait(&ms_global.init_lock.cond, &ms_global.init_lock.lock); } pthread_mutex_unlock(&ms_global.init_lock.lock); /* only when there is no set operation it need warm up */ - if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) - { + if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) { /* Wait all the connects complete warm up. */ pthread_mutex_lock(&ms_global.warmup_lock.lock); - while (ms_global.warmup_lock.count < ms_setting.nconns) - { + while (ms_global.warmup_lock.count < ms_setting.nconns) { pthread_cond_wait(&ms_global.warmup_lock.cond, &ms_global.warmup_lock.lock); } pthread_mutex_unlock(&ms_global.warmup_lock.lock); } - ms_global.finish_warmup= true; + ms_global.finish_warmup = true; /* running in "run time" mode, user specify run time */ - if (ms_setting.run_time > 0) - { - int second= 0; + if (ms_setting.run_time > 0) { + int second = 0; gettimeofday(&start_time, NULL); - while (1) - { + while (1) { sleep(1); second++; if ((ms_setting.stat_freq > 0) && (second % ms_setting.stat_freq == 0) - && (ms_stats.active_conns >= ms_setting.nconns) - && (ms_stats.active_conns <= INT_MAX)) + && (ms_stats.active_conns >= ms_setting.nconns) && (ms_stats.active_conns <= INT_MAX)) { ms_print_statistics(second); } - if (ms_setting.run_time <= second) - { - ms_global.time_out= true; + if (ms_setting.run_time <= second) { + ms_global.time_out = true; break; } /* all connections disconnect */ - if ((second > 5) && (ms_stats.active_conns == 0)) - { + if ((second > 5) && (ms_stats.active_conns == 0)) { break; } } gettimeofday(&end_time, NULL); - sleep(1); /* wait all threads clean up */ - } - else - { + sleep(1); /* wait all threads clean up */ + } else { /* running in "execute number" mode, user specify execute number */ gettimeofday(&start_time, NULL); @@ -882,8 +695,7 @@ static void ms_monitor_slap_mode() * We loop until we know that all connects have cleaned up. */ pthread_mutex_lock(&ms_global.run_lock.lock); - while (ms_global.run_lock.count < ms_setting.nconns) - { + while (ms_global.run_lock.count < ms_setting.nconns) { pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock); } pthread_mutex_unlock(&ms_global.run_lock.lock); @@ -894,18 +706,15 @@ static void ms_monitor_slap_mode() ms_print_memaslap_stats(&start_time, &end_time); } /* ms_monitor_slap_mode */ - /* the main function */ -int main(int argc, char *argv[]) -{ - srandom((unsigned int)time(NULL)); +int main(int argc, char *argv[]) { + srandom((unsigned int) time(NULL)); ms_global_struct_init(); /* initialization */ ms_setting_init_pre(); ms_options_parse(argc, argv); - if (ms_check_para()) - { + if (ms_check_para()) { ms_help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION); exit(1); } diff --git a/src/bin/memaslap/ms_atomic.h b/src/bin/memaslap/ms_atomic.h index 6ec990c0..3cb69919 100644 --- a/src/bin/memaslap/ms_atomic.h +++ b/src/bin/memaslap/ms_atomic.h @@ -1,114 +1,118 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #ifndef CLIENTS_MS_ATOMIC_H #define CLIENTS_MS_ATOMIC_H #if HAVE_C_STDATOMIC -# define ATOMIC _Atomic +# define ATOMIC _Atomic #else -# define ATOMIC volatile +# define ATOMIC volatile #endif #if defined(__SUNPRO_C) -# define _KERNEL -# include -# if SIZEOF_SIZE_T == 8 -# define atomic_add_size(X, Y) atomic_add_64((X), (Y)) -# define atomic_add_size_nv(X, Y) atomic_add_64((X), (Y)) -# define atomic_dec_size(X, Y) atomic_add_64((X), (Y)) -# define atomic_dec_size_nv(X, Y) atomic_add_64((X), (Y)) -# else -# define atomic_add_size(X, Y) atomic_add_32((X), (Y)) -# define atomic_add_size_nv(X, Y) atomic_add_32((X), (Y)) -# define atomic_dec_size(X, Y) atomic_add_32((X), (Y)) -# define atomic_dec_size_nv(X, Y) atomic_add_32((X), (Y)) -# endif -# undef _KERNEL +# define _KERNEL +# include +# if SIZEOF_SIZE_T == 8 +# define atomic_add_size(X, Y) atomic_add_64((X), (Y)) +# define atomic_add_size_nv(X, Y) atomic_add_64((X), (Y)) +# define atomic_dec_size(X, Y) atomic_add_64((X), (Y)) +# define atomic_dec_size_nv(X, Y) atomic_add_64((X), (Y)) +# else +# define atomic_add_size(X, Y) atomic_add_32((X), (Y)) +# define atomic_add_size_nv(X, Y) atomic_add_32((X), (Y)) +# define atomic_dec_size(X, Y) atomic_add_32((X), (Y)) +# define atomic_dec_size_nv(X, Y) atomic_add_32((X), (Y)) +# endif +# undef _KERNEL #elif HAVE_GCC_ATOMIC_BUILTINS -# define atomic_add_8(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_16(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_32(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_size(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_dec_8(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_16(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_32(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_size(X) __sync_fetch_and_sub((X), 1) +# define atomic_add_8(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_16(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_32(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_size(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_dec_8(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_16(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_32(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_size(X) __sync_fetch_and_sub((X), 1) /* The same as above, but these return the new value instead of void */ -# define atomic_add_8_nv(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_16_nv(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_32_nv(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_add_size_nv(X, Y) __sync_fetch_and_add((X), (Y)) -# define atomic_dec_8_nv(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_16_nv(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_32_nv(X) __sync_fetch_and_sub((X), 1) -# define atomic_dec_size_nv(X) __sync_fetch_and_sub((X), 1) +# define atomic_add_8_nv(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_16_nv(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_32_nv(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_add_size_nv(X, Y) __sync_fetch_and_add((X), (Y)) +# define atomic_dec_8_nv(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_16_nv(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_32_nv(X) __sync_fetch_and_sub((X), 1) +# define atomic_dec_size_nv(X) __sync_fetch_and_sub((X), 1) #elif HAVE_C_STDATOMIC -# include -# define atomic_add_8(X, Y) atomic_fetch_add(X, Y) -# define atomic_add_16(X, Y) atomic_fetch_add(X, Y) -# define atomic_add_32(X, Y) atomic_fetch_add(X, Y) -# define atomic_add_size(X, Y) atomic_fetch_add(X, Y) -# define atomic_dec_8(X) atomic_fetch_sub(X, 1) -# define atomic_dec_16(X) atomic_fetch_sub(X, 1) -# define atomic_dec_32(X) atomic_fetch_sub(X, 1) -# define atomic_dec_size(X) atomic_fetch_sub(X, 1) +# include +# define atomic_add_8(X, Y) atomic_fetch_add(X, Y) +# define atomic_add_16(X, Y) atomic_fetch_add(X, Y) +# define atomic_add_32(X, Y) atomic_fetch_add(X, Y) +# define atomic_add_size(X, Y) atomic_fetch_add(X, Y) +# define atomic_dec_8(X) atomic_fetch_sub(X, 1) +# define atomic_dec_16(X) atomic_fetch_sub(X, 1) +# define atomic_dec_32(X) atomic_fetch_sub(X, 1) +# define atomic_dec_size(X) atomic_fetch_sub(X, 1) /* The same as above, but these return the new value instead of void */ -# define ATOMIC_ADD_FETCH_DECL(T) \ -static inline T atomic_add_fetch_##T(ATOMIC T *ptr, T add) { \ - T des, cur = atomic_load(ptr); \ - do { \ - des = cur + add; \ - } while(!atomic_compare_exchange_weak(ptr, &cur, des)); \ - return des; \ -} -# define ATOMIC_SUB_FETCH_DECL(T) \ -T atomic_sub_fetch_##T(ATOMIC T *ptr) { \ - T des, cur = atomic_load(ptr); \ - do { \ - des = cur - 1; \ - } while(!atomic_compare_exchange_weak(ptr, &cur, des)); \ - return des; \ -} +# define ATOMIC_ADD_FETCH_DECL(T) \ + static inline T atomic_add_fetch_##T(ATOMIC T *ptr, T add) { \ + T des, cur = atomic_load(ptr); \ + do { \ + des = cur + add; \ + } while (!atomic_compare_exchange_weak(ptr, &cur, des)); \ + return des; \ + } +# define ATOMIC_SUB_FETCH_DECL(T) \ + T atomic_sub_fetch_##T(ATOMIC T *ptr) { \ + T des, cur = atomic_load(ptr); \ + do { \ + des = cur - 1; \ + } while (!atomic_compare_exchange_weak(ptr, &cur, des)); \ + return des; \ + } ATOMIC_ADD_FETCH_DECL(uint8_t) -# define atomic_add_8_nv(X, Y) atomic_add_fetch_uint8_t(X, Y) +# define atomic_add_8_nv(X, Y) atomic_add_fetch_uint8_t(X, Y) ATOMIC_ADD_FETCH_DECL(uint16_t) -# define atomic_add_16_nv(X, Y) atomic_add_fetch_uint16_t(X, Y) +# define atomic_add_16_nv(X, Y) atomic_add_fetch_uint16_t(X, Y) ATOMIC_ADD_FETCH_DECL(uint32_t) -# define atomic_add_32_nv(X, Y) atomic_add_fetch_uint32_t(X, Y) +# define atomic_add_32_nv(X, Y) atomic_add_fetch_uint32_t(X, Y) ATOMIC_ADD_FETCH_DECL(size_t) -# define atomic_add_size_nv(X, Y) atomic_add_fetch_size_t(X, Y) -# define atomic_dec_8_nv(X) atomic_sub_fetch(X, Y) -# define atomic_dec_16_nv(X) atomic_sub_fetch(X, Y) -# define atomic_dec_32_nv(X) atomic_sub_fetch(X, Y) -# define atomic_dec_size_nv(X) atomic_sub_fetch(X, Y) +# define atomic_add_size_nv(X, Y) atomic_add_fetch_size_t(X, Y) +# define atomic_dec_8_nv(X) atomic_sub_fetch(X, Y) +# define atomic_dec_16_nv(X) atomic_sub_fetch(X, Y) +# define atomic_dec_32_nv(X) atomic_sub_fetch(X, Y) +# define atomic_dec_size_nv(X) atomic_sub_fetch(X, Y) #else -#warning "Atomic operators not found so memslap will not work correctly" -# define atomic_add_8(X, Y) 0 -# define atomic_add_16(X, Y) 0 -# define atomic_add_32(X, Y) 0 -# define atomic_add_size(X, Y) 0 -# define atomic_dec_8(X) 0 -# define atomic_dec_16(X) 0 -# define atomic_dec_32(X) 0 -# define atomic_dec_size(X) 0 +# warning "Atomic operators not found so memslap will not work correctly" +# define atomic_add_8(X, Y) 0 +# define atomic_add_16(X, Y) 0 +# define atomic_add_32(X, Y) 0 +# define atomic_add_size(X, Y) 0 +# define atomic_dec_8(X) 0 +# define atomic_dec_16(X) 0 +# define atomic_dec_32(X) 0 +# define atomic_dec_size(X) 0 /* The same as above, but these return the new value instead of void */ -# define atomic_add_8_nv(X, Y) 0 -# define atomic_add_16_nv(X, Y) 0 -# define atomic_add_32_nv(X, Y) 0 -# define atomic_add_size_nv(X, Y) 0 -# define atomic_dec_8_nv(X) 0 -# define atomic_dec_16_nv(X) 0 -# define atomic_dec_32_nv(X) 0 -# define atomic_dec_size_nv(X) 0 +# define atomic_add_8_nv(X, Y) 0 +# define atomic_add_16_nv(X, Y) 0 +# define atomic_add_32_nv(X, Y) 0 +# define atomic_add_size_nv(X, Y) 0 +# define atomic_dec_8_nv(X) 0 +# define atomic_dec_16_nv(X) 0 +# define atomic_dec_32_nv(X) 0 +# define atomic_dec_size_nv(X) 0 #endif /* defined(__SUNPRO_C) */ #endif /* CLIENTS_MS_ATOMIC_H */ diff --git a/src/bin/memaslap/ms_conn.c b/src/bin/memaslap/ms_conn.c index 39f284b0..815c67ff 100644 --- a/src/bin/memaslap/ms_conn.c +++ b/src/bin/memaslap/ms_conn.c @@ -1,13 +1,17 @@ /* - * File: ms_conn.c - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -21,15 +25,15 @@ #include #if defined(HAVE_ARPA_INET_H) -# include +# include #endif #if defined(HAVE_SYS_TIME_H) -# include +# include #endif #if defined(HAVE_TIME_H) -# include +# include #endif #include "ms_setting.h" @@ -42,85 +46,70 @@ * from gcc. The conversion methods isn't the bottleneck for my app, so * just remove the warnings by undef'ing the optimization .. */ -#undef ntohs -#undef ntohl -#undef htons -#undef htonl +# undef ntohs +# undef ntohl +# undef htons +# undef htonl #endif /* for network write */ -#define TRANSMIT_COMPLETE 0 -#define TRANSMIT_INCOMPLETE 1 -#define TRANSMIT_SOFT_ERROR 2 -#define TRANSMIT_HARD_ERROR 3 +#define TRANSMIT_COMPLETE 0 +#define TRANSMIT_INCOMPLETE 1 +#define TRANSMIT_SOFT_ERROR 2 +#define TRANSMIT_HARD_ERROR 3 /* for generating key */ -#define KEY_PREFIX_BASE 0x1010101010101010 /* not include ' ' '\r' '\n' '\0' */ -#define KEY_PREFIX_MASK 0x1010101010101010 +#define KEY_PREFIX_BASE 0x1010101010101010 /* not include ' ' '\r' '\n' '\0' */ +#define KEY_PREFIX_MASK 0x1010101010101010 /* For parse the value length return by server */ -#define KEY_TOKEN 1 -#define VALUELEN_TOKEN 3 +#define KEY_TOKEN 1 +#define VALUELEN_TOKEN 3 /* global increasing counter, to ensure the key prefix unique */ -static uint64_t key_prefix_seq= KEY_PREFIX_BASE; +static uint64_t key_prefix_seq = KEY_PREFIX_BASE; /* global increasing counter, generating request id for UDP */ -static ATOMIC uint32_t udp_request_id= 0; +static ATOMIC uint32_t udp_request_id = 0; extern pthread_key_t ms_thread_key; /* generate upd request id */ static uint32_t ms_get_udp_request_id(void); - /* connect initialize */ static void ms_task_init(ms_conn_t *c); static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp); static int ms_conn_sock_init(ms_conn_t *c); static int ms_conn_event_init(ms_conn_t *c); -static int ms_conn_init(ms_conn_t *c, - const int init_state, - const int read_buffer_size, +static int ms_conn_init(ms_conn_t *c, const int init_state, const int read_buffer_size, const bool is_udp); static void ms_warmup_num_init(ms_conn_t *c); static int ms_item_win_init(ms_conn_t *c); - /* connection close */ void ms_conn_free(ms_conn_t *c); static void ms_conn_close(ms_conn_t *c); - /* create network connection */ static int ms_new_socket(struct addrinfo *ai); static void ms_maximize_sndbuf(const int sfd); -static int ms_network_connect(ms_conn_t *c, - char *srv_host_name, - const int srv_port, - const bool is_udp, - int *ret_sfd); +static int ms_network_connect(ms_conn_t *c, char *srv_host_name, const int srv_port, + const bool is_udp, int *ret_sfd); static int ms_reconn(ms_conn_t *c); - /* read and parse */ -static int ms_tokenize_command(char *command, - token_t *tokens, - const int max_tokens); +static int ms_tokenize_command(char *command, token_t *tokens, const int max_tokens); static int ms_ascii_process_line(ms_conn_t *c, char *command); static int ms_try_read_line(ms_conn_t *c); static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes); static int ms_udp_read(ms_conn_t *c, char *buf, int len); static int ms_try_read_network(ms_conn_t *c); -static void ms_verify_value(ms_conn_t *c, - ms_mlget_task_item_t *mlget_item, - char *value, - int vlen); +static void ms_verify_value(ms_conn_t *c, ms_mlget_task_item_t *mlget_item, char *value, int vlen); static void ms_ascii_complete_nread(ms_conn_t *c); static void ms_bin_complete_nread(ms_conn_t *c); static void ms_complete_nread(ms_conn_t *c); - /* send functions */ static int ms_add_msghdr(ms_conn_t *c); static int ms_ensure_iov_space(ms_conn_t *c); @@ -128,7 +117,6 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len); static int ms_build_udp_headers(ms_conn_t *c); static int ms_transmit(ms_conn_t *c); - /* status adjustment */ static void ms_conn_shrink(ms_conn_t *c); static void ms_conn_set_state(ms_conn_t *c, int state); @@ -139,31 +127,24 @@ static int ms_update_conn_sock_event(ms_conn_t *c); static bool ms_need_yield(ms_conn_t *c); static void ms_update_start_time(ms_conn_t *c); - /* main loop */ static void ms_drive_machine(ms_conn_t *c); void ms_event_handler(const int fd, const short which, void *arg); - /* ascii protocol */ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item); static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item); static int ms_build_ascii_write_buf_mlget(ms_conn_t *c); - /* binary protocol */ static int ms_bin_process_response(ms_conn_t *c); -static void ms_add_bin_header(ms_conn_t *c, - uint8_t opcode, - uint8_t hdr_len, - uint16_t key_len, +static void ms_add_bin_header(ms_conn_t *c, uint8_t opcode, uint8_t hdr_len, uint16_t key_len, uint32_t body_len); static void ms_add_key_to_iov(ms_conn_t *c, ms_task_item_t *item); static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item); static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item); static int ms_build_bin_write_buf_mlget(ms_conn_t *c); - /** * each key has two parts, prefix and suffix. The suffix is a * string random get form the character table. The prefix is a @@ -173,53 +154,47 @@ static int ms_build_bin_write_buf_mlget(ms_conn_t *c); * * @return uint64_t */ -uint64_t ms_get_key_prefix(void) -{ +uint64_t ms_get_key_prefix(void) { uint64_t key_prefix; pthread_mutex_lock(&ms_global.seq_mutex); - key_prefix_seq|= KEY_PREFIX_MASK; - key_prefix= key_prefix_seq; + key_prefix_seq |= KEY_PREFIX_MASK; + key_prefix = key_prefix_seq; key_prefix_seq++; pthread_mutex_unlock(&ms_global.seq_mutex); return key_prefix; } /* ms_get_key_prefix */ - /** * get an unique udp request id * * @return an unique UDP request id */ -static uint32_t ms_get_udp_request_id(void) -{ +static uint32_t ms_get_udp_request_id(void) { return atomic_add_32_nv(&udp_request_id, 1); } - /** * initialize current task structure * * @param c, pointer of the concurrency */ -static void ms_task_init(ms_conn_t *c) -{ - c->curr_task.cmd= CMD_NULL; - c->curr_task.item= 0; - c->curr_task.verify= false; - c->curr_task.finish_verify= true; - c->curr_task.get_miss= true; - - c->curr_task.get_opt= 0; - c->curr_task.set_opt= 0; - c->curr_task.cycle_undo_get= 0; - c->curr_task.cycle_undo_set= 0; - c->curr_task.verified_get= 0; - c->curr_task.overwrite_set= 0; +static void ms_task_init(ms_conn_t *c) { + c->curr_task.cmd = CMD_NULL; + c->curr_task.item = 0; + c->curr_task.verify = false; + c->curr_task.finish_verify = true; + c->curr_task.get_miss = true; + + c->curr_task.get_opt = 0; + c->curr_task.set_opt = 0; + c->curr_task.cycle_undo_get = 0; + c->curr_task.cycle_undo_set = 0; + c->curr_task.verified_get = 0; + c->curr_task.overwrite_set = 0; } /* ms_task_init */ - /** * initialize udp for the connection structure * @@ -228,30 +203,27 @@ static void ms_task_init(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) -{ - c->hdrbuf= 0; - c->rudpbuf= 0; - c->udppkt= 0; +static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) { + c->hdrbuf = 0; + c->rudpbuf = 0; + c->udppkt = 0; - c->rudpsize= UDP_DATA_BUFFER_SIZE; - c->hdrsize= 0; + c->rudpsize = UDP_DATA_BUFFER_SIZE; + c->hdrsize = 0; - c->rudpbytes= 0; - c->packets= 0; - c->recvpkt= 0; - c->pktcurr= 0; - c->ordcurr= 0; + c->rudpbytes = 0; + c->packets = 0; + c->recvpkt = 0; + c->pktcurr = 0; + c->ordcurr = 0; - c->udp= is_udp; + c->udp = is_udp; - if (c->udp || (! c->udp && ms_setting.facebook_test)) - { - c->rudpbuf= (char *)malloc((size_t)c->rudpsize); - c->udppkt= (ms_udppkt_t *)malloc(MAX_UDP_PACKET * sizeof(ms_udppkt_t)); + if (c->udp || (!c->udp && ms_setting.facebook_test)) { + c->rudpbuf = (char *) malloc((size_t) c->rudpsize); + c->udppkt = (ms_udppkt_t *) malloc(MAX_UDP_PACKET * sizeof(ms_udppkt_t)); - if ((c->rudpbuf == NULL) || (c->udppkt == NULL)) - { + if ((c->rudpbuf == NULL) || (c->udppkt == NULL)) { if (c->rudpbuf != NULL) free(c->rudpbuf); if (c->udppkt != NULL) @@ -265,7 +237,6 @@ static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) return EXIT_SUCCESS; } /* ms_conn_udp_init */ - /** * initialize the connection structure * @@ -276,50 +247,40 @@ static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_conn_init(ms_conn_t *c, - const int init_state, - const int read_buffer_size, - const bool is_udp) -{ +static int ms_conn_init(ms_conn_t *c, const int init_state, const int read_buffer_size, + const bool is_udp) { assert(c != NULL); - c->rbuf= c->wbuf= 0; - c->iov= 0; - c->msglist= 0; + c->rbuf = c->wbuf = 0; + c->iov = 0; + c->msglist = 0; - c->rsize= read_buffer_size; - c->wsize= WRITE_BUFFER_SIZE; - c->iovsize= IOV_LIST_INITIAL; - c->msgsize= MSG_LIST_INITIAL; + c->rsize = read_buffer_size; + c->wsize = WRITE_BUFFER_SIZE; + c->iovsize = IOV_LIST_INITIAL; + c->msgsize = MSG_LIST_INITIAL; /* for replication, each connection need connect all the server */ - if (ms_setting.rep_write_srv > 0) - { - c->total_sfds= ms_setting.srv_cnt * ms_setting.sock_per_conn; + if (ms_setting.rep_write_srv > 0) { + c->total_sfds = ms_setting.srv_cnt * ms_setting.sock_per_conn; + } else { + c->total_sfds = ms_setting.sock_per_conn; } - else - { - c->total_sfds= ms_setting.sock_per_conn; - } - c->alive_sfds= 0; + c->alive_sfds = 0; - c->rbuf= (char *)malloc((size_t)c->rsize); - c->wbuf= (char *)malloc((size_t)c->wsize); - c->iov= (struct iovec *)malloc(sizeof(struct iovec) * (size_t)c->iovsize); - c->msglist= (struct msghdr *)malloc( - sizeof(struct msghdr) * (size_t)c->msgsize); - if (ms_setting.mult_key_num > 1) - { - c->mlget_task.mlget_item= (ms_mlget_task_item_t *) - malloc( - sizeof(ms_mlget_task_item_t) * (size_t)ms_setting.mult_key_num); + c->rbuf = (char *) malloc((size_t) c->rsize); + c->wbuf = (char *) malloc((size_t) c->wsize); + c->iov = (struct iovec *) malloc(sizeof(struct iovec) * (size_t) c->iovsize); + c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * (size_t) c->msgsize); + if (ms_setting.mult_key_num > 1) { + c->mlget_task.mlget_item = (ms_mlget_task_item_t *) malloc(sizeof(ms_mlget_task_item_t) + * (size_t) ms_setting.mult_key_num); } - c->tcpsfd= (int *)malloc((size_t)c->total_sfds * sizeof(int)); + c->tcpsfd = (int *) malloc((size_t) c->total_sfds * sizeof(int)); - if ((c->rbuf == NULL) || (c->wbuf == NULL) || (c->iov == NULL) - || (c->msglist == NULL) || (c->tcpsfd == NULL) - || ((ms_setting.mult_key_num > 1) - && (c->mlget_task.mlget_item == NULL))) + if ((c->rbuf == NULL) || (c->wbuf == NULL) || (c->iov == NULL) || (c->msglist == NULL) + || (c->tcpsfd == NULL) + || ((ms_setting.mult_key_num > 1) && (c->mlget_task.mlget_item == NULL))) { if (c->rbuf != NULL) free(c->rbuf); @@ -337,56 +298,50 @@ static int ms_conn_init(ms_conn_t *c, return -1; } - c->state= init_state; - c->rvbytes= 0; - c->rbytes= 0; - c->rcurr= c->rbuf; - c->wcurr= c->wbuf; - c->iovused= 0; - c->msgcurr= 0; - c->msgused= 0; - c->cur_idx= c->total_sfds; /* default index is a invalid value */ + c->state = init_state; + c->rvbytes = 0; + c->rbytes = 0; + c->rcurr = c->rbuf; + c->wcurr = c->wbuf; + c->iovused = 0; + c->msgcurr = 0; + c->msgused = 0; + c->cur_idx = c->total_sfds; /* default index is a invalid value */ - c->ctnwrite= false; - c->readval= false; - c->change_sfd= false; + c->ctnwrite = false; + c->readval = false; + c->change_sfd = false; - c->precmd.cmd= c->currcmd.cmd= CMD_NULL; - c->precmd.isfinish= true; /* default the previous command finished */ - c->currcmd.isfinish= false; - c->precmd.retstat= c->currcmd.retstat= MCD_FAILURE; - c->precmd.key_prefix= c->currcmd.key_prefix= 0; + c->precmd.cmd = c->currcmd.cmd = CMD_NULL; + c->precmd.isfinish = true; /* default the previous command finished */ + c->currcmd.isfinish = false; + c->precmd.retstat = c->currcmd.retstat = MCD_FAILURE; + c->precmd.key_prefix = c->currcmd.key_prefix = 0; - c->mlget_task.mlget_num= 0; - c->mlget_task.value_index= -1; /* default invalid value */ + c->mlget_task.mlget_num = 0; + c->mlget_task.value_index = -1; /* default invalid value */ - if (ms_setting.binary_prot_) - { - c->protocol= binary_prot; - } - else - { - c->protocol= ascii_prot; + if (ms_setting.binary_prot_) { + c->protocol = binary_prot; + } else { + c->protocol = ascii_prot; } /* initialize udp */ - if (ms_conn_udp_init(c, is_udp) != 0) - { + if (ms_conn_udp_init(c, is_udp) != 0) { return -1; } /* initialize task */ ms_task_init(c); - if (! (ms_setting.facebook_test && is_udp)) - { + if (!(ms_setting.facebook_test && is_udp)) { atomic_add_32(&ms_stats.active_conns, 1); } return EXIT_SUCCESS; } /* ms_conn_init */ - /** * when doing 100% get operation, it could preset some objects * to warmup the server. this function is used to initialize the @@ -394,22 +349,17 @@ static int ms_conn_init(ms_conn_t *c, * * @param c, pointer of the concurrency */ -static void ms_warmup_num_init(ms_conn_t *c) -{ +static void ms_warmup_num_init(ms_conn_t *c) { /* no set operation, preset all the items in the window */ - if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) - { - c->warmup_num= c->win_size; - c->remain_warmup_num= c->warmup_num; - } - else - { - c->warmup_num= 0; - c->remain_warmup_num= c->warmup_num; + if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR) { + c->warmup_num = c->win_size; + c->remain_warmup_num = c->warmup_num; + } else { + c->warmup_num = 0; + c->remain_warmup_num = c->warmup_num; } } /* ms_warmup_num_init */ - /** * each connection has an item window, this function initialize * the window. The window is used to generate task. @@ -418,43 +368,36 @@ static void ms_warmup_num_init(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_item_win_init(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - int exp_cnt= 0; - - c->win_size= (int)ms_setting.win_size; - c->set_cursor= 0; - c->exec_num= ms_thread->thread_ctx->exec_num_perconn; - c->remain_exec_num= c->exec_num; - - c->item_win= (ms_task_item_t *)malloc( - sizeof(ms_task_item_t) * (size_t)c->win_size); - if (c->item_win == NULL) - { +static int ms_item_win_init(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + int exp_cnt = 0; + + c->win_size = (int) ms_setting.win_size; + c->set_cursor = 0; + c->exec_num = ms_thread->thread_ctx->exec_num_perconn; + c->remain_exec_num = c->exec_num; + + c->item_win = (ms_task_item_t *) malloc(sizeof(ms_task_item_t) * (size_t) c->win_size); + if (c->item_win == NULL) { fprintf(stderr, "Can't allocate task item array for conn.\n"); return -1; } - memset(c->item_win, 0, sizeof(ms_task_item_t) * (size_t)c->win_size); + memset(c->item_win, 0, sizeof(ms_task_item_t) * (size_t) c->win_size); - for (int i= 0; i < c->win_size; i++) - { - c->item_win[i].key_size= (int)ms_setting.distr[i].key_size; - c->item_win[i].key_prefix= ms_get_key_prefix(); - c->item_win[i].key_suffix_offset= ms_setting.distr[i].key_offset; - c->item_win[i].value_size= (int)ms_setting.distr[i].value_size; - c->item_win[i].value_offset= INVALID_OFFSET; /* default in invalid offset */ - c->item_win[i].client_time= 0; + for (int i = 0; i < c->win_size; i++) { + c->item_win[i].key_size = (int) ms_setting.distr[i].key_size; + c->item_win[i].key_prefix = ms_get_key_prefix(); + c->item_win[i].key_suffix_offset = ms_setting.distr[i].key_offset; + c->item_win[i].value_size = (int) ms_setting.distr[i].value_size; + c->item_win[i].value_offset = INVALID_OFFSET; /* default in invalid offset */ + c->item_win[i].client_time = 0; /* set expire time base on the proportion */ - if (exp_cnt < ms_setting.exp_ver_per * i) - { - c->item_win[i].exp_time= FIXED_EXPIRE_TIME; + if (exp_cnt < ms_setting.exp_ver_per * i) { + c->item_win[i].exp_time = FIXED_EXPIRE_TIME; exp_cnt++; - } - else - { - c->item_win[i].exp_time= 0; + } else { + c->item_win[i].exp_time = 0; } } @@ -463,7 +406,6 @@ static int ms_item_win_init(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_item_win_init */ - /** * each connection structure can include one or more sock * handlers. this function create these socks and connect the @@ -473,82 +415,66 @@ static int ms_item_win_init(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_conn_sock_init(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); +static int ms_conn_sock_init(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); uint32_t i; int ret_sfd; - uint32_t srv_idx= 0; + uint32_t srv_idx = 0; assert(c != NULL); assert(c->tcpsfd != NULL); - for (i= 0; i < c->total_sfds; i++) - { - ret_sfd= 0; - if (ms_setting.rep_write_srv > 0) - { + for (i = 0; i < c->total_sfds; i++) { + ret_sfd = 0; + if (ms_setting.rep_write_srv > 0) { /* for replication, each connection need connect all the server */ - srv_idx= i % ms_setting.srv_cnt; - } - else - { + srv_idx = i % ms_setting.srv_cnt; + } else { /* all the connections in a thread connects the same server */ - srv_idx= ms_thread->thread_ctx->srv_idx; + srv_idx = ms_thread->thread_ctx->srv_idx; } if (ms_network_connect(c, ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, - ms_setting.udp, &ret_sfd) != 0) + ms_setting.servers[srv_idx].srv_port, ms_setting.udp, &ret_sfd) + != 0) { break; } - if (i == 0) - { - c->sfd= ret_sfd; + if (i == 0) { + c->sfd = ret_sfd; } - if (! ms_setting.udp) - { - c->tcpsfd[i]= ret_sfd; + if (!ms_setting.udp) { + c->tcpsfd[i] = ret_sfd; } c->alive_sfds++; } /* initialize udp sock handler if necessary */ - if (ms_setting.facebook_test) - { - ret_sfd= 0; + if (ms_setting.facebook_test) { + ret_sfd = 0; if (ms_network_connect(c, ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, - true, &ret_sfd) != 0) + ms_setting.servers[srv_idx].srv_port, true, &ret_sfd) + != 0) { - c->udpsfd= 0; - } - else - { - c->udpsfd= ret_sfd; + c->udpsfd = 0; + } else { + c->udpsfd = ret_sfd; } } - if ((i != c->total_sfds) || (ms_setting.facebook_test && (c->udpsfd == 0))) - { - if (ms_setting.udp) - { + if ((i != c->total_sfds) || (ms_setting.facebook_test && (c->udpsfd == 0))) { + if (ms_setting.udp) { close(c->sfd); - } - else - { - for (uint32_t j= 0; j < i; j++) - { + } else { + for (uint32_t j = 0; j < i; j++) { close(c->tcpsfd[j]); } } - if (c->udpsfd != 0) - { + if (c->udpsfd != 0) { close(c->udpsfd); } @@ -558,7 +484,6 @@ static int ms_conn_sock_init(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_conn_sock_init */ - /** * each connection is managed by libevent, this function * initialize the event of the connection structure. @@ -567,24 +492,21 @@ static int ms_conn_sock_init(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_conn_event_init(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - short event_flags= EV_WRITE | EV_PERSIST; +static int ms_conn_event_init(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + short event_flags = EV_WRITE | EV_PERSIST; - event_set(&c->event, c->sfd, event_flags, ms_event_handler, (void *)c); + event_set(&c->event, c->sfd, event_flags, ms_event_handler, (void *) c); event_base_set(ms_thread->base, &c->event); - c->ev_flags= event_flags; + c->ev_flags = event_flags; - if (event_add(&c->event, NULL) == -1) - { + if (event_add(&c->event, NULL) == -1) { return -1; } return EXIT_SUCCESS; } /* ms_conn_event_init */ - /** * setup a connection, each connection structure of each * thread must call this function to initialize. @@ -593,42 +515,34 @@ static int ms_conn_event_init(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_setup_conn(ms_conn_t *c) -{ - if (ms_item_win_init(c) != 0) - { +int ms_setup_conn(ms_conn_t *c) { + if (ms_item_win_init(c) != 0) { return -1; } - if (ms_conn_init(c, conn_write, DATA_BUFFER_SIZE, ms_setting.udp) != 0) - { + if (ms_conn_init(c, conn_write, DATA_BUFFER_SIZE, ms_setting.udp) != 0) { return -1; } - if (ms_conn_sock_init(c) != 0) - { + if (ms_conn_sock_init(c) != 0) { return -1; } - if (ms_conn_event_init(c) != 0) - { + if (ms_conn_event_init(c) != 0) { return -1; } return EXIT_SUCCESS; } /* ms_setup_conn */ - /** * Frees a connection. * * @param c, pointer of the concurrency */ -void ms_conn_free(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - if (c != NULL) - { +void ms_conn_free(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + if (c != NULL) { if (c->hdrbuf != NULL) free(c->hdrbuf); if (c->msglist != NULL) @@ -650,38 +564,32 @@ void ms_conn_free(ms_conn_t *c) if (c->tcpsfd != NULL) free(c->tcpsfd); - if (--ms_thread->nactive_conn == 0) - { + if (--ms_thread->nactive_conn == 0) { free(ms_thread->conn); } } } /* ms_conn_free */ - /** * close a connection * * @param c, pointer of the concurrency */ -static void ms_conn_close(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); +static void ms_conn_close(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); assert(c != NULL); /* delete the event, the socket and the connection */ event_del(&c->event); - for (uint32_t i= 0; i < c->total_sfds; i++) - { - if (c->tcpsfd[i] > 0) - { + for (uint32_t i = 0; i < c->total_sfds; i++) { + if (c->tcpsfd[i] > 0) { close(c->tcpsfd[i]); } } - c->sfd= 0; + c->sfd = 0; - if (ms_setting.facebook_test) - { + if (ms_setting.facebook_test) { close(c->udpsfd); } @@ -689,21 +597,18 @@ static void ms_conn_close(ms_conn_t *c) ms_conn_free(c); - if (ms_setting.run_time == 0) - { + if (ms_setting.run_time == 0) { pthread_mutex_lock(&ms_global.run_lock.lock); ms_global.run_lock.count++; pthread_cond_signal(&ms_global.run_lock.cond); pthread_mutex_unlock(&ms_global.run_lock.lock); } - if (ms_thread->nactive_conn == 0) - { + if (ms_thread->nactive_conn == 0) { pthread_exit(NULL); } } /* ms_conn_close */ - /** * create a new sock * @@ -711,12 +616,10 @@ static void ms_conn_close(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_new_socket(struct addrinfo *ai) -{ +static int ms_new_socket(struct addrinfo *ai) { int sfd; - if ((sfd= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) - { + if ((sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { fprintf(stderr, "socket() error: %s.\n", strerror(errno)); return -1; } @@ -724,47 +627,39 @@ static int ms_new_socket(struct addrinfo *ai) return sfd; } /* ms_new_socket */ - /** * Sets a socket's send buffer size to the maximum allowed by the system. * * @param sfd, file descriptor of socket */ -static void ms_maximize_sndbuf(const int sfd) -{ - socklen_t intsize= sizeof(int); - unsigned int last_good= 0; +static void ms_maximize_sndbuf(const int sfd) { + socklen_t intsize = sizeof(int); + unsigned int last_good = 0; unsigned int min, max, avg; unsigned int old_size; /* Start with the default size. */ - if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) - { + if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { fprintf(stderr, "getsockopt(SO_SNDBUF)\n"); return; } /* Binary-search for the real maximum. */ - min= old_size; - max= MAX_SENDBUF_SIZE; + min = old_size; + max = MAX_SENDBUF_SIZE; - while (min <= max) - { - avg= ((unsigned int)(min + max)) / 2; - if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void *)&avg, intsize) == 0) - { - last_good= avg; - min= avg + 1; - } - else - { - max= avg - 1; + while (min <= max) { + avg = ((unsigned int) (min + max)) / 2; + if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (void *) &avg, intsize) == 0) { + last_good = avg; + min = avg + 1; + } else { + max = avg - 1; } } - (void)last_good; + (void) last_good; } /* ms_maximize_sndbuf */ - /** * socket connects the server * @@ -776,25 +671,18 @@ static void ms_maximize_sndbuf(const int sfd) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_network_connect(ms_conn_t *c, - char *srv_host_name, - const int srv_port, - const bool is_udp, - int *ret_sfd) -{ +static int ms_network_connect(ms_conn_t *c, char *srv_host_name, const int srv_port, + const bool is_udp, int *ret_sfd) { int sfd; - struct linger ling= - { - 0, 0 - }; + struct linger ling = {0, 0}; struct addrinfo *ai; struct addrinfo *next; struct addrinfo hints; char port_buf[NI_MAXSERV]; - int error; - int success= 0; + int error; + int success = 0; - int flags= 1; + int flags = 1; /* * the memset call clears nonstandard fields in some impementations @@ -802,27 +690,23 @@ static int ms_network_connect(ms_conn_t *c, */ memset(&hints, 0, sizeof(hints)); #ifdef AI_ADDRCONFIG - hints.ai_flags= AI_PASSIVE | AI_ADDRCONFIG; + hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; #else - hints.ai_flags= AI_PASSIVE; + hints.ai_flags = AI_PASSIVE; #endif /* AI_ADDRCONFIG */ - if (is_udp) - { - hints.ai_protocol= IPPROTO_UDP; - hints.ai_socktype= SOCK_DGRAM; - hints.ai_family= AF_INET; /* This left here because of issues with OSX 10.5 */ - } - else - { - hints.ai_family= AF_UNSPEC; - hints.ai_protocol= IPPROTO_TCP; - hints.ai_socktype= SOCK_STREAM; + if (is_udp) { + hints.ai_protocol = IPPROTO_UDP; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_family = AF_INET; /* This left here because of issues with OSX 10.5 */ + } else { + hints.ai_family = AF_UNSPEC; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_socktype = SOCK_STREAM; } snprintf(port_buf, NI_MAXSERV, "%d", srv_port); - error= getaddrinfo(srv_host_name, port_buf, &hints, &ai); - if (error != 0) - { + error = getaddrinfo(srv_host_name, port_buf, &hints, &ai); + if (error != 0) { if (error != EAI_SYSTEM) fprintf(stderr, "getaddrinfo(): %s.\n", gai_strerror(error)); else @@ -831,55 +715,41 @@ static int ms_network_connect(ms_conn_t *c, return -1; } - for (next= ai; next; next= next->ai_next) - { - if ((sfd= ms_new_socket(next)) == -1) - { + for (next = ai; next; next = next->ai_next) { + if ((sfd = ms_new_socket(next)) == -1) { freeaddrinfo(ai); return -1; } - setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags)); - if (is_udp) - { + setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *) &flags, sizeof(flags)); + if (is_udp) { ms_maximize_sndbuf(sfd); - } - else - { - setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, - sizeof(flags)); - setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling)); - setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, - sizeof(flags)); + } else { + setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *) &flags, sizeof(flags)); + setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *) &ling, sizeof(ling)); + setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *) &flags, sizeof(flags)); } - if (is_udp) - { - c->srv_recv_addr_size= sizeof(struct sockaddr); + if (is_udp) { + c->srv_recv_addr_size = sizeof(struct sockaddr); memcpy(&c->srv_recv_addr, next->ai_addr, c->srv_recv_addr_size); - } - else - { - if (connect(sfd, next->ai_addr, next->ai_addrlen) == -1) - { + } else { + if (connect(sfd, next->ai_addr, next->ai_addrlen) == -1) { close(sfd); freeaddrinfo(ai); return -1; } } - if (((flags= fcntl(sfd, F_GETFL, 0)) < 0) - || (fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0)) - { + if (((flags = fcntl(sfd, F_GETFL, 0)) < 0) || (fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0)) { fprintf(stderr, "setting O_NONBLOCK\n"); close(sfd); freeaddrinfo(ai); return -1; } - if (ret_sfd != NULL) - { - *ret_sfd= sfd; + if (ret_sfd != NULL) { + *ret_sfd = sfd; } success++; @@ -891,7 +761,6 @@ static int ms_network_connect(ms_conn_t *c, return success == 0; } /* ms_network_connect */ - /** * reconnect a disconnected sock * @@ -899,98 +768,77 @@ static int ms_network_connect(ms_conn_t *c, * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_reconn(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - uint32_t srv_idx= 0; - uint32_t srv_conn_cnt= 0; +static int ms_reconn(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + uint32_t srv_idx = 0; + uint32_t srv_conn_cnt = 0; - if (ms_setting.rep_write_srv > 0) - { - srv_idx= c->cur_idx % ms_setting.srv_cnt; - srv_conn_cnt= ms_setting.sock_per_conn * ms_setting.nconns; - } - else - { - srv_idx= ms_thread->thread_ctx->srv_idx; - srv_conn_cnt= ms_setting.nconns / ms_setting.srv_cnt; + if (ms_setting.rep_write_srv > 0) { + srv_idx = c->cur_idx % ms_setting.srv_cnt; + srv_conn_cnt = ms_setting.sock_per_conn * ms_setting.nconns; + } else { + srv_idx = ms_thread->thread_ctx->srv_idx; + srv_conn_cnt = ms_setting.nconns / ms_setting.srv_cnt; } /* close the old socket handler */ close(c->sfd); - c->tcpsfd[c->cur_idx]= 0; + c->tcpsfd[c->cur_idx] = 0; - if (atomic_add_32_nv(&ms_setting.servers[srv_idx].disconn_cnt, 1) - % srv_conn_cnt == 0) - { + if (atomic_add_32_nv(&ms_setting.servers[srv_idx].disconn_cnt, 1) % srv_conn_cnt == 0) { gettimeofday(&ms_setting.servers[srv_idx].disconn_time, NULL); - fprintf(stderr, "Server %s:%d disconnect\n", - ms_setting.servers[srv_idx].srv_host_name, + fprintf(stderr, "Server %s:%d disconnect\n", ms_setting.servers[srv_idx].srv_host_name, ms_setting.servers[srv_idx].srv_port); } - if (ms_setting.rep_write_srv > 0) - { - uint32_t i= 0; + if (ms_setting.rep_write_srv > 0) { + uint32_t i = 0; - for (i= 0; i < c->total_sfds; i++) - { - if (c->tcpsfd[i] != 0) - { + for (i = 0; i < c->total_sfds; i++) { + if (c->tcpsfd[i] != 0) { break; } } /* all socks disconnect */ - if (i == c->total_sfds) - { + if (i == c->total_sfds) { return -1; } - } - else - { - do - { + } else { + do { /* reconnect success, break the loop */ if (ms_network_connect(c, ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, - ms_setting.udp, &c->sfd) == 0) + ms_setting.servers[srv_idx].srv_port, ms_setting.udp, &c->sfd) + == 0) { - c->tcpsfd[c->cur_idx]= c->sfd; - if (atomic_add_32_nv(&ms_setting.servers[srv_idx].reconn_cnt, 1) - % (uint32_t)srv_conn_cnt == 0) - { + c->tcpsfd[c->cur_idx] = c->sfd; + if (atomic_add_32_nv(&ms_setting.servers[srv_idx].reconn_cnt, 1) % (uint32_t) srv_conn_cnt + == 0) { gettimeofday(&ms_setting.servers[srv_idx].reconn_time, NULL); - int reconn_time= - (int)(ms_setting.servers[srv_idx].reconn_time.tv_sec - - ms_setting.servers[srv_idx].disconn_time - .tv_sec); + int reconn_time = (int) (ms_setting.servers[srv_idx].reconn_time.tv_sec + - ms_setting.servers[srv_idx].disconn_time.tv_sec); fprintf(stderr, "Server %s:%d reconnect after %ds\n", - ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, reconn_time); + ms_setting.servers[srv_idx].srv_host_name, ms_setting.servers[srv_idx].srv_port, + reconn_time); } break; } - if (ms_setting.rep_write_srv == 0 && c->total_sfds > 0) - { + if (ms_setting.rep_write_srv == 0 && c->total_sfds > 0) { /* wait a second and reconnect */ sleep(1); } - } - while (ms_setting.rep_write_srv == 0 && c->total_sfds > 0); + } while (ms_setting.rep_write_srv == 0 && c->total_sfds > 0); } - if ((c->total_sfds > 1) && (c->tcpsfd[c->cur_idx] == 0)) - { - c->sfd= 0; + if ((c->total_sfds > 1) && (c->tcpsfd[c->cur_idx] == 0)) { + c->sfd = 0; c->alive_sfds--; } return EXIT_SUCCESS; } /* ms_reconn */ - /** * reconnect several disconnected socks in the connection * structure, the ever-1-second timer of the thread will check @@ -1001,25 +849,21 @@ static int ms_reconn(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_reconn_socks(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - uint32_t srv_idx= 0; - int ret_sfd= 0; - uint32_t srv_conn_cnt= 0; +int ms_reconn_socks(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + uint32_t srv_idx = 0; + int ret_sfd = 0; + uint32_t srv_conn_cnt = 0; struct timeval cur_time; assert(c != NULL); - if ((c->total_sfds == 1) || (c->total_sfds == c->alive_sfds)) - { + if ((c->total_sfds == 1) || (c->total_sfds == c->alive_sfds)) { return EXIT_SUCCESS; } - for (uint32_t i= 0; i < c->total_sfds; i++) - { - if (c->tcpsfd[i] == 0) - { + for (uint32_t i = 0; i < c->total_sfds; i++) { + if (c->tcpsfd[i] == 0) { gettimeofday(&cur_time, NULL); /** @@ -1028,41 +872,33 @@ int ms_reconn_socks(ms_conn_t *c) * block at connect() function and the work threads can't work * in this interval. */ - if (cur_time.tv_sec - - ms_setting.servers[srv_idx].disconn_time.tv_sec < 5) - { + if (cur_time.tv_sec - ms_setting.servers[srv_idx].disconn_time.tv_sec < 5) { break; } - if (ms_setting.rep_write_srv > 0) - { - srv_idx= i % ms_setting.srv_cnt; - srv_conn_cnt= ms_setting.sock_per_conn * ms_setting.nconns; - } - else - { - srv_idx= ms_thread->thread_ctx->srv_idx; - srv_conn_cnt= ms_setting.nconns / ms_setting.srv_cnt; + if (ms_setting.rep_write_srv > 0) { + srv_idx = i % ms_setting.srv_cnt; + srv_conn_cnt = ms_setting.sock_per_conn * ms_setting.nconns; + } else { + srv_idx = ms_thread->thread_ctx->srv_idx; + srv_conn_cnt = ms_setting.nconns / ms_setting.srv_cnt; } if (ms_network_connect(c, ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, - ms_setting.udp, &ret_sfd) == 0) + ms_setting.servers[srv_idx].srv_port, ms_setting.udp, &ret_sfd) + == 0) { - c->tcpsfd[i]= ret_sfd; + c->tcpsfd[i] = ret_sfd; c->alive_sfds++; - if (atomic_add_32_nv(&ms_setting.servers[srv_idx].reconn_cnt, 1) - % (uint32_t)srv_conn_cnt == 0) - { + if (atomic_add_32_nv(&ms_setting.servers[srv_idx].reconn_cnt, 1) % (uint32_t) srv_conn_cnt + == 0) { gettimeofday(&ms_setting.servers[srv_idx].reconn_time, NULL); - int reconn_time= - (int)(ms_setting.servers[srv_idx].reconn_time.tv_sec - - ms_setting.servers[srv_idx].disconn_time - .tv_sec); + int reconn_time = (int) (ms_setting.servers[srv_idx].reconn_time.tv_sec + - ms_setting.servers[srv_idx].disconn_time.tv_sec); fprintf(stderr, "Server %s:%d reconnect after %ds\n", - ms_setting.servers[srv_idx].srv_host_name, - ms_setting.servers[srv_idx].srv_port, reconn_time); + ms_setting.servers[srv_idx].srv_host_name, ms_setting.servers[srv_idx].srv_port, + reconn_time); } } } @@ -1071,7 +907,6 @@ int ms_reconn_socks(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_reconn_socks */ - /** * Tokenize the command string by replacing whitespace with '\0' and update * the token array tokens with pointer to start of each token and length. @@ -1095,45 +930,35 @@ int ms_reconn_socks(ms_conn_t *c) * * @return int, the number of tokens */ -static int ms_tokenize_command(char *command, - token_t *tokens, - const int max_tokens) -{ +static int ms_tokenize_command(char *command, token_t *tokens, const int max_tokens) { char *s, *e; - int ntokens= 0; + int ntokens = 0; assert(command != NULL && tokens != NULL && max_tokens > 1); - for (s= e= command; ntokens < max_tokens - 1; ++e) - { - if (*e == ' ') - { - if (s != e) - { - tokens[ntokens].value= s; - tokens[ntokens].length= (size_t)(e - s); + for (s = e = command; ntokens < max_tokens - 1; ++e) { + if (*e == ' ') { + if (s != e) { + tokens[ntokens].value = s; + tokens[ntokens].length = (size_t)(e - s); ntokens++; - *e= '\0'; + *e = '\0'; } - s= e + 1; - } - else if (*e == '\0') - { - if (s != e) - { - tokens[ntokens].value= s; - tokens[ntokens].length= (size_t)(e - s); + s = e + 1; + } else if (*e == '\0') { + if (s != e) { + tokens[ntokens].value = s; + tokens[ntokens].length = (size_t)(e - s); ntokens++; } - break; /* string end */ + break; /* string end */ } } return ntokens; } /* ms_tokenize_command */ - /** * parse the response of server. * @@ -1143,11 +968,10 @@ static int ms_tokenize_command(char *command, * @return int, if the command completed return EXIT_SUCCESS, else return * -1 */ -static int ms_ascii_process_line(ms_conn_t *c, char *command) -{ - int ret= 0; +static int ms_ascii_process_line(ms_conn_t *c, char *command) { + int ret = 0; int64_t value_len; - char *buffer= command; + char *buffer = command; assert(c != NULL); @@ -1156,17 +980,14 @@ static int ms_ascii_process_line(ms_conn_t *c, char *command) * then continue in ms_complete_nread(). */ - switch (buffer[0]) - { - case 'V': /* VALUE || VERSION */ - if (buffer[1] == 'A') /* VALUE */ - { + switch (buffer[0]) { + case 'V': /* VALUE || VERSION */ + if (buffer[1] == 'A') /* VALUE */ { token_t tokens[MAX_TOKENS]; ms_tokenize_command(command, tokens, MAX_TOKENS); - errno= 0; - value_len= strtol(tokens[VALUELEN_TOKEN].value, NULL, 10); - if (errno != 0) - { + errno = 0; + value_len = strtol(tokens[VALUELEN_TOKEN].value, NULL, 10); + if (errno != 0) { printf("<%d ERROR %s\n", c->sfd, strerror(errno)); } memcpy(&c->currcmd.key_prefix, tokens[KEY_TOKEN].value, sizeof(c->currcmd.key_prefix)); @@ -1178,144 +999,112 @@ static int ms_ascii_process_line(ms_conn_t *c, char *command) * We are null terminating through, which will most likely make * some people lazy about using the return length. */ - c->rvbytes= (int)(value_len + 2); - c->readval= true; - ret= -1; + c->rvbytes = (int) (value_len + 2); + c->readval = true; + ret = -1; } break; - case 'O': /* OK */ - c->currcmd.retstat= MCD_SUCCESS; - break; + case 'O': /* OK */ c->currcmd.retstat = MCD_SUCCESS; break; - case 'S': /* STORED STATS SERVER_ERROR */ - if (buffer[2] == 'A') /* STORED STATS */ - { /* STATS*/ - c->currcmd.retstat= MCD_STAT; - } - else if (buffer[1] == 'E') - { + case 'S': /* STORED STATS SERVER_ERROR */ + if (buffer[2] == 'A') /* STORED STATS */ { /* STATS*/ + c->currcmd.retstat = MCD_STAT; + } else if (buffer[1] == 'E') { /* SERVER_ERROR */ printf("<%d %s\n", c->sfd, buffer); - c->currcmd.retstat= MCD_SERVER_ERROR; - } - else if (buffer[1] == 'T') - { + c->currcmd.retstat = MCD_SERVER_ERROR; + } else if (buffer[1] == 'T') { /* STORED */ - c->currcmd.retstat= MCD_STORED; - } - else - { - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + c->currcmd.retstat = MCD_STORED; + } else { + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; } break; - case 'D': /* DELETED DATA */ - if (buffer[1] == 'E') - { - c->currcmd.retstat= MCD_DELETED; - } - else - { - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + case 'D': /* DELETED DATA */ + if (buffer[1] == 'E') { + c->currcmd.retstat = MCD_DELETED; + } else { + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; } break; - case 'N': /* NOT_FOUND NOT_STORED*/ - if (buffer[4] == 'F') - { - c->currcmd.retstat= MCD_NOTFOUND; - } - else if (buffer[4] == 'S') - { + case 'N': /* NOT_FOUND NOT_STORED*/ + if (buffer[4] == 'F') { + c->currcmd.retstat = MCD_NOTFOUND; + } else if (buffer[4] == 'S') { printf("<%d %s\n", c->sfd, buffer); - c->currcmd.retstat= MCD_NOTSTORED; - } - else - { - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + c->currcmd.retstat = MCD_NOTSTORED; + } else { + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; } break; - case 'E': /* PROTOCOL ERROR or END */ - if (buffer[1] == 'N') - { + case 'E': /* PROTOCOL ERROR or END */ + if (buffer[1] == 'N') { /* END */ - c->currcmd.retstat= MCD_END; - } - else if (buffer[1] == 'R') - { + c->currcmd.retstat = MCD_END; + } else if (buffer[1] == 'R') { printf("<%d ERROR\n", c->sfd); - c->currcmd.retstat= MCD_PROTOCOL_ERROR; - } - else if (buffer[1] == 'X') - { - c->currcmd.retstat= MCD_DATA_EXISTS; + c->currcmd.retstat = MCD_PROTOCOL_ERROR; + } else if (buffer[1] == 'X') { + c->currcmd.retstat = MCD_DATA_EXISTS; printf("<%d %s\n", c->sfd, buffer); - } - else - { - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + } else { + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; } break; - case 'C': /* CLIENT ERROR */ + case 'C': /* CLIENT ERROR */ printf("<%d %s\n", c->sfd, buffer); - c->currcmd.retstat= MCD_CLIENT_ERROR; + c->currcmd.retstat = MCD_CLIENT_ERROR; break; - default: - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; - break; + default: c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; break; } /* switch */ return ret; } /* ms_ascii_process_line */ - /** * after one operation completes, reset the concurrency * * @param c, pointer of the concurrency * @param timeout, whether it's timeout */ -void ms_reset_conn(ms_conn_t *c, bool timeout) -{ +void ms_reset_conn(ms_conn_t *c, bool timeout) { assert(c != NULL); - if (c->udp) - { - if ((c->packets > 0) && (c->packets < MAX_UDP_PACKET)) - { - memset(c->udppkt, 0, sizeof(ms_udppkt_t) * (size_t)c->packets); + if (c->udp) { + if ((c->packets > 0) && (c->packets < MAX_UDP_PACKET)) { + memset(c->udppkt, 0, sizeof(ms_udppkt_t) * (size_t) c->packets); } - c->packets= 0; - c->recvpkt= 0; - c->pktcurr= 0; - c->ordcurr= 0; - c->rudpbytes= 0; + c->packets = 0; + c->recvpkt = 0; + c->pktcurr = 0; + c->ordcurr = 0; + c->rudpbytes = 0; } - c->currcmd.isfinish= true; - c->ctnwrite= false; - c->rbytes= 0; - c->rcurr= c->rbuf; + c->currcmd.isfinish = true; + c->ctnwrite = false; + c->rbytes = 0; + c->rcurr = c->rbuf; c->msgcurr = 0; c->msgused = 0; c->iovused = 0; ms_conn_set_state(c, conn_write); - memcpy(&c->precmd, &c->currcmd, sizeof(ms_cmdstat_t)); /* replicate command state */ + memcpy(&c->precmd, &c->currcmd, sizeof(ms_cmdstat_t)); /* replicate command state */ - if (timeout) - { + if (timeout) { ms_drive_machine(c); } } /* ms_reset_conn */ - /** * if we have a complete line in the buffer, process it. * @@ -1323,63 +1112,49 @@ void ms_reset_conn(ms_conn_t *c, bool timeout) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_try_read_line(ms_conn_t *c) -{ - if (c->protocol == binary_prot) - { +static int ms_try_read_line(ms_conn_t *c) { + if (c->protocol == binary_prot) { /* Do we have the complete packet header? */ - if ((uint64_t)c->rbytes < sizeof(c->binary_header)) - { + if ((uint64_t) c->rbytes < sizeof(c->binary_header)) { /* need more data! */ return EXIT_SUCCESS; - } - else - { + } else { #ifdef NEED_ALIGN - if (((long)(c->rcurr)) % 8 != 0) - { + if (((long) (c->rcurr)) % 8 != 0) { /* must realign input buffer */ memmove(c->rbuf, c->rcurr, c->rbytes); - c->rcurr= c->rbuf; - if (settings.verbose) - { + c->rcurr = c->rbuf; + if (settings.verbose) { fprintf(stderr, "%d: Realign input buffer.\n", c->sfd); } } #endif protocol_binary_response_header *rsp; - rsp= (protocol_binary_response_header *)c->rcurr; + rsp = (protocol_binary_response_header *) c->rcurr; - c->binary_header= *rsp; - c->binary_header.response.extlen= rsp->response.extlen; - c->binary_header.response.keylen= ntohs(rsp->response.keylen); - c->binary_header.response.bodylen= ntohl(rsp->response.bodylen); - c->binary_header.response.status= ntohs(rsp->response.status); + c->binary_header = *rsp; + c->binary_header.response.extlen = rsp->response.extlen; + c->binary_header.response.keylen = ntohs(rsp->response.keylen); + c->binary_header.response.bodylen = ntohl(rsp->response.bodylen); + c->binary_header.response.status = ntohs(rsp->response.status); - if (c->binary_header.response.magic != PROTOCOL_BINARY_RES) - { - fprintf(stderr, "Invalid magic: %x\n", - c->binary_header.response.magic); + if (c->binary_header.response.magic != PROTOCOL_BINARY_RES) { + fprintf(stderr, "Invalid magic: %x\n", c->binary_header.response.magic); ms_conn_set_state(c, conn_closing); return EXIT_SUCCESS; } /* process this complete response */ - if (ms_bin_process_response(c) == 0) - { + if (ms_bin_process_response(c) == 0) { /* current operation completed */ ms_reset_conn(c, false); return -1; - } - else - { - c->rbytes-= (int32_t)sizeof(c->binary_header); - c->rcurr+= sizeof(c->binary_header); + } else { + c->rbytes -= (int32_t) sizeof(c->binary_header); + c->rcurr += sizeof(c->binary_header); } } - } - else - { + } else { char *el, *cont; assert(c != NULL); @@ -1388,31 +1163,27 @@ static int ms_try_read_line(ms_conn_t *c) if (c->rbytes == 0) return EXIT_SUCCESS; - el= memchr(c->rcurr, '\n', (size_t)c->rbytes); - if (! el) + el = memchr(c->rcurr, '\n', (size_t) c->rbytes); + if (!el) return EXIT_SUCCESS; - cont= el + 1; - if (((el - c->rcurr) > 1) && (*(el - 1) == '\r')) - { + cont = el + 1; + if (((el - c->rcurr) > 1) && (*(el - 1) == '\r')) { el--; } - *el= '\0'; + *el = '\0'; assert(cont <= (c->rcurr + c->rbytes)); /* process this complete line */ - if (ms_ascii_process_line(c, c->rcurr) == 0) - { + if (ms_ascii_process_line(c, c->rcurr) == 0) { /* current operation completed */ ms_reset_conn(c, false); return -1; - } - else - { + } else { /* current operation didn't complete */ - c->rbytes-= (int32_t)(cont - c->rcurr); - c->rcurr= cont; + c->rbytes -= (int32_t)(cont - c->rcurr); + c->rcurr = cont; } assert(c->rcurr <= (c->rbuf + c->rsize)); @@ -1421,7 +1192,6 @@ static int ms_try_read_line(ms_conn_t *c) return -1; } /* ms_try_read_line */ - /** * because the packet of UDP can't ensure the order, the * function is used to sort the received udp packet. @@ -1433,14 +1203,13 @@ static int ms_try_read_line(ms_conn_t *c) * @return int, if success, return the copy bytes, else return * -1 */ -static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) -{ - int len= 0; - int wbytes= 0; - uint16_t req_id= 0; - uint16_t seq_num= 0; - uint16_t packets= 0; - unsigned char *header= NULL; +static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) { + int len = 0; + int wbytes = 0; + uint16_t req_id = 0; + uint16_t seq_num = 0; + uint16_t packets = 0; + unsigned char *header = NULL; /* no enough data */ assert(c != NULL); @@ -1448,77 +1217,60 @@ static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) assert(c->rudpbytes >= UDP_HEADER_SIZE); /* calculate received packets count */ - if (c->rudpbytes % UDP_MAX_PAYLOAD_SIZE >= UDP_HEADER_SIZE) - { + if (c->rudpbytes % UDP_MAX_PAYLOAD_SIZE >= UDP_HEADER_SIZE) { /* the last packet has some data */ - c->recvpkt= c->rudpbytes / UDP_MAX_PAYLOAD_SIZE + 1; - } - else - { - c->recvpkt= c->rudpbytes / UDP_MAX_PAYLOAD_SIZE; + c->recvpkt = c->rudpbytes / UDP_MAX_PAYLOAD_SIZE + 1; + } else { + c->recvpkt = c->rudpbytes / UDP_MAX_PAYLOAD_SIZE; } /* get the total packets count if necessary */ - if (c->packets == 0) - { - c->packets= HEADER_TO_PACKETS((unsigned char *)c->rudpbuf); + if (c->packets == 0) { + c->packets = HEADER_TO_PACKETS((unsigned char *) c->rudpbuf); } /* build the ordered packet array */ - for (int i= c->pktcurr; i < c->recvpkt; i++) - { - header= (unsigned char *)c->rudpbuf + i * UDP_MAX_PAYLOAD_SIZE; - req_id= (uint16_t)HEADER_TO_REQID(header); + for (int i = c->pktcurr; i < c->recvpkt; i++) { + header = (unsigned char *) c->rudpbuf + i * UDP_MAX_PAYLOAD_SIZE; + req_id = (uint16_t) HEADER_TO_REQID(header); assert(req_id == c->request_id % (1 << 16)); - packets= (uint16_t)HEADER_TO_PACKETS(header); + packets = (uint16_t) HEADER_TO_PACKETS(header); assert(c->packets == HEADER_TO_PACKETS(header)); - seq_num= (uint16_t)HEADER_TO_SEQNUM(header); - c->udppkt[seq_num].header= header; - c->udppkt[seq_num].data= (char *)header + UDP_HEADER_SIZE; + seq_num = (uint16_t) HEADER_TO_SEQNUM(header); + c->udppkt[seq_num].header = header; + c->udppkt[seq_num].data = (char *) header + UDP_HEADER_SIZE; - if (i == c->recvpkt - 1) - { + if (i == c->recvpkt - 1) { /* last received packet */ - if (c->rudpbytes % UDP_MAX_PAYLOAD_SIZE == 0) - { - c->udppkt[seq_num].rbytes= UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE; + if (c->rudpbytes % UDP_MAX_PAYLOAD_SIZE == 0) { + c->udppkt[seq_num].rbytes = UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE; c->pktcurr++; + } else { + c->udppkt[seq_num].rbytes = c->rudpbytes % UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE; } - else - { - c->udppkt[seq_num].rbytes= c->rudpbytes % UDP_MAX_PAYLOAD_SIZE - - UDP_HEADER_SIZE; - } - } - else - { - c->udppkt[seq_num].rbytes= UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE; + } else { + c->udppkt[seq_num].rbytes = UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE; c->pktcurr++; } } - for (int i= c->ordcurr; i < c->recvpkt; i++) - { + for (int i = c->ordcurr; i < c->recvpkt; i++) { /* there is some data to copy */ - if ((c->udppkt[i].data != NULL) - && (c->udppkt[i].copybytes < c->udppkt[i].rbytes)) - { - header= c->udppkt[i].header; - len= c->udppkt[i].rbytes - c->udppkt[i].copybytes; - if (len > rbytes - wbytes) - { - len= rbytes - wbytes; + if ((c->udppkt[i].data != NULL) && (c->udppkt[i].copybytes < c->udppkt[i].rbytes)) { + header = c->udppkt[i].header; + len = c->udppkt[i].rbytes - c->udppkt[i].copybytes; + if (len > rbytes - wbytes) { + len = rbytes - wbytes; } assert(len <= rbytes - wbytes); assert(i == HEADER_TO_SEQNUM(header)); - memcpy(buf + wbytes, c->udppkt[i].data + c->udppkt[i].copybytes, - (size_t)len); - wbytes+= len; - c->udppkt[i].copybytes+= len; + memcpy(buf + wbytes, c->udppkt[i].data + c->udppkt[i].copybytes, (size_t) len); + wbytes += len; + c->udppkt[i].copybytes += len; if ((c->udppkt[i].copybytes == c->udppkt[i].rbytes) && (c->udppkt[i].rbytes == UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE)) @@ -1535,30 +1287,24 @@ static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) } /* no space to copy data */ - if (wbytes >= rbytes) - { + if (wbytes >= rbytes) { break; } /* it doesn't finish reading all the data of the packet from network */ - if ((i != c->recvpkt - 1) - && (c->udppkt[i].rbytes < UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE)) - { + if ((i != c->recvpkt - 1) && (c->udppkt[i].rbytes < UDP_MAX_PAYLOAD_SIZE - UDP_HEADER_SIZE)) { break; } - } - else - { + } else { /* no data to copy */ break; } } - (void)packets; + (void) packets; return wbytes == 0 ? -1 : wbytes; } /* ms_sort_udp_packet */ - /** * encapsulate upd read like tcp read * @@ -1569,77 +1315,64 @@ static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) * @return int, if success, return the read bytes, else return * -1 */ -static int ms_udp_read(ms_conn_t *c, char *buf, int len) -{ - int res= 0; - int avail= 0; - int rbytes= 0; - int copybytes= 0; +static int ms_udp_read(ms_conn_t *c, char *buf, int len) { + int res = 0; + int avail = 0; + int rbytes = 0; + int copybytes = 0; assert(c->udp); - while (1) - { - if (c->rudpbytes + UDP_MAX_PAYLOAD_SIZE > c->rudpsize) - { - char *new_rbuf= realloc(c->rudpbuf, (size_t)c->rudpsize * 2); - if (! new_rbuf) - { + while (1) { + if (c->rudpbytes + UDP_MAX_PAYLOAD_SIZE > c->rudpsize) { + char *new_rbuf = realloc(c->rudpbuf, (size_t) c->rudpsize * 2); + if (!new_rbuf) { fprintf(stderr, "Couldn't realloc input buffer.\n"); - c->rudpbytes= 0; /* ignore what we read */ + c->rudpbytes = 0; /* ignore what we read */ return -1; } - c->rudpbuf= new_rbuf; - c->rudpsize*= 2; + c->rudpbuf = new_rbuf; + c->rudpsize *= 2; } - avail= c->rudpsize - c->rudpbytes; + avail = c->rudpsize - c->rudpbytes; /* UDP each time read a packet, 1400 bytes */ - res= (int)read(c->sfd, c->rudpbuf + c->rudpbytes, (size_t)avail); + res = (int) read(c->sfd, c->rudpbuf + c->rudpbytes, (size_t) avail); - if (res > 0) - { + if (res > 0) { atomic_add_size(&ms_stats.bytes_read, res); - c->rudpbytes+= res; - rbytes+= res; - if (res == avail) - { + c->rudpbytes += res; + rbytes += res; + if (res == avail) { continue; - } - else - { + } else { break; } } - if (res == 0) - { + if (res == 0) { /* "connection" closed */ return res; } - if (res == -1) - { + if (res == -1) { /* no data to read */ return res; } } /* copy data to read buffer */ - if (rbytes > 0) - { - copybytes= ms_sort_udp_packet(c, buf, len); + if (rbytes > 0) { + copybytes = ms_sort_udp_packet(c, buf, len); } - if (copybytes == -1) - { + if (copybytes == -1) { atomic_add_size(&ms_stats.pkt_disorder, 1); } return copybytes; } /* ms_udp_read */ - /* * read from network as much as we can, handle buffer overflow and connection * close. @@ -1660,78 +1393,63 @@ static int ms_udp_read(ms_conn_t *c, char *buf, int len) * return EXIT_FAILURE if get data * return -1 if error happens */ -static int ms_try_read_network(ms_conn_t *c) -{ - int gotdata= 0; +static int ms_try_read_network(ms_conn_t *c) { + int gotdata = 0; int res; int64_t avail; assert(c != NULL); if ((c->rcurr != c->rbuf) - && (! c->readval || (c->rvbytes > c->rsize - (c->rcurr - c->rbuf)) + && (!c->readval || (c->rvbytes > c->rsize - (c->rcurr - c->rbuf)) || (c->readval && (c->rcurr - c->rbuf > c->rbytes)))) { - if (c->rbytes != 0) /* otherwise there's nothing to copy */ - memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); - c->rcurr= c->rbuf; + if (c->rbytes != 0) /* otherwise there's nothing to copy */ + memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); + c->rcurr = c->rbuf; } - while (1) - { - if (c->rbytes >= c->rsize) - { - char *new_rbuf= realloc(c->rbuf, (size_t)c->rsize * 2); - if (! new_rbuf) - { + while (1) { + if (c->rbytes >= c->rsize) { + char *new_rbuf = realloc(c->rbuf, (size_t) c->rsize * 2); + if (!new_rbuf) { fprintf(stderr, "Couldn't realloc input buffer.\n"); - c->rbytes= 0; /* ignore what we read */ + c->rbytes = 0; /* ignore what we read */ return -1; } - c->rcurr= c->rbuf= new_rbuf; - c->rsize*= 2; + c->rcurr = c->rbuf = new_rbuf; + c->rsize *= 2; } - avail= c->rsize - c->rbytes - (c->rcurr - c->rbuf); - if (avail == 0) - { + avail = c->rsize - c->rbytes - (c->rcurr - c->rbuf); + if (avail == 0) { break; } - if (c->udp) - { - res= (int32_t)ms_udp_read(c, c->rcurr + c->rbytes, (int32_t)avail); - } - else - { - res= (int)read(c->sfd, c->rcurr + c->rbytes, (size_t)avail); + if (c->udp) { + res = (int32_t) ms_udp_read(c, c->rcurr + c->rbytes, (int32_t) avail); + } else { + res = (int) read(c->sfd, c->rcurr + c->rbytes, (size_t) avail); } - if (res > 0) - { - if (! c->udp) - { + if (res > 0) { + if (!c->udp) { atomic_add_size(&ms_stats.bytes_read, res); } - gotdata= 1; - c->rbytes+= res; - if (res == avail) - { + gotdata = 1; + c->rbytes += res; + if (res == avail) { continue; - } - else - { + } else { break; } } - if (res == 0) - { + if (res == 0) { /* connection closed */ ms_conn_set_state(c, conn_closing); return -1; } - if (res == -1) - { + if (res == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; /* Should close on unhandled errors. */ @@ -1743,7 +1461,6 @@ static int ms_try_read_network(ms_conn_t *c) return gotdata; } /* ms_try_read_network */ - /** * after get the object from server, verify the value if * necessary. @@ -1753,21 +1470,14 @@ static int ms_try_read_network(ms_conn_t *c) * @param value, received value string * @param vlen, received value string length */ -static void ms_verify_value(ms_conn_t *c, - ms_mlget_task_item_t *mlget_item, - char *value, - int vlen) -{ - if (c->curr_task.verify) - { +static void ms_verify_value(ms_conn_t *c, ms_mlget_task_item_t *mlget_item, char *value, int vlen) { + if (c->curr_task.verify) { assert(c->curr_task.item->value_offset != INVALID_OFFSET); - char *orignval= &ms_setting.char_block[c->curr_task.item->value_offset]; - char *orignkey= - &ms_setting.char_block[c->curr_task.item->key_suffix_offset]; + char *orignval = &ms_setting.char_block[c->curr_task.item->value_offset]; + char *orignkey = &ms_setting.char_block[c->curr_task.item->key_suffix_offset]; /* verify expire time if necessary */ - if (c->curr_task.item->exp_time > 0) - { + if (c->curr_task.item->exp_time > 0) { struct timeval curr_time; gettimeofday(&curr_time, NULL); @@ -1777,14 +1487,11 @@ static void ms_verify_value(ms_conn_t *c, { atomic_add_size(&ms_stats.exp_get, 1); - if (ms_setting.verbose) - { + if (ms_setting.verbose) { char set_time[64]; char cur_time[64]; - strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&c->curr_task.item->client_time)); - strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&curr_time.tv_sec)); + strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&c->curr_task.item->client_time)); + strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&curr_time.tv_sec)); fprintf(stderr, "\n<%d expire time verification failed, " "object expired but get it now\n" @@ -1795,110 +1502,80 @@ static void ms_verify_value(ms_conn_t *c, "\texpected data: \n" "\treceived data len: %d\n" "\treceived data: %.*s\n", - c->sfd, - c->curr_task.item->key_size, - c->curr_task.item->key_prefix, - c->curr_task.item->key_size - (int)KEY_PREFIX_SIZE, - orignkey, - set_time, - cur_time, - (int)(curr_time.tv_sec - c->curr_task.item->client_time), - c->curr_task.item->exp_time, - vlen, - vlen, - value); + c->sfd, c->curr_task.item->key_size, c->curr_task.item->key_prefix, + c->curr_task.item->key_size - (int) KEY_PREFIX_SIZE, orignkey, set_time, cur_time, + (int) (curr_time.tv_sec - c->curr_task.item->client_time), + c->curr_task.item->exp_time, vlen, vlen, value); fflush(stderr); } } - } - else - { - if ((c->curr_task.item->value_size != vlen) - || (memcmp(orignval, value, (size_t)vlen) != 0)) + } else { + if ((c->curr_task.item->value_size != vlen) || (memcmp(orignval, value, (size_t) vlen) != 0)) { atomic_add_size(&ms_stats.vef_failed, 1); - if (ms_setting.verbose) - { + if (ms_setting.verbose) { fprintf(stderr, "\n<%d data verification failed\n" "\tkey len: %d\n" - "\tkey: %" PRIx64" %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data len: %d\n" "\treceived data: %.*s\n", - c->sfd, - c->curr_task.item->key_size, - c->curr_task.item->key_prefix, - c->curr_task.item->key_size - (int)KEY_PREFIX_SIZE, - orignkey, - c->curr_task.item->value_size, - c->curr_task.item->value_size, - orignval, - vlen, - vlen, - value); + c->sfd, c->curr_task.item->key_size, c->curr_task.item->key_prefix, + c->curr_task.item->key_size - (int) KEY_PREFIX_SIZE, orignkey, + c->curr_task.item->value_size, c->curr_task.item->value_size, orignval, vlen, + vlen, value); fflush(stderr); } } } - c->curr_task.finish_verify= true; + c->curr_task.finish_verify = true; - if (mlget_item != NULL) - { - mlget_item->finish_verify= true; + if (mlget_item != NULL) { + mlget_item->finish_verify = true; } } } /* ms_verify_value */ - /** * For ASCII protocol, after store the data into the local * buffer, run this function to handle the data. * * @param c, pointer of the concurrency */ -static void ms_ascii_complete_nread(ms_conn_t *c) -{ +static void ms_ascii_complete_nread(ms_conn_t *c) { assert(c != NULL); assert(c->rbytes >= c->rvbytes); assert(c->protocol == ascii_prot); - if (c->rvbytes > 2) - { - assert( - c->rcurr[c->rvbytes - 1] == '\n' && c->rcurr[c->rvbytes - 2] == '\r'); + if (c->rvbytes > 2) { + assert(c->rcurr[c->rvbytes - 1] == '\n' && c->rcurr[c->rvbytes - 2] == '\r'); } /* multi-get */ - ms_mlget_task_item_t *mlget_item= NULL; - if (((ms_setting.mult_key_num > 1) - && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) + ms_mlget_task_item_t *mlget_item = NULL; + if (((ms_setting.mult_key_num > 1) && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) || ((c->remain_exec_num == 0) && (c->mlget_task.mlget_num > 0))) { c->mlget_task.value_index++; - mlget_item= &c->mlget_task.mlget_item[c->mlget_task.value_index]; - - if (mlget_item->item->key_prefix == c->currcmd.key_prefix) - { - c->curr_task.item= mlget_item->item; - c->curr_task.verify= mlget_item->verify; - c->curr_task.finish_verify= mlget_item->finish_verify; - mlget_item->get_miss= false; - } - else - { + mlget_item = &c->mlget_task.mlget_item[c->mlget_task.value_index]; + + if (mlget_item->item->key_prefix == c->currcmd.key_prefix) { + c->curr_task.item = mlget_item->item; + c->curr_task.verify = mlget_item->verify; + c->curr_task.finish_verify = mlget_item->finish_verify; + mlget_item->get_miss = false; + } else { /* Try to find the task item in multi-get task array */ - for (int i= 0; i < c->mlget_task.mlget_num; i++) - { - mlget_item= &c->mlget_task.mlget_item[i]; - if (mlget_item->item->key_prefix == c->currcmd.key_prefix) - { - c->curr_task.item= mlget_item->item; - c->curr_task.verify= mlget_item->verify; - c->curr_task.finish_verify= mlget_item->finish_verify; - mlget_item->get_miss= false; + for (int i = 0; i < c->mlget_task.mlget_num; i++) { + mlget_item = &c->mlget_task.mlget_item[i]; + if (mlget_item->item->key_prefix == c->currcmd.key_prefix) { + c->curr_task.item = mlget_item->item; + c->curr_task.verify = mlget_item->verify; + c->curr_task.finish_verify = mlget_item->finish_verify; + mlget_item->get_miss = false; break; } @@ -1908,116 +1585,97 @@ static void ms_ascii_complete_nread(ms_conn_t *c) ms_verify_value(c, mlget_item, c->rcurr, c->rvbytes - 2); - c->curr_task.get_miss= false; - c->rbytes-= c->rvbytes; - c->rcurr= c->rcurr + c->rvbytes; + c->curr_task.get_miss = false; + c->rbytes -= c->rvbytes; + c->rcurr = c->rcurr + c->rvbytes; assert(c->rcurr <= (c->rbuf + c->rsize)); - c->readval= false; - c->rvbytes= 0; + c->readval = false; + c->rvbytes = 0; } /* ms_ascii_complete_nread */ - /** * For binary protocol, after store the data into the local * buffer, run this function to handle the data. * * @param c, pointer of the concurrency */ -static void ms_bin_complete_nread(ms_conn_t *c) -{ +static void ms_bin_complete_nread(ms_conn_t *c) { assert(c != NULL); assert(c->rbytes >= c->rvbytes); assert(c->protocol == binary_prot); - int extlen= c->binary_header.response.extlen; - int keylen= c->binary_header.response.keylen; - uint8_t opcode= c->binary_header.response.opcode; + int extlen = c->binary_header.response.extlen; + int keylen = c->binary_header.response.keylen; + uint8_t opcode = c->binary_header.response.opcode; /* not get command or not include value, just return */ - if (((opcode != PROTOCOL_BINARY_CMD_GET) - && (opcode != PROTOCOL_BINARY_CMD_GETQ)) + if (((opcode != PROTOCOL_BINARY_CMD_GET) && (opcode != PROTOCOL_BINARY_CMD_GETQ)) || (c->rvbytes <= extlen + keylen)) { /* get miss */ - if (c->binary_header.response.opcode == PROTOCOL_BINARY_CMD_GET) - { - c->currcmd.retstat= MCD_END; - c->curr_task.get_miss= true; + if (c->binary_header.response.opcode == PROTOCOL_BINARY_CMD_GET) { + c->currcmd.retstat = MCD_END; + c->curr_task.get_miss = true; } - c->readval= false; - c->rvbytes= 0; + c->readval = false; + c->rvbytes = 0; ms_reset_conn(c, false); return; } /* multi-get */ - ms_mlget_task_item_t *mlget_item= NULL; - if (((ms_setting.mult_key_num > 1) - && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) + ms_mlget_task_item_t *mlget_item = NULL; + if (((ms_setting.mult_key_num > 1) && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) || ((c->remain_exec_num == 0) && (c->mlget_task.mlget_num > 0))) { c->mlget_task.value_index++; - mlget_item= &c->mlget_task.mlget_item[c->mlget_task.value_index]; + mlget_item = &c->mlget_task.mlget_item[c->mlget_task.value_index]; - c->curr_task.item= mlget_item->item; - c->curr_task.verify= mlget_item->verify; - c->curr_task.finish_verify= mlget_item->finish_verify; - mlget_item->get_miss= false; + c->curr_task.item = mlget_item->item; + c->curr_task.verify = mlget_item->verify; + c->curr_task.finish_verify = mlget_item->finish_verify; + mlget_item->get_miss = false; } - ms_verify_value(c, - mlget_item, - c->rcurr + extlen + keylen, - c->rvbytes - extlen - keylen); + ms_verify_value(c, mlget_item, c->rcurr + extlen + keylen, c->rvbytes - extlen - keylen); - c->currcmd.retstat= MCD_END; - c->curr_task.get_miss= false; - c->rbytes-= c->rvbytes; - c->rcurr= c->rcurr + c->rvbytes; + c->currcmd.retstat = MCD_END; + c->curr_task.get_miss = false; + c->rbytes -= c->rvbytes; + c->rcurr = c->rcurr + c->rvbytes; assert(c->rcurr <= (c->rbuf + c->rsize)); - c->readval= false; - c->rvbytes= 0; + c->readval = false; + c->rvbytes = 0; - if (ms_setting.mult_key_num > 1) - { + if (ms_setting.mult_key_num > 1) { /* multi-get have check all the item */ - if (c->mlget_task.value_index == c->mlget_task.mlget_num - 1) - { + if (c->mlget_task.value_index == c->mlget_task.mlget_num - 1) { ms_reset_conn(c, false); } - } - else - { + } else { /* single get */ ms_reset_conn(c, false); } } /* ms_bin_complete_nread */ - /** * we get here after reading the value of get commands. * * @param c, pointer of the concurrency */ -static void ms_complete_nread(ms_conn_t *c) -{ +static void ms_complete_nread(ms_conn_t *c) { assert(c != NULL); assert(c->rbytes >= c->rvbytes); - assert(c->protocol == ascii_prot - || c->protocol == binary_prot); + assert(c->protocol == ascii_prot || c->protocol == binary_prot); - if (c->protocol == binary_prot) - { + if (c->protocol == binary_prot) { ms_bin_complete_nread(c); - } - else - { + } else { ms_ascii_complete_nread(c); } } /* ms_complete_nread */ - /** * Adds a message header to a connection. * @@ -2025,24 +1683,21 @@ static void ms_complete_nread(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_add_msghdr(ms_conn_t *c) -{ +static int ms_add_msghdr(ms_conn_t *c) { struct msghdr *msg; assert(c != NULL); - if (c->msgsize == c->msgused) - { - msg= - realloc(c->msglist, (size_t)c->msgsize * 2 * sizeof(struct msghdr)); - if (! msg) + if (c->msgsize == c->msgused) { + msg = realloc(c->msglist, (size_t) c->msgsize * 2 * sizeof(struct msghdr)); + if (!msg) return -1; - c->msglist= msg; - c->msgsize*= 2; + c->msglist = msg; + c->msgsize *= 2; } - msg= c->msglist + c->msgused; + msg = c->msglist + c->msgused; /** * this wipes msg_iovlen, msg_control, msg_controllen, and @@ -2050,19 +1705,17 @@ static int ms_add_msghdr(ms_conn_t *c) */ memset(msg, 0, sizeof(struct msghdr)); - msg->msg_iov= &c->iov[c->iovused]; + msg->msg_iov = &c->iov[c->iovused]; - if (c->udp && (c->srv_recv_addr_size > 0)) - { - msg->msg_name= &c->srv_recv_addr; - msg->msg_namelen= c->srv_recv_addr_size; + if (c->udp && (c->srv_recv_addr_size > 0)) { + msg->msg_name = &c->srv_recv_addr; + msg->msg_namelen = c->srv_recv_addr_size; } - c->msgbytes= 0; + c->msgbytes = 0; c->msgused++; - if (c->udp) - { + if (c->udp) { /* Leave room for the UDP header, which we'll fill in later. */ return ms_add_iov(c, NULL, UDP_HEADER_SIZE); } @@ -2070,7 +1723,6 @@ static int ms_add_msghdr(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_add_msghdr */ - /** * Ensures that there is room for another structure iovec in a connection's * iov list. @@ -2079,35 +1731,29 @@ static int ms_add_msghdr(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_ensure_iov_space(ms_conn_t *c) -{ +static int ms_ensure_iov_space(ms_conn_t *c) { assert(c != NULL); - if (c->iovused >= c->iovsize) - { + if (c->iovused >= c->iovsize) { int i, iovnum; - struct iovec *new_iov= (struct iovec *)realloc(c->iov, - ((size_t)c->iovsize - * 2) - * sizeof(struct iovec)); - if (! new_iov) + struct iovec *new_iov = + (struct iovec *) realloc(c->iov, ((size_t) c->iovsize * 2) * sizeof(struct iovec)); + if (!new_iov) return -1; - c->iov= new_iov; - c->iovsize*= 2; + c->iov = new_iov; + c->iovsize *= 2; /* Point all the msghdr structures at the new list. */ - for (i= 0, iovnum= 0; i < c->msgused; i++) - { - c->msglist[i].msg_iov= &c->iov[iovnum]; - iovnum+= (int)c->msglist[i].msg_iovlen; + for (i = 0, iovnum = 0; i < c->msgused; i++) { + c->msglist[i].msg_iov = &c->iov[iovnum]; + iovnum += (int) c->msglist[i].msg_iovlen; } } return EXIT_SUCCESS; } /* ms_ensure_iov_space */ - /** * Adds data to the list of pending data that will be written out to a * connection. @@ -2118,30 +1764,27 @@ static int ms_ensure_iov_space(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_add_iov(ms_conn_t *c, const void *buf, int len) -{ +static int ms_add_iov(ms_conn_t *c, const void *buf, int len) { struct msghdr *m; - int leftover; + int leftover; bool limit_to_mtu; assert(c != NULL); - do - { - m= &c->msglist[c->msgused - 1]; + do { + m = &c->msglist[c->msgused - 1]; /* * Limit UDP packets, to UDP_MAX_PAYLOAD_SIZE bytes. */ - limit_to_mtu= c->udp; + limit_to_mtu = c->udp; #ifdef IOV_MAX /* We may need to start a new msghdr if this one is full. */ - if ((m->msg_iovlen == IOV_MAX) - || (limit_to_mtu && (c->msgbytes >= UDP_MAX_SEND_PAYLOAD_SIZE))) + if ((m->msg_iovlen == IOV_MAX) || (limit_to_mtu && (c->msgbytes >= UDP_MAX_SEND_PAYLOAD_SIZE))) { ms_add_msghdr(c); - m= &c->msglist[c->msgused - 1]; + m = &c->msglist[c->msgused - 1]; } #endif @@ -2149,33 +1792,28 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) return -1; /* If the fragment is too big to fit in the datagram, split it up */ - if (limit_to_mtu && (len + c->msgbytes > UDP_MAX_SEND_PAYLOAD_SIZE)) - { - leftover= len + c->msgbytes - UDP_MAX_SEND_PAYLOAD_SIZE; - len-= leftover; - } - else - { - leftover= 0; + if (limit_to_mtu && (len + c->msgbytes > UDP_MAX_SEND_PAYLOAD_SIZE)) { + leftover = len + c->msgbytes - UDP_MAX_SEND_PAYLOAD_SIZE; + len -= leftover; + } else { + leftover = 0; } - m= &c->msglist[c->msgused - 1]; - m->msg_iov[m->msg_iovlen].iov_base= (void *)buf; - m->msg_iov[m->msg_iovlen].iov_len= (size_t)len; + m = &c->msglist[c->msgused - 1]; + m->msg_iov[m->msg_iovlen].iov_base = (void *) buf; + m->msg_iov[m->msg_iovlen].iov_len = (size_t) len; - c->msgbytes+= len; + c->msgbytes += len; c->iovused++; m->msg_iovlen++; - buf= ((char *)buf) + len; - len= leftover; - } - while (leftover > 0); + buf = ((char *) buf) + len; + len = leftover; + } while (leftover > 0); return EXIT_SUCCESS; } /* ms_add_iov */ - /** * Constructs a set of UDP headers and attaches them to the outgoing messages. * @@ -2183,59 +1821,51 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_udp_headers(ms_conn_t *c) -{ +static int ms_build_udp_headers(ms_conn_t *c) { int i; unsigned char *hdr; assert(c != NULL); - c->request_id= ms_get_udp_request_id(); + c->request_id = ms_get_udp_request_id(); - if (c->msgused > c->hdrsize) - { + if (c->msgused > c->hdrsize) { void *new_hdrbuf; if (c->hdrbuf) - new_hdrbuf= realloc(c->hdrbuf, - (size_t)c->msgused * 2 * UDP_HEADER_SIZE); + new_hdrbuf = realloc(c->hdrbuf, (size_t) c->msgused * 2 * UDP_HEADER_SIZE); else - new_hdrbuf= malloc((size_t)c->msgused * 2 * UDP_HEADER_SIZE); - if (! new_hdrbuf) + new_hdrbuf = malloc((size_t) c->msgused * 2 * UDP_HEADER_SIZE); + if (!new_hdrbuf) return -1; - c->hdrbuf= (unsigned char *)new_hdrbuf; - c->hdrsize= c->msgused * 2; + c->hdrbuf = (unsigned char *) new_hdrbuf; + c->hdrsize = c->msgused * 2; } /* If this is a multi-packet request, drop it. */ - if (c->udp && (c->msgused > 1)) - { + if (c->udp && (c->msgused > 1)) { fprintf(stderr, "multi-packet request for UDP not supported.\n"); return -1; } - hdr= c->hdrbuf; - for (i= 0; i < c->msgused; i++) - { - c->msglist[i].msg_iov[0].iov_base= (void *)hdr; - c->msglist[i].msg_iov[0].iov_len= UDP_HEADER_SIZE; - *hdr++= (unsigned char)(c->request_id / 256); - *hdr++= (unsigned char)(c->request_id % 256); - *hdr++= (unsigned char)(i / 256); - *hdr++= (unsigned char)(i % 256); - *hdr++= (unsigned char)(c->msgused / 256); - *hdr++= (unsigned char)(c->msgused % 256); - *hdr++= (unsigned char)1; /* support facebook memcached */ - *hdr++= (unsigned char)0; - assert(hdr == - ((unsigned char *)c->msglist[i].msg_iov[0].iov_base - + UDP_HEADER_SIZE)); + hdr = c->hdrbuf; + for (i = 0; i < c->msgused; i++) { + c->msglist[i].msg_iov[0].iov_base = (void *) hdr; + c->msglist[i].msg_iov[0].iov_len = UDP_HEADER_SIZE; + *hdr++ = (unsigned char) (c->request_id / 256); + *hdr++ = (unsigned char) (c->request_id % 256); + *hdr++ = (unsigned char) (i / 256); + *hdr++ = (unsigned char) (i % 256); + *hdr++ = (unsigned char) (c->msgused / 256); + *hdr++ = (unsigned char) (c->msgused % 256); + *hdr++ = (unsigned char) 1; /* support facebook memcached */ + *hdr++ = (unsigned char) 0; + assert(hdr == ((unsigned char *) c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE)); } return EXIT_SUCCESS; } /* ms_build_udp_headers */ - /** * Transmit the next chunk of data from our list of msgbuf structures. * @@ -2246,49 +1876,40 @@ static int ms_build_udp_headers(ms_conn_t *c) * TRANSMIT_SOFT_ERROR Can't write any more right now. * TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) */ -static int ms_transmit(ms_conn_t *c) -{ +static int ms_transmit(ms_conn_t *c) { assert(c != NULL); - if ((c->msgcurr < c->msgused) - && (c->msglist[c->msgcurr].msg_iovlen == 0)) - { + if ((c->msgcurr < c->msgused) && (c->msglist[c->msgcurr].msg_iovlen == 0)) { /* Finished writing the current msg; advance to the next. */ c->msgcurr++; } - if (c->msgcurr < c->msgused) - { + if (c->msgcurr < c->msgused) { ssize_t res; - struct msghdr *m= &c->msglist[c->msgcurr]; + struct msghdr *m = &c->msglist[c->msgcurr]; - res= sendmsg(c->sfd, m, 0); - if (res > 0) - { + res = sendmsg(c->sfd, m, 0); + if (res > 0) { atomic_add_size(&ms_stats.bytes_written, res); /* We've written some of the data. Remove the completed * iovec entries from the list of pending writes. */ - while (m->msg_iovlen > 0 && res >= (ssize_t)m->msg_iov->iov_len) - { - res-= (ssize_t)m->msg_iov->iov_len; + while (m->msg_iovlen > 0 && res >= (ssize_t) m->msg_iov->iov_len) { + res -= (ssize_t) m->msg_iov->iov_len; m->msg_iovlen--; m->msg_iov++; } /* Might have written just part of the last iovec entry; * adjust it so the next write will do the rest. */ - if (res > 0) - { - m->msg_iov->iov_base= (void *)((unsigned char *)m->msg_iov->iov_base + res); - m->msg_iov->iov_len-= (size_t)res; + if (res > 0) { + m->msg_iov->iov_base = (void *) ((unsigned char *) m->msg_iov->iov_base + res); + m->msg_iov->iov_len -= (size_t) res; } return TRANSMIT_INCOMPLETE; } - if ((res == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) - { - if (! ms_update_event(c, EV_WRITE | EV_PERSIST)) - { + if ((res == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { + if (!ms_update_event(c, EV_WRITE | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); return TRANSMIT_HARD_ERROR; @@ -2302,14 +1923,11 @@ static int ms_transmit(ms_conn_t *c) ms_conn_set_state(c, conn_closing); return TRANSMIT_HARD_ERROR; - } - else - { + } else { return TRANSMIT_COMPLETE; } } /* ms_transmit */ - /** * Shrinks a connection's buffers if they're too big. This prevents * periodic large "mget" response from server chewing lots of client @@ -2320,71 +1938,59 @@ static int ms_transmit(ms_conn_t *c) * * @param c, pointer of the concurrency */ -static void ms_conn_shrink(ms_conn_t *c) -{ +static void ms_conn_shrink(ms_conn_t *c) { assert(c != NULL); if (c->udp) return; - if ((c->rsize > READ_BUFFER_HIGHWAT) && (c->rbytes < DATA_BUFFER_SIZE)) - { + if ((c->rsize > READ_BUFFER_HIGHWAT) && (c->rbytes < DATA_BUFFER_SIZE)) { char *newbuf; if (c->rcurr != c->rbuf) - memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); + memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); - newbuf= (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); + newbuf = (char *) realloc((void *) c->rbuf, DATA_BUFFER_SIZE); - if (newbuf) - { - c->rbuf= newbuf; - c->rsize= DATA_BUFFER_SIZE; + if (newbuf) { + c->rbuf = newbuf; + c->rsize = DATA_BUFFER_SIZE; } - c->rcurr= c->rbuf; + c->rcurr = c->rbuf; } if (c->udp && (c->rudpsize > UDP_DATA_BUFFER_HIGHWAT) && (c->rudpbytes + UDP_MAX_PAYLOAD_SIZE < UDP_DATA_BUFFER_SIZE)) { - char *new_rbuf= (char *)realloc(c->rudpbuf, (size_t)c->rudpsize * 2); - if (new_rbuf) - { - c->rudpbuf= new_rbuf; - c->rudpsize= UDP_DATA_BUFFER_SIZE; + char *new_rbuf = (char *) realloc(c->rudpbuf, (size_t) c->rudpsize * 2); + if (new_rbuf) { + c->rudpbuf = new_rbuf; + c->rudpsize = UDP_DATA_BUFFER_SIZE; } /* TODO check error condition? */ } - if (c->msgsize > MSG_LIST_HIGHWAT) - { - struct msghdr *newbuf= (struct msghdr *)realloc( - (void *)c->msglist, - MSG_LIST_INITIAL - * sizeof(c->msglist[0])); - if (newbuf) - { - c->msglist= newbuf; - c->msgsize= MSG_LIST_INITIAL; + if (c->msgsize > MSG_LIST_HIGHWAT) { + struct msghdr *newbuf = + (struct msghdr *) realloc((void *) c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); + if (newbuf) { + c->msglist = newbuf; + c->msgsize = MSG_LIST_INITIAL; } /* TODO check error condition? */ } - if (c->iovsize > IOV_LIST_HIGHWAT) - { - struct iovec *newbuf= (struct iovec *)realloc((void *)c->iov, - IOV_LIST_INITIAL - * sizeof(c->iov[0])); - if (newbuf) - { - c->iov= newbuf; - c->iovsize= IOV_LIST_INITIAL; + if (c->iovsize > IOV_LIST_HIGHWAT) { + struct iovec *newbuf = + (struct iovec *) realloc((void *) c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); + if (newbuf) { + c->iov = newbuf; + c->iovsize = IOV_LIST_INITIAL; } /* TODO check return value */ } } /* ms_conn_shrink */ - /** * Sets a connection's current state in the state machine. Any special * processing that needs to happen on certain state transitions can @@ -2393,21 +1999,17 @@ static void ms_conn_shrink(ms_conn_t *c) * @param c, pointer of the concurrency * @param state, connection state */ -static void ms_conn_set_state(ms_conn_t *c, int state) -{ +static void ms_conn_set_state(ms_conn_t *c, int state) { assert(c != NULL); - if (state != c->state) - { - if (state == conn_read) - { + if (state != c->state) { + if (state == conn_read) { ms_conn_shrink(c); } - c->state= state; + c->state = state; } } /* ms_conn_set_state */ - /** * update the event if socks change state. for example: when * change the listen scoket read event to sock write event, or @@ -2418,43 +2020,34 @@ static void ms_conn_set_state(ms_conn_t *c, int state) * * @return bool, if success, return true, else return false */ -static bool ms_update_event(ms_conn_t *c, const int new_flags) -{ +static bool ms_update_event(ms_conn_t *c, const int new_flags) { assert(c != NULL); - struct event_base *base= c->event.ev_base; + struct event_base *base = c->event.ev_base; if ((c->ev_flags == new_flags) && (ms_setting.rep_write_srv == 0) - && (! ms_setting.facebook_test || (c->total_sfds == 1))) + && (!ms_setting.facebook_test || (c->total_sfds == 1))) { return true; } - if (event_del(&c->event) == -1) - { + if (event_del(&c->event) == -1) { /* try to delete the event again */ - if (event_del(&c->event) == -1) - { + if (event_del(&c->event) == -1) { return false; } } - event_set(&c->event, - c->sfd, - (short)new_flags, - ms_event_handler, - (void *)c); + event_set(&c->event, c->sfd, (short) new_flags, ms_event_handler, (void *) c); event_base_set(base, &c->event); - c->ev_flags= (short)new_flags; + c->ev_flags = (short) new_flags; - if (event_add(&c->event, NULL) == -1) - { + if (event_add(&c->event, NULL) == -1) { return false; } return true; } /* ms_update_event */ - /** * If user want to get the expected throughput, we could limit * the performance of memslap. we could give up some work and @@ -2465,23 +2058,20 @@ static bool ms_update_event(ms_conn_t *c, const int new_flags) * * @return bool, if success, return true, else return false */ -static bool ms_need_yield(ms_conn_t *c) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - int64_t tps= 0; - int64_t time_diff= 0; +static bool ms_need_yield(ms_conn_t *c) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + int64_t tps = 0; + int64_t time_diff = 0; struct timeval curr_time; - ms_task_t *task= &c->curr_task; + ms_task_t *task = &c->curr_task; - if (ms_setting.expected_tps > 0) - { + if (ms_setting.expected_tps > 0) { gettimeofday(&curr_time, NULL); - time_diff= ms_time_diff(&ms_thread->startup_time, &curr_time); - tps= (int64_t)(((task->get_opt + task->set_opt) / (uint64_t)time_diff) * 1000000); + time_diff = ms_time_diff(&ms_thread->startup_time, &curr_time); + tps = (int64_t)(((task->get_opt + task->set_opt) / (uint64_t) time_diff) * 1000000); /* current throughput is greater than expected throughput */ - if (tps > ms_thread->thread_ctx->tps_perconn) - { + if (tps > ms_thread->thread_ctx->tps_perconn) { return true; } } @@ -2489,191 +2079,156 @@ static bool ms_need_yield(ms_conn_t *c) return false; } /* ms_need_yield */ - /** * used to update the start time of each operation * * @param c, pointer of the concurrency */ -static void ms_update_start_time(ms_conn_t *c) -{ - ms_task_item_t *item= c->curr_task.item; +static void ms_update_start_time(ms_conn_t *c) { + ms_task_item_t *item = c->curr_task.item; - if ((ms_setting.stat_freq > 0) || c->udp - || ((c->currcmd.cmd == CMD_SET) && (item->exp_time > 0))) + if ((ms_setting.stat_freq > 0) || c->udp || ((c->currcmd.cmd == CMD_SET) && (item->exp_time > 0))) { gettimeofday(&c->start_time, NULL); - if ((c->currcmd.cmd == CMD_SET) && (item->exp_time > 0)) - { + if ((c->currcmd.cmd == CMD_SET) && (item->exp_time > 0)) { /* record the current time */ - item->client_time= c->start_time.tv_sec; + item->client_time = c->start_time.tv_sec; } } } /* ms_update_start_time */ - /** * run the state machine * * @param c, pointer of the concurrency */ -static void ms_drive_machine(ms_conn_t *c) -{ - bool stop= false; +static void ms_drive_machine(ms_conn_t *c) { + bool stop = false; assert(c != NULL); - while (! stop) - { - switch (c->state) - { + while (!stop) { + switch (c->state) { case conn_read: - if (c->readval) - { - if (c->rbytes >= c->rvbytes) - { + if (c->readval) { + if (c->rbytes >= c->rvbytes) { ms_complete_nread(c); break; } - } - else - { - if (ms_try_read_line(c) != 0) - { + } else { + if (ms_try_read_line(c) != 0) { break; } } - if (ms_try_read_network(c) != 0) - { + if (ms_try_read_network(c) != 0) { break; } /* doesn't read all the response data, wait event wake up */ - if (! c->currcmd.isfinish) - { - if (! ms_update_event(c, EV_READ | EV_PERSIST)) - { + if (!c->currcmd.isfinish) { + if (!ms_update_event(c, EV_READ | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); break; } - stop= true; + stop = true; break; } /* we have no command line and no data to read from network, next write */ ms_conn_set_state(c, conn_write); - memcpy(&c->precmd, &c->currcmd, sizeof(ms_cmdstat_t)); /* replicate command state */ + memcpy(&c->precmd, &c->currcmd, sizeof(ms_cmdstat_t)); /* replicate command state */ break; case conn_write: - if (! c->ctnwrite && ms_need_yield(c)) - { + if (!c->ctnwrite && ms_need_yield(c)) { usleep(10); - if (! ms_update_event(c, EV_WRITE | EV_PERSIST)) - { + if (!ms_update_event(c, EV_WRITE | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); break; } - stop= true; + stop = true; break; } - if (! c->ctnwrite && (ms_exec_task(c) != 0)) - { + if (!c->ctnwrite && (ms_exec_task(c) != 0)) { ms_conn_set_state(c, conn_closing); break; } /* record the start time before starting to send data if necessary */ - if (! c->ctnwrite || (c->change_sfd && c->ctnwrite)) - { - if (c->change_sfd) - { - c->change_sfd= false; + if (!c->ctnwrite || (c->change_sfd && c->ctnwrite)) { + if (c->change_sfd) { + c->change_sfd = false; } ms_update_start_time(c); } /* change sfd if necessary */ - if (c->change_sfd) - { - c->ctnwrite= true; - stop= true; + if (c->change_sfd) { + c->ctnwrite = true; + stop = true; break; } /* execute task until nothing need be written to network */ - if (! c->ctnwrite && (c->msgcurr == c->msgused)) - { - if (! ms_update_event(c, EV_WRITE | EV_PERSIST)) - { + if (!c->ctnwrite && (c->msgcurr == c->msgused)) { + if (!ms_update_event(c, EV_WRITE | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); break; } - stop= true; + stop = true; break; } - switch (ms_transmit(c)) - { + switch (ms_transmit(c)) { case TRANSMIT_COMPLETE: /* we have no data to write to network, next wait repose */ - if (! ms_update_event(c, EV_READ | EV_PERSIST)) - { + if (!ms_update_event(c, EV_READ | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); - c->ctnwrite= false; + c->ctnwrite = false; break; } ms_conn_set_state(c, conn_read); - c->ctnwrite= false; - stop= true; + c->ctnwrite = false; + stop = true; break; - case TRANSMIT_INCOMPLETE: - c->ctnwrite= true; - break; /* Continue in state machine. */ + case TRANSMIT_INCOMPLETE: c->ctnwrite = true; break; /* Continue in state machine. */ - case TRANSMIT_HARD_ERROR: - c->ctnwrite= false; - break; + case TRANSMIT_HARD_ERROR: c->ctnwrite = false; break; case TRANSMIT_SOFT_ERROR: - c->ctnwrite= true; - stop= true; + c->ctnwrite = true; + stop = true; break; - default: - break; + default: break; } /* switch */ break; case conn_closing: /* recovery mode, need reconnect if connection close */ - if (ms_setting.reconnect && (! ms_global.time_out - || ((ms_setting.run_time == 0) - && (c->remain_exec_num > 0)))) + if (ms_setting.reconnect + && (!ms_global.time_out || ((ms_setting.run_time == 0) && (c->remain_exec_num > 0)))) { - if (ms_reconn(c) != 0) - { + if (ms_reconn(c) != 0) { ms_conn_close(c); - stop= true; + stop = true; break; } ms_reset_conn(c, false); - if (c->total_sfds == 1) - { - if (! ms_update_event(c, EV_WRITE | EV_PERSIST)) - { + if (c->total_sfds == 1) { + if (!ms_update_event(c, EV_WRITE | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); break; @@ -2681,21 +2236,17 @@ static void ms_drive_machine(ms_conn_t *c) } break; - } - else - { + } else { ms_conn_close(c); - stop= true; + stop = true; break; } - default: - assert(0); + default: assert(0); } /* switch */ } } /* ms_drive_machine */ - /** * the event handler of each thread * @@ -2703,21 +2254,16 @@ static void ms_drive_machine(ms_conn_t *c) * @param which, event flag * @param arg, argument */ -void ms_event_handler(const int fd, const short which, void *arg) -{ - ms_conn_t *c= (ms_conn_t *)arg; +void ms_event_handler(const int fd, const short which, void *arg) { + ms_conn_t *c = (ms_conn_t *) arg; assert(c != NULL); - c->which= which; + c->which = which; /* sanity */ - if (fd != c->sfd) - { - fprintf(stderr, - "Catastrophic: event fd: %d doesn't match conn fd: %d\n", - fd, - c->sfd); + if (fd != c->sfd) { + fprintf(stderr, "Catastrophic: event fd: %d doesn't match conn fd: %d\n", fd, c->sfd); ms_conn_close(c); exit(1); } @@ -2728,7 +2274,6 @@ void ms_event_handler(const int fd, const short which, void *arg) /* wait for next event */ } /* ms_event_handler */ - /** * get the next socket descriptor index to run for replication * @@ -2737,56 +2282,42 @@ void ms_event_handler(const int fd, const short which, void *arg) * * @return int, if success, return the index, else return EXIT_SUCCESS */ -static uint32_t ms_get_rep_sock_index(ms_conn_t *c, int cmd) -{ - uint32_t sock_index= 0; - uint32_t i= 0; +static uint32_t ms_get_rep_sock_index(ms_conn_t *c, int cmd) { + uint32_t sock_index = 0; + uint32_t i = 0; - if (c->total_sfds == 1) - { + if (c->total_sfds == 1) { return EXIT_SUCCESS; } - if (ms_setting.rep_write_srv == 0) - { + if (ms_setting.rep_write_srv == 0) { return sock_index; } - do - { - if (cmd == CMD_SET) - { - for (i= 0; i < ms_setting.rep_write_srv; i++) - { - if (c->tcpsfd[i] > 0) - { + do { + if (cmd == CMD_SET) { + for (i = 0; i < ms_setting.rep_write_srv; i++) { + if (c->tcpsfd[i] > 0) { break; } } - if (i == ms_setting.rep_write_srv) - { + if (i == ms_setting.rep_write_srv) { /* random get one replication server to read */ - sock_index= (uint32_t)random() % c->total_sfds; - } - else - { + sock_index = (uint32_t) random() % c->total_sfds; + } else { /* random get one replication writing server to write */ - sock_index= (uint32_t)random() % ms_setting.rep_write_srv; + sock_index = (uint32_t) random() % ms_setting.rep_write_srv; } - } - else if (cmd == CMD_GET) - { + } else if (cmd == CMD_GET) { /* random get one replication server to read */ - sock_index= (uint32_t)random() % c->total_sfds; + sock_index = (uint32_t) random() % c->total_sfds; } - } - while (c->tcpsfd[sock_index] == 0); + } while (c->tcpsfd[sock_index] == 0); return sock_index; } /* ms_get_rep_sock_index */ - /** * get the next socket descriptor index to run * @@ -2794,20 +2325,16 @@ static uint32_t ms_get_rep_sock_index(ms_conn_t *c, int cmd) * * @return int, return the index */ -static uint32_t ms_get_next_sock_index(ms_conn_t *c) -{ - uint32_t sock_index= 0; +static uint32_t ms_get_next_sock_index(ms_conn_t *c) { + uint32_t sock_index = 0; - do - { - sock_index= (++c->cur_idx == c->total_sfds) ? 0 : c->cur_idx; - } - while (c->tcpsfd[sock_index] == 0); + do { + sock_index = (++c->cur_idx == c->total_sfds) ? 0 : c->cur_idx; + } while (c->tcpsfd[sock_index] == 0); return sock_index; } /* ms_get_next_sock_index */ - /** * update socket event of the connections * @@ -2815,62 +2342,48 @@ static uint32_t ms_get_next_sock_index(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_update_conn_sock_event(ms_conn_t *c) -{ +static int ms_update_conn_sock_event(ms_conn_t *c) { assert(c != NULL); - switch (c->currcmd.cmd) - { + switch (c->currcmd.cmd) { case CMD_SET: - if (ms_setting.facebook_test && c->udp) - { - c->sfd= c->tcpsfd[0]; - c->udp= false; - c->change_sfd= true; + if (ms_setting.facebook_test && c->udp) { + c->sfd = c->tcpsfd[0]; + c->udp = false; + c->change_sfd = true; } break; case CMD_GET: - if (ms_setting.facebook_test && ! c->udp) - { - c->sfd= c->udpsfd; - c->udp= true; - c->change_sfd= true; + if (ms_setting.facebook_test && !c->udp) { + c->sfd = c->udpsfd; + c->udp = true; + c->change_sfd = true; } break; - default: - break; + default: break; } /* switch */ - if (! c->udp && (c->total_sfds > 1)) - { - if (c->cur_idx != c->total_sfds) - { - if (ms_setting.rep_write_srv == 0) - { - c->cur_idx= ms_get_next_sock_index(c); - } - else - { - c->cur_idx= ms_get_rep_sock_index(c, c->currcmd.cmd); + if (!c->udp && (c->total_sfds > 1)) { + if (c->cur_idx != c->total_sfds) { + if (ms_setting.rep_write_srv == 0) { + c->cur_idx = ms_get_next_sock_index(c); + } else { + c->cur_idx = ms_get_rep_sock_index(c, c->currcmd.cmd); } - } - else - { + } else { /* must select the first sock of the connection at the beginning */ - c->cur_idx= 0; + c->cur_idx = 0; } - c->sfd= c->tcpsfd[c->cur_idx]; + c->sfd = c->tcpsfd[c->cur_idx]; assert(c->sfd != 0); - c->change_sfd= true; + c->change_sfd = true; } - if (c->change_sfd) - { - if (! ms_update_event(c, EV_WRITE | EV_PERSIST)) - { + if (c->change_sfd) { + if (!ms_update_event(c, EV_WRITE | EV_PERSIST)) { fprintf(stderr, "Couldn't update event.\n"); ms_conn_set_state(c, conn_closing); return -1; @@ -2880,7 +2393,6 @@ static int ms_update_conn_sock_event(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_update_conn_sock_event */ - /** * for ASCII protocol, this function build the set command * string and send the command. @@ -2891,45 +2403,33 @@ static int ms_update_conn_sock_event(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) -{ +static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) { int value_offset; int write_len; - char *buffer= c->wbuf; + char *buffer = c->wbuf; - write_len= snprintf(buffer, - c->wsize, - " %u %d %d\r\n", - 0, - item->exp_time, - item->value_size); + write_len = snprintf(buffer, c->wsize, " %u %d %d\r\n", 0, item->exp_time, item->value_size); - if (write_len > c->wsize || write_len < 0) - { + if (write_len > c->wsize || write_len < 0) { /* ought to be always enough. just fail for simplicity */ fprintf(stderr, "output command line too long.\n"); return -1; } - if (item->value_offset == INVALID_OFFSET) - { - value_offset= item->key_suffix_offset; - } - else - { - value_offset= item->value_offset; + if (item->value_offset == INVALID_OFFSET) { + value_offset = item->key_suffix_offset; + } else { + value_offset = item->value_offset; } if ((ms_add_iov(c, "set ", 4) != 0) - || (ms_add_iov(c, (char *)&item->key_prefix, - (int)KEY_PREFIX_SIZE) != 0) + || (ms_add_iov(c, (char *) &item->key_prefix, (int) KEY_PREFIX_SIZE) != 0) || (ms_add_iov(c, &ms_setting.char_block[item->key_suffix_offset], - item->key_size - (int)KEY_PREFIX_SIZE) != 0) + item->key_size - (int) KEY_PREFIX_SIZE) + != 0) || (ms_add_iov(c, buffer, write_len) != 0) - || (ms_add_iov(c, &ms_setting.char_block[value_offset], - item->value_size) != 0) - || (ms_add_iov(c, "\r\n", 2) != 0) - || (c->udp && (ms_build_udp_headers(c) != 0))) + || (ms_add_iov(c, &ms_setting.char_block[value_offset], item->value_size) != 0) + || (ms_add_iov(c, "\r\n", 2) != 0) || (c->udp && (ms_build_udp_headers(c) != 0))) { return -1; } @@ -2937,7 +2437,6 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_set */ - /** * used to send set command to server * @@ -2947,52 +2446,42 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) -{ +int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) { assert(c != NULL); - c->currcmd.cmd= CMD_SET; - c->currcmd.isfinish= false; - c->currcmd.retstat= MCD_FAILURE; + c->currcmd.cmd = CMD_SET; + c->currcmd.isfinish = false; + c->currcmd.retstat = MCD_FAILURE; - if (ms_update_conn_sock_event(c) != 0) - { + if (ms_update_conn_sock_event(c) != 0) { return -1; } - c->msgcurr= 0; - c->msgused= 0; - c->iovused= 0; - if (ms_add_msghdr(c) != 0) - { + c->msgcurr = 0; + c->msgused = 0; + c->iovused = 0; + if (ms_add_msghdr(c) != 0) { fprintf(stderr, "Out of memory preparing request."); return -1; } /* binary protocol */ - if (c->protocol == binary_prot) - { - if (ms_build_bin_write_buf_set(c, item) != 0) - { + if (c->protocol == binary_prot) { + if (ms_build_bin_write_buf_set(c, item) != 0) { return -1; } - } - else - { - if (ms_build_ascii_write_buf_set(c, item) != 0) - { + } else { + if (ms_build_ascii_write_buf_set(c, item) != 0) { return -1; } } - atomic_add_size(&ms_stats.obj_bytes, - item->key_size + item->value_size); + atomic_add_size(&ms_stats.obj_bytes, item->key_size + item->value_size); atomic_add_size(&ms_stats.cmd_set, 1); return EXIT_SUCCESS; } /* ms_mcd_set */ - /** * for ASCII protocol, this function build the get command * string and send the command. @@ -3003,15 +2492,13 @@ int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) -{ +static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) { if ((ms_add_iov(c, "get ", 4) != 0) - || (ms_add_iov(c, (char *)&item->key_prefix, - (int)KEY_PREFIX_SIZE) != 0) + || (ms_add_iov(c, (char *) &item->key_prefix, (int) KEY_PREFIX_SIZE) != 0) || (ms_add_iov(c, &ms_setting.char_block[item->key_suffix_offset], - item->key_size - (int)KEY_PREFIX_SIZE) != 0) - || (ms_add_iov(c, "\r\n", 2) != 0) - || (c->udp && (ms_build_udp_headers(c) != 0))) + item->key_size - (int) KEY_PREFIX_SIZE) + != 0) + || (ms_add_iov(c, "\r\n", 2) != 0) || (c->udp && (ms_build_udp_headers(c) != 0))) { return -1; } @@ -3019,7 +2506,6 @@ static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_get */ - /** * used to send the get command to server * @@ -3029,40 +2515,32 @@ static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) -{ +int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) { assert(c != NULL); - c->currcmd.cmd= CMD_GET; - c->currcmd.isfinish= false; - c->currcmd.retstat= MCD_FAILURE; + c->currcmd.cmd = CMD_GET; + c->currcmd.isfinish = false; + c->currcmd.retstat = MCD_FAILURE; - if (ms_update_conn_sock_event(c) != 0) - { + if (ms_update_conn_sock_event(c) != 0) { return -1; } - c->msgcurr= 0; - c->msgused= 0; - c->iovused= 0; - if (ms_add_msghdr(c) != 0) - { + c->msgcurr = 0; + c->msgused = 0; + c->iovused = 0; + if (ms_add_msghdr(c) != 0) { fprintf(stderr, "Out of memory preparing request."); return -1; } /* binary protocol */ - if (c->protocol == binary_prot) - { - if (ms_build_bin_write_buf_get(c, item) != 0) - { + if (c->protocol == binary_prot) { + if (ms_build_bin_write_buf_get(c, item) != 0) { return -1; } - } - else - { - if (ms_build_ascii_write_buf_get(c, item) != 0) - { + } else { + if (ms_build_ascii_write_buf_get(c, item) != 0) { return -1; } } @@ -3072,7 +2550,6 @@ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) return EXIT_SUCCESS; } /* ms_mcd_get */ - /** * for ASCII protocol, this function build the multi-get command * string and send the command. @@ -3081,39 +2558,33 @@ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) -{ +static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) { ms_task_item_t *item; - if (ms_add_iov(c, "get", 3) != 0) - { + if (ms_add_iov(c, "get", 3) != 0) { return -1; } - for (int i= 0; i < c->mlget_task.mlget_num; i++) - { - item= c->mlget_task.mlget_item[i].item; + for (int i = 0; i < c->mlget_task.mlget_num; i++) { + item = c->mlget_task.mlget_item[i].item; assert(item != NULL); if ((ms_add_iov(c, " ", 1) != 0) - || (ms_add_iov(c, (char *)&item->key_prefix, - (int)KEY_PREFIX_SIZE) != 0) + || (ms_add_iov(c, (char *) &item->key_prefix, (int) KEY_PREFIX_SIZE) != 0) || (ms_add_iov(c, &ms_setting.char_block[item->key_suffix_offset], - item->key_size - (int)KEY_PREFIX_SIZE) != 0)) + item->key_size - (int) KEY_PREFIX_SIZE) + != 0)) { return -1; } } - if ((ms_add_iov(c, "\r\n", 2) != 0) - || (c->udp && (ms_build_udp_headers(c) != 0))) - { + if ((ms_add_iov(c, "\r\n", 2) != 0) || (c->udp && (ms_build_udp_headers(c) != 0))) { return -1; } return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_mlget */ - /** * used to send the multi-get command to server * @@ -3121,60 +2592,50 @@ static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_mcd_mlget(ms_conn_t *c) -{ +int ms_mcd_mlget(ms_conn_t *c) { ms_task_item_t *item; assert(c != NULL); assert(c->mlget_task.mlget_num >= 1); - c->currcmd.cmd= CMD_GET; - c->currcmd.isfinish= false; - c->currcmd.retstat= MCD_FAILURE; + c->currcmd.cmd = CMD_GET; + c->currcmd.isfinish = false; + c->currcmd.retstat = MCD_FAILURE; - if (ms_update_conn_sock_event(c) != 0) - { + if (ms_update_conn_sock_event(c) != 0) { return -1; } - c->msgcurr= 0; - c->msgused= 0; - c->iovused= 0; - if (ms_add_msghdr(c) != 0) - { + c->msgcurr = 0; + c->msgused = 0; + c->iovused = 0; + if (ms_add_msghdr(c) != 0) { fprintf(stderr, "Out of memory preparing request."); return -1; } /* binary protocol */ - if (c->protocol == binary_prot) - { - if (ms_build_bin_write_buf_mlget(c) != 0) - { + if (c->protocol == binary_prot) { + if (ms_build_bin_write_buf_mlget(c) != 0) { return -1; } - } - else - { - if (ms_build_ascii_write_buf_mlget(c) != 0) - { + } else { + if (ms_build_ascii_write_buf_mlget(c) != 0) { return -1; } } /* decrease operation time of each item */ - for (int i= 0; i < c->mlget_task.mlget_num; i++) - { - item= c->mlget_task.mlget_item[i].item; + for (int i = 0; i < c->mlget_task.mlget_num; i++) { + item = c->mlget_task.mlget_item[i].item; atomic_add_size(&ms_stats.cmd_get, 1); } - (void)item; + (void) item; return EXIT_SUCCESS; } /* ms_mcd_mlget */ - /** * binary protocol support */ @@ -3186,83 +2647,70 @@ int ms_mcd_mlget(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_bin_process_response(ms_conn_t *c) -{ - const char *errstr= NULL; +static int ms_bin_process_response(ms_conn_t *c) { + const char *errstr = NULL; assert(c != NULL); - uint32_t bodylen= c->binary_header.response.bodylen; - uint8_t opcode= c->binary_header.response.opcode; - uint16_t status= c->binary_header.response.status; + uint32_t bodylen = c->binary_header.response.bodylen; + uint8_t opcode = c->binary_header.response.opcode; + uint16_t status = c->binary_header.response.status; - if (bodylen > 0) - { - c->rvbytes= (int32_t)bodylen; - c->readval= true; + if (bodylen > 0) { + c->rvbytes = (int32_t) bodylen; + c->readval = true; return EXIT_FAILURE; - } - else - { - switch (status) - { + } else { + switch (status) { case PROTOCOL_BINARY_RESPONSE_SUCCESS: - if (opcode == PROTOCOL_BINARY_CMD_SET) - { - c->currcmd.retstat= MCD_STORED; - } - else if (opcode == PROTOCOL_BINARY_CMD_DELETE) - { - c->currcmd.retstat= MCD_DELETED; - } - else if (opcode == PROTOCOL_BINARY_CMD_GET) - { - c->currcmd.retstat= MCD_END; + if (opcode == PROTOCOL_BINARY_CMD_SET) { + c->currcmd.retstat = MCD_STORED; + } else if (opcode == PROTOCOL_BINARY_CMD_DELETE) { + c->currcmd.retstat = MCD_DELETED; + } else if (opcode == PROTOCOL_BINARY_CMD_GET) { + c->currcmd.retstat = MCD_END; } break; case PROTOCOL_BINARY_RESPONSE_ENOMEM: - errstr= "Out of memory"; - c->currcmd.retstat= MCD_SERVER_ERROR; + errstr = "Out of memory"; + c->currcmd.retstat = MCD_SERVER_ERROR; break; case PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND: - errstr= "Unknown command"; - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + errstr = "Unknown command"; + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; break; case PROTOCOL_BINARY_RESPONSE_KEY_ENOENT: - errstr= "Not found"; - c->currcmd.retstat= MCD_NOTFOUND; + errstr = "Not found"; + c->currcmd.retstat = MCD_NOTFOUND; break; case PROTOCOL_BINARY_RESPONSE_EINVAL: - errstr= "Invalid arguments"; - c->currcmd.retstat= MCD_PROTOCOL_ERROR; + errstr = "Invalid arguments"; + c->currcmd.retstat = MCD_PROTOCOL_ERROR; break; - case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS: - errstr= "Data exists for key."; - break; + case PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS: errstr = "Data exists for key."; break; case PROTOCOL_BINARY_RESPONSE_E2BIG: - errstr= "Too large."; - c->currcmd.retstat= MCD_SERVER_ERROR; + errstr = "Too large."; + c->currcmd.retstat = MCD_SERVER_ERROR; break; case PROTOCOL_BINARY_RESPONSE_NOT_STORED: - errstr= "Not stored."; - c->currcmd.retstat= MCD_NOTSTORED; + errstr = "Not stored."; + c->currcmd.retstat = MCD_NOTSTORED; break; default: - errstr= "Unknown error"; - c->currcmd.retstat= MCD_UNKNOWN_READ_FAILURE; + errstr = "Unknown error"; + c->currcmd.retstat = MCD_UNKNOWN_READ_FAILURE; break; } /* switch */ - if (errstr != NULL) - { + if (errstr != NULL) { fprintf(stderr, "%s\n", errstr); } } @@ -3270,7 +2718,6 @@ static int ms_bin_process_response(ms_conn_t *c) return EXIT_SUCCESS; } /* ms_bin_process_response */ - /* build binary header and add the header to the buffer to send */ /** @@ -3282,34 +2729,29 @@ static int ms_bin_process_response(ms_conn_t *c) * @param key_len, length of key * @param body_len. length of body */ -static void ms_add_bin_header(ms_conn_t *c, - uint8_t opcode, - uint8_t hdr_len, - uint16_t key_len, - uint32_t body_len) -{ +static void ms_add_bin_header(ms_conn_t *c, uint8_t opcode, uint8_t hdr_len, uint16_t key_len, + uint32_t body_len) { protocol_binary_request_header *header; assert(c != NULL); - header= (protocol_binary_request_header *)c->wcurr; + header = (protocol_binary_request_header *) c->wcurr; - header->request.magic= (uint8_t)PROTOCOL_BINARY_REQ; - header->request.opcode= (uint8_t)opcode; - header->request.keylen= htons(key_len); + header->request.magic = (uint8_t) PROTOCOL_BINARY_REQ; + header->request.opcode = (uint8_t) opcode; + header->request.keylen = htons(key_len); - header->request.extlen= (uint8_t)hdr_len; - header->request.datatype= (uint8_t)PROTOCOL_BINARY_RAW_BYTES; - header->request.vbucket= 0; + header->request.extlen = (uint8_t) hdr_len; + header->request.datatype = (uint8_t) PROTOCOL_BINARY_RAW_BYTES; + header->request.vbucket = 0; - header->request.bodylen= htonl(body_len); - header->request.opaque= 0; - header->request.cas= 0; + header->request.bodylen = htonl(body_len); + header->request.opaque = 0; + header->request.cas = 0; ms_add_iov(c, c->wcurr, sizeof(header->request)); } /* ms_add_bin_header */ - /** * add the key to the socket write buffer array * @@ -3317,14 +2759,12 @@ static void ms_add_bin_header(ms_conn_t *c, * @param item, pointer of task item which includes the object * information */ -static void ms_add_key_to_iov(ms_conn_t *c, ms_task_item_t *item) -{ - ms_add_iov(c, (char *)&item->key_prefix, (int)KEY_PREFIX_SIZE); +static void ms_add_key_to_iov(ms_conn_t *c, ms_task_item_t *item) { + ms_add_iov(c, (char *) &item->key_prefix, (int) KEY_PREFIX_SIZE); ms_add_iov(c, &ms_setting.char_block[item->key_suffix_offset], - item->key_size - (int)KEY_PREFIX_SIZE); + item->key_size - (int) KEY_PREFIX_SIZE); } - /** * for binary protocol, this function build the set command * and add the command to send buffer array. @@ -3335,40 +2775,31 @@ static void ms_add_key_to_iov(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item) -{ +static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item) { assert(c->wbuf == c->wcurr); int value_offset; - protocol_binary_request_set *rep= (protocol_binary_request_set *)c->wcurr; - uint16_t keylen= (uint16_t)item->key_size; - uint32_t bodylen= (uint32_t)sizeof(rep->message.body) - + (uint32_t)keylen + (uint32_t)item->value_size; - - ms_add_bin_header(c, - PROTOCOL_BINARY_CMD_SET, - sizeof(rep->message.body), - keylen, - bodylen); - rep->message.body.flags= 0; - rep->message.body.expiration= htonl((uint32_t)item->exp_time); + protocol_binary_request_set *rep = (protocol_binary_request_set *) c->wcurr; + uint16_t keylen = (uint16_t) item->key_size; + uint32_t bodylen = + (uint32_t) sizeof(rep->message.body) + (uint32_t) keylen + (uint32_t) item->value_size; + + ms_add_bin_header(c, PROTOCOL_BINARY_CMD_SET, sizeof(rep->message.body), keylen, bodylen); + rep->message.body.flags = 0; + rep->message.body.expiration = htonl((uint32_t) item->exp_time); ms_add_iov(c, &rep->message.body, sizeof(rep->message.body)); ms_add_key_to_iov(c, item); - if (item->value_offset == INVALID_OFFSET) - { - value_offset= item->key_suffix_offset; - } - else - { - value_offset= item->value_offset; + if (item->value_offset == INVALID_OFFSET) { + value_offset = item->key_suffix_offset; + } else { + value_offset = item->value_offset; } ms_add_iov(c, &ms_setting.char_block[value_offset], item->value_size); return EXIT_SUCCESS; } /* ms_build_bin_write_buf_set */ - /** * for binary protocol, this function build the get command and * add the command to send buffer array. @@ -3379,18 +2810,16 @@ static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item) -{ +static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item) { assert(c->wbuf == c->wcurr); - ms_add_bin_header(c, PROTOCOL_BINARY_CMD_GET, 0, (uint16_t)item->key_size, - (uint32_t)item->key_size); + ms_add_bin_header(c, PROTOCOL_BINARY_CMD_GET, 0, (uint16_t) item->key_size, + (uint32_t) item->key_size); ms_add_key_to_iov(c, item); return EXIT_SUCCESS; } /* ms_build_bin_write_buf_get */ - /** * for binary protocol, this function build the multi-get * command and add the command to send buffer array. @@ -3401,27 +2830,22 @@ static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_build_bin_write_buf_mlget(ms_conn_t *c) -{ +static int ms_build_bin_write_buf_mlget(ms_conn_t *c) { ms_task_item_t *item; assert(c->wbuf == c->wcurr); - for (int i= 0; i < c->mlget_task.mlget_num; i++) - { - item= c->mlget_task.mlget_item[i].item; + for (int i = 0; i < c->mlget_task.mlget_num; i++) { + item = c->mlget_task.mlget_item[i].item; assert(item != NULL); - ms_add_bin_header(c, - PROTOCOL_BINARY_CMD_GET, - 0, - (uint16_t)item->key_size, - (uint32_t)item->key_size); + ms_add_bin_header(c, PROTOCOL_BINARY_CMD_GET, 0, (uint16_t) item->key_size, + (uint32_t) item->key_size); ms_add_key_to_iov(c, item); - c->wcurr+= sizeof(protocol_binary_request_get); + c->wcurr += sizeof(protocol_binary_request_get); } - c->wcurr= c->wbuf; + c->wcurr = c->wbuf; return EXIT_SUCCESS; } /* ms_build_bin_write_buf_mlget */ diff --git a/src/bin/memaslap/ms_conn.h b/src/bin/memaslap/ms_conn.h index d0530a7c..426eae82 100644 --- a/src/bin/memaslap/ms_conn.h +++ b/src/bin/memaslap/ms_conn.h @@ -1,13 +1,18 @@ /* - * File: ms_conn.h - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_CONN_H #define MS_CONN_H @@ -23,87 +28,78 @@ extern "C" { #endif -#define DATA_BUFFER_SIZE (1024 * 1024 + 2048) /* read buffer, 1M + 2k, enough for the max value(1M) */ -#define WRITE_BUFFER_SIZE (32 * 1024) /* write buffer, 32k */ -#define UDP_DATA_BUFFER_SIZE (1 * 1024 * 1024) /* read buffer for UDP, 1M */ -#define UDP_MAX_PAYLOAD_SIZE 1400 /* server limit UDP payload size */ -#define UDP_MAX_SEND_PAYLOAD_SIZE 1400 /* mtu size is 1500 */ -#define UDP_HEADER_SIZE 8 /* UDP header size */ -#define MAX_SENDBUF_SIZE (256 * 1024 * 1024) /* Maximum socket buffer size */ -#define SOCK_WAIT_TIMEOUT 30 /* maximum waiting time of UDP, 30s */ -#define MAX_UDP_PACKET (1 << 16) /* maximum UDP packets, 65536 */ +#define DATA_BUFFER_SIZE \ + (1024 * 1024 + 2048) /* read buffer, 1M + 2k, enough for the max value(1M) */ +#define WRITE_BUFFER_SIZE (32 * 1024) /* write buffer, 32k */ +#define UDP_DATA_BUFFER_SIZE (1 * 1024 * 1024) /* read buffer for UDP, 1M */ +#define UDP_MAX_PAYLOAD_SIZE 1400 /* server limit UDP payload size */ +#define UDP_MAX_SEND_PAYLOAD_SIZE 1400 /* mtu size is 1500 */ +#define UDP_HEADER_SIZE 8 /* UDP header size */ +#define MAX_SENDBUF_SIZE (256 * 1024 * 1024) /* Maximum socket buffer size */ +#define SOCK_WAIT_TIMEOUT 30 /* maximum waiting time of UDP, 30s */ +#define MAX_UDP_PACKET (1 << 16) /* maximum UDP packets, 65536 */ /* Initial size of the sendmsg() scatter/gather array. */ -#define IOV_LIST_INITIAL 400 +#define IOV_LIST_INITIAL 400 /* Initial number of sendmsg() argument structures to allocate. */ -#define MSG_LIST_INITIAL 10 +#define MSG_LIST_INITIAL 10 /* High water marks for buffer shrinking */ -#define READ_BUFFER_HIGHWAT (2 * DATA_BUFFER_SIZE) -#define UDP_DATA_BUFFER_HIGHWAT (4 * UDP_DATA_BUFFER_SIZE) -#define IOV_LIST_HIGHWAT 600 -#define MSG_LIST_HIGHWAT 100 +#define READ_BUFFER_HIGHWAT (2 * DATA_BUFFER_SIZE) +#define UDP_DATA_BUFFER_HIGHWAT (4 * UDP_DATA_BUFFER_SIZE) +#define IOV_LIST_HIGHWAT 600 +#define MSG_LIST_HIGHWAT 100 /* parse udp header */ -#define HEADER_TO_REQID(ptr) ((uint16_t)*ptr * 256 \ - + (uint16_t)*(ptr + 1)) -#define HEADER_TO_SEQNUM(ptr) ((uint16_t)*(ptr \ - + 2) * 256 \ - + (uint16_t)*(ptr + 3)) -#define HEADER_TO_PACKETS(ptr) ((uint16_t)*(ptr \ - + 4) * 256 \ - + (uint16_t)*(ptr + 5)) +#define HEADER_TO_REQID(ptr) ((uint16_t) *ptr * 256 + (uint16_t) * (ptr + 1)) +#define HEADER_TO_SEQNUM(ptr) ((uint16_t) * (ptr + 2) * 256 + (uint16_t) * (ptr + 3)) +#define HEADER_TO_PACKETS(ptr) ((uint16_t) * (ptr + 4) * 256 + (uint16_t) * (ptr + 5)) /* states of connection */ -enum conn_states -{ - conn_read, /* reading in a command line */ - conn_write, /* writing out a simple response */ - conn_closing /* closing this connection */ +enum conn_states { + conn_read, /* reading in a command line */ + conn_write, /* writing out a simple response */ + conn_closing /* closing this connection */ }; /* returned states of memcached command */ -enum mcd_ret -{ - MCD_SUCCESS, /* command success */ - MCD_FAILURE, /* command failure */ - MCD_UNKNOWN_READ_FAILURE, /* unknown read failure */ - MCD_PROTOCOL_ERROR, /* protocol error */ - MCD_CLIENT_ERROR, /* client error, wrong command */ - MCD_SERVER_ERROR, /* server error, server run command failed */ - MCD_DATA_EXISTS, /* object is existent in server */ - MCD_NOTSTORED, /* server doesn't set the object successfully */ - MCD_STORED, /* server set the object successfully */ - MCD_NOTFOUND, /* server not find the object */ - MCD_END, /* end of the response of get command */ - MCD_DELETED, /* server delete the object successfully */ - MCD_STAT /* response of stats command */ +enum mcd_ret { + MCD_SUCCESS, /* command success */ + MCD_FAILURE, /* command failure */ + MCD_UNKNOWN_READ_FAILURE, /* unknown read failure */ + MCD_PROTOCOL_ERROR, /* protocol error */ + MCD_CLIENT_ERROR, /* client error, wrong command */ + MCD_SERVER_ERROR, /* server error, server run command failed */ + MCD_DATA_EXISTS, /* object is existent in server */ + MCD_NOTSTORED, /* server doesn't set the object successfully */ + MCD_STORED, /* server set the object successfully */ + MCD_NOTFOUND, /* server not find the object */ + MCD_END, /* end of the response of get command */ + MCD_DELETED, /* server delete the object successfully */ + MCD_STAT /* response of stats command */ }; /* used to store the current or previous running command state */ -typedef struct cmdstat -{ - int cmd; /* command name */ - int retstat; /* return state of this command */ - bool isfinish; /* if it read all the response data */ - uint64_t key_prefix; /* key prefix */ +typedef struct cmdstat { + int cmd; /* command name */ + int retstat; /* return state of this command */ + bool isfinish; /* if it read all the response data */ + uint64_t key_prefix; /* key prefix */ } ms_cmdstat_t; /* udp packet structure */ -typedef struct udppkt -{ - uint8_t *header; /* udp header of the packet */ - char *data; /* udp data of the packet */ - int rbytes; /* number of data in the packet */ - int copybytes; /* number of copied data in the packet */ +typedef struct udppkt { + uint8_t *header; /* udp header of the packet */ + char *data; /* udp data of the packet */ + int rbytes; /* number of data in the packet */ + int copybytes; /* number of copied data in the packet */ } ms_udppkt_t; /* three protocols supported */ -enum protocol -{ - ascii_prot = 3, /* ASCII protocol */ - binary_prot /* binary protocol */ +enum protocol { + ascii_prot = 3, /* ASCII protocol */ + binary_prot /* binary protocol */ }; /** @@ -115,104 +111,100 @@ enum protocol * concurrency structure includes all the private variables of * the concurrency. */ -typedef struct conn -{ - uint32_t conn_idx; /* connection index in the thread */ - int sfd; /* current tcp sock handler of the connection structure */ - int udpsfd; /* current udp sock handler of the connection structure*/ - int state; /* state of the connection */ - struct event event; /* event for libevent */ - short ev_flags; /* event flag for libevent */ - short which; /* which events were just triggered */ - bool change_sfd; /* whether change sfd */ - - int *tcpsfd; /* TCP sock array */ - uint32_t total_sfds; /* how many socks in the tcpsfd array */ - uint32_t alive_sfds; /* alive socks */ - uint32_t cur_idx; /* current sock index in tcpsfd array */ - - ms_cmdstat_t precmd; /* previous command state */ - ms_cmdstat_t currcmd; /* current command state */ - - char *rbuf; /* buffer to read commands into */ - char *rcurr; /* but if we parsed some already, this is where we stopped */ - int rsize; /* total allocated size of rbuf */ - int rbytes; /* how much data, starting from rcur, do we have unparsed */ - - bool readval; /* read value state, read known data size */ - int rvbytes; /* total value size need to read */ - - char *wbuf; /* buffer to write commands out */ - char *wcurr; /* for multi-get, where we stopped */ - int wsize; /* total allocated size of wbuf */ - bool ctnwrite; /* continue to write */ +typedef struct conn { + uint32_t conn_idx; /* connection index in the thread */ + int sfd; /* current tcp sock handler of the connection structure */ + int udpsfd; /* current udp sock handler of the connection structure*/ + int state; /* state of the connection */ + struct event event; /* event for libevent */ + short ev_flags; /* event flag for libevent */ + short which; /* which events were just triggered */ + bool change_sfd; /* whether change sfd */ + + int *tcpsfd; /* TCP sock array */ + uint32_t total_sfds; /* how many socks in the tcpsfd array */ + uint32_t alive_sfds; /* alive socks */ + uint32_t cur_idx; /* current sock index in tcpsfd array */ + + ms_cmdstat_t precmd; /* previous command state */ + ms_cmdstat_t currcmd; /* current command state */ + + char *rbuf; /* buffer to read commands into */ + char *rcurr; /* but if we parsed some already, this is where we stopped */ + int rsize; /* total allocated size of rbuf */ + int rbytes; /* how much data, starting from rcur, do we have unparsed */ + + bool readval; /* read value state, read known data size */ + int rvbytes; /* total value size need to read */ + + char *wbuf; /* buffer to write commands out */ + char *wcurr; /* for multi-get, where we stopped */ + int wsize; /* total allocated size of wbuf */ + bool ctnwrite; /* continue to write */ /* data for the mwrite state */ struct iovec *iov; - int iovsize; /* number of elements allocated in iov[] */ - int iovused; /* number of elements used in iov[] */ + int iovsize; /* number of elements allocated in iov[] */ + int iovused; /* number of elements used in iov[] */ struct msghdr *msglist; - int msgsize; /* number of elements allocated in msglist[] */ - int msgused; /* number of elements used in msglist[] */ - int msgcurr; /* element in msglist[] being transmitted now */ - int msgbytes; /* number of bytes in current msg */ + int msgsize; /* number of elements allocated in msglist[] */ + int msgused; /* number of elements used in msglist[] */ + int msgcurr; /* element in msglist[] being transmitted now */ + int msgbytes; /* number of bytes in current msg */ /* data for UDP clients */ - bool udp; /* is this is a UDP "connection" */ - uint32_t request_id; /* UDP request ID of current operation, if this is a UDP "connection" */ - uint8_t *hdrbuf; /* udp packet headers */ - int hdrsize; /* number of headers' worth of space is allocated */ - struct sockaddr srv_recv_addr; /* Sent the most recent request to which server */ + bool udp; /* is this is a UDP "connection" */ + uint32_t request_id; /* UDP request ID of current operation, if this is a UDP "connection" */ + uint8_t *hdrbuf; /* udp packet headers */ + int hdrsize; /* number of headers' worth of space is allocated */ + struct sockaddr srv_recv_addr; /* Sent the most recent request to which server */ socklen_t srv_recv_addr_size; /* udp read buffer */ - char *rudpbuf; /* buffer to read commands into for udp */ - int rudpsize; /* total allocated size of rudpbuf */ - int rudpbytes; /* how much data, starting from rudpbuf */ + char *rudpbuf; /* buffer to read commands into for udp */ + int rudpsize; /* total allocated size of rudpbuf */ + int rudpbytes; /* how much data, starting from rudpbuf */ /* order udp packet */ - ms_udppkt_t *udppkt; /* the offset of udp packet in rudpbuf */ - int packets; /* number of total packets need to read */ - int recvpkt; /* number of received packets */ - int pktcurr; /* current packet in rudpbuf being ordered */ - int ordcurr; /* current ordered packet */ - - ms_task_item_t *item_win; /* task sequence */ - int win_size; /* current task window size */ - uint64_t set_cursor; /* current set item index in the item window */ - ms_task_t curr_task; /* current running task */ - ms_mlget_task_t mlget_task; /* multi-get task */ - - int warmup_num; /* to run how many warm up operations*/ - int remain_warmup_num; /* left how many warm up operations to run */ - int64_t exec_num; /* to run how many task operations */ - int64_t remain_exec_num; /* how many remained task operations to run */ + ms_udppkt_t *udppkt; /* the offset of udp packet in rudpbuf */ + int packets; /* number of total packets need to read */ + int recvpkt; /* number of received packets */ + int pktcurr; /* current packet in rudpbuf being ordered */ + int ordcurr; /* current ordered packet */ + + ms_task_item_t *item_win; /* task sequence */ + int win_size; /* current task window size */ + uint64_t set_cursor; /* current set item index in the item window */ + ms_task_t curr_task; /* current running task */ + ms_mlget_task_t mlget_task; /* multi-get task */ + + int warmup_num; /* to run how many warm up operations*/ + int remain_warmup_num; /* left how many warm up operations to run */ + int64_t exec_num; /* to run how many task operations */ + int64_t remain_exec_num; /* how many remained task operations to run */ /* response time statistic and time out control */ - struct timeval start_time; /* start time of current operation(s) */ - struct timeval end_time; /* end time of current operation(s) */ + struct timeval start_time; /* start time of current operation(s) */ + struct timeval end_time; /* end time of current operation(s) */ /* Binary protocol stuff */ - protocol_binary_response_header binary_header; /* local temporary binary header */ - enum protocol protocol; /* which protocol this connection speaks */ + protocol_binary_response_header binary_header; /* local temporary binary header */ + enum protocol protocol; /* which protocol this connection speaks */ } ms_conn_t; /* used to generate the key prefix */ uint64_t ms_get_key_prefix(void); - /** * setup a connection, each connection structure of each * thread must call this function to initialize. */ int ms_setup_conn(ms_conn_t *c); - /* after one operation completes, reset the connection */ void ms_reset_conn(ms_conn_t *c, bool timeout); - /** * reconnect several disconnected socks in the connection * structure, the ever-1-second timer of the thread will check @@ -221,19 +213,15 @@ void ms_reset_conn(ms_conn_t *c, bool timeout); */ int ms_reconn_socks(ms_conn_t *c); - /* used to send set command to server */ int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item); - /* used to send the get command to server */ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item); - /* used to send the multi-get command to server */ int ms_mcd_mlget(ms_conn_t *c); - #ifdef __cplusplus } #endif diff --git a/src/bin/memaslap/ms_memslap.h b/src/bin/memaslap/ms_memslap.h index 72af301a..36bb8f7f 100644 --- a/src/bin/memaslap/ms_memslap.h +++ b/src/bin/memaslap/ms_memslap.h @@ -1,13 +1,18 @@ /* - * File: ms_memslap.h - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_MEMSLAP_H #define MS_MEMSLAP_H @@ -20,7 +25,7 @@ #include #include #if !defined(__cplusplus) -# include +# include #endif #include @@ -32,75 +37,70 @@ extern "C" { #endif /* command line option */ -typedef enum -{ - OPT_VERSION= 'V', - OPT_HELP= 'h', - OPT_UDP= 'U', - OPT_SERVERS= 's', - OPT_EXECUTE_NUMBER= 'x', - OPT_THREAD_NUMBER= 'T', - OPT_CONCURRENCY= 'c', - OPT_FIXED_LTH= 'X', - OPT_VERIFY= 'v', - OPT_GETS_DIVISION= 'd', - OPT_TIME= 't', - OPT_CONFIG_CMD= 'F', - OPT_WINDOW_SIZE= 'w', - OPT_EXPIRE= 'e', - OPT_STAT_FREQ= 'S', - OPT_RECONNECT= 'R', - OPT_VERBOSE= 'b', - OPT_FACEBOOK_TEST= 'a', - OPT_SOCK_PER_CONN= 'n', - OPT_BINARY_PROTOCOL= 'B', - OPT_OVERWRITE= 'o', - OPT_TPS= 'P', - OPT_REP_WRITE_SRV= 'p' +typedef enum { + OPT_VERSION = 'V', + OPT_HELP = 'h', + OPT_UDP = 'U', + OPT_SERVERS = 's', + OPT_EXECUTE_NUMBER = 'x', + OPT_THREAD_NUMBER = 'T', + OPT_CONCURRENCY = 'c', + OPT_FIXED_LTH = 'X', + OPT_VERIFY = 'v', + OPT_GETS_DIVISION = 'd', + OPT_TIME = 't', + OPT_CONFIG_CMD = 'F', + OPT_WINDOW_SIZE = 'w', + OPT_EXPIRE = 'e', + OPT_STAT_FREQ = 'S', + OPT_RECONNECT = 'R', + OPT_VERBOSE = 'b', + OPT_FACEBOOK_TEST = 'a', + OPT_SOCK_PER_CONN = 'n', + OPT_BINARY_PROTOCOL = 'B', + OPT_OVERWRITE = 'o', + OPT_TPS = 'P', + OPT_REP_WRITE_SRV = 'p' } ms_options_t; /* global statistic of response time */ -typedef struct statistic -{ - pthread_mutex_t stat_mutex; /* synchronize the following members */ +typedef struct statistic { + pthread_mutex_t stat_mutex; /* synchronize the following members */ - ms_stat_t get_stat; /* statistics of get command */ - ms_stat_t set_stat; /* statistics of set command */ - ms_stat_t total_stat; /* statistics of both get and set commands */ + ms_stat_t get_stat; /* statistics of get command */ + ms_stat_t set_stat; /* statistics of set command */ + ms_stat_t total_stat; /* statistics of both get and set commands */ } ms_statistic_t; /* global status statistic structure */ -typedef struct stats -{ - ATOMIC uint32_t active_conns; /* active connections */ - ATOMIC size_t bytes_read; /* read bytes */ - ATOMIC size_t bytes_written; /* written bytes */ - ATOMIC size_t obj_bytes; /* object bytes */ - ATOMIC size_t pre_cmd_get; /* previous total get command count */ - ATOMIC size_t pre_cmd_set; /* previous total set command count */ - ATOMIC size_t cmd_get; /* current total get command count */ - ATOMIC size_t cmd_set; /* current total set command count */ - ATOMIC size_t get_misses; /* total objects of get miss */ - ATOMIC size_t vef_miss; /* total objects of verification miss */ - ATOMIC size_t vef_failed; /* total objects of verification failed */ - ATOMIC size_t unexp_unget; /* total objects which is unexpired but not get */ - ATOMIC size_t exp_get; /* total objects which is expired but get */ - ATOMIC size_t pkt_disorder; /* disorder packages of UDP */ - ATOMIC size_t pkt_drop; /* packages dropped of UDP */ - ATOMIC size_t udp_timeout; /* how many times timeout of UDP happens */ +typedef struct stats { + ATOMIC uint32_t active_conns; /* active connections */ + ATOMIC size_t bytes_read; /* read bytes */ + ATOMIC size_t bytes_written; /* written bytes */ + ATOMIC size_t obj_bytes; /* object bytes */ + ATOMIC size_t pre_cmd_get; /* previous total get command count */ + ATOMIC size_t pre_cmd_set; /* previous total set command count */ + ATOMIC size_t cmd_get; /* current total get command count */ + ATOMIC size_t cmd_set; /* current total set command count */ + ATOMIC size_t get_misses; /* total objects of get miss */ + ATOMIC size_t vef_miss; /* total objects of verification miss */ + ATOMIC size_t vef_failed; /* total objects of verification failed */ + ATOMIC size_t unexp_unget; /* total objects which is unexpired but not get */ + ATOMIC size_t exp_get; /* total objects which is expired but get */ + ATOMIC size_t pkt_disorder; /* disorder packages of UDP */ + ATOMIC size_t pkt_drop; /* packages dropped of UDP */ + ATOMIC size_t udp_timeout; /* how many times timeout of UDP happens */ } ms_stats_t; /* lock adapter */ -typedef struct sync_lock -{ +typedef struct sync_lock { uint32_t count; pthread_mutex_t lock; pthread_cond_t cond; } ms_sync_lock_t; /* global variable structure */ -typedef struct global -{ +typedef struct global { /* synchronize lock */ ms_sync_lock_t init_lock; ms_sync_lock_t warmup_lock; diff --git a/src/bin/memaslap/ms_setting.c b/src/bin/memaslap/ms_setting.c index 289466e0..adab65a1 100644 --- a/src/bin/memaslap/ms_setting.c +++ b/src/bin/memaslap/ms_setting.c @@ -1,13 +1,17 @@ /* - * File: ms_setting.c - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -21,35 +25,31 @@ #include #include - - #include "ms_setting.h" #include "ms_conn.h" -#define MAX_EXEC_NUM 0x4000000000000000 /* 1 << 62 */ -#define ADDR_ALIGN(addr) ((addr + 15) & ~(16 - 1)) /* 16 bytes aligned */ -#define RAND_CHAR_SIZE (10 * 1024 * 1024) /* 10M character table */ -#define RESERVED_RAND_CHAR_SIZE (2 * 1024 * 1024) /* reserved 2M to avoid pointer sloping over */ +#define MAX_EXEC_NUM 0x4000000000000000 /* 1 << 62 */ +#define ADDR_ALIGN(addr) ((addr + 15) & ~(16 - 1)) /* 16 bytes aligned */ +#define RAND_CHAR_SIZE (10 * 1024 * 1024) /* 10M character table */ +#define RESERVED_RAND_CHAR_SIZE (2 * 1024 * 1024) /* reserved 2M to avoid pointer sloping over */ #define DEFAULT_CONFIG_NAME ".memslap.cnf" -#define DEFAULT_THREADS_NUM 1 /* default start one thread */ -#define DEFAULT_CONNS_NUM 16 /* default each thread with 16 connections */ -#define DEFAULT_EXE_NUM 0 /* default execute number is 0 */ -#define DEFAULT_VERIFY_RATE 0.0 /* default it doesn't do data verification */ -#define DEFAULT_OVERWRITE_RATE 0.0 /* default it doesn't do overwrite */ -#define DEFAULT_DIV 1 /* default it runs single get */ -#define DEFAULT_RUN_TIME 600 /* default run time 10 minutes */ -#define DEFAULT_WINDOW_SIZE (10 * UNIT_ITEMS_COUNT) /* default window size is 10k */ -#define DEFAULT_SOCK_PER_CONN 1 /* default socks per connection is 1 */ +#define DEFAULT_THREADS_NUM 1 /* default start one thread */ +#define DEFAULT_CONNS_NUM 16 /* default each thread with 16 connections */ +#define DEFAULT_EXE_NUM 0 /* default execute number is 0 */ +#define DEFAULT_VERIFY_RATE 0.0 /* default it doesn't do data verification */ +#define DEFAULT_OVERWRITE_RATE 0.0 /* default it doesn't do overwrite */ +#define DEFAULT_DIV 1 /* default it runs single get */ +#define DEFAULT_RUN_TIME 600 /* default run time 10 minutes */ +#define DEFAULT_WINDOW_SIZE (10 * UNIT_ITEMS_COUNT) /* default window size is 10k */ +#define DEFAULT_SOCK_PER_CONN 1 /* default socks per connection is 1 */ /* Use this for string generation */ -#define CHAR_COUNT 64 /* number of characters used to generate character table */ -const char ALPHANUMBERICS[]= - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-"; - -ms_setting_st ms_setting; /* store the settings specified by user */ +#define CHAR_COUNT 64 /* number of characters used to generate character table */ +const char ALPHANUMBERICS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-"; +ms_setting_st ms_setting; /* store the settings specified by user */ /* read setting from configuration file */ static void ms_get_serverlist(char *str); @@ -60,7 +60,6 @@ static int ms_read_is_data(char *line, ssize_t nread); static void ms_no_config_file(void); static void ms_parse_cfg_file(char *cfg_file); - /* initialize setting structure */ static void ms_init_random_block(void); static void ms_calc_avg_size(void); @@ -71,70 +70,60 @@ static void ms_setting_slapmode_init_pre(void); static void ms_setting_slapmode_init_post(void); #if !defined(HAVE_GETLINE) -#include -static ssize_t getline (char **line, size_t *line_size, FILE *fp) -{ - char delim= '\n'; - ssize_t result= 0; - size_t cur_len= 0; - - if (line == NULL || line_size == NULL || fp == NULL) - { +# include +static ssize_t getline(char **line, size_t *line_size, FILE *fp) { + char delim = '\n'; + ssize_t result = 0; + size_t cur_len = 0; + + if (line == NULL || line_size == NULL || fp == NULL) { errno = EINVAL; return -1; } - if (*line == NULL || *line_size == 0) - { + if (*line == NULL || *line_size == 0) { char *new_line; *line_size = 120; - new_line= (char *) realloc (*line, *line_size); - if (new_line == NULL) - { - result= -1; + new_line = (char *) realloc(*line, *line_size); + if (new_line == NULL) { + result = -1; return result; } - *line= new_line; + *line = new_line; } - for (;;) - { - int i= getc(fp); - if (i == EOF) - { + for (;;) { + int i = getc(fp); + if (i == EOF) { result = -1; break; } /* Make enough space for len+1 (for final NUL) bytes. */ - if (cur_len + 1 >= *line_size) - { - size_t needed_max= - SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX; - size_t needed= (2 * (*line_size)) + 1; + if (cur_len + 1 >= *line_size) { + size_t needed_max = SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX; + size_t needed = (2 * (*line_size)) + 1; char *new_line; if (needed_max < needed) - needed= needed_max; - if (cur_len + 1 >= needed) - { - result= -1; - errno= EOVERFLOW; + needed = needed_max; + if (cur_len + 1 >= needed) { + result = -1; + errno = EOVERFLOW; return result; } - new_line= (char *)realloc(*line, needed); - if (new_line == NULL) - { - result= -1; + new_line = (char *) realloc(*line, needed); + if (new_line == NULL) { + result = -1; return result; } - *line= new_line; - *line_size= needed; + *line = new_line; + *line_size = needed; } - (*line)[cur_len]= (char)i; + (*line)[cur_len] = (char) i; cur_len++; if (i == delim) @@ -142,7 +131,7 @@ static ssize_t getline (char **line, size_t *line_size, FILE *fp) } (*line)[cur_len] = '\0'; if (cur_len != 0) - return (ssize_t)cur_len; + return (ssize_t) cur_len; return result; } #endif @@ -154,9 +143,8 @@ static ssize_t getline (char **line, size_t *line_size, FILE *fp) * * @param str, the string of server list */ -static void ms_get_serverlist(char *str) -{ - ms_mcd_server_t *srvs= NULL; +static void ms_get_serverlist(char *str) { + ms_mcd_server_t *srvs = NULL; /** * Servers list format is like this. For example: @@ -165,68 +153,60 @@ static void ms_get_serverlist(char *str) memcached_server_st *server_pool; server_pool = memcached_servers_parse(str); - for (uint32_t loop= 0; loop < memcached_server_list_count(server_pool); loop++) - { + for (uint32_t loop = 0; loop < memcached_server_list_count(server_pool); loop++) { assert(ms_setting.srv_cnt < ms_setting.total_srv_cnt); strcpy(ms_setting.servers[ms_setting.srv_cnt].srv_host_name, server_pool[loop].hostname); - ms_setting.servers[ms_setting.srv_cnt].srv_port= server_pool[loop].port; - ms_setting.servers[ms_setting.srv_cnt].disconn_cnt= 0; - ms_setting.servers[ms_setting.srv_cnt].reconn_cnt= 0; + ms_setting.servers[ms_setting.srv_cnt].srv_port = server_pool[loop].port; + ms_setting.servers[ms_setting.srv_cnt].disconn_cnt = 0; + ms_setting.servers[ms_setting.srv_cnt].reconn_cnt = 0; ms_setting.srv_cnt++; - if (ms_setting.srv_cnt >= ms_setting.total_srv_cnt) - { - srvs= (ms_mcd_server_t *)realloc( ms_setting.servers, - (size_t)ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t) * 2); - if (srvs == NULL) - { + if (ms_setting.srv_cnt >= ms_setting.total_srv_cnt) { + srvs = (ms_mcd_server_t *) realloc( + ms_setting.servers, (size_t) ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t) * 2); + if (srvs == NULL) { fprintf(stderr, "Can't reallocate servers structure.\n"); exit(1); } - ms_setting.servers= srvs; - ms_setting.total_srv_cnt*= 2; + ms_setting.servers = srvs; + ms_setting.total_srv_cnt *= 2; } } memcached_server_free(server_pool); } /* ms_get_serverlist */ - /** * used to get the CPU count of the current system * * @return return the cpu count if get, else return EXIT_FAILURE */ -static uint32_t ms_get_cpu_count() -{ +static uint32_t ms_get_cpu_count() { #ifdef HAVE__SC_NPROCESSORS_ONLN return sysconf(_SC_NPROCESSORS_CONF); #else -# ifdef HAVE_CPU_SET_T - int cpu_count= 0; +# ifdef HAVE_CPU_SET_T + int cpu_count = 0; cpu_set_t cpu_set; sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set); - for (int i= 0; i < (sizeof(cpu_set_t) * 8); i++) - { - if (CPU_ISSET(i, &cpu_set)) - { + for (int i = 0; i < (sizeof(cpu_set_t) * 8); i++) { + if (CPU_ISSET(i, &cpu_set)) { cpu_count++; } } return cpu_count; -# endif +# endif #endif /* the system with one cpu at least */ return EXIT_FAILURE; } /* ms_get_cpu_count */ - /** * used to get the configure type based on the type string read * from the configuration file. @@ -235,27 +215,18 @@ static uint32_t ms_get_cpu_count() * * @return ms_conf_type_t */ -ms_conf_type_t ms_get_conf_type(char *line) -{ - if (! memcmp(line, "key", strlen("key"))) - { +ms_conf_type_t ms_get_conf_type(char *line) { + if (!memcmp(line, "key", strlen("key"))) { return CONF_KEY; - } - else if (! memcmp(line, "value", strlen("value"))) - { + } else if (!memcmp(line, "value", strlen("value"))) { return CONF_VALUE; - } - else if (! memcmp(line, "cmd", strlen("cmd"))) - { + } else if (!memcmp(line, "cmd", strlen("cmd"))) { return CONF_CMD; - } - else - { + } else { return CONF_NULL; } } /* ms_get_conf_type */ - /** * judge whether the line is a line with useful data. used to * parse the configuration file. @@ -264,14 +235,12 @@ ms_conf_type_t ms_get_conf_type(char *line) * * @return if success, return EXIT_FAILURE, else return EXIT_SUCCESS */ -static int ms_is_line_data(char *line) -{ +static int ms_is_line_data(char *line) { assert(line != NULL); - char *begin_ptr= line; + char *begin_ptr = line; - while (isspace(*begin_ptr)) - { + while (isspace(*begin_ptr)) { begin_ptr++; } if ((begin_ptr[0] == '\0') || (begin_ptr[0] == '#')) @@ -280,7 +249,6 @@ static int ms_is_line_data(char *line) return EXIT_FAILURE; } /* ms_is_line_data */ - /** * function to bypass blank line and comments * @@ -289,36 +257,32 @@ static int ms_is_line_data(char *line) * * @return if it's EOF or not line data, return EXIT_SUCCESS, else return EXIT_FAILURE */ -static int ms_read_is_data(char *line, ssize_t nread) -{ - if ((nread == EOF) || ! ms_is_line_data(line)) +static int ms_read_is_data(char *line, ssize_t nread) { + if ((nread == EOF) || !ms_is_line_data(line)) return EXIT_SUCCESS; return EXIT_FAILURE; } /* ms_read_is_data */ - /** * if no configuration file, use this function to create the default * configuration file. */ -static void ms_no_config_file() -{ +static void ms_no_config_file() { char userpath[PATH_MAX]; - struct passwd *usr= NULL; + struct passwd *usr = NULL; FILE *fd; - usr= getpwuid(getuid()); + usr = getpwuid(getuid()); snprintf(userpath, PATH_MAX, "%s/%s", usr->pw_dir, DEFAULT_CONFIG_NAME); - if (access (userpath, F_OK | R_OK) == 0) + if (access(userpath, F_OK | R_OK) == 0) goto exit; - fd= fopen(userpath, "w+"); + fd = fopen(userpath, "w+"); - if (fd == NULL) - { + if (fd == NULL) { fprintf(stderr, "Could not create default configure file %s\n", userpath); perror("fopen"); exit(1); @@ -327,247 +291,204 @@ static void ms_no_config_file() fclose(fd); exit: - ms_setting.cfg_file= strdup(userpath); + ms_setting.cfg_file = strdup(userpath); } /* ms_no_config_file */ - /** * parse the configuration file * * @param cfg_file, the configuration file name */ -static void ms_parse_cfg_file(char *cfg_file) -{ +static void ms_parse_cfg_file(char *cfg_file) { FILE *f; size_t start_len, end_len; double proportion; - char *line= NULL; - size_t read_len; + char *line = NULL; + size_t read_len; ssize_t nread; int cmd_type; ms_conf_type_t conf_type; - int end_of_file= 0; - ms_key_distr_t *key_distr= NULL; - ms_value_distr_t *val_distr= NULL; + int end_of_file = 0; + ms_key_distr_t *key_distr = NULL; + ms_value_distr_t *val_distr = NULL; - if (cfg_file == NULL) - { + if (cfg_file == NULL) { ms_no_config_file(); - cfg_file= ms_setting.cfg_file; + cfg_file = ms_setting.cfg_file; } /*read key value configure file*/ - if ((f= fopen(cfg_file, "r")) == NULL) - { + if ((f = fopen(cfg_file, "r")) == NULL) { fprintf(stderr, "Can not open file: '%s'.\n", cfg_file); exit(1); } - while (1) - { - if ((((nread= getline(&line, &read_len, f)) == 1) - || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */ + while (1) { + if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread)) + && (nread != EOF)) /* bypass blank line */ continue; - if (nread == EOF) - { + if (nread == EOF) { fprintf(stderr, "Bad configuration file, no configuration find.\n"); exit(1); } - conf_type= ms_get_conf_type(line); + conf_type = ms_get_conf_type(line); break; } - while (! end_of_file) - { - switch (conf_type) - { + while (!end_of_file) { + switch (conf_type) { case CONF_KEY: - while (1) - { - if ((((nread= getline(&line, &read_len, f)) == 1) - || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */ + while (1) { + if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread)) + && (nread != EOF)) /* bypass blank line */ continue; - if (nread != EOF) - { - if (sscanf(line, "%zu %zu %lf ", &start_len, - &end_len, &proportion) != 3) - { - conf_type= ms_get_conf_type(line); + if (nread != EOF) { + if (sscanf(line, "%zu %zu %lf ", &start_len, &end_len, &proportion) != 3) { + conf_type = ms_get_conf_type(line); break; } - ms_setting.key_distr[ms_setting.key_rng_cnt].start_len= start_len; - ms_setting.key_distr[ms_setting.key_rng_cnt].end_len= end_len; - ms_setting.key_distr[ms_setting.key_rng_cnt].key_prop= proportion; + ms_setting.key_distr[ms_setting.key_rng_cnt].start_len = start_len; + ms_setting.key_distr[ms_setting.key_rng_cnt].end_len = end_len; + ms_setting.key_distr[ms_setting.key_rng_cnt].key_prop = proportion; ms_setting.key_rng_cnt++; - if (ms_setting.key_rng_cnt >= ms_setting.total_key_rng_cnt) - { - key_distr= (ms_key_distr_t *)realloc( - ms_setting.key_distr, - (size_t)ms_setting. - total_key_rng_cnt * sizeof(ms_key_distr_t) * 2); - if (key_distr == NULL) - { - fprintf(stderr, - "Can't reallocate key distribution structure.\n"); + if (ms_setting.key_rng_cnt >= ms_setting.total_key_rng_cnt) { + key_distr = (ms_key_distr_t *) realloc(ms_setting.key_distr, + (size_t) ms_setting.total_key_rng_cnt + * sizeof(ms_key_distr_t) * 2); + if (key_distr == NULL) { + fprintf(stderr, "Can't reallocate key distribution structure.\n"); exit(1); } - ms_setting.key_distr= key_distr; - ms_setting.total_key_rng_cnt*= 2; + ms_setting.key_distr = key_distr; + ms_setting.total_key_rng_cnt *= 2; } continue; } - end_of_file= 1; + end_of_file = 1; break; } break; case CONF_VALUE: - while (1) - { - if ((((nread= getline(&line, &read_len, f)) == 1) - || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */ + while (1) { + if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread)) + && (nread != EOF)) /* bypass blank line */ continue; - if (nread != EOF) - { - if (sscanf(line, "%zu %zu %lf", &start_len, &end_len, - &proportion) != 3) - { - conf_type= ms_get_conf_type(line); + if (nread != EOF) { + if (sscanf(line, "%zu %zu %lf", &start_len, &end_len, &proportion) != 3) { + conf_type = ms_get_conf_type(line); break; } - ms_setting.value_distr[ms_setting.val_rng_cnt].start_len= - start_len; - ms_setting.value_distr[ms_setting.val_rng_cnt].end_len= end_len; - ms_setting.value_distr[ms_setting.val_rng_cnt].value_prop= - proportion; + ms_setting.value_distr[ms_setting.val_rng_cnt].start_len = start_len; + ms_setting.value_distr[ms_setting.val_rng_cnt].end_len = end_len; + ms_setting.value_distr[ms_setting.val_rng_cnt].value_prop = proportion; ms_setting.val_rng_cnt++; - if (ms_setting.val_rng_cnt >= ms_setting.total_val_rng_cnt) - { - val_distr= (ms_value_distr_t *)realloc( - ms_setting.value_distr, - (size_t)ms_setting. - total_val_rng_cnt * sizeof(ms_value_distr_t) * 2); - if (val_distr == NULL) - { - fprintf(stderr, - "Can't reallocate key distribution structure.\n"); + if (ms_setting.val_rng_cnt >= ms_setting.total_val_rng_cnt) { + val_distr = (ms_value_distr_t *) realloc(ms_setting.value_distr, + (size_t) ms_setting.total_val_rng_cnt + * sizeof(ms_value_distr_t) * 2); + if (val_distr == NULL) { + fprintf(stderr, "Can't reallocate key distribution structure.\n"); exit(1); } - ms_setting.value_distr= val_distr; - ms_setting.total_val_rng_cnt*= 2; + ms_setting.value_distr = val_distr; + ms_setting.total_val_rng_cnt *= 2; } continue; } - end_of_file= 1; + end_of_file = 1; break; } break; case CONF_CMD: - while (1) - { - if ((((nread= getline(&line, &read_len, f)) == 1) - || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */ + while (1) { + if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread)) + && (nread != EOF)) /* bypass blank line */ continue; - if (nread != EOF) - { - if (sscanf(line, "%d %lf", &cmd_type, &proportion) != 2) - { - conf_type= ms_get_conf_type(line); + if (nread != EOF) { + if (sscanf(line, "%d %lf", &cmd_type, &proportion) != 2) { + conf_type = ms_get_conf_type(line); break; } - if (cmd_type >= CMD_NULL) - { + if (cmd_type >= CMD_NULL) { continue; } - ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_type= - cmd_type; - ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_prop= - proportion; + ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_type = cmd_type; + ms_setting.cmd_distr[ms_setting.cmd_used_count].cmd_prop = proportion; ms_setting.cmd_used_count++; continue; } - end_of_file= 1; + end_of_file = 1; break; } case CONF_NULL: - while (1) - { - if ((((nread= getline(&line, &read_len, f)) == 1) - || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */ + while (1) { + if ((((nread = getline(&line, &read_len, f)) == 1) || !ms_read_is_data(line, nread)) + && (nread != EOF)) /* bypass blank line */ continue; - if (nread != EOF) - { - if ((conf_type= ms_get_conf_type(line)) != CONF_NULL) - { + if (nread != EOF) { + if ((conf_type = ms_get_conf_type(line)) != CONF_NULL) { break; } continue; } - end_of_file= 1; + end_of_file = 1; break; } break; - default: - assert(0); - break; + default: assert(0); break; } /* switch */ } fclose(f); - if (line != NULL) - { + if (line != NULL) { free(line); } } /* ms_parse_cfg_file */ - /* calculate the average size of key and value */ -static void ms_calc_avg_size() -{ - double avg_val_size= 0.0; - double avg_key_size= 0.0; - double val_pro= 0.0; - double key_pro= 0.0; - double averge_len= 0.0; - size_t start_len= 0; - size_t end_len= 0; - - for (int j= 0; j < ms_setting.val_rng_cnt; j++) - { - val_pro= ms_setting.value_distr[j].value_prop; - start_len= ms_setting.value_distr[j].start_len; - end_len= ms_setting.value_distr[j].end_len; - - averge_len= val_pro * ((double)(start_len + end_len)) / 2; - avg_val_size+= averge_len; +static void ms_calc_avg_size() { + double avg_val_size = 0.0; + double avg_key_size = 0.0; + double val_pro = 0.0; + double key_pro = 0.0; + double averge_len = 0.0; + size_t start_len = 0; + size_t end_len = 0; + + for (int j = 0; j < ms_setting.val_rng_cnt; j++) { + val_pro = ms_setting.value_distr[j].value_prop; + start_len = ms_setting.value_distr[j].start_len; + end_len = ms_setting.value_distr[j].end_len; + + averge_len = val_pro * ((double) (start_len + end_len)) / 2; + avg_val_size += averge_len; } - for (int j= 0; j < ms_setting.key_rng_cnt; j++) - { - key_pro= ms_setting.key_distr[j].key_prop; - start_len= ms_setting.key_distr[j].start_len; - end_len= ms_setting.key_distr[j].end_len; + for (int j = 0; j < ms_setting.key_rng_cnt; j++) { + key_pro = ms_setting.key_distr[j].key_prop; + start_len = ms_setting.key_distr[j].start_len; + end_len = ms_setting.key_distr[j].end_len; - averge_len= key_pro * ((double)(start_len + end_len)) / 2; - avg_key_size+= averge_len; + averge_len = key_pro * ((double) (start_len + end_len)) / 2; + avg_key_size += averge_len; } - ms_setting.avg_val_size= (size_t)avg_val_size; - ms_setting.avg_key_size= (size_t)avg_key_size; + ms_setting.avg_val_size = (size_t) avg_val_size; + ms_setting.avg_key_size = (size_t) avg_key_size; } /* ms_calc_avg_size */ - /** * used to shuffle key and value distribution array to ensure * (key, value) pair with different set. @@ -577,47 +498,42 @@ static void ms_calc_avg_size() * * @return always return EXIT_SUCCESS */ -static int ms_shuffle_distr(ms_distr_t *distr, int length) -{ +static int ms_shuffle_distr(ms_distr_t *distr, int length) { int i, j; int tmp_offset; - size_t tmp_size; + size_t tmp_size; int64_t rnd; - for (i= 0; i < length; i++) - { - rnd= random(); - j= (int)(rnd % (length - i)) + i; + for (i = 0; i < length; i++) { + rnd = random(); + j = (int) (rnd % (length - i)) + i; - switch (rnd % 3) - { + switch (rnd % 3) { case 0: - tmp_size= distr[j].key_size; - distr[j].key_size= distr[i].key_size; - distr[i].key_size= tmp_size; + tmp_size = distr[j].key_size; + distr[j].key_size = distr[i].key_size; + distr[i].key_size = tmp_size; break; case 1: - tmp_offset= distr[j].key_offset; - distr[j].key_offset= distr[i].key_offset; - distr[i].key_offset= tmp_offset; + tmp_offset = distr[j].key_offset; + distr[j].key_offset = distr[i].key_offset; + distr[i].key_offset = tmp_offset; break; case 2: - tmp_size= distr[j].value_size; - distr[j].value_size= distr[i].value_size; - distr[i].value_size= tmp_size; + tmp_size = distr[j].value_size; + distr[j].value_size = distr[i].value_size; + distr[i].value_size = tmp_size; break; - default: - break; + default: break; } /* switch */ } return EXIT_SUCCESS; } /* ms_shuffle_distr */ - /** * according to the key and value distribution, to build the * (key, value) pair distribution. the (key, value) pair @@ -625,32 +541,28 @@ static int ms_shuffle_distr(ms_distr_t *distr, int length) * object keeping this distribution, for the final result, we * can reach the expected key and value distribution. */ -static void ms_build_distr() -{ - int offset= 0; - int end= 0; - int key_cnt= 0; - int value_cnt= 0; - size_t average_len= 0; - size_t diff_len= 0; - size_t start_len= 0; - size_t end_len= 0; - int rnd= 0; - ms_distr_t *distr= NULL; - int units= (int)ms_setting.win_size / UNIT_ITEMS_COUNT; +static void ms_build_distr() { + int offset = 0; + int end = 0; + int key_cnt = 0; + int value_cnt = 0; + size_t average_len = 0; + size_t diff_len = 0; + size_t start_len = 0; + size_t end_len = 0; + int rnd = 0; + ms_distr_t *distr = NULL; + int units = (int) ms_setting.win_size / UNIT_ITEMS_COUNT; /* calculate average value size and key size */ ms_calc_avg_size(); - ms_setting.char_blk_size= RAND_CHAR_SIZE; - int key_scope_size= - (int)((ms_setting.char_blk_size - RESERVED_RAND_CHAR_SIZE) - / UNIT_ITEMS_COUNT); + ms_setting.char_blk_size = RAND_CHAR_SIZE; + int key_scope_size = + (int) ((ms_setting.char_blk_size - RESERVED_RAND_CHAR_SIZE) / UNIT_ITEMS_COUNT); - ms_setting.distr= (ms_distr_t *)malloc( - sizeof(ms_distr_t) * ms_setting.win_size); - if (ms_setting.distr == NULL) - { + ms_setting.distr = (ms_distr_t *) malloc(sizeof(ms_distr_t) * ms_setting.win_size); + if (ms_setting.distr == NULL) { fprintf(stderr, "Can't allocate distribution array."); exit(1); } @@ -660,155 +572,113 @@ static void ms_build_distr() * size, each different key size has the same size character * range. */ - for (int m= 0; m < units; m++) - { - for (int i= 0; i < UNIT_ITEMS_COUNT; i++) - { - ms_setting.distr[m * UNIT_ITEMS_COUNT + i].key_offset= - ADDR_ALIGN(key_scope_size * i); + for (int m = 0; m < units; m++) { + for (int i = 0; i < UNIT_ITEMS_COUNT; i++) { + ms_setting.distr[m * UNIT_ITEMS_COUNT + i].key_offset = ADDR_ALIGN(key_scope_size * i); } } /* initialize key size distribution */ - for (int m= 0; m < units; m++) - { - for (int j= 0; j < ms_setting.key_rng_cnt; j++) - { - key_cnt= (int)(UNIT_ITEMS_COUNT * ms_setting.key_distr[j].key_prop); - start_len= ms_setting.key_distr[j].start_len; - end_len= ms_setting.key_distr[j].end_len; - if ((start_len < MIN_KEY_SIZE) || (end_len < MIN_KEY_SIZE)) - { + for (int m = 0; m < units; m++) { + for (int j = 0; j < ms_setting.key_rng_cnt; j++) { + key_cnt = (int) (UNIT_ITEMS_COUNT * ms_setting.key_distr[j].key_prop); + start_len = ms_setting.key_distr[j].start_len; + end_len = ms_setting.key_distr[j].end_len; + if ((start_len < MIN_KEY_SIZE) || (end_len < MIN_KEY_SIZE)) { fprintf(stderr, "key length must be greater than 16 bytes.\n"); exit(1); } - if (! ms_setting.binary_prot_ - && ((start_len > MAX_KEY_SIZE) || (end_len > MAX_KEY_SIZE))) - { + if (!ms_setting.binary_prot_ && ((start_len > MAX_KEY_SIZE) || (end_len > MAX_KEY_SIZE))) { fprintf(stderr, "key length must be less than 250 bytes.\n"); exit(1); } - average_len= (start_len + end_len) / 2; - diff_len= (end_len - start_len) / 2; - for (int k= 0; k < key_cnt; k++) - { - if (offset >= (m + 1) * UNIT_ITEMS_COUNT) - { + average_len = (start_len + end_len) / 2; + diff_len = (end_len - start_len) / 2; + for (int k = 0; k < key_cnt; k++) { + if (offset >= (m + 1) * UNIT_ITEMS_COUNT) { break; } - rnd= (int)random(); - if (k % 2 == 0) - { - ms_setting.distr[offset].key_size= - (diff_len == 0) ? average_len : - average_len + (size_t)rnd - % diff_len; - } - else - { - ms_setting.distr[offset].key_size= - (diff_len == 0) ? average_len : - average_len - (size_t)rnd - % diff_len; + rnd = (int) random(); + if (k % 2 == 0) { + ms_setting.distr[offset].key_size = + (diff_len == 0) ? average_len : average_len + (size_t) rnd % diff_len; + } else { + ms_setting.distr[offset].key_size = + (diff_len == 0) ? average_len : average_len - (size_t) rnd % diff_len; } offset++; } } - if (offset < (m + 1) * UNIT_ITEMS_COUNT) - { - end= (m + 1) * UNIT_ITEMS_COUNT - offset; - for (int i= 0; i < end; i++) - { - ms_setting.distr[offset].key_size= ms_setting.avg_key_size; + if (offset < (m + 1) * UNIT_ITEMS_COUNT) { + end = (m + 1) * UNIT_ITEMS_COUNT - offset; + for (int i = 0; i < end; i++) { + ms_setting.distr[offset].key_size = ms_setting.avg_key_size; offset++; } } } - offset= 0; + offset = 0; /* initialize value distribution */ - if (ms_setting.fixed_value_size != 0) - { - for (int i= 0; i < units * UNIT_ITEMS_COUNT; i++) - { - ms_setting.distr[i].value_size= ms_setting.fixed_value_size; + if (ms_setting.fixed_value_size != 0) { + for (int i = 0; i < units * UNIT_ITEMS_COUNT; i++) { + ms_setting.distr[i].value_size = ms_setting.fixed_value_size; } - } - else - { - for (int m= 0; m < units; m++) - { - for (int j= 0; j < ms_setting.val_rng_cnt; j++) - { - value_cnt= - (int)(UNIT_ITEMS_COUNT * ms_setting.value_distr[j].value_prop); - start_len= ms_setting.value_distr[j].start_len; - end_len= ms_setting.value_distr[j].end_len; - if ((start_len <= 0) || (end_len <= 0)) - { + } else { + for (int m = 0; m < units; m++) { + for (int j = 0; j < ms_setting.val_rng_cnt; j++) { + value_cnt = (int) (UNIT_ITEMS_COUNT * ms_setting.value_distr[j].value_prop); + start_len = ms_setting.value_distr[j].start_len; + end_len = ms_setting.value_distr[j].end_len; + if ((start_len <= 0) || (end_len <= 0)) { fprintf(stderr, "value length must be greater than 0 bytes.\n"); exit(1); } - if ((start_len > MAX_VALUE_SIZE) || (end_len > MAX_VALUE_SIZE)) - { + if ((start_len > MAX_VALUE_SIZE) || (end_len > MAX_VALUE_SIZE)) { fprintf(stderr, "key length must be less than or equal to 1M.\n"); exit(1); } - average_len= (start_len + end_len) / 2; - diff_len= (end_len - start_len) / 2; - for (int k= 0; k < value_cnt; k++) - { - if (offset >= (m + 1) * UNIT_ITEMS_COUNT) - { + average_len = (start_len + end_len) / 2; + diff_len = (end_len - start_len) / 2; + for (int k = 0; k < value_cnt; k++) { + if (offset >= (m + 1) * UNIT_ITEMS_COUNT) { break; } - rnd= (int)random(); - if (k % 2 == 0) - { - ms_setting.distr[offset].value_size= - (diff_len == 0) ? average_len : - average_len - + (size_t)rnd % diff_len; - } - else - { - ms_setting.distr[offset].value_size= - (diff_len == 0) ? average_len : - average_len - - (size_t)rnd % diff_len; + rnd = (int) random(); + if (k % 2 == 0) { + ms_setting.distr[offset].value_size = + (diff_len == 0) ? average_len : average_len + (size_t) rnd % diff_len; + } else { + ms_setting.distr[offset].value_size = + (diff_len == 0) ? average_len : average_len - (size_t) rnd % diff_len; } offset++; } } - if (offset < (m + 1) * UNIT_ITEMS_COUNT) - { - end= (m + 1) * UNIT_ITEMS_COUNT - offset; - for (int i= 0; i < end; i++) - { - ms_setting.distr[offset++].value_size= ms_setting.avg_val_size; + if (offset < (m + 1) * UNIT_ITEMS_COUNT) { + end = (m + 1) * UNIT_ITEMS_COUNT - offset; + for (int i = 0; i < end; i++) { + ms_setting.distr[offset++].value_size = ms_setting.avg_val_size; } } } } /* shuffle distribution */ - for (int i= 0; i < units; i++) - { - distr= &ms_setting.distr[i * UNIT_ITEMS_COUNT]; - for (int j= 0; j < 4; j++) - { + for (int i = 0; i < units; i++) { + distr = &ms_setting.distr[i * UNIT_ITEMS_COUNT]; + for (int j = 0; j < 4; j++) { ms_shuffle_distr(distr, UNIT_ITEMS_COUNT); } } } /* ms_build_distr */ - /** * used to initialize the global character block. The character * block is used to generate the suffix of the key and value. we @@ -816,98 +686,81 @@ static void ms_build_distr() * suffix or value string. It can save much memory to store key * or value string. */ -static void ms_init_random_block() -{ - char *ptr= NULL; +static void ms_init_random_block() { + char *ptr = NULL; assert(ms_setting.char_blk_size > 0); - ms_setting.char_block= (char *)malloc(ms_setting.char_blk_size); - if (ms_setting.char_block == NULL) - { + ms_setting.char_block = (char *) malloc(ms_setting.char_blk_size); + if (ms_setting.char_block == NULL) { fprintf(stderr, "Can't allocate global char block."); exit(1); } - ptr= ms_setting.char_block; + ptr = ms_setting.char_block; - for (int i= 0; (size_t)i < ms_setting.char_blk_size; i++) - { - *(ptr++)= ALPHANUMBERICS[random() % CHAR_COUNT]; + for (int i = 0; (size_t) i < ms_setting.char_blk_size; i++) { + *(ptr++) = ALPHANUMBERICS[random() % CHAR_COUNT]; } } /* ms_init_random_block */ - /** * after initialization, call this function to output the main * configuration user specified. */ -static void ms_print_setting() -{ +static void ms_print_setting() { fprintf(stdout, "servers: %s\n", ms_setting.srv_str); fprintf(stdout, "threads count: %d\n", ms_setting.nthreads); fprintf(stdout, "concurrency: %d\n", ms_setting.nconns); - if (ms_setting.run_time > 0) - { + if (ms_setting.run_time > 0) { fprintf(stdout, "run time: %ds\n", ms_setting.run_time); - } - else - { + } else { fprintf(stdout, "execute number: %" PRId64 "\n", ms_setting.exec_num); } - fprintf(stdout, "windows size: %" PRId64 "k\n", - (int64_t)(ms_setting.win_size / 1024)); - fprintf(stdout, "set proportion: set_prop=%.2f\n", - ms_setting.cmd_distr[CMD_SET].cmd_prop); - fprintf(stdout, "get proportion: get_prop=%.2f\n", - ms_setting.cmd_distr[CMD_GET].cmd_prop); + fprintf(stdout, "windows size: %" PRId64 "k\n", (int64_t)(ms_setting.win_size / 1024)); + fprintf(stdout, "set proportion: set_prop=%.2f\n", ms_setting.cmd_distr[CMD_SET].cmd_prop); + fprintf(stdout, "get proportion: get_prop=%.2f\n", ms_setting.cmd_distr[CMD_GET].cmd_prop); fflush(stdout); } /* ms_print_setting */ - /** * previous part of slap mode initialization of setting structure */ -static void ms_setting_slapmode_init_pre() -{ - ms_setting.exec_num= DEFAULT_EXE_NUM; - ms_setting.verify_percent= DEFAULT_VERIFY_RATE; - ms_setting.exp_ver_per= DEFAULT_VERIFY_RATE; - ms_setting.overwrite_percent= DEFAULT_OVERWRITE_RATE; - ms_setting.mult_key_num= DEFAULT_DIV; - ms_setting.fixed_value_size= 0; - ms_setting.win_size= DEFAULT_WINDOW_SIZE; - ms_setting.udp= false; - ms_setting.reconnect= false; - ms_setting.verbose= false; - ms_setting.facebook_test= false; - ms_setting.binary_prot_= false; - ms_setting.stat_freq= 0; - ms_setting.srv_str= NULL; - ms_setting.cfg_file= NULL; - ms_setting.sock_per_conn= DEFAULT_SOCK_PER_CONN; - ms_setting.expected_tps= 0; - ms_setting.rep_write_srv= 0; +static void ms_setting_slapmode_init_pre() { + ms_setting.exec_num = DEFAULT_EXE_NUM; + ms_setting.verify_percent = DEFAULT_VERIFY_RATE; + ms_setting.exp_ver_per = DEFAULT_VERIFY_RATE; + ms_setting.overwrite_percent = DEFAULT_OVERWRITE_RATE; + ms_setting.mult_key_num = DEFAULT_DIV; + ms_setting.fixed_value_size = 0; + ms_setting.win_size = DEFAULT_WINDOW_SIZE; + ms_setting.udp = false; + ms_setting.reconnect = false; + ms_setting.verbose = false; + ms_setting.facebook_test = false; + ms_setting.binary_prot_ = false; + ms_setting.stat_freq = 0; + ms_setting.srv_str = NULL; + ms_setting.cfg_file = NULL; + ms_setting.sock_per_conn = DEFAULT_SOCK_PER_CONN; + ms_setting.expected_tps = 0; + ms_setting.rep_write_srv = 0; } /* ms_setting_slapmode_init_pre */ - /** * previous part of initialization of setting structure */ -void ms_setting_init_pre() -{ +void ms_setting_init_pre() { memset(&ms_setting, 0, sizeof(ms_setting)); /* common initialize */ - ms_setting.ncpu= ms_get_cpu_count(); - ms_setting.nthreads= DEFAULT_THREADS_NUM; - ms_setting.nconns= DEFAULT_CONNS_NUM; - ms_setting.run_time= DEFAULT_RUN_TIME; - ms_setting.total_srv_cnt= MCD_SRVS_NUM_INIT; - ms_setting.servers= (ms_mcd_server_t *)malloc( - (size_t)ms_setting.total_srv_cnt - * sizeof(ms_mcd_server_t)); - if (ms_setting.servers == NULL) - { + ms_setting.ncpu = ms_get_cpu_count(); + ms_setting.nthreads = DEFAULT_THREADS_NUM; + ms_setting.nconns = DEFAULT_CONNS_NUM; + ms_setting.run_time = DEFAULT_RUN_TIME; + ms_setting.total_srv_cnt = MCD_SRVS_NUM_INIT; + ms_setting.servers = + (ms_mcd_server_t *) malloc((size_t) ms_setting.total_srv_cnt * sizeof(ms_mcd_server_t)); + if (ms_setting.servers == NULL) { fprintf(stderr, "Can't allocate servers structure.\n"); exit(1); } @@ -915,29 +768,25 @@ void ms_setting_init_pre() ms_setting_slapmode_init_pre(); } /* ms_setting_init_pre */ - /** * post part of slap mode initialization of setting structure */ -static void ms_setting_slapmode_init_post() -{ - ms_setting.total_key_rng_cnt= KEY_RANGE_COUNT_INIT; - ms_setting.key_distr= - (ms_key_distr_t *)malloc((size_t)ms_setting.total_key_rng_cnt * sizeof(ms_key_distr_t)); - - if (ms_setting.key_distr == NULL) - { +static void ms_setting_slapmode_init_post() { + ms_setting.total_key_rng_cnt = KEY_RANGE_COUNT_INIT; + ms_setting.key_distr = + (ms_key_distr_t *) malloc((size_t) ms_setting.total_key_rng_cnt * sizeof(ms_key_distr_t)); + + if (ms_setting.key_distr == NULL) { fprintf(stderr, "Can't allocate key distribution structure.\n"); exit(1); } - ms_setting.total_val_rng_cnt= VALUE_RANGE_COUNT_INIT; + ms_setting.total_val_rng_cnt = VALUE_RANGE_COUNT_INIT; - ms_setting.value_distr= - (ms_value_distr_t *)malloc((size_t)ms_setting.total_val_rng_cnt * sizeof( ms_value_distr_t)); + ms_setting.value_distr = + (ms_value_distr_t *) malloc((size_t) ms_setting.total_val_rng_cnt * sizeof(ms_value_distr_t)); - if (ms_setting.value_distr == NULL) - { + if (ms_setting.value_distr == NULL) { fprintf(stderr, "Can't allocate value distribution structure.\n"); exit(1); } @@ -945,65 +794,56 @@ static void ms_setting_slapmode_init_post() ms_parse_cfg_file(ms_setting.cfg_file); /* run time mode */ - if ((ms_setting.exec_num == 0) && (ms_setting.run_time != 0)) - { - ms_setting.exec_num= (int64_t)MAX_EXEC_NUM; - } - else - { + if ((ms_setting.exec_num == 0) && (ms_setting.run_time != 0)) { + ms_setting.exec_num = (int64_t) MAX_EXEC_NUM; + } else { /* execute number mode */ - ms_setting.run_time= 0; + ms_setting.run_time = 0; } - if (ms_setting.rep_write_srv > 0) - { + if (ms_setting.rep_write_srv > 0) { /* for replication test, need enable reconnect feature */ - ms_setting.reconnect= true; + ms_setting.reconnect = true; } - if (ms_setting.facebook_test && (ms_setting.mult_key_num < 2)) - { - fprintf(stderr, "facebook test must work with multi-get, " - "please specify multi-get key number " - "with '--division' option.\n"); + if (ms_setting.facebook_test && (ms_setting.mult_key_num < 2)) { + fprintf(stderr, + "facebook test must work with multi-get, " + "please specify multi-get key number " + "with '--division' option.\n"); exit(1); } - if (ms_setting.facebook_test && ms_setting.udp) - { + if (ms_setting.facebook_test && ms_setting.udp) { fprintf(stderr, "facebook test couldn't work with UDP.\n"); exit(1); } - if (ms_setting.udp && (ms_setting.sock_per_conn > 1)) - { - fprintf(stderr, "UDP doesn't support multi-socks " - "in one connection structure.\n"); + if (ms_setting.udp && (ms_setting.sock_per_conn > 1)) { + fprintf(stderr, + "UDP doesn't support multi-socks " + "in one connection structure.\n"); exit(1); } - if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < 2)) - { + if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < 2)) { fprintf(stderr, "Please specify 2 servers at least for replication\n"); exit(1); } - if ((ms_setting.rep_write_srv > 0) - && (ms_setting.srv_cnt < ms_setting.rep_write_srv)) - { - fprintf(stderr, "Servers to do replication writing " - "is larger than the total servers\n"); + if ((ms_setting.rep_write_srv > 0) && (ms_setting.srv_cnt < ms_setting.rep_write_srv)) { + fprintf(stderr, + "Servers to do replication writing " + "is larger than the total servers\n"); exit(1); } - if (ms_setting.udp && (ms_setting.rep_write_srv > 0)) - { + if (ms_setting.udp && (ms_setting.rep_write_srv > 0)) { fprintf(stderr, "UDP doesn't support replication.\n"); exit(1); } - if (ms_setting.facebook_test && (ms_setting.rep_write_srv > 0)) - { + if (ms_setting.facebook_test && (ms_setting.rep_write_srv > 0)) { fprintf(stderr, "facebook test couldn't work with replication.\n"); exit(1); } @@ -1015,54 +855,43 @@ static void ms_setting_slapmode_init_post() ms_print_setting(); } /* ms_setting_slapmode_init_post */ - /** * post part of initialization of setting structure */ -void ms_setting_init_post() -{ +void ms_setting_init_post() { ms_get_serverlist(ms_setting.srv_str); ms_setting_slapmode_init_post(); } - /** * clean up the global setting structure */ -void ms_setting_cleanup() -{ - if (ms_setting.distr != NULL) - { +void ms_setting_cleanup() { + if (ms_setting.distr != NULL) { free(ms_setting.distr); } - if (ms_setting.char_block != NULL) - { + if (ms_setting.char_block != NULL) { free(ms_setting.char_block); } - if (ms_setting.srv_str != NULL) - { + if (ms_setting.srv_str != NULL) { free(ms_setting.srv_str); } - if (ms_setting.cfg_file != NULL) - { + if (ms_setting.cfg_file != NULL) { free(ms_setting.cfg_file); } - if (ms_setting.servers != NULL) - { + if (ms_setting.servers != NULL) { free(ms_setting.servers); } - if (ms_setting.key_distr != NULL) - { + if (ms_setting.key_distr != NULL) { free(ms_setting.key_distr); } - if (ms_setting.value_distr != NULL) - { + if (ms_setting.value_distr != NULL) { free(ms_setting.value_distr); } } /* ms_setting_cleanup */ diff --git a/src/bin/memaslap/ms_setting.h b/src/bin/memaslap/ms_setting.h index 9db956c9..51cab2f7 100644 --- a/src/bin/memaslap/ms_setting.h +++ b/src/bin/memaslap/ms_setting.h @@ -1,13 +1,18 @@ /* - * File: ms_setting.h - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_SETTING_H #define MS_SETTING_H @@ -17,145 +22,127 @@ extern "C" { #endif -#define MCD_SRVS_NUM_INIT 8 -#define MCD_HOST_LENGTH 64 -#define KEY_RANGE_COUNT_INIT 8 -#define VALUE_RANGE_COUNT_INIT 8 -#define PROP_ERROR 0.001 +#define MCD_SRVS_NUM_INIT 8 +#define MCD_HOST_LENGTH 64 +#define KEY_RANGE_COUNT_INIT 8 +#define VALUE_RANGE_COUNT_INIT 8 +#define PROP_ERROR 0.001 -#define MIN_KEY_SIZE 16 -#define MAX_KEY_SIZE 250 -#define MAX_VALUE_SIZE (1024 * 1024) +#define MIN_KEY_SIZE 16 +#define MAX_KEY_SIZE 250 +#define MAX_VALUE_SIZE (1024 * 1024) /* the content of the configuration file for memslap running without configuration file */ #define DEFAULT_CONGIF_STR \ - "key\n" \ - "64 64 1\n" \ - "value\n" \ - "1024 1024 1\n" \ - "cmd\n" \ - "0 0.1\n" \ + "key\n" \ + "64 64 1\n" \ + "value\n" \ + "1024 1024 1\n" \ + "cmd\n" \ + "0 0.1\n" \ "1 0.9" /* Used to parse the value length return by server and path string */ -typedef struct token_s -{ +typedef struct token_s { char *value; size_t length; } token_t; -#define MAX_TOKENS 10 +#define MAX_TOKENS 10 /* server information */ -typedef struct mcd_server -{ - char srv_host_name[MCD_HOST_LENGTH]; /* host name of server */ - int srv_port; /* server port */ +typedef struct mcd_server { + char srv_host_name[MCD_HOST_LENGTH]; /* host name of server */ + int srv_port; /* server port */ /* for calculating how long the server disconnects */ - ATOMIC uint32_t disconn_cnt; /* number of disconnections count */ - ATOMIC uint32_t reconn_cnt; /* number of reconnections count */ - struct timeval disconn_time; /* start time of disconnection */ - struct timeval reconn_time; /* end time of reconnection */ + ATOMIC uint32_t disconn_cnt; /* number of disconnections count */ + ATOMIC uint32_t reconn_cnt; /* number of reconnections count */ + struct timeval disconn_time; /* start time of disconnection */ + struct timeval reconn_time; /* end time of reconnection */ } ms_mcd_server_t; /* information of an item distribution including key and value */ -typedef struct distr -{ - size_t key_size; /* size of key */ - int key_offset; /* offset of one key in character block */ - size_t value_size; /* size of value */ +typedef struct distr { + size_t key_size; /* size of key */ + int key_offset; /* offset of one key in character block */ + size_t value_size; /* size of value */ } ms_distr_t; /* information of key distribution */ -typedef struct key_distr -{ - size_t start_len; /* start of the key length range */ - size_t end_len; /* end of the key length range */ - double key_prop; /* key proportion */ +typedef struct key_distr { + size_t start_len; /* start of the key length range */ + size_t end_len; /* end of the key length range */ + double key_prop; /* key proportion */ } ms_key_distr_t; /* information of value distribution */ -typedef struct value_distr -{ - size_t start_len; /* start of the value length range */ - size_t end_len; /* end of the value length range */ - double value_prop; /* value proportion */ +typedef struct value_distr { + size_t start_len; /* start of the value length range */ + size_t end_len; /* end of the value length range */ + double value_prop; /* value proportion */ } ms_value_distr_t; /* memcached command types */ -typedef enum cmd_type -{ - CMD_SET, - CMD_GET, - CMD_NULL -} ms_cmd_type_t; +typedef enum cmd_type { CMD_SET, CMD_GET, CMD_NULL } ms_cmd_type_t; /* types in the configuration file */ -typedef enum conf_type -{ - CONF_KEY, - CONF_VALUE, - CONF_CMD, - CONF_NULL -} ms_conf_type_t; +typedef enum conf_type { CONF_KEY, CONF_VALUE, CONF_CMD, CONF_NULL } ms_conf_type_t; /* information of command distribution */ -typedef struct cmd_distr -{ - ms_cmd_type_t cmd_type; /* command type */ - double cmd_prop; /* proportion of the command */ +typedef struct cmd_distr { + ms_cmd_type_t cmd_type; /* command type */ + double cmd_prop; /* proportion of the command */ } ms_cmd_distr_t; /* global setting structure */ -typedef struct setting -{ - uint32_t ncpu; /* cpu count of this system */ - uint32_t nthreads; /* total thread count, must equal or less than cpu cores */ - uint32_t nconns; /* total conn count, must multiply by total thread count */ - int64_t exec_num; /* total execute number */ - int run_time; /* total run time */ - - uint32_t char_blk_size; /* global character block size */ - char *char_block; /* global character block with random character */ - ms_distr_t *distr; /* distribution from configure file */ - - char *srv_str; /* string includes servers information */ - char *cfg_file; /* configure file name */ - - ms_mcd_server_t *servers; /* servers array */ - uint32_t total_srv_cnt; /* total servers count of the servers array */ - uint32_t srv_cnt; /* servers count */ - - ms_key_distr_t *key_distr; /* array of key distribution */ - int total_key_rng_cnt; /* total key range count of the array */ - int key_rng_cnt; /* actual key range count */ - - ms_value_distr_t *value_distr; /* array of value distribution */ - int total_val_rng_cnt; /* total value range count of the array */ - int val_rng_cnt; /* actual value range count */ - - ms_cmd_distr_t cmd_distr[CMD_NULL]; /* total we have CMD_NULL commands */ - int cmd_used_count; /* supported command count */ - - size_t fixed_value_size; /* fixed value size */ - size_t avg_val_size; /* average value size */ - size_t avg_key_size; /* average value size */ - - double verify_percent; /* percent of data verification */ - double exp_ver_per; /* percent of data verification with expire time */ - double overwrite_percent; /* percent of overwrite */ - int mult_key_num; /* number of keys used by multi-get once */ - size_t win_size; /* item window size per connection */ - bool udp; /* whether or not use UDP */ - int stat_freq; /* statistic frequency second */ - bool reconnect; /* whether it reconnect when connection close */ - bool verbose; /* whether it outputs detailed information when verification */ - bool facebook_test; /* facebook test, TCP set and multi-get with UDP */ - uint32_t sock_per_conn; /* number of socks per connection structure */ - bool binary_prot_; /* whether it use binary protocol */ - int expected_tps; /* expected throughput */ - uint32_t rep_write_srv; /* which servers are used to do replication writing */ +typedef struct setting { + uint32_t ncpu; /* cpu count of this system */ + uint32_t nthreads; /* total thread count, must equal or less than cpu cores */ + uint32_t nconns; /* total conn count, must multiply by total thread count */ + int64_t exec_num; /* total execute number */ + int run_time; /* total run time */ + + uint32_t char_blk_size; /* global character block size */ + char *char_block; /* global character block with random character */ + ms_distr_t *distr; /* distribution from configure file */ + + char *srv_str; /* string includes servers information */ + char *cfg_file; /* configure file name */ + + ms_mcd_server_t *servers; /* servers array */ + uint32_t total_srv_cnt; /* total servers count of the servers array */ + uint32_t srv_cnt; /* servers count */ + + ms_key_distr_t *key_distr; /* array of key distribution */ + int total_key_rng_cnt; /* total key range count of the array */ + int key_rng_cnt; /* actual key range count */ + + ms_value_distr_t *value_distr; /* array of value distribution */ + int total_val_rng_cnt; /* total value range count of the array */ + int val_rng_cnt; /* actual value range count */ + + ms_cmd_distr_t cmd_distr[CMD_NULL]; /* total we have CMD_NULL commands */ + int cmd_used_count; /* supported command count */ + + size_t fixed_value_size; /* fixed value size */ + size_t avg_val_size; /* average value size */ + size_t avg_key_size; /* average value size */ + + double verify_percent; /* percent of data verification */ + double exp_ver_per; /* percent of data verification with expire time */ + double overwrite_percent; /* percent of overwrite */ + int mult_key_num; /* number of keys used by multi-get once */ + size_t win_size; /* item window size per connection */ + bool udp; /* whether or not use UDP */ + int stat_freq; /* statistic frequency second */ + bool reconnect; /* whether it reconnect when connection close */ + bool verbose; /* whether it outputs detailed information when verification */ + bool facebook_test; /* facebook test, TCP set and multi-get with UDP */ + uint32_t sock_per_conn; /* number of socks per connection structure */ + bool binary_prot_; /* whether it use binary protocol */ + int expected_tps; /* expected throughput */ + uint32_t rep_write_srv; /* which servers are used to do replication writing */ } ms_setting_st; extern ms_setting_st ms_setting; @@ -163,16 +150,13 @@ extern ms_setting_st ms_setting; /* previous part of initialization of setting structure */ void ms_setting_init_pre(void); - /* post part of initialization of setting structure */ void ms_setting_init_post(void); - /* clean up the global setting structure */ void ms_setting_cleanup(void); - -#define UNUSED_ARGUMENT(x) (void)x +#define UNUSED_ARGUMENT(x) (void) x #ifdef __cplusplus } diff --git a/src/bin/memaslap/ms_sigsegv.c b/src/bin/memaslap/ms_sigsegv.c index 303381f4..4c421489 100644 --- a/src/bin/memaslap/ms_sigsegv.c +++ b/src/bin/memaslap/ms_sigsegv.c @@ -1,16 +1,17 @@ /* - * File: ms_sigsegv.c - * Author: Mingqiang Zhuang - * - * Created on March 15, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - * Rewrite of stack dump: - * Copyright (C) 2009 Sun Microsystems - * Author Trond Norbye - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -28,10 +29,8 @@ int ms_setup_sigsegv(void); int ms_setup_sigpipe(void); int ms_setup_sigint(void); - /* signal seg reaches, this function will run */ -static void ms_signal_segv(int signum, siginfo_t *info, void *ptr) -{ +static void ms_signal_segv(int signum, siginfo_t *info, void *ptr) { UNUSED_ARGUMENT(signum); UNUSED_ARGUMENT(info); UNUSED_ARGUMENT(ptr); @@ -47,8 +46,7 @@ static void ms_signal_segv(int signum, siginfo_t *info, void *ptr) } /* signal int reaches, this function will run */ -static void ms_signal_int(int signum, siginfo_t *info, void *ptr) -{ +static void ms_signal_int(int signum, siginfo_t *info, void *ptr) { UNUSED_ARGUMENT(signum); UNUSED_ARGUMENT(info); UNUSED_ARGUMENT(ptr); @@ -59,21 +57,18 @@ static void ms_signal_int(int signum, siginfo_t *info, void *ptr) exit(1); } /* ms_signal_int */ - /** * redirect signal seg * * @return if success, return EXIT_SUCCESS, else return -1 */ -int ms_setup_sigsegv(void) -{ +int ms_setup_sigsegv(void) { struct sigaction action; memset(&action, 0, sizeof(action)); - action.sa_sigaction= ms_signal_segv; - action.sa_flags= SA_SIGINFO; - if (sigaction(SIGSEGV, &action, NULL) < 0) - { + action.sa_sigaction = ms_signal_segv; + action.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &action, NULL) < 0) { perror("sigaction"); return EXIT_SUCCESS; } @@ -81,35 +76,30 @@ int ms_setup_sigsegv(void) return -1; } /* ms_setup_sigsegv */ - /** * redirect signal pipe * * @return if success, return EXIT_SUCCESS, else return -1 */ -int ms_setup_sigpipe(void) -{ +int ms_setup_sigpipe(void) { /* ignore the SIGPIPE signal */ signal(SIGPIPE, SIG_IGN); return -1; } /* ms_setup_sigpipe */ - /** * redirect signal int * * @return if success, return EXIT_SUCCESS, else return -1 */ -int ms_setup_sigint(void) -{ +int ms_setup_sigint(void) { struct sigaction action_3; memset(&action_3, 0, sizeof(action_3)); - action_3.sa_sigaction= ms_signal_int; - action_3.sa_flags= SA_SIGINFO; - if (sigaction(SIGINT, &action_3, NULL) < 0) - { + action_3.sa_sigaction = ms_signal_int; + action_3.sa_flags = SA_SIGINFO; + if (sigaction(SIGINT, &action_3, NULL) < 0) { perror("sigaction"); return EXIT_SUCCESS; } @@ -117,10 +107,8 @@ int ms_setup_sigint(void) return -1; } /* ms_setup_sigint */ - #ifndef SIGSEGV_NO_AUTO_INIT -static void __attribute((constructor)) ms_init(void) -{ +static void __attribute((constructor)) ms_init(void) { ms_setup_sigsegv(); ms_setup_sigpipe(); ms_setup_sigint(); diff --git a/src/bin/memaslap/ms_sigsegv.h b/src/bin/memaslap/ms_sigsegv.h index 7990ff67..c990087c 100644 --- a/src/bin/memaslap/ms_sigsegv.h +++ b/src/bin/memaslap/ms_sigsegv.h @@ -1,13 +1,18 @@ /* - * File: ms_sigsegv.h - * Author: Mingqiang Zhuang - * - * Created on March 15, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_SIGSEGV_H #define MS_SIGSEGV_H @@ -18,15 +23,12 @@ extern "C" { /* redirect signal seg */ int ms_setup_sigsegv(void); - /* redirect signal pipe */ int ms_setup_sigpipe(void); - /* redirect signal int */ int ms_setup_sigint(void); - #ifdef __cplusplus } #endif diff --git a/src/bin/memaslap/ms_stats.c b/src/bin/memaslap/ms_stats.c index 086fb3ef..0b35f51f 100644 --- a/src/bin/memaslap/ms_stats.c +++ b/src/bin/memaslap/ms_stats.c @@ -1,25 +1,28 @@ /* - * File: ms_stats.h - * Author: Mingqiang Zhuang - * - * Created on March 25, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" #include #include "ms_stats.h" -#define array_size(x) (sizeof(x) / sizeof((x)[0])) +#define array_size(x) (sizeof(x) / sizeof((x)[0])) static int ms_local_log2(uint64_t value); static uint64_t ms_get_events(ms_stat_t *stat); - /** * get the index of local log2 array * @@ -27,46 +30,41 @@ static uint64_t ms_get_events(ms_stat_t *stat); * * @return return the index of local log2 array */ -static int ms_local_log2(uint64_t value) -{ - int result= 0; +static int ms_local_log2(uint64_t value) { + int result = 0; - while (result <= 63 && ((uint64_t)1 << result) < value) - { + while (result <= 63 && ((uint64_t) 1 << result) < value) { result++; } return result; } /* ms_local_log2 */ - /** * initialize statistic structure * * @param stat, pointer of the statistic structure * @param name, name of the statistic */ -void ms_init_stats(ms_stat_t *stat, const char *name) -{ +void ms_init_stats(ms_stat_t *stat, const char *name) { memset(stat, 0, sizeof(*stat)); - stat->name= (char *)name; - stat->min_time= (uint64_t)-1; - stat->max_time= 0; - stat->period_min_time= (uint64_t)-1; - stat->period_max_time= 0; - stat->log_product= 0; - stat->total_time= 0; - stat->pre_total_time= 0; - stat->squares= 0; - stat->pre_squares= 0; - stat->pre_events= 0; - stat->pre_log_product= 0; - stat->get_miss= 0; - stat->pre_get_miss= 0; + stat->name = (char *) name; + stat->min_time = (uint64_t) -1; + stat->max_time = 0; + stat->period_min_time = (uint64_t) -1; + stat->period_max_time = 0; + stat->log_product = 0; + stat->total_time = 0; + stat->pre_total_time = 0; + stat->squares = 0; + stat->pre_squares = 0; + stat->pre_events = 0; + stat->pre_log_product = 0; + stat->get_miss = 0; + stat->pre_get_miss = 0; } /* ms_init_stats */ - /** * record one event * @@ -74,45 +72,37 @@ void ms_init_stats(ms_stat_t *stat, const char *name) * @param total_time, response time of the command * @param get_miss, whether it gets miss */ -void ms_record_event(ms_stat_t *stat, uint64_t total_time, int get_miss) -{ - stat->total_time+= total_time; +void ms_record_event(ms_stat_t *stat, uint64_t total_time, int get_miss) { + stat->total_time += total_time; - if (total_time < stat->min_time) - { - stat->min_time= total_time; + if (total_time < stat->min_time) { + stat->min_time = total_time; } - if (total_time > stat->max_time) - { - stat->max_time= total_time; + if (total_time > stat->max_time) { + stat->max_time = total_time; } - if (total_time < stat->period_min_time) - { - stat->period_min_time= total_time; + if (total_time < stat->period_min_time) { + stat->period_min_time = total_time; } - if (total_time > stat->period_max_time) - { - stat->period_max_time= total_time; + if (total_time > stat->period_max_time) { + stat->period_max_time = total_time; } - if (get_miss) - { + if (get_miss) { stat->get_miss++; } stat->dist[ms_local_log2(total_time)]++; - stat->squares+= (double)(total_time * total_time); + stat->squares += (double) (total_time * total_time); - if (total_time != 0) - { - stat->log_product+= log((double)total_time); + if (total_time != 0) { + stat->log_product += log((double) total_time); } } /* ms_record_event */ - /** * get the events count * @@ -120,85 +110,70 @@ void ms_record_event(ms_stat_t *stat, uint64_t total_time, int get_miss) * * @return total events recorded */ -static uint64_t ms_get_events(ms_stat_t *stat) -{ - uint64_t events= 0; +static uint64_t ms_get_events(ms_stat_t *stat) { + uint64_t events = 0; - for (uint32_t i= 0; i < array_size(stat->dist); i++) - { - events+= stat->dist[i]; + for (uint32_t i = 0; i < array_size(stat->dist); i++) { + events += stat->dist[i]; } return events; } /* ms_get_events */ - /** * dump the statistics * * @param stat, pointer of the statistic structure */ -void ms_dump_stats(ms_stat_t *stat) -{ - uint64_t events= 0; - int max_non_zero= 0; - int min_non_zero= 0; - double average= 0; - - for (uint32_t i= 0; i < array_size(stat->dist); i++) - { - events+= stat->dist[i]; - if (stat->dist[i] != 0) - { - max_non_zero= (int)i; +void ms_dump_stats(ms_stat_t *stat) { + uint64_t events = 0; + int max_non_zero = 0; + int min_non_zero = 0; + double average = 0; + + for (uint32_t i = 0; i < array_size(stat->dist); i++) { + events += stat->dist[i]; + if (stat->dist[i] != 0) { + max_non_zero = (int) i; } } - if (events == 0) - { + if (events == 0) { return; } - average= (double)(stat->total_time / events); + average = (double) (stat->total_time / events); - printf("%s Statistics (%lld events)\n", stat->name, (long long)events); - printf(" Min: %8lld\n", (long long)stat->min_time); - printf(" Max: %8lld\n", (long long)stat->max_time); - printf(" Avg: %8lld\n", (long long)(stat->total_time / events)); - printf(" Geo: %8.2lf\n", exp(stat->log_product / (double)events)); + printf("%s Statistics (%lld events)\n", stat->name, (long long) events); + printf(" Min: %8lld\n", (long long) stat->min_time); + printf(" Max: %8lld\n", (long long) stat->max_time); + printf(" Avg: %8lld\n", (long long) (stat->total_time / events)); + printf(" Geo: %8.2lf\n", exp(stat->log_product / (double) events)); - if (events > 1) - { + if (events > 1) { printf(" Std: %8.2lf\n", - sqrt((stat->squares - (double)events * average - * average) / ((double)events - 1))); + sqrt((stat->squares - (double) events * average * average) / ((double) events - 1))); } printf(" Log2 Dist:"); - for (int i= 0; i <= max_non_zero - 4; i+= 4) - { - if ((stat->dist[i + 0] != 0) - || (stat->dist[i + 1] != 0) - || (stat->dist[i + 2] != 0) + for (int i = 0; i <= max_non_zero - 4; i += 4) { + if ((stat->dist[i + 0] != 0) || (stat->dist[i + 1] != 0) || (stat->dist[i + 2] != 0) || (stat->dist[i + 3] != 0)) { - min_non_zero= i; + min_non_zero = i; break; } } - for (int i= min_non_zero; i <= max_non_zero; i++) - { - if ((i % 4) == 0) - { - printf("\n %2d:", (int)i); + for (int i = min_non_zero; i <= max_non_zero; i++) { + if ((i % 4) == 0) { + printf("\n %2d:", (int) i); } - printf(" %6" PRIu64 , stat->dist[i]); + printf(" %6" PRIu64, stat->dist[i]); } printf("\n\n"); } /* ms_dump_stats */ - /** * dump the format statistics * @@ -207,97 +182,64 @@ void ms_dump_stats(ms_stat_t *stat) * @param freq, statistic frequency * @param obj_size, average object size */ -void ms_dump_format_stats(ms_stat_t *stat, - int run_time, - int freq, - int obj_size) -{ - uint64_t events= 0; - double global_average= 0; - uint64_t global_tps= 0; - double global_rate= 0; - double global_std= 0; - double global_log= 0; - - double period_average= 0; - uint64_t period_tps= 0; - double period_rate= 0; - double period_std= 0; - double period_log= 0; - - if ((events= ms_get_events(stat)) == 0) - { +void ms_dump_format_stats(ms_stat_t *stat, int run_time, int freq, int obj_size) { + uint64_t events = 0; + double global_average = 0; + uint64_t global_tps = 0; + double global_rate = 0; + double global_std = 0; + double global_log = 0; + + double period_average = 0; + uint64_t period_tps = 0; + double period_rate = 0; + double period_std = 0; + double period_log = 0; + + if ((events = ms_get_events(stat)) == 0) { return; } - global_average= (double)(stat->total_time / events); - global_tps= events / (uint64_t)run_time; - global_rate= (double)events * obj_size / 1024 / 1024 / run_time; - global_std= sqrt((stat->squares - (double)events * global_average - * global_average) / (double)(events - 1)); - global_log= exp(stat->log_product / (double)events); - - uint64_t diff_time= stat->total_time - stat->pre_total_time; - uint64_t diff_events= events - stat->pre_events; - if (diff_events >= 1) - { - period_average= (double)(diff_time / diff_events); - period_tps= diff_events / (uint64_t)freq; - period_rate= (double)diff_events * obj_size / 1024 / 1024 / freq; - double diff_squares= (double)stat->squares - (double)stat->pre_squares; - period_std= sqrt((diff_squares - (double)diff_events * period_average - * period_average) / (double)(diff_events - 1)); - double diff_log_product= stat->log_product - stat->pre_log_product; - period_log= exp(diff_log_product / (double)diff_events); + global_average = (double) (stat->total_time / events); + global_tps = events / (uint64_t) run_time; + global_rate = (double) events * obj_size / 1024 / 1024 / run_time; + global_std = sqrt((stat->squares - (double) events * global_average * global_average) + / (double) (events - 1)); + global_log = exp(stat->log_product / (double) events); + + uint64_t diff_time = stat->total_time - stat->pre_total_time; + uint64_t diff_events = events - stat->pre_events; + if (diff_events >= 1) { + period_average = (double) (diff_time / diff_events); + period_tps = diff_events / (uint64_t) freq; + period_rate = (double) diff_events * obj_size / 1024 / 1024 / freq; + double diff_squares = (double) stat->squares - (double) stat->pre_squares; + period_std = sqrt((diff_squares - (double) diff_events * period_average * period_average) + / (double) (diff_events - 1)); + double diff_log_product = stat->log_product - stat->pre_log_product; + period_log = exp(diff_log_product / (double) diff_events); } printf("%s Statistics\n", stat->name); - printf("%-8s %-8s %-12s %-12s %-10s %-10s %-8s %-10s %-10s %-10s %-10s\n", - "Type", - "Time(s)", - "Ops", - "TPS(ops/s)", - "Net(M/s)", - "Get_miss", - "Min(us)", - "Max(us)", - "Avg(us)", - "Std_dev", + printf("%-8s %-8s %-12s %-12s %-10s %-10s %-8s %-10s %-10s %-10s %-10s\n", "Type", "Time(s)", + "Ops", "TPS(ops/s)", "Net(M/s)", "Get_miss", "Min(us)", "Max(us)", "Avg(us)", "Std_dev", "Geo_dist"); - printf( - "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n", - "Period", - freq, - (long long)diff_events, - (long long)period_tps, - global_rate, - (long long)(stat->get_miss - stat->pre_get_miss), - (long long)stat->period_min_time, - (long long)stat->period_max_time, - (long long)period_average, - period_std, - period_log); - - printf( - "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n\n", - "Global", - run_time, - (long long)events, - (long long)global_tps, - period_rate, - (long long)stat->get_miss, - (long long)stat->min_time, - (long long)stat->max_time, - (long long)global_average, - global_std, - global_log); - - stat->pre_events= events; - stat->pre_squares= (uint64_t)stat->squares; - stat->pre_total_time= stat->total_time; - stat->pre_log_product= stat->log_product; - stat->period_min_time= (uint64_t)-1; - stat->period_max_time= 0; - stat->pre_get_miss= stat->get_miss; + printf("%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n", + "Period", freq, (long long) diff_events, (long long) period_tps, global_rate, + (long long) (stat->get_miss - stat->pre_get_miss), (long long) stat->period_min_time, + (long long) stat->period_max_time, (long long) period_average, period_std, period_log); + + printf("%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n\n", + "Global", run_time, (long long) events, (long long) global_tps, period_rate, + (long long) stat->get_miss, (long long) stat->min_time, (long long) stat->max_time, + (long long) global_average, global_std, global_log); + + stat->pre_events = events; + stat->pre_squares = (uint64_t) stat->squares; + stat->pre_total_time = stat->total_time; + stat->pre_log_product = stat->log_product; + stat->period_min_time = (uint64_t) -1; + stat->period_max_time = 0; + stat->pre_get_miss = stat->get_miss; } /* ms_dump_format_stats */ diff --git a/src/bin/memaslap/ms_stats.h b/src/bin/memaslap/ms_stats.h index 5ac88b3f..9d12a393 100644 --- a/src/bin/memaslap/ms_stats.h +++ b/src/bin/memaslap/ms_stats.h @@ -1,13 +1,18 @@ /* - * File: ms_stats.h - * Author: Mingqiang Zhuang - * - * Created on March 25, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_STAT_H #define MS_STAT_H @@ -23,8 +28,7 @@ extern "C" { #endif /* statistic structure of response time */ -typedef struct -{ +typedef struct { char *name; uint64_t total_time; uint64_t min_time; @@ -46,24 +50,17 @@ typedef struct /* initialize statistic */ void ms_init_stats(ms_stat_t *stat, const char *name); - /* record one event */ void ms_record_event(ms_stat_t *stat, uint64_t time, int get_miss); - /* dump the statistics */ void ms_dump_stats(ms_stat_t *stat); - /* dump the format statistics */ -void ms_dump_format_stats(ms_stat_t *stat, - int run_time, - int freq, - int obj_size); - +void ms_dump_format_stats(ms_stat_t *stat, int run_time, int freq, int obj_size); #ifdef __cplusplus } #endif -#endif /* MS_STAT_H */ +#endif /* MS_STAT_H */ diff --git a/src/bin/memaslap/ms_task.c b/src/bin/memaslap/ms_task.c index f2cb8657..638fb485 100644 --- a/src/bin/memaslap/ms_task.c +++ b/src/bin/memaslap/ms_task.c @@ -1,22 +1,26 @@ /* - * File: ms_task.c - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" #if defined(HAVE_SYS_TIME_H) -# include +# include #endif #if defined(HAVE_TIME_H) -# include +# include #endif #include "ms_thread.h" @@ -24,13 +28,14 @@ #include "ms_atomic.h" /* command distribution adjustment cycle */ -#define CMD_DISTR_ADJUST_CYCLE 1000 -#define DISADJUST_FACTOR 0.03 /** - * In one adjustment cycle, if undo set or get - * operations proportion is more than 3% , means - * there are too many new item or need more new - * item in the window. This factor shows it. - */ +#define CMD_DISTR_ADJUST_CYCLE 1000 +#define DISADJUST_FACTOR \ + 0.03 /** \ + * In one adjustment cycle, if undo set or get \ + * operations proportion is more than 3% , means \ + * there are too many new item or need more new \ + * item in the window. This factor shows it. \ + */ /* get item from task window */ static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c); @@ -38,31 +43,25 @@ static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c); static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c); static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c); - /* select next operation to do */ static void ms_select_opt(ms_conn_t *c, ms_task_t *task); - /* set and get speed estimate for controlling and adjustment */ static bool ms_is_set_too_fast(ms_task_t *task); static bool ms_is_get_too_fast(ms_task_t *task); static void ms_kick_out_item(ms_task_item_t *item); - /* miss rate adjustment */ static bool ms_need_overwrite_item(ms_task_t *task); static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task); - /* deal with data verification initialization */ static void ms_task_data_verify_init(ms_task_t *task); static void ms_task_expire_verify_init(ms_task_t *task); - /* select a new task to do */ static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup); - /* run the selected task */ static void ms_update_set_result(ms_conn_t *c, ms_task_item_t *item); static void ms_update_stat_result(ms_conn_t *c); @@ -75,7 +74,6 @@ static void ms_send_signal(ms_sync_lock_t *sync_lock); static void ms_warmup_server(ms_conn_t *c); static int ms_run_getset_task(ms_conn_t *c); - /** * used to get the current operation item(object) * @@ -83,12 +81,10 @@ static int ms_run_getset_task(ms_conn_t *c); * * @return ms_task_item_t*, current operating item */ -static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c) -{ +static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c) { return c->curr_task.item; } - /** * used to get the next item to do get operation * @@ -97,30 +93,23 @@ static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c) * @return ms_task_item_t*, the pointer of the next item to do * get operation */ -static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c) -{ - ms_task_item_t *item= NULL; +static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c) { + ms_task_item_t *item = NULL; - if (c->set_cursor <= 0) - { + if (c->set_cursor <= 0) { /* the first item in the window */ - item= &c->item_win[0]; - } - else if (c->set_cursor > 0 && c->set_cursor < (uint32_t)c->win_size) - { + item = &c->item_win[0]; + } else if (c->set_cursor > 0 && c->set_cursor < (uint32_t) c->win_size) { /* random get one item set before */ - item= &c->item_win[random() % (int64_t)c->set_cursor]; - } - else - { + item = &c->item_win[random() % (int64_t) c->set_cursor]; + } else { /* random get one item from the window */ - item= &c->item_win[random() % c->win_size]; + item = &c->item_win[random() % c->win_size]; } return item; } /* ms_get_next_get_item */ - /** * used to get the next item to do set operation * @@ -129,17 +118,15 @@ static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c) * @return ms_task_item_t*, the pointer of the next item to do * set operation */ -static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c) -{ +static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c) { /** * when a set command successes, the cursor will plus 1. If set * fails, the cursor doesn't change. it isn't necessary to * increase the cursor here. */ - return &c->item_win[(int64_t)c->set_cursor % c->win_size]; + return &c->item_win[(int64_t) c->set_cursor % c->win_size]; } - /** * If we need do overwrite, we could select a item set before. * This function is used to get a item set before to do @@ -150,9 +137,8 @@ static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c) * @return ms_task_item_t*, the pointer of the previous item of * set operation */ -static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c) -{ - return ms_get_next_get_item(c); +static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c) { + return ms_get_next_get_item(c); } /* ms_get_random_overwrite_item */ /** @@ -162,16 +148,14 @@ static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c) * @param c, pointer of the concurrency * @param task, pointer of current task in the concurrency */ -static void ms_select_opt(ms_conn_t *c, ms_task_t *task) -{ - double get_prop= ms_setting.cmd_distr[CMD_GET].cmd_prop; - double set_prop= ms_setting.cmd_distr[CMD_SET].cmd_prop; +static void ms_select_opt(ms_conn_t *c, ms_task_t *task) { + double get_prop = ms_setting.cmd_distr[CMD_GET].cmd_prop; + double set_prop = ms_setting.cmd_distr[CMD_SET].cmd_prop; /* update cycle operation number if necessary */ - if ((task->cycle_undo_get == 0) || (task->cycle_undo_set == 0)) - { - task->cycle_undo_get+= (int)(CMD_DISTR_ADJUST_CYCLE * get_prop); - task->cycle_undo_set+= (int)(CMD_DISTR_ADJUST_CYCLE * set_prop); + if ((task->cycle_undo_get == 0) || (task->cycle_undo_set == 0)) { + task->cycle_undo_get += (int) (CMD_DISTR_ADJUST_CYCLE * get_prop); + task->cycle_undo_set += (int) (CMD_DISTR_ADJUST_CYCLE * set_prop); } /** @@ -180,20 +164,16 @@ static void ms_select_opt(ms_conn_t *c, ms_task_t *task) * to do get operation. */ if ((set_prop > PROP_ERROR) - && ((double)task->get_opt * set_prop >= (double)task->set_opt - * get_prop)) + && ((double) task->get_opt * set_prop >= (double) task->set_opt * get_prop)) { - task->cmd= CMD_SET; - task->item= ms_get_next_set_item(c); - } - else - { - task->cmd= CMD_GET; - task->item= ms_get_next_get_item(c); + task->cmd = CMD_SET; + task->item = ms_get_next_set_item(c); + } else { + task->cmd = CMD_GET; + task->item = ms_get_next_get_item(c); } } /* ms_select_opt */ - /** * used to judge whether the number of get operations done is * more than expected number of get operations to do right now. @@ -202,21 +182,18 @@ static void ms_select_opt(ms_conn_t *c, ms_task_t *task) * * @return bool, if get too fast, return true, else return false */ -static bool ms_is_get_too_fast(ms_task_t *task) -{ - double get_prop= ms_setting.cmd_distr[CMD_GET].cmd_prop; - double set_prop= ms_setting.cmd_distr[CMD_SET].cmd_prop; +static bool ms_is_get_too_fast(ms_task_t *task) { + double get_prop = ms_setting.cmd_distr[CMD_GET].cmd_prop; + double set_prop = ms_setting.cmd_distr[CMD_SET].cmd_prop; /* no get operation */ - if (get_prop < PROP_ERROR) - { + if (get_prop < PROP_ERROR) { return false; } - int max_undo_set= (int)(set_prop / get_prop * (1.0 + DISADJUST_FACTOR)) - * task->cycle_undo_get; + int max_undo_set = (int) (set_prop / get_prop * (1.0 + DISADJUST_FACTOR)) * task->cycle_undo_get; - if (((double)task->get_opt * set_prop > (double)task->set_opt * get_prop) + if (((double) task->get_opt * set_prop > (double) task->set_opt * get_prop) && (task->cycle_undo_set > max_undo_set)) { return true; @@ -225,7 +202,6 @@ static bool ms_is_get_too_fast(ms_task_t *task) return false; } /* ms_is_get_too_fast */ - /** * used to judge whether the number of set operations done is * more than expected number of set operations to do right now. @@ -234,22 +210,20 @@ static bool ms_is_get_too_fast(ms_task_t *task) * * @return bool, if set too fast, return true, else return false */ -static bool ms_is_set_too_fast(ms_task_t *task) -{ - double get_prop= ms_setting.cmd_distr[CMD_GET].cmd_prop; - double set_prop= ms_setting.cmd_distr[CMD_SET].cmd_prop; +static bool ms_is_set_too_fast(ms_task_t *task) { + double get_prop = ms_setting.cmd_distr[CMD_GET].cmd_prop; + double set_prop = ms_setting.cmd_distr[CMD_SET].cmd_prop; /* no set operation */ - if (set_prop < PROP_ERROR) - { + if (set_prop < PROP_ERROR) { return false; } /* If it does set operation too fast, skip some */ - int max_undo_get= (int)((get_prop / set_prop * (1.0 + DISADJUST_FACTOR)) - * (double)task->cycle_undo_set); + int max_undo_get = + (int) ((get_prop / set_prop * (1.0 + DISADJUST_FACTOR)) * (double) task->cycle_undo_set); - if (((double)task->get_opt * set_prop < (double)task->set_opt * get_prop) + if (((double) task->get_opt * set_prop < (double) task->set_opt * get_prop) && (task->cycle_undo_get > max_undo_get)) { return true; @@ -258,7 +232,6 @@ static bool ms_is_set_too_fast(ms_task_t *task) return false; } /* ms_is_set_too_fast */ - /** * kick out the old item in the window, and add a new item to * overwrite the old item. When we don't want to do overwrite @@ -269,17 +242,15 @@ static bool ms_is_set_too_fast(ms_task_t *task) * @param item, pointer of task item which includes the object * information */ -static void ms_kick_out_item(ms_task_item_t *item) -{ +static void ms_kick_out_item(ms_task_item_t *item) { /* allocate a new item */ - item->key_prefix= ms_get_key_prefix(); + item->key_prefix = ms_get_key_prefix(); item->key_suffix_offset++; - item->value_offset= INVALID_OFFSET; /* new item use invalid value offset */ - item->client_time= 0; + item->value_offset = INVALID_OFFSET; /* new item use invalid value offset */ + item->client_time = 0; } /* ms_kick_out_item */ - /** * used to judge whether we need overwrite object based on the * options user specified @@ -289,9 +260,8 @@ static void ms_kick_out_item(ms_task_item_t *item) * @return bool, if need overwrite, return true, else return * false */ -static bool ms_need_overwrite_item(ms_task_t *task) -{ - ms_task_item_t *item= task->item; +static bool ms_need_overwrite_item(ms_task_t *task) { + ms_task_item_t *item = task->item; assert(item != NULL); assert(task->cmd == CMD_SET); @@ -300,16 +270,13 @@ static bool ms_need_overwrite_item(ms_task_t *task) * according to data overwrite percent to determine if do data * overwrite. */ - if (task->overwrite_set < (double)task->set_opt - * ms_setting.overwrite_percent) - { + if (task->overwrite_set < (double) task->set_opt * ms_setting.overwrite_percent) { return true; } return false; } /* ms_need_overwirte_item */ - /** * used to adjust operation. the function must be called after * select operation. the function change get operation to set @@ -321,71 +288,54 @@ static bool ms_need_overwrite_item(ms_task_t *task) * * @return bool, if success, return true, else return false */ -static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task) -{ - ms_task_item_t *item= task->item; +static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task) { + ms_task_item_t *item = task->item; assert(item != NULL); - if (task->cmd == CMD_SET) - { + if (task->cmd == CMD_SET) { /* If did set operation too fast, skip some */ - if (ms_is_set_too_fast(task)) - { + if (ms_is_set_too_fast(task)) { /* get the item instead */ - if (item->value_offset != INVALID_OFFSET) - { - task->cmd= CMD_GET; + if (item->value_offset != INVALID_OFFSET) { + task->cmd = CMD_GET; return true; } } /* If the current item is not a new item, kick it out */ - if (item->value_offset != INVALID_OFFSET) - { - if (ms_need_overwrite_item(task)) - { + if (item->value_offset != INVALID_OFFSET) { + if (ms_need_overwrite_item(task)) { /* overwrite */ task->overwrite_set++; - } - else - { + } else { /* kick out the current item to do set operation */ ms_kick_out_item(item); } - } - else /* it's a new item */ - { + } else /* it's a new item */ { /* need overwrite */ - if (ms_need_overwrite_item(task)) - { + if (ms_need_overwrite_item(task)) { /** * overwrite not use the item with current set cursor, revert * set cursor. */ c->set_cursor--; - item= ms_get_random_overwrite_item(c); - if (item->value_offset != INVALID_OFFSET) - { - task->item= item; + item = ms_get_random_overwrite_item(c); + if (item->value_offset != INVALID_OFFSET) { + task->item = item; task->overwrite_set++; - } - else /* item is a new item */ - { + } else /* item is a new item */ { /* select the item to run, and cancel overwrite */ - task->item= item; + task->item = item; } } } - task->cmd= CMD_SET; + task->cmd = CMD_SET; return true; - } - else - { - if (item->value_offset == INVALID_OFFSET) - { - task->cmd= CMD_SET; + } else { + if (item->value_offset == INVALID_OFFSET) { + task->cmd = CMD_SET; return true; } @@ -393,37 +343,31 @@ static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task) * If It does get operation too fast, it will change the * operation to set. */ - if (ms_is_get_too_fast(task)) - { + if (ms_is_get_too_fast(task)) { /* don't kick out the first item in the window */ - if (! ms_is_set_too_fast(task)) - { + if (!ms_is_set_too_fast(task)) { ms_kick_out_item(item); - task->cmd= CMD_SET; + task->cmd = CMD_SET; return true; - } - else - { + } else { return false; } } assert(item->value_offset != INVALID_OFFSET); - task->cmd= CMD_GET; + task->cmd = CMD_GET; return true; } } /* ms_adjust_opt */ - /** * used to initialize the task which need verify data. * * @param task, pointer of current task in the concurrency */ -static void ms_task_data_verify_init(ms_task_t *task) -{ - ms_task_item_t *item= task->item; +static void ms_task_data_verify_init(ms_task_t *task) { + ms_task_item_t *item = task->item; assert(item != NULL); assert(task->cmd == CMD_GET); @@ -432,42 +376,35 @@ static void ms_task_data_verify_init(ms_task_t *task) * according to data verification percent to determine if do * data verification. */ - if (task->verified_get < (double)task->get_opt - * ms_setting.verify_percent) - { + if (task->verified_get < (double) task->get_opt * ms_setting.verify_percent) { /** * currently it doesn't do verify, just increase the counter, * and do verification next proper get command */ - if ((task->item->value_offset != INVALID_OFFSET) - && (item->exp_time == 0)) - { - task->verify= true; - task->finish_verify= false; + if ((task->item->value_offset != INVALID_OFFSET) && (item->exp_time == 0)) { + task->verify = true; + task->finish_verify = false; task->verified_get++; } } } /* ms_task_data_verify_init */ - /** * used to initialize the task which need verify expire time. * * @param task, pointer of current task in the concurrency */ -static void ms_task_expire_verify_init(ms_task_t *task) -{ - ms_task_item_t *item= task->item; +static void ms_task_expire_verify_init(ms_task_t *task) { + ms_task_item_t *item = task->item; assert(item != NULL); assert(task->cmd == CMD_GET); assert(item->exp_time > 0); - task->verify= true; - task->finish_verify= false; + task->verify = true; + task->finish_verify = false; } /* ms_task_expire_verify_init */ - /** * used to get one task, the function initializes the task * structure. @@ -478,20 +415,17 @@ static void ms_task_expire_verify_init(ms_task_t *task) * @return ms_task_t*, pointer of current task in the * concurrency */ -static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup) -{ - ms_task_t *task= &c->curr_task; +static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup) { + ms_task_t *task = &c->curr_task; - while (1) - { - task->verify= false; - task->finish_verify= true; - task->get_miss= true; + while (1) { + task->verify = false; + task->finish_verify = true; + task->get_miss = true; - if (warmup) - { - task->cmd= CMD_SET; - task->item= ms_get_next_set_item(c); + if (warmup) { + task->cmd = CMD_SET; + task->item = ms_get_next_set_item(c); return task; } @@ -499,19 +433,15 @@ static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup) /* according to operation distribution to choose doing which operation */ ms_select_opt(c, task); - if (! ms_adjust_opt(c, task)) - { + if (!ms_adjust_opt(c, task)) { continue; } - if ((ms_setting.verify_percent > 0) && (task->cmd == CMD_GET)) - { + if ((ms_setting.verify_percent > 0) && (task->cmd == CMD_GET)) { ms_task_data_verify_init(task); } - if ((ms_setting.exp_ver_per > 0) && (task->cmd == CMD_GET) - && (task->item->exp_time > 0)) - { + if ((ms_setting.exp_ver_per > 0) && (task->cmd == CMD_GET) && (task->item->exp_time > 0)) { ms_task_expire_verify_init(task); } @@ -522,8 +452,7 @@ static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup) * Only update get and delete counter, set counter will be * updated after set operation successes. */ - if (task->cmd == CMD_GET) - { + if (task->cmd == CMD_GET) { task->get_opt++; task->cycle_undo_get--; } @@ -531,21 +460,18 @@ static ms_task_t *ms_get_task(ms_conn_t *c, bool warmup) return task; } /* ms_get_task */ - /** * send a signal to the main monitor thread * * @param sync_lock, pointer of the lock */ -static void ms_send_signal(ms_sync_lock_t *sync_lock) -{ +static void ms_send_signal(ms_sync_lock_t *sync_lock) { pthread_mutex_lock(&sync_lock->lock); sync_lock->count++; pthread_cond_signal(&sync_lock->cond); pthread_mutex_unlock(&sync_lock->lock); } /* ms_send_signal */ - /** * If user only want to do get operation, but there is no object * in server , so we use this function to warmup the server, and @@ -553,8 +479,7 @@ static void ms_send_signal(ms_sync_lock_t *sync_lock) * * @param c, pointer of the concurrency */ -static void ms_warmup_server(ms_conn_t *c) -{ +static void ms_warmup_server(ms_conn_t *c) { ms_task_t *task; ms_task_item_t *item; @@ -562,29 +487,23 @@ static void ms_warmup_server(ms_conn_t *c) * Extra one loop to get the last command returned state. * Normally it gets the previous command returned state. */ - if ((c->remain_warmup_num >= 0) - && (c->remain_warmup_num != c->warmup_num)) - { - item= ms_get_cur_opt_item(c); + if ((c->remain_warmup_num >= 0) && (c->remain_warmup_num != c->warmup_num)) { + item = ms_get_cur_opt_item(c); /* only update the set command result state for data verification */ - if ((c->precmd.cmd == CMD_SET) && (c->precmd.retstat == MCD_STORED)) - { - item->value_offset= item->key_suffix_offset; + if ((c->precmd.cmd == CMD_SET) && (c->precmd.retstat == MCD_STORED)) { + item->value_offset = item->key_suffix_offset; /* set success, update counter */ c->set_cursor++; - } - else if (c->precmd.cmd == CMD_SET && c->precmd.retstat != MCD_STORED) - { + } else if (c->precmd.cmd == CMD_SET && c->precmd.retstat != MCD_STORED) { printf("key: %" PRIx64 " didn't set success\n", item->key_prefix); } } /* the last time don't run a task */ - if (c->remain_warmup_num-- > 0) - { + if (c->remain_warmup_num-- > 0) { /* operate next task item */ - task= ms_get_task(c, true); - item= task->item; + task = ms_get_task(c, true); + item = task->item; ms_mcd_set(c, item); } @@ -593,70 +512,56 @@ static void ms_warmup_server(ms_conn_t *c) * complete. Then all connects can start do task at the same * time. */ - if (c->remain_warmup_num == -1) - { + if (c->remain_warmup_num == -1) { ms_send_signal(&ms_global.warmup_lock); - c->remain_warmup_num--; /* never run the if branch */ + c->remain_warmup_num--; /* never run the if branch */ } } /* ms_warmup_server */ - /** * dispatch single get and set task * * @param c, pointer of the concurrency */ -static void ms_single_getset_task_sch(ms_conn_t *c) -{ +static void ms_single_getset_task_sch(ms_conn_t *c) { ms_task_t *task; ms_task_item_t *item; /* the last time don't run a task */ - if (c->remain_exec_num-- > 0) - { - task= ms_get_task(c, false); - item= task->item; - if (task->cmd == CMD_SET) - { + if (c->remain_exec_num-- > 0) { + task = ms_get_task(c, false); + item = task->item; + if (task->cmd == CMD_SET) { ms_mcd_set(c, item); - } - else if (task->cmd == CMD_GET) - { + } else if (task->cmd == CMD_GET) { assert(task->cmd == CMD_GET); ms_mcd_get(c, item); } } } /* ms_single_getset_task_sch */ - /** * dispatch multi-get and set task * * @param c, pointer of the concurrency */ -static void ms_multi_getset_task_sch(ms_conn_t *c) -{ +static void ms_multi_getset_task_sch(ms_conn_t *c) { ms_task_t *task; ms_mlget_task_item_t *mlget_item; - while (1) - { - if (c->remain_exec_num-- > 0) - { - task= ms_get_task(c, false); - if (task->cmd == CMD_SET) /* just do it */ - { + while (1) { + if (c->remain_exec_num-- > 0) { + task = ms_get_task(c, false); + if (task->cmd == CMD_SET) /* just do it */ { ms_mcd_set(c, task->item); break; - } - else - { + } else { assert(task->cmd == CMD_GET); - mlget_item= &c->mlget_task.mlget_item[c->mlget_task.mlget_num]; - mlget_item->item= task->item; - mlget_item->verify= task->verify; - mlget_item->finish_verify= task->finish_verify; - mlget_item->get_miss= task->get_miss; + mlget_item = &c->mlget_task.mlget_item[c->mlget_task.mlget_num]; + mlget_item->item = task->item; + mlget_item->verify = task->verify; + mlget_item->finish_verify = task->finish_verify; + mlget_item->get_miss = task->get_miss; c->mlget_task.mlget_num++; /* enough multi-get task items can be done */ @@ -667,11 +572,8 @@ static void ms_multi_getset_task_sch(ms_conn_t *c) break; } } - } - else - { - if ((c->remain_exec_num <= 0) && (c->mlget_task.mlget_num > 0)) - { + } else { + if ((c->remain_exec_num <= 0) && (c->mlget_task.mlget_num > 0)) { ms_mcd_mlget(c); } break; @@ -679,7 +581,6 @@ static void ms_multi_getset_task_sch(ms_conn_t *c) } } /* ms_multi_getset_task_sch */ - /** * calculate the difference value of two time points * @@ -688,17 +589,15 @@ static void ms_multi_getset_task_sch(ms_conn_t *c) * * @return uint64_t, the difference value between start_time and end_time in us */ -int64_t ms_time_diff(struct timeval *start_time, struct timeval *end_time) -{ - int64_t endtime= end_time->tv_sec * 1000000 + end_time->tv_usec; - int64_t starttime= start_time->tv_sec * 1000000 + start_time->tv_usec; +int64_t ms_time_diff(struct timeval *start_time, struct timeval *end_time) { + int64_t endtime = end_time->tv_sec * 1000000 + end_time->tv_usec; + int64_t starttime = start_time->tv_sec * 1000000 + start_time->tv_usec; assert(endtime >= starttime); return endtime - starttime; } /* ms_time_diff */ - /** * after get the response from server for multi-get, the * function update the state of the task and do data verify if @@ -706,55 +605,44 @@ int64_t ms_time_diff(struct timeval *start_time, struct timeval *end_time) * * @param c, pointer of the concurrency */ -static void ms_update_multi_get_result(ms_conn_t *c) -{ +static void ms_update_multi_get_result(ms_conn_t *c) { ms_mlget_task_item_t *mlget_item; ms_task_item_t *item; - char *orignval= NULL; - char *orignkey= NULL; + char *orignval = NULL; + char *orignkey = NULL; - if (c == NULL) - { + if (c == NULL) { return; } assert(c != NULL); - for (int i= 0; i < c->mlget_task.mlget_num; i++) - { - mlget_item= &c->mlget_task.mlget_item[i]; - item= mlget_item->item; - orignval= &ms_setting.char_block[item->value_offset]; - orignkey= &ms_setting.char_block[item->key_suffix_offset]; + for (int i = 0; i < c->mlget_task.mlget_num; i++) { + mlget_item = &c->mlget_task.mlget_item[i]; + item = mlget_item->item; + orignval = &ms_setting.char_block[item->value_offset]; + orignkey = &ms_setting.char_block[item->key_suffix_offset]; /* update get miss counter */ - if (mlget_item->get_miss) - { + if (mlget_item->get_miss) { atomic_add_size(&ms_stats.get_misses, 1); } /* get nothing from server for this task item */ - if (mlget_item->verify && ! mlget_item->finish_verify) - { + if (mlget_item->verify && !mlget_item->finish_verify) { /* verify expire time if necessary */ - if (item->exp_time > 0) - { + if (item->exp_time > 0) { struct timeval curr_time; gettimeofday(&curr_time, NULL); /* object doesn't expire but can't get it now */ - if (curr_time.tv_sec - item->client_time - < item->exp_time - EXPIRE_TIME_ERROR) - { + if (curr_time.tv_sec - item->client_time < item->exp_time - EXPIRE_TIME_ERROR) { atomic_add_size(&ms_stats.unexp_unget, 1); - if (ms_setting.verbose) - { + if (ms_setting.verbose) { char set_time[64]; char cur_time[64]; - strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&item->client_time)); - strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&curr_time.tv_sec)); + strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&item->client_time)); + strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&curr_time.tv_sec)); fprintf(stderr, "\n\t<%d expire time verification failed, object " "doesn't expire but can't get it now\n" @@ -765,47 +653,35 @@ static void ms_update_multi_get_result(ms_conn_t *c) "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data: \n", - c->sfd, - item->key_size, - item->key_prefix, - item->key_size - (int)KEY_PREFIX_SIZE, - orignkey, - set_time, - cur_time, - (int)(curr_time.tv_sec - item->client_time), - item->exp_time, - item->value_size, - item->value_size, - orignval); + c->sfd, item->key_size, item->key_prefix, + item->key_size - (int) KEY_PREFIX_SIZE, orignkey, set_time, cur_time, + (int) (curr_time.tv_sec - item->client_time), item->exp_time, item->value_size, + item->value_size, orignval); fflush(stderr); } } - } - else - { + } else { atomic_add_size(&ms_stats.vef_miss, 1); - if (ms_setting.verbose) - { - fprintf(stderr, "\n<%d data verification failed\n" - "\tkey len: %d\n" - "\tkey: %" PRIx64 " %.*s\n" - "\texpected data len: %d\n" - "\texpected data: %.*s\n" - "\treceived data: \n", - c->sfd, item->key_size, item->key_prefix, - item->key_size - (int)KEY_PREFIX_SIZE, + if (ms_setting.verbose) { + fprintf(stderr, + "\n<%d data verification failed\n" + "\tkey len: %d\n" + "\tkey: %" PRIx64 " %.*s\n" + "\texpected data len: %d\n" + "\texpected data: %.*s\n" + "\treceived data: \n", + c->sfd, item->key_size, item->key_prefix, item->key_size - (int) KEY_PREFIX_SIZE, orignkey, item->value_size, item->value_size, orignval); fflush(stderr); } } } } - c->mlget_task.mlget_num= 0; - c->mlget_task.value_index= INVALID_OFFSET; + c->mlget_task.mlget_num = 0; + c->mlget_task.value_index = INVALID_OFFSET; } /* ms_update_multi_get_result */ - /** * after get the response from server for single get, the * function update the state of the task and do data verify if @@ -815,51 +691,40 @@ static void ms_update_multi_get_result(ms_conn_t *c) * @param item, pointer of task item which includes the object * information */ -static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) -{ - char *orignval= NULL; - char *orignkey= NULL; +static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) { + char *orignval = NULL; + char *orignkey = NULL; - if ((c == NULL) || (item == NULL)) - { + if ((c == NULL) || (item == NULL)) { return; } assert(c != NULL); assert(item != NULL); - orignval= &ms_setting.char_block[item->value_offset]; - orignkey= &ms_setting.char_block[item->key_suffix_offset]; + orignval = &ms_setting.char_block[item->value_offset]; + orignkey = &ms_setting.char_block[item->key_suffix_offset]; /* update get miss counter */ - if ((c->precmd.cmd == CMD_GET) && c->curr_task.get_miss) - { + if ((c->precmd.cmd == CMD_GET) && c->curr_task.get_miss) { atomic_add_size(&ms_stats.get_misses, 1); } /* get nothing from server for this task item */ - if ((c->precmd.cmd == CMD_GET) && c->curr_task.verify - && ! c->curr_task.finish_verify) - { + if ((c->precmd.cmd == CMD_GET) && c->curr_task.verify && !c->curr_task.finish_verify) { /* verify expire time if necessary */ - if (item->exp_time > 0) - { + if (item->exp_time > 0) { struct timeval curr_time; gettimeofday(&curr_time, NULL); /* object doesn't expire but can't get it now */ - if (curr_time.tv_sec - item->client_time - < item->exp_time - EXPIRE_TIME_ERROR) - { + if (curr_time.tv_sec - item->client_time < item->exp_time - EXPIRE_TIME_ERROR) { atomic_add_size(&ms_stats.unexp_unget, 1); - if (ms_setting.verbose) - { + if (ms_setting.verbose) { char set_time[64]; char cur_time[64]; - strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&item->client_time)); - strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", - localtime(&curr_time.tv_sec)); + strftime(set_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&item->client_time)); + strftime(cur_time, 64, "%Y-%m-%d %H:%M:%S", localtime(&curr_time.tv_sec)); fprintf(stderr, "\n\t<%d expire time verification failed, object " "doesn't expire but can't get it now\n" @@ -870,36 +735,24 @@ static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data: \n", - c->sfd, - item->key_size, - item->key_prefix, - item->key_size - (int)KEY_PREFIX_SIZE, - orignkey, - set_time, - cur_time, - (int)(curr_time.tv_sec - item->client_time), - item->exp_time, - item->value_size, - item->value_size, - orignval); + c->sfd, item->key_size, item->key_prefix, item->key_size - (int) KEY_PREFIX_SIZE, + orignkey, set_time, cur_time, (int) (curr_time.tv_sec - item->client_time), + item->exp_time, item->value_size, item->value_size, orignval); fflush(stderr); } } - } - else - { + } else { atomic_add_size(&ms_stats.vef_miss, 1); - if (ms_setting.verbose) - { - fprintf(stderr, "\n<%d data verification failed\n" - "\tkey len: %d\n" - "\tkey: %" PRIx64 " %.*s\n" - "\texpected data len: %d\n" - "\texpected data: %.*s\n" - "\treceived data: \n", - c->sfd, item->key_size, item->key_prefix, - item->key_size - (int)KEY_PREFIX_SIZE, + if (ms_setting.verbose) { + fprintf(stderr, + "\n<%d data verification failed\n" + "\tkey len: %d\n" + "\tkey: %" PRIx64 " %.*s\n" + "\texpected data len: %d\n" + "\texpected data: %.*s\n" + "\treceived data: \n", + c->sfd, item->key_size, item->key_prefix, item->key_size - (int) KEY_PREFIX_SIZE, orignkey, item->value_size, item->value_size, orignval); fflush(stderr); } @@ -907,7 +760,6 @@ static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) } } /* ms_update_single_get_result */ - /** * after get the response from server for set the function * update the state of the task and do data verify if necessary. @@ -916,29 +768,22 @@ static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information */ -static void ms_update_set_result(ms_conn_t *c, ms_task_item_t *item) -{ - if ((c == NULL) || (item == NULL)) - { +static void ms_update_set_result(ms_conn_t *c, ms_task_item_t *item) { + if ((c == NULL) || (item == NULL)) { return; } assert(c != NULL); assert(item != NULL); - if (c->precmd.cmd == CMD_SET) - { - switch (c->precmd.retstat) - { + if (c->precmd.cmd == CMD_SET) { + switch (c->precmd.retstat) { case MCD_STORED: - if (item->value_offset == INVALID_OFFSET) - { + if (item->value_offset == INVALID_OFFSET) { /* first set with the same offset of key suffix */ - item->value_offset= item->key_suffix_offset; - } - else - { + item->value_offset = item->key_suffix_offset; + } else { /* not first set, just increase the value offset */ - item->value_offset+= 1; + item->value_offset += 1; } /* set successes, update counter */ @@ -948,56 +793,46 @@ static void ms_update_set_result(ms_conn_t *c, ms_task_item_t *item) break; case MCD_SERVER_ERROR: - default: - break; + default: break; } /* switch */ } } /* ms_update_set_result */ - /** * update the response time result * * @param c, pointer of the concurrency */ -static void ms_update_stat_result(ms_conn_t *c) -{ - bool get_miss= false; +static void ms_update_stat_result(ms_conn_t *c) { + bool get_miss = false; - if (c == NULL) - { + if (c == NULL) { return; } assert(c != NULL); gettimeofday(&c->end_time, NULL); - uint64_t time_diff= (uint64_t)ms_time_diff(&c->start_time, &c->end_time); + uint64_t time_diff = (uint64_t) ms_time_diff(&c->start_time, &c->end_time); pthread_mutex_lock(&ms_statistic.stat_mutex); - switch (c->precmd.cmd) - { - case CMD_SET: - ms_record_event(&ms_statistic.set_stat, time_diff, false); - break; + switch (c->precmd.cmd) { + case CMD_SET: ms_record_event(&ms_statistic.set_stat, time_diff, false); break; case CMD_GET: - if (c->curr_task.get_miss) - { - get_miss= true; + if (c->curr_task.get_miss) { + get_miss = true; } ms_record_event(&ms_statistic.get_stat, time_diff, get_miss); break; - default: - break; + default: break; } /* switch */ ms_record_event(&ms_statistic.total_stat, time_diff, get_miss); pthread_mutex_unlock(&ms_statistic.stat_mutex); } /* ms_update_stat_result */ - /** * after get response from server for the current operation, and * before doing the next operation, update the state of the @@ -1005,45 +840,36 @@ static void ms_update_stat_result(ms_conn_t *c) * * @param c, pointer of the concurrency */ -static void ms_update_task_result(ms_conn_t *c) -{ +static void ms_update_task_result(ms_conn_t *c) { ms_task_item_t *item; - if (c == NULL) - { + if (c == NULL) { return; } assert(c != NULL); - item= ms_get_cur_opt_item(c); - if (item == NULL) - { + item = ms_get_cur_opt_item(c); + if (item == NULL) { return; } assert(item != NULL); ms_update_set_result(c, item); - if ((ms_setting.stat_freq > 0) - && ((c->precmd.cmd == CMD_SET) || (c->precmd.cmd == CMD_GET))) - { + if ((ms_setting.stat_freq > 0) && ((c->precmd.cmd == CMD_SET) || (c->precmd.cmd == CMD_GET))) { ms_update_stat_result(c); } /* update multi-get task item */ - if (((ms_setting.mult_key_num > 1) - && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) + if (((ms_setting.mult_key_num > 1) && (c->mlget_task.mlget_num >= ms_setting.mult_key_num)) || ((c->remain_exec_num == 0) && (c->mlget_task.mlget_num > 0))) { ms_update_multi_get_result(c); - } - else - { + } else { ms_update_single_get_result(c, item); } } /* ms_update_task_result */ - /** * run get and set operation * @@ -1051,40 +877,32 @@ static void ms_update_task_result(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -static int ms_run_getset_task(ms_conn_t *c) -{ +static int ms_run_getset_task(ms_conn_t *c) { /** * extra one loop to get the last command return state. get the * last command return state. */ - if ((c->remain_exec_num >= 0) - && (c->remain_exec_num != c->exec_num)) - { + if ((c->remain_exec_num >= 0) && (c->remain_exec_num != c->exec_num)) { ms_update_task_result(c); } /* multi-get */ - if (ms_setting.mult_key_num > 1) - { + if (ms_setting.mult_key_num > 1) { /* operate next task item */ ms_multi_getset_task_sch(c); - } - else - { + } else { /* operate next task item */ ms_single_getset_task_sch(c); } /* no task to do, exit */ - if ((c->remain_exec_num == -1) || ms_global.time_out) - { + if ((c->remain_exec_num == -1) || ms_global.time_out) { return -1; } return EXIT_SUCCESS; } /* ms_run_getset_task */ - /** * the state machine call the function to execute task. * @@ -1092,16 +910,11 @@ static int ms_run_getset_task(ms_conn_t *c) * * @return int, if success, return EXIT_SUCCESS, else return -1 */ -int ms_exec_task(struct conn *c) -{ - if (! ms_global.finish_warmup) - { +int ms_exec_task(struct conn *c) { + if (!ms_global.finish_warmup) { ms_warmup_server(c); - } - else - { - if (ms_run_getset_task(c) != 0) - { + } else { + if (ms_run_getset_task(c) != 0) { return -1; } } diff --git a/src/bin/memaslap/ms_task.h b/src/bin/memaslap/ms_task.h index c4917d11..add33f4b 100644 --- a/src/bin/memaslap/ms_task.h +++ b/src/bin/memaslap/ms_task.h @@ -1,20 +1,25 @@ /* - * File: ms_task.h - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #ifndef MS_TASK_H #define MS_TASK_H #include #include #if !defined(__cplusplus) -# include +# include #endif #include @@ -22,59 +27,55 @@ extern "C" { #endif -#define UNIT_ITEMS_COUNT 1024 /* each window unit has 1024 items */ -#define KEY_PREFIX_SIZE (sizeof(uint64_t)) /* key prefix length: 8 bytes */ -#define INVALID_OFFSET (-1) /* invalid offset in the character table */ -#define FIXED_EXPIRE_TIME 60 /* default expire time is 60s */ -#define EXPIRE_TIME_ERROR 5 /* default expire time error is 5s */ +#define UNIT_ITEMS_COUNT 1024 /* each window unit has 1024 items */ +#define KEY_PREFIX_SIZE (sizeof(uint64_t)) /* key prefix length: 8 bytes */ +#define INVALID_OFFSET (-1) /* invalid offset in the character table */ +#define FIXED_EXPIRE_TIME 60 /* default expire time is 60s */ +#define EXPIRE_TIME_ERROR 5 /* default expire time error is 5s */ /* information of a task item(object) */ -typedef struct task_item -{ - uint64_t key_prefix; /* prefix of the key, 8 bytes, binary */ - int key_size; /* key size */ - int key_suffix_offset; /* suffix offset in the global character table */ +typedef struct task_item { + uint64_t key_prefix; /* prefix of the key, 8 bytes, binary */ + int key_size; /* key size */ + int key_suffix_offset; /* suffix offset in the global character table */ - int value_size; /* data size */ - int value_offset; /* data offset in the global character table */ + int value_size; /* data size */ + int value_offset; /* data offset in the global character table */ - time_t client_time; /* the current client time */ - int exp_time; /* expire time */ + time_t client_time; /* the current client time */ + int exp_time; /* expire time */ } ms_task_item_t; /* task item for multi-get */ -typedef struct mlget_task_item -{ - ms_task_item_t *item; /* task item */ - bool verify; /* whether verify data or not */ - bool finish_verify; /* whether finish data verify or not */ - bool get_miss; /* whether get miss or not */ +typedef struct mlget_task_item { + ms_task_item_t *item; /* task item */ + bool verify; /* whether verify data or not */ + bool finish_verify; /* whether finish data verify or not */ + bool get_miss; /* whether get miss or not */ } ms_mlget_task_item_t; /* information of multi-get task */ -typedef struct mlget_task -{ - ms_mlget_task_item_t *mlget_item; /* multi-get task array */ - int mlget_num; /* how many tasks in mlget_task array */ - int value_index; /* the nth value received by the connect, for multi-get */ +typedef struct mlget_task { + ms_mlget_task_item_t *mlget_item; /* multi-get task array */ + int mlget_num; /* how many tasks in mlget_task array */ + int value_index; /* the nth value received by the connect, for multi-get */ } ms_mlget_task_t; /* structure used to store the state of the running task */ -typedef struct task -{ - int cmd; /* command name */ - bool verify; /* whether verify data or not */ - bool finish_verify; /* whether finish data verify or not */ - bool get_miss; /* whether get miss or not */ - ms_task_item_t *item; /* task item */ +typedef struct task { + int cmd; /* command name */ + bool verify; /* whether verify data or not */ + bool finish_verify; /* whether finish data verify or not */ + bool get_miss; /* whether get miss or not */ + ms_task_item_t *item; /* task item */ /* counter for command distribution adjustment */ - uint64_t get_opt; /* number of total get operations */ - uint64_t set_opt; /* number of total set operations, no including warmup set count */ - int cycle_undo_get; /* number of undo get in an adjustment cycle */ - int cycle_undo_set; /* number of undo set in an adjustment cycle */ - uint64_t verified_get; /* number of total verified get operations */ - uint64_t overwrite_set; /* number of total overwrite set operations */ + uint64_t get_opt; /* number of total get operations */ + uint64_t set_opt; /* number of total set operations, no including warmup set count */ + int cycle_undo_get; /* number of undo get in an adjustment cycle */ + int cycle_undo_set; /* number of undo set in an adjustment cycle */ + uint64_t verified_get; /* number of total verified get operations */ + uint64_t overwrite_set; /* number of total overwrite set operations */ } ms_task_t; struct conn; @@ -82,11 +83,9 @@ struct conn; /* the state machine call the function to execute task.*/ int ms_exec_task(struct conn *c); - /* calculate the difference value of two time points */ int64_t ms_time_diff(struct timeval *start_time, struct timeval *end_time); - #ifdef __cplusplus } #endif diff --git a/src/bin/memaslap/ms_thread.c b/src/bin/memaslap/ms_thread.c index f9f52bfb..b06aa526 100644 --- a/src/bin/memaslap/ms_thread.c +++ b/src/bin/memaslap/ms_thread.c @@ -1,22 +1,26 @@ /* - * File: ms_thread.c - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" #if defined(HAVE_SYS_TIME_H) -# include +# include #endif #if defined(HAVE_TIME_H) -# include +# include #endif #include "ms_thread.h" @@ -38,45 +42,37 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx); static void *ms_worker_libevent(void *arg); static void ms_create_worker(void *(*func)(void *), void *arg); - /** * time-sensitive callers can call it by hand with this, * outside the normal ever-1-second timer */ -static void ms_set_current_time() -{ +static void ms_set_current_time() { struct timeval timer; - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); gettimeofday(&timer, NULL); - ms_thread->curr_time= (rel_time_t)timer.tv_sec; + ms_thread->curr_time = (rel_time_t) timer.tv_sec; } /* ms_set_current_time */ - /** * used to check whether UDP of command are waiting timeout * by the ever-1-second timer */ -static void ms_check_sock_timeout(void) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - ms_conn_t *c= NULL; - int time_diff= 0; +static void ms_check_sock_timeout(void) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + ms_conn_t *c = NULL; + int time_diff = 0; - for (uint32_t i= 0; i < ms_thread->thread_ctx->nconns; i++) - { - c= &ms_thread->conn[i]; + for (uint32_t i = 0; i < ms_thread->thread_ctx->nconns; i++) { + c = &ms_thread->conn[i]; - if (c->udp) - { - time_diff= (int)(ms_thread->curr_time - (rel_time_t)c->start_time.tv_sec); + if (c->udp) { + time_diff = (int) (ms_thread->curr_time - (rel_time_t) c->start_time.tv_sec); /* wait time out */ - if (time_diff > SOCK_WAIT_TIMEOUT) - { + if (time_diff > SOCK_WAIT_TIMEOUT) { /* calculate dropped packets count */ - if (c->recvpkt > 0) - { + if (c->recvpkt > 0) { atomic_add_size(&ms_stats.pkt_drop, c->packets - c->recvpkt); } @@ -87,18 +83,14 @@ static void ms_check_sock_timeout(void) } } /* ms_check_sock_timeout */ - /* if disconnect, the ever-1-second timer will call this function to reconnect */ -static void ms_reconn_thread_socks(void) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - for (uint32_t i= 0; i < ms_thread->thread_ctx->nconns; i++) - { +static void ms_reconn_thread_socks(void) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + for (uint32_t i = 0; i < ms_thread->thread_ctx->nconns; i++) { ms_reconn_socks(&ms_thread->conn[i]); } } /* ms_reconn_thread_socks */ - /** * the handler of the ever-1-second timer * @@ -106,13 +98,9 @@ static void ms_reconn_thread_socks(void) * @param which, event flags * @param arg, argument */ -static void ms_clock_handler(const int fd, const short which, void *arg) -{ - ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); - struct timeval t= - { - .tv_sec= 1, .tv_usec= 0 - }; +static void ms_clock_handler(const int fd, const short which, void *arg) { + ms_thread_t *ms_thread = pthread_getspecific(ms_thread_key); + struct timeval t = {.tv_sec = 1, .tv_usec = 0}; UNUSED_ARGUMENT(fd); UNUSED_ARGUMENT(which); @@ -120,15 +108,12 @@ static void ms_clock_handler(const int fd, const short which, void *arg) ms_set_current_time(); - if (ms_thread->initialized) - { + if (ms_thread->initialized) { /* only delete the event if it's actually there. */ evtimer_del(&ms_thread->clock_event); ms_check_sock_timeout(); - } - else - { - ms_thread->initialized= true; + } else { + ms_thread->initialized = true; } ms_reconn_thread_socks(); @@ -138,7 +123,6 @@ static void ms_clock_handler(const int fd, const short which, void *arg) evtimer_add(&ms_thread->clock_event, &t); } /* ms_clock_handler */ - /** * used to bind thread to CPU if the system supports * @@ -146,19 +130,17 @@ static void ms_clock_handler(const int fd, const short which, void *arg) * * @return if success, return EXIT_SUCCESS, else return -1 */ -static uint32_t ms_set_thread_cpu_affinity(uint32_t cpu) -{ - uint32_t ret= 0; +static uint32_t ms_set_thread_cpu_affinity(uint32_t cpu) { + uint32_t ret = 0; #ifdef HAVE_CPU_SET_T cpu_set_t cpu_set; CPU_ZERO(&cpu_set); CPU_SET(cpu, &cpu_set); - if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) == -1) - { + if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) == -1) { fprintf(stderr, "WARNING: Could not set CPU Affinity, continuing...\n"); - ret= 1; + ret = 1; } #else UNUSED_ARGUMENT(cpu); @@ -167,7 +149,6 @@ static uint32_t ms_set_thread_cpu_affinity(uint32_t cpu) return ret; } /* ms_set_thread_cpu_affinity */ - /** * Set up a thread's information. * @@ -175,53 +156,41 @@ static uint32_t ms_set_thread_cpu_affinity(uint32_t cpu) * * @return if success, return EXIT_SUCCESS, else return -1 */ -static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) -{ - - ms_thread_t *ms_thread= (ms_thread_t *)calloc(sizeof(*ms_thread), 1); - pthread_setspecific(ms_thread_key, (void *)ms_thread); +static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) { + ms_thread_t *ms_thread = (ms_thread_t *) calloc(sizeof(*ms_thread), 1); + pthread_setspecific(ms_thread_key, (void *) ms_thread); - ms_thread->thread_ctx= thread_ctx; - ms_thread->nactive_conn= thread_ctx->nconns; - ms_thread->initialized= false; - static ATOMIC uint32_t cnt= 0; + ms_thread->thread_ctx = thread_ctx; + ms_thread->nactive_conn = thread_ctx->nconns; + ms_thread->initialized = false; + static ATOMIC uint32_t cnt = 0; gettimeofday(&ms_thread->startup_time, NULL); - ms_thread->base= event_init(); - if (ms_thread->base == NULL) - { - if (atomic_add_32_nv(&cnt, 1) == 0) - { + ms_thread->base = event_init(); + if (ms_thread->base == NULL) { + if (atomic_add_32_nv(&cnt, 1) == 0) { fprintf(stderr, "Can't allocate event base.\n"); } return -1; } - ms_thread->conn= - (ms_conn_t *)malloc((size_t)thread_ctx->nconns * sizeof(ms_conn_t)); - if (ms_thread->conn == NULL) - { - if (atomic_add_32_nv(&cnt, 1) == 0) - { - fprintf( - stderr, - "Can't allocate concurrency structure for thread descriptors."); + ms_thread->conn = (ms_conn_t *) malloc((size_t) thread_ctx->nconns * sizeof(ms_conn_t)); + if (ms_thread->conn == NULL) { + if (atomic_add_32_nv(&cnt, 1) == 0) { + fprintf(stderr, "Can't allocate concurrency structure for thread descriptors."); } return -1; } - memset(ms_thread->conn, 0, (size_t)thread_ctx->nconns * sizeof(ms_conn_t)); + memset(ms_thread->conn, 0, (size_t) thread_ctx->nconns * sizeof(ms_conn_t)); - for (uint32_t i= 0; i < thread_ctx->nconns; i++) - { - ms_thread->conn[i].conn_idx= i; - if (ms_setup_conn(&ms_thread->conn[i]) != 0) - { + for (uint32_t i = 0; i < thread_ctx->nconns; i++) { + ms_thread->conn[i].conn_idx = i; + if (ms_setup_conn(&ms_thread->conn[i]) != 0) { /* only output this error once */ - if (atomic_add_32_nv(&cnt, 1) == 0) - { + if (atomic_add_32_nv(&cnt, 1) == 0) { fprintf(stderr, "Initializing connection failed.\n"); } @@ -232,7 +201,6 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) return EXIT_SUCCESS; } /* ms_setup_thread */ - /** * Worker thread: main event loop * @@ -240,22 +208,19 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) * * @return void* */ -static void *ms_worker_libevent(void *arg) -{ - ms_thread_t *ms_thread= NULL; - ms_thread_ctx_t *thread_ctx= (ms_thread_ctx_t *)arg; +static void *ms_worker_libevent(void *arg) { + ms_thread_t *ms_thread = NULL; + ms_thread_ctx_t *thread_ctx = (ms_thread_ctx_t *) arg; /** * If system has more than one cpu and supports set cpu * affinity, try to bind each thread to a cpu core; */ - if (ms_setting.ncpu > 1) - { + if (ms_setting.ncpu > 1) { ms_set_thread_cpu_affinity(thread_ctx->thd_idx % ms_setting.ncpu); } - if (ms_setup_thread(thread_ctx) != 0) - { + if (ms_setup_thread(thread_ctx) != 0) { exit(1); } @@ -267,82 +232,67 @@ static void *ms_worker_libevent(void *arg) pthread_cond_signal(&ms_global.init_lock.cond); pthread_mutex_unlock(&ms_global.init_lock.lock); - ms_thread= pthread_getspecific(ms_thread_key); + ms_thread = pthread_getspecific(ms_thread_key); event_base_loop(ms_thread->base, 0); return NULL; } /* ms_worker_libevent */ - /** * Creates a worker thread. * * @param func, the callback function * @param arg, the argument to pass to the callback function */ -static void ms_create_worker(void *(*func)(void *), void *arg) -{ +static void ms_create_worker(void *(*func)(void *), void *arg) { pthread_t thread; pthread_attr_t attr; int ret; pthread_attr_init(&attr); - if ((ret= pthread_create(&thread, &attr, func, arg)) != 0) - { + if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) { fprintf(stderr, "Can't create thread: %s.\n", strerror(ret)); exit(1); } } /* ms_create_worker */ - /* initialize threads */ -void ms_thread_init() -{ - ms_thread_ctx= - (ms_thread_ctx_t *)malloc( - sizeof(ms_thread_ctx_t) * (size_t)ms_setting.nthreads); - if (ms_thread_ctx == NULL) - { +void ms_thread_init() { + ms_thread_ctx = + (ms_thread_ctx_t *) malloc(sizeof(ms_thread_ctx_t) * (size_t) ms_setting.nthreads); + if (ms_thread_ctx == NULL) { fprintf(stderr, "Can't allocate thread descriptors."); exit(1); } - for (uint32_t i= 0; i < ms_setting.nthreads; i++) - { - ms_thread_ctx[i].thd_idx= i; - ms_thread_ctx[i].nconns= ms_setting.nconns / ms_setting.nthreads; + for (uint32_t i = 0; i < ms_setting.nthreads; i++) { + ms_thread_ctx[i].thd_idx = i; + ms_thread_ctx[i].nconns = ms_setting.nconns / ms_setting.nthreads; /** * If only one server, all the connections in all threads * connects the same server. For support multi-servers, simple * distribute thread to server. */ - ms_thread_ctx[i].srv_idx= i % ms_setting.srv_cnt; - ms_thread_ctx[i].tps_perconn= ms_setting.expected_tps - / (int)ms_setting.nconns; - ms_thread_ctx[i].exec_num_perconn= ms_setting.exec_num - / ms_setting.nconns; + ms_thread_ctx[i].srv_idx = i % ms_setting.srv_cnt; + ms_thread_ctx[i].tps_perconn = ms_setting.expected_tps / (int) ms_setting.nconns; + ms_thread_ctx[i].exec_num_perconn = ms_setting.exec_num / ms_setting.nconns; } - if (pthread_key_create(&ms_thread_key, NULL)) - { + if (pthread_key_create(&ms_thread_key, NULL)) { fprintf(stderr, "Can't create pthread keys. Major malfunction!\n"); exit(1); } /* Create threads after we've done all the epoll setup. */ - for (uint32_t i= 0; i < ms_setting.nthreads; i++) - { - ms_create_worker(ms_worker_libevent, (void *)&ms_thread_ctx[i]); + for (uint32_t i = 0; i < ms_setting.nthreads; i++) { + ms_create_worker(ms_worker_libevent, (void *) &ms_thread_ctx[i]); } } /* ms_thread_init */ - /* cleanup some resource of threads when all the threads exit */ -void ms_thread_cleanup() -{ - if (ms_thread_ctx != NULL) - { +void ms_thread_cleanup() { + if (ms_thread_ctx != NULL) { free(ms_thread_ctx); } pthread_key_delete(ms_thread_key); diff --git a/src/bin/memaslap/ms_thread.h b/src/bin/memaslap/ms_thread.h index 8cb98bde..6ae1bb7b 100644 --- a/src/bin/memaslap/ms_thread.h +++ b/src/bin/memaslap/ms_thread.h @@ -1,29 +1,18 @@ /* - * File: ms_thread.h - * Author: Mingqiang Zhuang - * - * Created on February 10, 2009 - * - * (c) Copyright 2009, Schooner Information Technology, Inc. - * http://www.schoonerinfotech.com/ - * - */ + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -/** - * Asynchronous memslap has the similar implementation of - * multi-threads with memcached. Asynchronous memslap creates - * one or more self-governed threads; each thread is bound with - * one CPU core if the system supports setting CPU core - * affinity. And every thread has private variables. There is - * less communication or some shared resources among all the - * threads. It can improve the performance because there are - * fewer locks and competition. In addition, each thread has a - * libevent to manage the events of network. Each thread has one - * or more self-governed concurrencies; each concurrency has one - * or more socket connections. All the concurrencies don't - * communicate with each other even though they are in the same - * thread. - */ #ifndef MS_THREAD_H #define MS_THREAD_H @@ -35,42 +24,38 @@ extern "C" { #endif /** Time relative to server start. Smaller than time_t on 64-bit systems. */ -typedef unsigned int rel_time_t; +typedef unsigned int rel_time_t; /* Used to store the context of each thread */ -typedef struct thread_ctx -{ - uint32_t thd_idx; /* the thread index */ - uint32_t nconns; /* how many connections included by the thread */ - uint32_t srv_idx; /* index of the thread */ - int tps_perconn; /* expected throughput per connection */ - int64_t exec_num_perconn; /* execute number per connection */ +typedef struct thread_ctx { + uint32_t thd_idx; /* the thread index */ + uint32_t nconns; /* how many connections included by the thread */ + uint32_t srv_idx; /* index of the thread */ + int tps_perconn; /* expected throughput per connection */ + int64_t exec_num_perconn; /* execute number per connection */ } ms_thread_ctx_t; /* Used to store the private variables of each thread */ -typedef struct thread -{ - ms_conn_t *conn; /* conn array to store all the conn in the thread */ - uint32_t nactive_conn; /* how many connects are active */ +typedef struct thread { + ms_conn_t *conn; /* conn array to store all the conn in the thread */ + uint32_t nactive_conn; /* how many connects are active */ - ms_thread_ctx_t *thread_ctx; /* thread context from the caller */ - struct event_base *base; /* libevent handler created by this thread */ + ms_thread_ctx_t *thread_ctx; /* thread context from the caller */ + struct event_base *base; /* libevent handler created by this thread */ - rel_time_t curr_time; /* current time */ - struct event clock_event; /* clock event to time each one second */ - bool initialized; /* whether clock_event has been initialized */ + rel_time_t curr_time; /* current time */ + struct event clock_event; /* clock event to time each one second */ + bool initialized; /* whether clock_event has been initialized */ - struct timeval startup_time; /* start time of the thread */ + struct timeval startup_time; /* start time of the thread */ } ms_thread_t; /* initialize threads */ void ms_thread_init(void); - /* cleanup some resource of threads when all the threads exit */ void ms_thread_cleanup(void); - #ifdef __cplusplus } #endif diff --git a/src/bin/memcapable.cc b/src/bin/memcapable.cc index e9508c49..feda49df 100644 --- a/src/bin/memcapable.cc +++ b/src/bin/memcapable.cc @@ -1,24 +1,26 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ -/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */ #undef NDEBUG #include "mem_config.h" #ifdef HAVE_POLL_H -#include +# include #else -#include "poll/poll.h" +# include "poll/poll.h" #endif #include @@ -49,19 +51,19 @@ * from gcc. The conversion methods isn't the bottleneck for my app, so * just remove the warnings by undef'ing the optimization .. */ -#undef ntohs -#undef ntohl +# undef ntohs +# undef ntohl #endif /* Should we generate coredumps when we enounter an error (-c) */ -static bool do_core= false; +static bool do_core = false; /* connection to the server */ static memcached_socket_t sock; /* Should the output from test failures be verbose or quiet? */ -static bool verbose= false; +static bool verbose = false; /* The number of seconds to wait for an IO-operation */ -static int timeout= 2; +static int timeout = 2; /* v1.6.x is more permissible */ static bool v16x_or_greater = false; @@ -75,8 +77,7 @@ static bool v16x_or_greater = false; * To avoid to have to do multiple writes, lets add a chunk of memory * to use. 1k should be more than enough for header, key and body. */ -typedef union -{ +typedef union { protocol_binary_request_no_extras plain; protocol_binary_request_flush flush; protocol_binary_request_incr incr; @@ -84,40 +85,31 @@ typedef union char bytes[1024]; } command; -typedef union -{ +typedef union { protocol_binary_response_no_extras plain; protocol_binary_response_incr incr; protocol_binary_response_decr decr; char bytes[1024]; } response; -enum test_return -{ - TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL -}; +enum test_return { TEST_SKIP, TEST_PASS, TEST_PASS_RECONNECT, TEST_FAIL }; /** * Try to get an addrinfo struct for a given port on a given host */ -static struct addrinfo *lookuphost(const char *hostname, const char *port) -{ - struct addrinfo *ai= 0; +static struct addrinfo *lookuphost(const char *hostname, const char *port) { + struct addrinfo *ai = 0; struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family=AF_UNSPEC; - hints.ai_protocol=IPPROTO_TCP; - hints.ai_socktype=SOCK_STREAM; - - int error= getaddrinfo(hostname, port, &hints, &ai); - if (error != 0) - { - if (error != EAI_SYSTEM) - { + hints.ai_family = AF_UNSPEC; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_socktype = SOCK_STREAM; + + int error = getaddrinfo(hostname, port, &hints, &ai); + if (error != 0) { + if (error != EAI_SYSTEM) { fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(error)); - } - else - { + } else { perror("getaddrinfo()"); } } @@ -129,29 +121,24 @@ static struct addrinfo *lookuphost(const char *hostname, const char *port) * Set the socket in nonblocking mode * @return -1 if failure, the socket otherwise */ -static memcached_socket_t set_noblock(void) -{ +static memcached_socket_t set_noblock(void) { #if defined(_WIN32) u_long arg = 1; - if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR) - { + if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR) { perror("Failed to set nonblocking io"); closesocket(sock); return INVALID_SOCKET; } #else - int flags= fcntl(sock, F_GETFL, 0); - if (flags == -1) - { + int flags = fcntl(sock, F_GETFL, 0); + if (flags == -1) { perror("Failed to get socket flags"); memcached_close_socket(sock); return INVALID_SOCKET; } - if ((flags & O_NONBLOCK) != O_NONBLOCK) - { - if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) - { + if ((flags & O_NONBLOCK) != O_NONBLOCK) { + if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { perror("Failed to set socket to nonblocking mode"); memcached_close_socket(sock); return INVALID_SOCKET; @@ -167,29 +154,19 @@ static memcached_socket_t set_noblock(void) * @param port the port number (or service) to connect to * @return positive integer if success, -1 otherwise */ -static memcached_socket_t connect_server(const char *hostname, const char *port) -{ - struct addrinfo *ai= lookuphost(hostname, port); - sock= INVALID_SOCKET; - if (ai != NULL) - { - if ((sock= socket(ai->ai_family, ai->ai_socktype, - ai->ai_protocol)) != INVALID_SOCKET) - { - if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) - { - fprintf(stderr, "Failed to connect socket: %s\n", - strerror(get_socket_errno())); +static memcached_socket_t connect_server(const char *hostname, const char *port) { + struct addrinfo *ai = lookuphost(hostname, port); + sock = INVALID_SOCKET; + if (ai != NULL) { + if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) != INVALID_SOCKET) { + if (connect(sock, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) { + fprintf(stderr, "Failed to connect socket: %s\n", strerror(get_socket_errno())); closesocket(sock); - sock= INVALID_SOCKET; - } - else - { - sock= set_noblock(); + sock = INVALID_SOCKET; + } else { + sock = set_noblock(); } - } - else - { + } else { fprintf(stderr, "Failed to create socket: %s\n", strerror(get_socket_errno())); } @@ -199,44 +176,31 @@ static memcached_socket_t connect_server(const char *hostname, const char *port) return sock; } -static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len) -{ +static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len) { ssize_t ret; - if (direction == POLLOUT) - { - ret= send(fd, buf, len, 0); - } - else - { - ret= recv(fd, buf, len, 0); + if (direction == POLLOUT) { + ret = send(fd, buf, len, 0); + } else { + ret = recv(fd, buf, len, 0); } - if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK) - { + if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK) { struct pollfd fds; memset(&fds, 0, sizeof(struct pollfd)); - fds.events= direction; - fds.fd= fd; - - int err= poll(&fds, 1, timeout * 1000); - if (err == 1) - { - if (direction == POLLOUT) - { - ret= send(fd, buf, len, 0); + fds.events = direction; + fds.fd = fd; + + int err = poll(&fds, 1, timeout * 1000); + if (err == 1) { + if (direction == POLLOUT) { + ret = send(fd, buf, len, 0); + } else { + ret = recv(fd, buf, len, 0); } - else - { - ret= recv(fd, buf, len, 0); - } - } - else if (err == 0) - { - errno= ETIMEDOUT; - } - else - { + } else if (err == 0) { + errno = ETIMEDOUT; + } else { perror("Failed to poll"); return -1; } @@ -251,17 +215,13 @@ static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, * message is returned. * */ -static enum test_return ensure(bool val, const char *expression, const char *file, int line) -{ - if (!val) - { - if (verbose) - { +static enum test_return ensure(bool val, const char *expression, const char *file, int line) { + if (!val) { + if (verbose) { fprintf(stdout, "\n%s:%d: %s", file, line, expression); } - if (do_core) - { + if (do_core) { abort(); } @@ -271,28 +231,31 @@ static enum test_return ensure(bool val, const char *expression, const char *fil return TEST_PASS; } -#define verify(expression) do { if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0) -#define execute(expression) do { if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) return TEST_FAIL; } while (0) +#define verify(expression) \ + do { \ + if (ensure(expression, #expression, __FILE__, __LINE__) == TEST_FAIL) \ + return TEST_FAIL; \ + } while (0) +#define execute(expression) \ + do { \ + if (ensure(expression == TEST_PASS, #expression, __FILE__, __LINE__) == TEST_FAIL) \ + return TEST_FAIL; \ + } while (0) /** * Send a chunk of memory over the socket (retry if the call is iterrupted */ -static enum test_return retry_write(const void* buf, size_t len) -{ - size_t offset= 0; - const char* ptr= static_cast(buf); - - do - { - size_t num_bytes= len - offset; - ssize_t nw= timeout_io_op(sock, POLLOUT, (void*)(ptr + offset), num_bytes); - if (nw == -1) - { +static enum test_return retry_write(const void *buf, size_t len) { + size_t offset = 0; + const char *ptr = static_cast(buf); + + do { + size_t num_bytes = len - offset; + ssize_t nw = timeout_io_op(sock, POLLOUT, (void *) (ptr + offset), num_bytes); + if (nw == -1) { verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN); - } - else - { - offset+= (size_t)nw; + } else { + offset += (size_t) nw; } } while (offset < len); @@ -304,10 +267,9 @@ static enum test_return retry_write(const void* buf, size_t len) * Resend a packet to the server (All fields in the command header should * be in network byte order) */ -static enum test_return resend_packet(command *cmd) -{ - size_t length= sizeof (protocol_binary_request_no_extras) + - ntohl(cmd->plain.message.header.request.bodylen); +static enum test_return resend_packet(command *cmd) { + size_t length = + sizeof(protocol_binary_request_no_extras) + ntohl(cmd->plain.message.header.request.bodylen); execute(retry_write(cmd, length)); return TEST_PASS; @@ -317,15 +279,11 @@ static enum test_return resend_packet(command *cmd) * Send a command to the server. The command header needs to be updated * to network byte order */ -static enum test_return send_packet(command *cmd) -{ +static enum test_return send_packet(command *cmd) { /* Fix the byteorder of the header */ - cmd->plain.message.header.request.keylen= - ntohs(cmd->plain.message.header.request.keylen); - cmd->plain.message.header.request.bodylen= - ntohl(cmd->plain.message.header.request.bodylen); - cmd->plain.message.header.request.cas= - memcached_ntohll(cmd->plain.message.header.request.cas); + cmd->plain.message.header.request.keylen = ntohs(cmd->plain.message.header.request.keylen); + cmd->plain.message.header.request.bodylen = ntohl(cmd->plain.message.header.request.bodylen); + cmd->plain.message.header.request.cas = memcached_ntohll(cmd->plain.message.header.request.cas); execute(resend_packet(cmd)); return TEST_PASS; @@ -334,23 +292,19 @@ static enum test_return send_packet(command *cmd) /** * Read a fixed length chunk of data from the server */ -static enum test_return retry_read(void *buf, size_t len) -{ - size_t offset= 0; - do - { - ssize_t nr= timeout_io_op(sock, POLLIN, ((char*) buf) + offset, len - offset); +static enum test_return retry_read(void *buf, size_t len) { + size_t offset = 0; + do { + ssize_t nr = timeout_io_op(sock, POLLIN, ((char *) buf) + offset, len - offset); switch (nr) { - case -1 : + case -1: fprintf(stderr, "Errno: %d %s\n", get_socket_errno(), strerror(errno)); verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN); break; - case 0: - return TEST_FAIL; + case 0: return TEST_FAIL; - default: - offset+= (size_t)nr; + default: offset += (size_t) nr; } } while (offset < len); @@ -361,23 +315,18 @@ static enum test_return retry_read(void *buf, size_t len) * Receive a response from the server and conver the fields in the header * to local byte order */ -static enum test_return recv_packet(response *rsp) -{ +static enum test_return recv_packet(response *rsp) { execute(retry_read(rsp, sizeof(protocol_binary_response_no_extras))); /* Fix the byte order in the packet header */ - rsp->plain.message.header.response.keylen= - ntohs(rsp->plain.message.header.response.keylen); - rsp->plain.message.header.response.status= - ntohs(rsp->plain.message.header.response.status); - rsp->plain.message.header.response.bodylen= - ntohl(rsp->plain.message.header.response.bodylen); - rsp->plain.message.header.response.cas= - memcached_ntohll(rsp->plain.message.header.response.cas); - - size_t bodysz= rsp->plain.message.header.response.bodylen; + rsp->plain.message.header.response.keylen = ntohs(rsp->plain.message.header.response.keylen); + rsp->plain.message.header.response.status = ntohs(rsp->plain.message.header.response.status); + rsp->plain.message.header.response.bodylen = ntohl(rsp->plain.message.header.response.bodylen); + rsp->plain.message.header.response.cas = memcached_ntohll(rsp->plain.message.header.response.cas); + + size_t bodysz = rsp->plain.message.header.response.bodylen; if (bodysz > 0) - execute(retry_read(rsp->bytes + sizeof (protocol_binary_response_no_extras), bodysz)); + execute(retry_read(rsp->bytes + sizeof(protocol_binary_response_no_extras), bodysz)); return TEST_PASS; } @@ -394,29 +343,22 @@ static enum test_return recv_packet(response *rsp) * @param flags the flags to store along with the key * @param exptime the expiry time for the key */ -static void storage_command(command *cmd, - uint8_t cc, - const void* key, - size_t keylen, - const void* dta, - size_t dtalen, - uint32_t flags, - uint32_t exptime) -{ +static void storage_command(command *cmd, uint8_t cc, const void *key, size_t keylen, + const void *dta, size_t dtalen, uint32_t flags, uint32_t exptime) { /* all of the storage commands use the same command layout */ - protocol_binary_request_set *request= &cmd->set; - - memset(request, 0, sizeof (*request)); - request->message.header.request.magic= PROTOCOL_BINARY_REQ; - request->message.header.request.opcode= cc; - request->message.header.request.keylen= (uint16_t)keylen; - request->message.header.request.extlen= 8; - request->message.header.request.bodylen= (uint32_t)(keylen + 8 + dtalen); - request->message.header.request.opaque= 0xdeadbeef; - request->message.body.flags= flags; - request->message.body.expiration= exptime; - - off_t key_offset= sizeof (protocol_binary_request_no_extras) + 8; + protocol_binary_request_set *request = &cmd->set; + + memset(request, 0, sizeof(*request)); + request->message.header.request.magic = PROTOCOL_BINARY_REQ; + request->message.header.request.opcode = cc; + request->message.header.request.keylen = (uint16_t) keylen; + request->message.header.request.extlen = 8; + request->message.header.request.bodylen = (uint32_t)(keylen + 8 + dtalen); + request->message.header.request.opaque = 0xdeadbeef; + request->message.body.flags = flags; + request->message.body.expiration = exptime; + + off_t key_offset = sizeof(protocol_binary_request_no_extras) + 8; memcpy(cmd->bytes + key_offset, key, keylen); if (dta != NULL) memcpy(cmd->bytes + key_offset + keylen, dta, dtalen); @@ -431,22 +373,17 @@ static void storage_command(command *cmd, * @param dta the data to store with the key * @param dtalen the length of the data to store with the key */ -static void raw_command(command *cmd, - uint8_t cc, - const void* key, - size_t keylen, - const void* dta, - size_t dtalen) -{ +static void raw_command(command *cmd, uint8_t cc, const void *key, size_t keylen, const void *dta, + size_t dtalen) { /* all of the storage commands use the same command layout */ - memset(cmd, 0, sizeof (*cmd)); - cmd->plain.message.header.request.magic= PROTOCOL_BINARY_REQ; - cmd->plain.message.header.request.opcode= cc; - cmd->plain.message.header.request.keylen= (uint16_t)keylen; - cmd->plain.message.header.request.bodylen= (uint32_t)(keylen + dtalen); - cmd->plain.message.header.request.opaque= 0xdeadbeef; + memset(cmd, 0, sizeof(*cmd)); + cmd->plain.message.header.request.magic = PROTOCOL_BINARY_REQ; + cmd->plain.message.header.request.opcode = cc; + cmd->plain.message.header.request.keylen = (uint16_t) keylen; + cmd->plain.message.header.request.bodylen = (uint32_t)(keylen + dtalen); + cmd->plain.message.header.request.opaque = 0xdeadbeef; - off_t key_offset= sizeof (protocol_binary_request_no_extras); + off_t key_offset = sizeof(protocol_binary_request_no_extras); if (key != NULL) memcpy(cmd->bytes + key_offset, key, keylen); @@ -462,19 +399,16 @@ static void raw_command(command *cmd, * @param exptime when to flush * @param use_extra to force using of the extra field? */ -static void flush_command(command *cmd, - uint8_t cc, uint32_t exptime, bool use_extra) -{ - memset(cmd, 0, sizeof (cmd->flush)); - cmd->flush.message.header.request.magic= PROTOCOL_BINARY_REQ; - cmd->flush.message.header.request.opcode= cc; - cmd->flush.message.header.request.opaque= 0xdeadbeef; - - if (exptime != 0 || use_extra) - { - cmd->flush.message.header.request.extlen= 4; - cmd->flush.message.body.expiration= htonl(exptime); - cmd->flush.message.header.request.bodylen= 4; +static void flush_command(command *cmd, uint8_t cc, uint32_t exptime, bool use_extra) { + memset(cmd, 0, sizeof(cmd->flush)); + cmd->flush.message.header.request.magic = PROTOCOL_BINARY_REQ; + cmd->flush.message.header.request.opcode = cc; + cmd->flush.message.header.request.opaque = 0xdeadbeef; + + if (exptime != 0 || use_extra) { + cmd->flush.message.header.request.extlen = 4; + cmd->flush.message.body.expiration = htonl(exptime); + cmd->flush.message.header.request.bodylen = 4; } } @@ -487,26 +421,20 @@ static void flush_command(command *cmd, * @param initial the initial value if the key doesn't exist * @param exptime when the key should expire if it isn't set */ -static void arithmetic_command(command *cmd, - uint8_t cc, - const void* key, - size_t keylen, - uint64_t delta, - uint64_t initial, - uint32_t exptime) -{ - memset(cmd, 0, sizeof (cmd->incr)); - cmd->incr.message.header.request.magic= PROTOCOL_BINARY_REQ; - cmd->incr.message.header.request.opcode= cc; - cmd->incr.message.header.request.keylen= (uint16_t)keylen; - cmd->incr.message.header.request.extlen= 20; - cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20); - cmd->incr.message.header.request.opaque= 0xdeadbeef; - cmd->incr.message.body.delta= memcached_htonll(delta); - cmd->incr.message.body.initial= memcached_htonll(initial); - cmd->incr.message.body.expiration= htonl(exptime); - - off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20; +static void arithmetic_command(command *cmd, uint8_t cc, const void *key, size_t keylen, + uint64_t delta, uint64_t initial, uint32_t exptime) { + memset(cmd, 0, sizeof(cmd->incr)); + cmd->incr.message.header.request.magic = PROTOCOL_BINARY_REQ; + cmd->incr.message.header.request.opcode = cc; + cmd->incr.message.header.request.keylen = (uint16_t) keylen; + cmd->incr.message.header.request.extlen = 20; + cmd->incr.message.header.request.bodylen = (uint32_t)(keylen + 20); + cmd->incr.message.header.request.opaque = 0xdeadbeef; + cmd->incr.message.body.delta = memcached_htonll(delta); + cmd->incr.message.body.initial = memcached_htonll(initial); + cmd->incr.message.body.expiration = htonl(exptime); + + off_t key_offset = sizeof(protocol_binary_request_no_extras) + 20; memcpy(cmd->bytes + key_offset, key, keylen); } @@ -516,17 +444,14 @@ static void arithmetic_command(command *cmd, * @param cc the expected command * @param status the expected status */ -static enum test_return do_validate_response_header(response *rsp, - uint8_t cc, uint16_t status) -{ +static enum test_return do_validate_response_header(response *rsp, uint8_t cc, uint16_t status) { verify(rsp->plain.message.header.response.magic == PROTOCOL_BINARY_RES); verify(rsp->plain.message.header.response.opcode == cc); verify(rsp->plain.message.header.response.datatype == PROTOCOL_BINARY_RAW_BYTES); verify(rsp->plain.message.header.response.status == status); verify(rsp->plain.message.header.response.opaque == 0xdeadbeef); - if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS) - { + if (status == PROTOCOL_BINARY_RESPONSE_SUCCESS) { switch (cc) { case PROTOCOL_BINARY_CMD_ADDQ: case PROTOCOL_BINARY_CMD_APPENDQ: @@ -540,8 +465,7 @@ static enum test_return do_validate_response_header(response *rsp, case PROTOCOL_BINARY_CMD_SETQ: verify("Quiet command shouldn't return on success" == NULL); /* fall through */ - default: - break; + default: break; } switch (cc) { @@ -604,13 +528,10 @@ static enum test_return do_validate_response_header(response *rsp, /* Undefined command code */ break; } - } - else - { + } else { verify(rsp->plain.message.header.response.cas == 0); verify(rsp->plain.message.header.response.extlen == 0); - if (cc != PROTOCOL_BINARY_CMD_GETK) - { + if (cc != PROTOCOL_BINARY_CMD_GETK) { verify(rsp->plain.message.header.response.keylen == 0); } } @@ -623,46 +544,39 @@ static enum test_return do_validate_response_header(response *rsp, * an enum.... Let's just create a macro to avoid cluttering * the code with all of the == TEST_PASS ;-) */ -#define validate_response_header(a,b,c) \ - do_validate_response_header(a,b,c) == TEST_PASS +#define validate_response_header(a, b, c) do_validate_response_header(a, b, c) == TEST_PASS - -static enum test_return send_binary_noop(void) -{ +static enum test_return send_binary_noop(void) { command cmd; raw_command(&cmd, PROTOCOL_BINARY_CMD_NOOP, NULL, 0, NULL, 0); execute(send_packet(&cmd)); return TEST_PASS; } -static enum test_return receive_binary_noop(void) -{ +static enum test_return receive_binary_noop(void) { response rsp; execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); + verify( + validate_response_header(&rsp, PROTOCOL_BINARY_CMD_NOOP, PROTOCOL_BINARY_RESPONSE_SUCCESS)); return TEST_PASS; } -static enum test_return test_binary_noop(void) -{ +static enum test_return test_binary_noop(void) { execute(send_binary_noop()); execute(receive_binary_noop()); return TEST_PASS; } -static enum test_return test_binary_quit_impl(uint8_t cc) -{ +static enum test_return test_binary_quit_impl(uint8_t cc) { command cmd; response rsp; raw_command(&cmd, cc, NULL, 0, NULL, 0); execute(send_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_QUIT) - { + if (cc == PROTOCOL_BINARY_CMD_QUIT) { execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); + verify( + validate_response_header(&rsp, PROTOCOL_BINARY_CMD_QUIT, PROTOCOL_BINARY_RESPONSE_SUCCESS)); } /* Socket should be closed now, read should return EXIT_SUCCESS */ @@ -671,42 +585,33 @@ static enum test_return test_binary_quit_impl(uint8_t cc) return TEST_PASS_RECONNECT; } -static enum test_return test_binary_quit(void) -{ +static enum test_return test_binary_quit(void) { return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUIT); } -static enum test_return test_binary_quitq(void) -{ +static enum test_return test_binary_quitq(void) { return test_binary_quit_impl(PROTOCOL_BINARY_CMD_QUITQ); } -static enum test_return test_binary_set_impl(const char* key, uint8_t cc) -{ +static enum test_return test_binary_set_impl(const char *key, uint8_t cc) { command cmd; response rsp; - uint64_t value= 0xdeadbeefdeadcafeULL; - storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0); + uint64_t value = 0xdeadbeefdeadcafeULL; + storage_command(&cmd, cc, key, strlen(key), &value, sizeof(value), 0, 0); /* set should always work */ - for (int ii= 0; ii < 10; ii++) - { - if (ii == 0) - { + for (int ii = 0; ii < 10; ii++) { + if (ii == 0) { execute(send_packet(&cmd)); - } - else - { + } else { execute(resend_packet(&cmd)); } - if (cc == PROTOCOL_BINARY_CMD_SET) - { + if (cc == PROTOCOL_BINARY_CMD_SET) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - } - else + } else execute(test_binary_noop()); } @@ -714,29 +619,27 @@ static enum test_return test_binary_set_impl(const char* key, uint8_t cc) * We need to get the current CAS id, and at this time we haven't * verified that we have a working get */ - if (cc == PROTOCOL_BINARY_CMD_SETQ) - { - cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET; + if (cc == PROTOCOL_BINARY_CMD_SETQ) { + cmd.set.message.header.request.opcode = PROTOCOL_BINARY_CMD_SET; execute(resend_packet(&cmd)); execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); - cmd.set.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ; + verify( + validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET, PROTOCOL_BINARY_RESPONSE_SUCCESS)); + cmd.set.message.header.request.opcode = PROTOCOL_BINARY_CMD_SETQ; } /* try to set with the correct CAS value */ - cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas); + cmd.plain.message.header.request.cas = memcached_htonll(rsp.plain.message.header.response.cas); execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_SET) - { + if (cc == PROTOCOL_BINARY_CMD_SET) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - } - else + } else execute(test_binary_noop()); /* try to set with an incorrect CAS value */ - cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1); + cmd.plain.message.header.request.cas = + memcached_htonll(rsp.plain.message.header.response.cas - 1); execute(resend_packet(&cmd)); execute(send_binary_noop()); execute(recv_packet(&rsp)); @@ -746,105 +649,85 @@ static enum test_return test_binary_set_impl(const char* key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_set(void) -{ +static enum test_return test_binary_set(void) { return test_binary_set_impl("test_binary_set", PROTOCOL_BINARY_CMD_SET); } -static enum test_return test_binary_setq(void) -{ +static enum test_return test_binary_setq(void) { return test_binary_set_impl("test_binary_setq", PROTOCOL_BINARY_CMD_SETQ); } -static enum test_return test_binary_add_impl(const char* key, uint8_t cc) -{ +static enum test_return test_binary_add_impl(const char *key, uint8_t cc) { command cmd; response rsp; - uint64_t value= 0xdeadbeefdeadcafeULL; - storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0); + uint64_t value = 0xdeadbeefdeadcafeULL; + storage_command(&cmd, cc, key, strlen(key), &value, sizeof(value), 0, 0); /* first add should work, rest of them should fail (even with cas as wildcard */ - for (int ii=0; ii < 10; ii++) - { + for (int ii = 0; ii < 10; ii++) { if (ii == 0) execute(send_packet(&cmd)); else execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0) - { + if (cc == PROTOCOL_BINARY_CMD_ADD || ii > 0) { uint16_t expected_result; if (ii == 0) - expected_result= PROTOCOL_BINARY_RESPONSE_SUCCESS; + expected_result = PROTOCOL_BINARY_RESPONSE_SUCCESS; else - expected_result= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS; + expected_result = PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS; execute(send_binary_noop()); execute(recv_packet(&rsp)); execute(receive_binary_noop()); verify(validate_response_header(&rsp, cc, expected_result)); - } - else + } else execute(test_binary_noop()); } return TEST_PASS; } -static enum test_return test_binary_add(void) -{ +static enum test_return test_binary_add(void) { return test_binary_add_impl("test_binary_add", PROTOCOL_BINARY_CMD_ADD); } -static enum test_return test_binary_addq(void) -{ +static enum test_return test_binary_addq(void) { return test_binary_add_impl("test_binary_addq", PROTOCOL_BINARY_CMD_ADDQ); } -static enum test_return binary_set_item(const char *key, const char *value) -{ +static enum test_return binary_set_item(const char *key, const char *value) { command cmd; response rsp; - storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key), - value, strlen(value), 0, 0); + storage_command(&cmd, PROTOCOL_BINARY_CMD_SET, key, strlen(key), value, strlen(value), 0, 0); execute(send_packet(&cmd)); execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); + verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_SET, PROTOCOL_BINARY_RESPONSE_SUCCESS)); return TEST_PASS; } -static enum test_return test_binary_replace_impl(const char* key, uint8_t cc) -{ +static enum test_return test_binary_replace_impl(const char *key, uint8_t cc) { command cmd; response rsp; - uint64_t value= 0xdeadbeefdeadcafeULL; - storage_command(&cmd, cc, key, strlen(key), &value, sizeof (value), 0, 0); + uint64_t value = 0xdeadbeefdeadcafeULL; + storage_command(&cmd, cc, key, strlen(key), &value, sizeof(value), 0, 0); /* first replace should fail, successive should succeed (when the item is added! */ - for (int ii= 0; ii < 10; ii++) - { - if (ii == 0) - { + for (int ii = 0; ii < 10; ii++) { + if (ii == 0) { execute(send_packet(&cmd)); - } - else - { + } else { execute(resend_packet(&cmd)); } - if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0) - { + if (cc == PROTOCOL_BINARY_CMD_REPLACE || ii == 0) { uint16_t expected_result; - if (ii == 0) - { - expected_result=PROTOCOL_BINARY_RESPONSE_KEY_ENOENT; - } - else - { - expected_result=PROTOCOL_BINARY_RESPONSE_SUCCESS; + if (ii == 0) { + expected_result = PROTOCOL_BINARY_RESPONSE_KEY_ENOENT; + } else { + expected_result = PROTOCOL_BINARY_RESPONSE_SUCCESS; } execute(send_binary_noop()); @@ -854,27 +737,24 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc) if (ii == 0) execute(binary_set_item(key, key)); - } - else - { + } else { execute(test_binary_noop()); } } /* verify that replace with CAS value works! */ - cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas); + cmd.plain.message.header.request.cas = memcached_htonll(rsp.plain.message.header.response.cas); execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_REPLACE) - { + if (cc == PROTOCOL_BINARY_CMD_REPLACE) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - } - else + } else execute(test_binary_noop()); /* try to set with an incorrect CAS value */ - cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1); + cmd.plain.message.header.request.cas = + memcached_htonll(rsp.plain.message.header.response.cas - 1); execute(resend_packet(&cmd)); execute(send_binary_noop()); execute(recv_packet(&rsp)); @@ -884,18 +764,15 @@ static enum test_return test_binary_replace_impl(const char* key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_replace(void) -{ +static enum test_return test_binary_replace(void) { return test_binary_replace_impl("test_binary_replace", PROTOCOL_BINARY_CMD_REPLACE); } -static enum test_return test_binary_replaceq(void) -{ +static enum test_return test_binary_replaceq(void) { return test_binary_replace_impl("test_binary_replaceq", PROTOCOL_BINARY_CMD_REPLACEQ); } -static enum test_return test_binary_delete_impl(const char *key, uint8_t cc) -{ +static enum test_return test_binary_delete_impl(const char *key, uint8_t cc) { command cmd; response rsp; raw_command(&cmd, cc, key, strlen(key), NULL, 0); @@ -910,8 +787,7 @@ static enum test_return test_binary_delete_impl(const char *key, uint8_t cc) /* The item should be present now, resend*/ execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_DELETE) - { + if (cc == PROTOCOL_BINARY_CMD_DELETE) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); } @@ -921,18 +797,15 @@ static enum test_return test_binary_delete_impl(const char *key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_delete(void) -{ +static enum test_return test_binary_delete(void) { return test_binary_delete_impl("test_binary_delete", PROTOCOL_BINARY_CMD_DELETE); } -static enum test_return test_binary_deleteq(void) -{ +static enum test_return test_binary_deleteq(void) { return test_binary_delete_impl("test_binary_deleteq", PROTOCOL_BINARY_CMD_DELETEQ); } -static enum test_return test_binary_get_impl(const char *key, uint8_t cc) -{ +static enum test_return test_binary_get_impl(const char *key, uint8_t cc) { command cmd; response rsp; @@ -940,8 +813,7 @@ static enum test_return test_binary_get_impl(const char *key, uint8_t cc) execute(send_packet(&cmd)); execute(send_binary_noop()); - if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK) - { + if (cc == PROTOCOL_BINARY_CMD_GET || cc == PROTOCOL_BINARY_CMD_GETK) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_KEY_ENOENT)); } @@ -959,47 +831,39 @@ static enum test_return test_binary_get_impl(const char *key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_get(void) -{ +static enum test_return test_binary_get(void) { return test_binary_get_impl("test_binary_get", PROTOCOL_BINARY_CMD_GET); } -static enum test_return test_binary_getk(void) -{ +static enum test_return test_binary_getk(void) { return test_binary_get_impl("test_binary_getk", PROTOCOL_BINARY_CMD_GETK); } -static enum test_return test_binary_getq(void) -{ +static enum test_return test_binary_getq(void) { return test_binary_get_impl("test_binary_getq", PROTOCOL_BINARY_CMD_GETQ); } -static enum test_return test_binary_getkq(void) -{ +static enum test_return test_binary_getkq(void) { return test_binary_get_impl("test_binary_getkq", PROTOCOL_BINARY_CMD_GETKQ); } -static enum test_return test_binary_incr_impl(const char* key, uint8_t cc) -{ +static enum test_return test_binary_incr_impl(const char *key, uint8_t cc) { command cmd; response rsp; arithmetic_command(&cmd, cc, key, strlen(key), 1, 0, 0); uint64_t ii; - for (ii= 0; ii < 10; ++ii) - { + for (ii = 0; ii < 10; ++ii) { if (ii == 0) execute(send_packet(&cmd)); else execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_INCREMENT) - { + if (cc == PROTOCOL_BINARY_CMD_INCREMENT) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); verify(memcached_ntohll(rsp.incr.message.body.value) == ii); - } - else + } else execute(test_binary_noop()); } @@ -1007,52 +871,42 @@ static enum test_return test_binary_incr_impl(const char* key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_incr(void) -{ +static enum test_return test_binary_incr(void) { return test_binary_incr_impl("test_binary_incr", PROTOCOL_BINARY_CMD_INCREMENT); } -static enum test_return test_binary_incrq(void) -{ +static enum test_return test_binary_incrq(void) { return test_binary_incr_impl("test_binary_incrq", PROTOCOL_BINARY_CMD_INCREMENTQ); } -static enum test_return test_binary_decr_impl(const char* key, uint8_t cc) -{ +static enum test_return test_binary_decr_impl(const char *key, uint8_t cc) { command cmd; response rsp; arithmetic_command(&cmd, cc, key, strlen(key), 1, 9, 0); int ii; - for (ii= 9; ii > -1; --ii) - { + for (ii = 9; ii > -1; --ii) { if (ii == 9) execute(send_packet(&cmd)); else execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_DECREMENT) - { + if (cc == PROTOCOL_BINARY_CMD_DECREMENT) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t)ii); - } - else + verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t) ii); + } else execute(test_binary_noop()); } /* decr 0 should not wrap */ execute(resend_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_DECREMENT) - { + if (cc == PROTOCOL_BINARY_CMD_DECREMENT) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); verify(memcached_ntohll(rsp.decr.message.body.value) == 0); - } - else - { + } else { /* @todo get the value and verify! */ - } /* @todo add incorrect cas */ @@ -1060,20 +914,15 @@ static enum test_return test_binary_decr_impl(const char* key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_decr(void) -{ - return test_binary_decr_impl("test_binary_decr", - PROTOCOL_BINARY_CMD_DECREMENT); +static enum test_return test_binary_decr(void) { + return test_binary_decr_impl("test_binary_decr", PROTOCOL_BINARY_CMD_DECREMENT); } -static enum test_return test_binary_decrq(void) -{ - return test_binary_decr_impl("test_binary_decrq", - PROTOCOL_BINARY_CMD_DECREMENTQ); +static enum test_return test_binary_decrq(void) { + return test_binary_decr_impl("test_binary_decrq", PROTOCOL_BINARY_CMD_DECREMENTQ); } -static enum test_return test_binary_version(void) -{ +static enum test_return test_binary_version(void) { command cmd; response rsp; raw_command(&cmd, PROTOCOL_BINARY_CMD_VERSION, NULL, 0, NULL, 0); @@ -1086,23 +935,19 @@ static enum test_return test_binary_version(void) return TEST_PASS; } -static enum test_return test_binary_flush_impl(const char *key, uint8_t cc) -{ +static enum test_return test_binary_flush_impl(const char *key, uint8_t cc) { command cmd; response rsp; - for (int ii= 0; ii < 2; ++ii) - { + for (int ii = 0; ii < 2; ++ii) { execute(binary_set_item(key, key)); flush_command(&cmd, cc, 0, ii == 0); execute(send_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_FLUSH) - { + if (cc == PROTOCOL_BINARY_CMD_FLUSH) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - } - else + } else execute(test_binary_noop()); raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0); @@ -1115,123 +960,98 @@ static enum test_return test_binary_flush_impl(const char *key, uint8_t cc) return TEST_PASS; } -static enum test_return test_binary_flush(void) -{ +static enum test_return test_binary_flush(void) { return test_binary_flush_impl("test_binary_flush", PROTOCOL_BINARY_CMD_FLUSH); } -static enum test_return test_binary_flushq(void) -{ +static enum test_return test_binary_flushq(void) { return test_binary_flush_impl("test_binary_flushq", PROTOCOL_BINARY_CMD_FLUSHQ); } -static enum test_return test_binary_concat_impl(const char *key, uint8_t cc) -{ +static enum test_return test_binary_concat_impl(const char *key, uint8_t cc) { command cmd; response rsp; const char *value; - if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ) - { - value="hello"; - } - else - { - value=" world"; + if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ) { + value = "hello"; + } else { + value = " world"; } execute(binary_set_item(key, value)); - if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ) - { - value=" world"; - } - else - { - value="hello"; + if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_APPENDQ) { + value = " world"; + } else { + value = "hello"; } raw_command(&cmd, cc, key, strlen(key), value, strlen(value)); execute(send_packet(&cmd)); - if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND) - { + if (cc == PROTOCOL_BINARY_CMD_APPEND || cc == PROTOCOL_BINARY_CMD_PREPEND) { execute(recv_packet(&rsp)); verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS)); - } - else - { + } else { execute(test_binary_noop()); } raw_command(&cmd, PROTOCOL_BINARY_CMD_GET, key, strlen(key), NULL, 0); execute(send_packet(&cmd)); execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); + verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_GET, PROTOCOL_BINARY_RESPONSE_SUCCESS)); verify(rsp.plain.message.header.response.bodylen - 4 == 11); verify(memcmp(rsp.bytes + 28, "hello world", 11) == 0); return TEST_PASS; } -static enum test_return test_binary_append(void) -{ +static enum test_return test_binary_append(void) { return test_binary_concat_impl("test_binary_append", PROTOCOL_BINARY_CMD_APPEND); } -static enum test_return test_binary_prepend(void) -{ +static enum test_return test_binary_prepend(void) { return test_binary_concat_impl("test_binary_prepend", PROTOCOL_BINARY_CMD_PREPEND); } -static enum test_return test_binary_appendq(void) -{ +static enum test_return test_binary_appendq(void) { return test_binary_concat_impl("test_binary_appendq", PROTOCOL_BINARY_CMD_APPENDQ); } -static enum test_return test_binary_prependq(void) -{ +static enum test_return test_binary_prependq(void) { return test_binary_concat_impl("test_binary_prependq", PROTOCOL_BINARY_CMD_PREPENDQ); } -static enum test_return test_binary_stat(void) -{ +static enum test_return test_binary_stat(void) { command cmd; response rsp; raw_command(&cmd, PROTOCOL_BINARY_CMD_STAT, NULL, 0, NULL, 0); execute(send_packet(&cmd)); - do - { + do { execute(recv_packet(&rsp)); - verify(validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT, - PROTOCOL_BINARY_RESPONSE_SUCCESS)); + verify( + validate_response_header(&rsp, PROTOCOL_BINARY_CMD_STAT, PROTOCOL_BINARY_RESPONSE_SUCCESS)); } while (rsp.plain.message.header.response.keylen != 0); return TEST_PASS; } -static enum test_return send_string(const char *cmd) -{ +static enum test_return send_string(const char *cmd) { execute(retry_write(cmd, strlen(cmd))); return TEST_PASS; } -static enum test_return receive_line(char *buffer, size_t size) -{ - size_t offset= 0; - while (offset < size) - { +static enum test_return receive_line(char *buffer, size_t size) { + size_t offset = 0; + while (offset < size) { execute(retry_read(buffer + offset, 1)); - if (buffer[offset] == '\n') - { - if (offset + 1 < size) - { - buffer[offset + 1]= '\0'; + if (buffer[offset] == '\n') { + if (offset + 1 < size) { + buffer[offset + 1] = '\0'; return TEST_PASS; - } - else + } else return TEST_FAIL; } ++offset; @@ -1244,24 +1064,21 @@ static enum test_return receive_response(const char *msg) { char buffer[80]; execute(receive_line(buffer, sizeof(buffer))); if (strcmp(msg, buffer) != 0) { - fprintf(stderr, "[%s]\n", buffer); + fprintf(stderr, "[%s]\n", buffer); } verify(strcmp(msg, buffer) == 0); return TEST_PASS; } -static enum test_return receive_error_response(void) -{ +static enum test_return receive_error_response(void) { char buffer[80]; execute(receive_line(buffer, sizeof(buffer))); - verify(strncmp(buffer, "ERROR", 5) == 0 || - strncmp(buffer, "CLIENT_ERROR", 12) == 0 || - strncmp(buffer, "SERVER_ERROR", 12) == 0); + verify(strncmp(buffer, "ERROR", 5) == 0 || strncmp(buffer, "CLIENT_ERROR", 12) == 0 + || strncmp(buffer, "SERVER_ERROR", 12) == 0); return TEST_PASS; } -static enum test_return test_ascii_quit(void) -{ +static enum test_return test_ascii_quit(void) { if (!v16x_or_greater) { /* Verify that quit handles unknown options */ execute(send_string("quit foo bar\r\n")); @@ -1279,11 +1096,9 @@ static enum test_return test_ascii_quit(void) char buffer[80]; verify(timeout_io_op(sock, POLLIN, buffer, sizeof(buffer)) == 0); return TEST_PASS_RECONNECT; - } -static enum test_return test_ascii_version(void) -{ +static enum test_return test_ascii_version(void) { /* Verify that version works */ execute(send_string("version\r\n")); char buffer[256]; @@ -1314,8 +1129,7 @@ static enum test_return test_ascii_version(void) return TEST_PASS; } -static enum test_return test_ascii_verbosity(void) -{ +static enum test_return test_ascii_verbosity(void) { /* This command does not adhere to the spec! */ execute(send_string("verbosity foo bar my\r\n")); execute(receive_error_response()); @@ -1338,100 +1152,86 @@ static enum test_return test_ascii_verbosity(void) return TEST_PASS; } - - -static enum test_return test_ascii_set_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_set_impl(const char *key, bool noreply) { /* @todo add tests for bogus format! */ char buffer[1024]; snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); - if (!noreply) - { + if (!noreply) { execute(receive_response("STORED\r\n")); } return test_ascii_version(); } -static enum test_return test_ascii_set(void) -{ +static enum test_return test_ascii_set(void) { return test_ascii_set_impl("test_ascii_set", false); } -static enum test_return test_ascii_set_noreply(void) -{ +static enum test_return test_ascii_set_noreply(void) { return test_ascii_set_impl("test_ascii_set_noreply", true); } -static enum test_return test_ascii_add_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_add_impl(const char *key, bool noreply) { /* @todo add tests for bogus format! */ char buffer[1024]; snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); - if (!noreply) - { + if (!noreply) { execute(receive_response("STORED\r\n")); } execute(send_string(buffer)); - if (!noreply) - { + if (!noreply) { execute(receive_response("NOT_STORED\r\n")); } return test_ascii_version(); } -static enum test_return test_ascii_add(void) -{ +static enum test_return test_ascii_add(void) { return test_ascii_add_impl("test_ascii_add", false); } -static enum test_return test_ascii_add_noreply(void) -{ +static enum test_return test_ascii_add_noreply(void) { return test_ascii_add_impl("test_ascii_add_noreply", true); } -static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata) -{ +static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_t *ndata) { char buffer[1024]; execute(receive_line(buffer, sizeof(buffer))); verify(strncmp(buffer, "VALUE ", 6) == 0); - char *end= strchr(buffer + 6, ' '); + char *end = strchr(buffer + 6, ' '); verify(end != NULL); - if (end) - { - *end= '\0'; + if (end) { + *end = '\0'; } - *key= strdup(buffer + 6); + *key = strdup(buffer + 6); verify(*key != NULL); - char *ptr= end + 1; + char *ptr = end + 1; - errno= 0; - unsigned long val= strtoul(ptr, &end, 10); /* flags */ + errno = 0; + unsigned long val = strtoul(ptr, &end, 10); /* flags */ verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); - errno= 0; - *ndata = (ssize_t)strtoul(end, &end, 10); /* size */ + errno = 0; + *ndata = (ssize_t) strtoul(end, &end, 10); /* size */ verify(errno == 0); verify(ptr != end); verify(end != NULL); - while (end and *end != '\n' and isspace(*end)) - ++end; + while (end and *end != '\n' and isspace(*end)) ++end; verify(end and *end == '\n'); - *value= static_cast(malloc((size_t)*ndata)); + *value = static_cast(malloc((size_t) *ndata)); verify(*value != NULL); - execute(retry_read(*value, (size_t)*ndata)); + execute(retry_read(*value, (size_t) *ndata)); execute(retry_read(buffer, 2)); verify(memcmp(buffer, "\r\n", 2) == 0); @@ -1439,34 +1239,31 @@ static enum test_return ascii_get_unknown_value(char **key, char **value, ssize_ return TEST_PASS; } -static enum test_return ascii_get_value(const char *key, const char *value) -{ - +static enum test_return ascii_get_value(const char *key, const char *value) { char buffer[1024]; - size_t datasize= strlen(value); + size_t datasize = strlen(value); verify(datasize < sizeof(buffer)); execute(receive_line(buffer, sizeof(buffer))); verify(strncmp(buffer, "VALUE ", 6) == 0); verify(strncmp(buffer + 6, key, strlen(key)) == 0); - char *ptr= buffer + 6 + strlen(key) + 1; + char *ptr = buffer + 6 + strlen(key) + 1; char *end; - errno= 0; - unsigned long val= strtoul(ptr, &end, 10); /* flags */ + errno = 0; + unsigned long val = strtoul(ptr, &end, 10); /* flags */ verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); - errno= 0; - val= strtoul(end, &end, 10); /* size */ + errno = 0; + val = strtoul(end, &end, 10); /* size */ verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); - while (end and *end != '\n' and isspace(*end)) - { + while (end and *end != '\n' and isspace(*end)) { ++end; } verify(end and *end == '\n'); @@ -1480,22 +1277,18 @@ static enum test_return ascii_get_value(const char *key, const char *value) return TEST_PASS; } -static enum test_return ascii_get_item(const char *key, const char *value, - bool exist) -{ +static enum test_return ascii_get_item(const char *key, const char *value, bool exist) { char buffer[1024]; - size_t datasize= 0; - if (value != NULL) - { - datasize= strlen(value); + size_t datasize = 0; + if (value != NULL) { + datasize = strlen(value); } verify(datasize < sizeof(buffer)); snprintf(buffer, sizeof(buffer), "get %s\r\n", key); execute(send_string(buffer)); - if (exist) - { + if (exist) { execute(ascii_get_value(key, value)); } @@ -1505,43 +1298,39 @@ static enum test_return ascii_get_item(const char *key, const char *value, return TEST_PASS; } -static enum test_return ascii_gets_value(const char *key, const char *value, - unsigned long *cas) -{ - +static enum test_return ascii_gets_value(const char *key, const char *value, unsigned long *cas) { char buffer[1024]; - size_t datasize= strlen(value); + size_t datasize = strlen(value); verify(datasize < sizeof(buffer)); execute(receive_line(buffer, sizeof(buffer))); verify(strncmp(buffer, "VALUE ", 6) == 0); verify(strncmp(buffer + 6, key, strlen(key)) == 0); - char *ptr= buffer + 6 + strlen(key) + 1; + char *ptr = buffer + 6 + strlen(key) + 1; char *end; - errno= 0; - unsigned long val= strtoul(ptr, &end, 10); /* flags */ + errno = 0; + unsigned long val = strtoul(ptr, &end, 10); /* flags */ verify(errno == 0); verify(ptr != end); verify(val == 0); verify(end != NULL); - errno= 0; - val= strtoul(end, &end, 10); /* size */ + errno = 0; + val = strtoul(end, &end, 10); /* size */ verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); - errno= 0; - *cas= strtoul(end, &end, 10); /* cas */ + errno = 0; + *cas = strtoul(end, &end, 10); /* cas */ verify(errno == 0); verify(ptr != end); verify(val == datasize); verify(end != NULL); - while (end and *end != '\n' and isspace(*end)) - { + while (end and *end != '\n' and isspace(*end)) { ++end; } verify(end and *end == '\n'); @@ -1555,14 +1344,12 @@ static enum test_return ascii_gets_value(const char *key, const char *value, return TEST_PASS; } -static enum test_return ascii_gets_item(const char *key, const char *value, - bool exist, unsigned long *cas) -{ +static enum test_return ascii_gets_item(const char *key, const char *value, bool exist, + unsigned long *cas) { char buffer[1024]; - size_t datasize= 0; - if (value != NULL) - { - datasize= strlen(value); + size_t datasize = 0; + if (value != NULL) { + datasize = strlen(value); } verify(datasize < sizeof(buffer)); @@ -1578,11 +1365,10 @@ static enum test_return ascii_gets_item(const char *key, const char *value, return TEST_PASS; } -static enum test_return ascii_set_item(const char *key, const char *value) -{ +static enum test_return ascii_set_item(const char *key, const char *value) { char buffer[300]; - size_t len= strlen(value); - snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len); + size_t len = strlen(value); + snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int) len); execute(send_string(buffer)); execute(retry_write(value, len)); execute(send_string("\r\n")); @@ -1590,25 +1376,21 @@ static enum test_return ascii_set_item(const char *key, const char *value) return TEST_PASS; } -static enum test_return test_ascii_replace_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_replace_impl(const char *key, bool noreply) { char buffer[1024]; - snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, + noreply ? " noreply" : ""); execute(send_string(buffer)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { execute(receive_response("NOT_STORED\r\n")); } execute(ascii_set_item(key, "value")); execute(ascii_get_item(key, "value", true)); - execute(send_string(buffer)); if (noreply) @@ -1619,63 +1401,52 @@ static enum test_return test_ascii_replace_impl(const char* key, bool noreply) return test_ascii_version(); } -static enum test_return test_ascii_replace(void) -{ +static enum test_return test_ascii_replace(void) { return test_ascii_replace_impl("test_ascii_replace", false); } -static enum test_return test_ascii_replace_noreply(void) -{ +static enum test_return test_ascii_replace_noreply(void) { return test_ascii_replace_impl("test_ascii_replace_noreply", true); } -static enum test_return test_ascii_cas_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_cas_impl(const char *key, bool noreply) { char buffer[1024]; unsigned long cas; execute(ascii_set_item(key, "value")); execute(ascii_gets_item(key, "value", true, &cas)); - snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, + noreply ? " noreply" : ""); execute(send_string(buffer)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { execute(receive_response("STORED\r\n")); } /* reexecute the same command should fail due to illegal cas */ execute(send_string(buffer)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { execute(receive_response("EXISTS\r\n")); } return test_ascii_version(); } -static enum test_return test_ascii_cas(void) -{ +static enum test_return test_ascii_cas(void) { return test_ascii_cas_impl("test_ascii_cas", false); } -static enum test_return test_ascii_cas_noreply(void) -{ +static enum test_return test_ascii_cas_noreply(void) { return test_ascii_cas_impl("test_ascii_cas_noreply", true); } -static enum test_return test_ascii_delete_impl(const char *key, bool noreply) -{ +static enum test_return test_ascii_delete_impl(const char *key, bool noreply) { execute(ascii_set_item(key, "value")); execute(send_string("delete\r\n")); @@ -1703,18 +1474,15 @@ static enum test_return test_ascii_delete_impl(const char *key, bool noreply) return TEST_PASS; } -static enum test_return test_ascii_delete(void) -{ +static enum test_return test_ascii_delete(void) { return test_ascii_delete_impl("test_ascii_delete", false); } -static enum test_return test_ascii_delete_noreply(void) -{ +static enum test_return test_ascii_delete_noreply(void) { return test_ascii_delete_impl("test_ascii_delete_noreply", true); } -static enum test_return test_ascii_get(void) -{ +static enum test_return test_ascii_get(void) { execute(ascii_set_item("test_ascii_get", "value")); execute(send_string("get\r\n")); @@ -1725,8 +1493,7 @@ static enum test_return test_ascii_get(void) return TEST_PASS; } -static enum test_return test_ascii_gets(void) -{ +static enum test_return test_ascii_gets(void) { execute(ascii_set_item("test_ascii_gets", "value")); execute(send_string("gets\r\n")); @@ -1738,20 +1505,13 @@ static enum test_return test_ascii_gets(void) return TEST_PASS; } -static enum test_return test_ascii_mget(void) -{ - const uint32_t nkeys= 5; - const char * const keys[]= { - "test_ascii_mget1", - "test_ascii_mget2", - /* test_ascii_mget_3 does not exist :) */ - "test_ascii_mget4", - "test_ascii_mget5", - "test_ascii_mget6" - }; - - for (uint32_t x= 0; x < nkeys; ++x) - { +static enum test_return test_ascii_mget(void) { + const uint32_t nkeys = 5; + const char *const keys[] = {"test_ascii_mget1", "test_ascii_mget2", + /* test_ascii_mget_3 does not exist :) */ + "test_ascii_mget4", "test_ascii_mget5", "test_ascii_mget6"}; + + for (uint32_t x = 0; x < nkeys; ++x) { execute(ascii_set_item(keys[x], "value")); } @@ -1763,10 +1523,9 @@ static enum test_return test_ascii_mget(void) std::vector returned; returned.resize(nkeys); - for (uint32_t x= 0; x < nkeys; ++x) - { + for (uint32_t x = 0; x < nkeys; ++x) { ssize_t nbytes = 0; - char *v= NULL; + char *v = NULL; execute(ascii_get_unknown_value(&returned[x], &v, &nbytes)); verify(nbytes == 5); verify(memcmp(v, "value", 5) == 0); @@ -1778,13 +1537,10 @@ static enum test_return test_ascii_mget(void) verify(memcmp(buffer, "END\r\n", 5) == 0); /* verify that we got all the keys we expected */ - for (uint32_t x= 0; x < nkeys; ++x) - { - bool found= false; - for (uint32_t y= 0; y < nkeys; ++y) - { - if (strcmp(keys[x], returned[y]) == 0) - { + for (uint32_t x = 0; x < nkeys; ++x) { + bool found = false; + for (uint32_t y = 0; y < nkeys; ++y) { + if (strcmp(keys[x], returned[y]) == 0) { found = true; break; } @@ -1792,31 +1548,27 @@ static enum test_return test_ascii_mget(void) verify(found); } - for (uint32_t x= 0; x < nkeys; ++x) - { + for (uint32_t x = 0; x < nkeys; ++x) { free(returned[x]); } return TEST_PASS; } -static enum test_return test_ascii_incr_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_incr_impl(const char *key, bool noreply) { char cmd[300]; snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : ""); execute(ascii_set_item(key, "0")); - for (int x= 1; x < 11; ++x) - { + for (int x = 1; x < 11; ++x) { execute(send_string(cmd)); if (noreply) execute(test_ascii_version()); - else - { + else { char buffer[80]; execute(receive_line(buffer, sizeof(buffer))); - int val= atoi(buffer); + int val = atoi(buffer); verify(val == x); } } @@ -1826,35 +1578,28 @@ static enum test_return test_ascii_incr_impl(const char* key, bool noreply) return TEST_PASS; } -static enum test_return test_ascii_incr(void) -{ +static enum test_return test_ascii_incr(void) { return test_ascii_incr_impl("test_ascii_incr", false); } -static enum test_return test_ascii_incr_noreply(void) -{ +static enum test_return test_ascii_incr_noreply(void) { return test_ascii_incr_impl("test_ascii_incr_noreply", true); } -static enum test_return test_ascii_decr_impl(const char* key, bool noreply) -{ +static enum test_return test_ascii_decr_impl(const char *key, bool noreply) { char cmd[300]; snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : ""); execute(ascii_set_item(key, "9")); - for (int x= 8; x > -1; --x) - { + for (int x = 8; x > -1; --x) { execute(send_string(cmd)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { char buffer[80]; execute(receive_line(buffer, sizeof(buffer))); - int val= atoi(buffer); + int val = atoi(buffer); verify(val == x); } } @@ -1863,12 +1608,9 @@ static enum test_return test_ascii_decr_impl(const char* key, bool noreply) /* verify that it doesn't wrap */ execute(send_string(cmd)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { char buffer[80]; execute(receive_line(buffer, sizeof(buffer))); } @@ -1877,19 +1619,15 @@ static enum test_return test_ascii_decr_impl(const char* key, bool noreply) return TEST_PASS; } -static enum test_return test_ascii_decr(void) -{ +static enum test_return test_ascii_decr(void) { return test_ascii_decr_impl("test_ascii_decr", false); } -static enum test_return test_ascii_decr_noreply(void) -{ +static enum test_return test_ascii_decr_noreply(void) { return test_ascii_decr_impl("test_ascii_decr_noreply", true); } - -static enum test_return test_ascii_flush_impl(const char *key, bool noreply) -{ +static enum test_return test_ascii_flush_impl(const char *key, bool noreply) { #if 0 /* Verify that the flush_all command handles unknown options */ /* Bug in the current memcached server! */ @@ -1900,13 +1638,10 @@ static enum test_return test_ascii_flush_impl(const char *key, bool noreply) execute(ascii_set_item(key, key)); execute(ascii_get_item(key, key, true)); - if (noreply) - { + if (noreply) { execute(send_string("flush_all noreply\r\n")); execute(test_ascii_version()); - } - else - { + } else { execute(send_string("flush_all\r\n")); execute(receive_response("OK\r\n")); } @@ -1916,96 +1651,73 @@ static enum test_return test_ascii_flush_impl(const char *key, bool noreply) return TEST_PASS; } -static enum test_return test_ascii_flush(void) -{ +static enum test_return test_ascii_flush(void) { return test_ascii_flush_impl("test_ascii_flush", false); } -static enum test_return test_ascii_flush_noreply(void) -{ +static enum test_return test_ascii_flush_noreply(void) { return test_ascii_flush_impl("test_ascii_flush_noreply", true); } -static enum test_return test_ascii_concat_impl(const char *key, - bool append, - bool noreply) -{ +static enum test_return test_ascii_concat_impl(const char *key, bool append, bool noreply) { const char *value; if (append) - value="hello"; + value = "hello"; else - value=" world"; + value = " world"; execute(ascii_set_item(key, value)); - if (append) - { - value=" world"; - } - else - { - value="hello"; + if (append) { + value = " world"; + } else { + value = "hello"; } char cmd[400]; - snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n", - append ? "append" : "prepend", - key, (unsigned int)strlen(value), noreply ? " noreply" : "", - value); + snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n", append ? "append" : "prepend", key, + (unsigned int) strlen(value), noreply ? " noreply" : "", value); execute(send_string(cmd)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { execute(receive_response("STORED\r\n")); } execute(ascii_get_item(key, "hello world", true)); - snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", - append ? "append" : "prepend", - key, (unsigned int)strlen(value), noreply ? " noreply" : "", - value); + snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", append ? "append" : "prepend", + key, (unsigned int) strlen(value), noreply ? " noreply" : "", value); execute(send_string(cmd)); - if (noreply) - { + if (noreply) { execute(test_ascii_version()); - } - else - { + } else { execute(receive_response("NOT_STORED\r\n")); } return TEST_PASS; } -static enum test_return test_ascii_append(void) -{ +static enum test_return test_ascii_append(void) { return test_ascii_concat_impl("test_ascii_append", true, false); } -static enum test_return test_ascii_prepend(void) -{ +static enum test_return test_ascii_prepend(void) { return test_ascii_concat_impl("test_ascii_prepend", false, false); } -static enum test_return test_ascii_append_noreply(void) -{ +static enum test_return test_ascii_append_noreply(void) { return test_ascii_concat_impl("test_ascii_append_noreply", true, true); } -static enum test_return test_ascii_prepend_noreply(void) -{ +static enum test_return test_ascii_prepend_noreply(void) { return test_ascii_concat_impl("test_ascii_prepend_noreply", false, true); } -static enum test_return test_ascii_stat(void) -{ +static enum test_return test_ascii_stat(void) { execute(send_string("stats noreply\r\n")); execute(receive_error_response()); execute(send_string("stats\r\n")); @@ -2017,138 +1729,122 @@ static enum test_return test_ascii_stat(void) return TEST_PASS_RECONNECT; } -typedef enum test_return(*TEST_FUNC)(void); +typedef enum test_return (*TEST_FUNC)(void); -struct testcase -{ +struct testcase { const char *description; TEST_FUNC function; }; -struct testcase testcases[]= { - { "ascii version", test_ascii_version }, - { "ascii quit", test_ascii_quit }, - { "ascii verbosity", test_ascii_verbosity }, - { "ascii set", test_ascii_set }, - { "ascii set noreply", test_ascii_set_noreply }, - { "ascii get", test_ascii_get }, - { "ascii gets", test_ascii_gets }, - { "ascii mget", test_ascii_mget }, - { "ascii flush", test_ascii_flush }, - { "ascii flush noreply", test_ascii_flush_noreply }, - { "ascii add", test_ascii_add }, - { "ascii add noreply", test_ascii_add_noreply }, - { "ascii replace", test_ascii_replace }, - { "ascii replace noreply", test_ascii_replace_noreply }, - { "ascii cas", test_ascii_cas }, - { "ascii cas noreply", test_ascii_cas_noreply }, - { "ascii delete", test_ascii_delete }, - { "ascii delete noreply", test_ascii_delete_noreply }, - { "ascii incr", test_ascii_incr }, - { "ascii incr noreply", test_ascii_incr_noreply }, - { "ascii decr", test_ascii_decr }, - { "ascii decr noreply", test_ascii_decr_noreply }, - { "ascii append", test_ascii_append }, - { "ascii append noreply", test_ascii_append_noreply }, - { "ascii prepend", test_ascii_prepend }, - { "ascii prepend noreply", test_ascii_prepend_noreply }, - { "ascii stat", test_ascii_stat }, - { "binary noop", test_binary_noop }, - { "binary quit", test_binary_quit }, - { "binary quitq", test_binary_quitq }, - { "binary set", test_binary_set }, - { "binary setq", test_binary_setq }, - { "binary flush", test_binary_flush }, - { "binary flushq", test_binary_flushq }, - { "binary add", test_binary_add }, - { "binary addq", test_binary_addq }, - { "binary replace", test_binary_replace }, - { "binary replaceq", test_binary_replaceq }, - { "binary delete", test_binary_delete }, - { "binary deleteq", test_binary_deleteq }, - { "binary get", test_binary_get }, - { "binary getq", test_binary_getq }, - { "binary getk", test_binary_getk }, - { "binary getkq", test_binary_getkq }, - { "binary incr", test_binary_incr }, - { "binary incrq", test_binary_incrq }, - { "binary decr", test_binary_decr }, - { "binary decrq", test_binary_decrq }, - { "binary version", test_binary_version }, - { "binary append", test_binary_append }, - { "binary appendq", test_binary_appendq }, - { "binary prepend", test_binary_prepend }, - { "binary prependq", test_binary_prependq }, - { "binary stat", test_binary_stat }, - { NULL, NULL} -}; - -struct test_type_st -{ +struct testcase testcases[] = {{"ascii version", test_ascii_version}, + {"ascii quit", test_ascii_quit}, + {"ascii verbosity", test_ascii_verbosity}, + {"ascii set", test_ascii_set}, + {"ascii set noreply", test_ascii_set_noreply}, + {"ascii get", test_ascii_get}, + {"ascii gets", test_ascii_gets}, + {"ascii mget", test_ascii_mget}, + {"ascii flush", test_ascii_flush}, + {"ascii flush noreply", test_ascii_flush_noreply}, + {"ascii add", test_ascii_add}, + {"ascii add noreply", test_ascii_add_noreply}, + {"ascii replace", test_ascii_replace}, + {"ascii replace noreply", test_ascii_replace_noreply}, + {"ascii cas", test_ascii_cas}, + {"ascii cas noreply", test_ascii_cas_noreply}, + {"ascii delete", test_ascii_delete}, + {"ascii delete noreply", test_ascii_delete_noreply}, + {"ascii incr", test_ascii_incr}, + {"ascii incr noreply", test_ascii_incr_noreply}, + {"ascii decr", test_ascii_decr}, + {"ascii decr noreply", test_ascii_decr_noreply}, + {"ascii append", test_ascii_append}, + {"ascii append noreply", test_ascii_append_noreply}, + {"ascii prepend", test_ascii_prepend}, + {"ascii prepend noreply", test_ascii_prepend_noreply}, + {"ascii stat", test_ascii_stat}, + {"binary noop", test_binary_noop}, + {"binary quit", test_binary_quit}, + {"binary quitq", test_binary_quitq}, + {"binary set", test_binary_set}, + {"binary setq", test_binary_setq}, + {"binary flush", test_binary_flush}, + {"binary flushq", test_binary_flushq}, + {"binary add", test_binary_add}, + {"binary addq", test_binary_addq}, + {"binary replace", test_binary_replace}, + {"binary replaceq", test_binary_replaceq}, + {"binary delete", test_binary_delete}, + {"binary deleteq", test_binary_deleteq}, + {"binary get", test_binary_get}, + {"binary getq", test_binary_getq}, + {"binary getk", test_binary_getk}, + {"binary getkq", test_binary_getkq}, + {"binary incr", test_binary_incr}, + {"binary incrq", test_binary_incrq}, + {"binary decr", test_binary_decr}, + {"binary decrq", test_binary_decrq}, + {"binary version", test_binary_version}, + {"binary append", test_binary_append}, + {"binary appendq", test_binary_appendq}, + {"binary prepend", test_binary_prepend}, + {"binary prependq", test_binary_prependq}, + {"binary stat", test_binary_stat}, + {NULL, NULL}}; + +struct test_type_st { bool ascii; bool binary; }; -int main(int argc, char **argv) -{ - static const char * const status_msg[]= {"[skip]", "[pass]", "[pass]", "[FAIL]"}; - struct test_type_st tests= { true, true }; - int total= 0; - int failed= 0; - const char *hostname= NULL; - const char *port= MEMCACHED_DEFAULT_PORT_STRING; +int main(int argc, char **argv) { + static const char *const status_msg[] = {"[skip]", "[pass]", "[pass]", "[FAIL]"}; + struct test_type_st tests = {true, true}; + int total = 0; + int failed = 0; + const char *hostname = NULL; + const char *port = MEMCACHED_DEFAULT_PORT_STRING; int cmd; - bool prompt= false; - const char *testname= NULL; - - + bool prompt = false; + const char *testname = NULL; - while ((cmd= getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF) - { + while ((cmd = getopt(argc, argv, "qt:vch:p:PT:?ab")) != EOF) { switch (cmd) { case 'a': - tests.ascii= true; - tests.binary= false; + tests.ascii = true; + tests.binary = false; break; case 'b': - tests.ascii= false; - tests.binary= true; + tests.ascii = false; + tests.binary = true; break; case 't': - timeout= atoi(optarg); - if (timeout == 0) - { + timeout = atoi(optarg); + if (timeout == 0) { fprintf(stderr, "Invalid timeout. Please specify a number for -t\n"); return EXIT_FAILURE; } break; - case 'v': verbose= true; - break; + case 'v': verbose = true; break; - case 'c': do_core= true; - break; + case 'c': do_core = true; break; - case 'h': hostname= optarg; - break; + case 'h': hostname = optarg; break; - case 'p': port= optarg; - break; + case 'p': port = optarg; break; - case 'q': - close_stdio(); - break; + case 'q': close_stdio(); break; - case 'P': prompt= true; - break; + case 'P': prompt = true; break; - case 'T': testname= optarg; - break; + case 'T': testname = optarg; break; default: - fprintf(stderr, "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n" + fprintf(stderr, + "Usage: %s [-h hostname] [-p port] [-c] [-v] [-t n] [-P] [-T testname]'\n" "\t-c\tGenerate coredump if a test fails\n" "\t-v\tVerbose test output (print out the assertion)\n" "\t-t n\tSet the timeout for io-operations to n seconds\n" @@ -2164,30 +1860,26 @@ int main(int argc, char **argv) } } - if (!hostname) - { + if (!hostname) { fprintf(stderr, "No hostname was provided.\n"); return EXIT_FAILURE; } initialize_sockets(); - sock= connect_server(hostname, port); - if (sock == INVALID_SOCKET) - { - fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", - hostname?:"(null)", port?:"(null)", strerror(get_socket_errno())); + sock = connect_server(hostname, port); + if (sock == INVALID_SOCKET) { + fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)", port ?: "(null)", + strerror(get_socket_errno())); return EXIT_FAILURE; } - for (int ii= 0; testcases[ii].description != NULL; ++ii) - { - if (testname != NULL && strcmp(testcases[ii].description, testname) != 0) - { + for (int ii = 0; testcases[ii].description != NULL; ++ii) { + if (testname != NULL && strcmp(testcases[ii].description, testname) != 0) { continue; } - if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) || - (testcases[ii].description[0] == 'b' && (tests.binary) == 0)) + if ((testcases[ii].description[0] == 'a' && (tests.ascii) == 0) + || (testcases[ii].description[0] == 'b' && (tests.binary) == 0)) { continue; } @@ -2195,20 +1887,16 @@ int main(int argc, char **argv) fprintf(stdout, "%-40s", testcases[ii].description); fflush(stdout); - if (prompt) - { + if (prompt) { fprintf(stdout, "\nPress when you are ready? "); char buffer[80] = {0}; if (fgets(buffer, sizeof(buffer), stdin) != NULL) { - if (strncmp(buffer, "skip", 4) == 0) - { - fprintf(stdout, "%-40s%s\n", testcases[ii].description, - status_msg[TEST_SKIP]); + if (strncmp(buffer, "skip", 4) == 0) { + fprintf(stdout, "%-40s%s\n", testcases[ii].description, status_msg[TEST_SKIP]); fflush(stdout); continue; } - if (strncmp(buffer, "quit", 4) == 0) - { + if (strncmp(buffer, "quit", 4) == 0) { exit(EXIT_SUCCESS); } } @@ -2217,37 +1905,29 @@ int main(int argc, char **argv) fflush(stdout); } - bool reconnect= false; - enum test_return ret= testcases[ii].function(); - if (ret == TEST_FAIL) - { - reconnect= true; + bool reconnect = false; + enum test_return ret = testcases[ii].function(); + if (ret == TEST_FAIL) { + reconnect = true; ++failed; - if (verbose) - { + if (verbose) { fprintf(stderr, "\n"); } - } - else if (ret == TEST_PASS_RECONNECT) - { - reconnect= true; + } else if (ret == TEST_PASS_RECONNECT) { + reconnect = true; } - if (ret == TEST_FAIL) - { + if (ret == TEST_FAIL) { fprintf(stderr, "%s\n", status_msg[ret]); - } - else - { + } else { fprintf(stdout, "%s\n", status_msg[ret]); } - if (reconnect) - { + if (reconnect) { closesocket(sock); - if ((sock= connect_server(hostname, port)) == INVALID_SOCKET) - { - fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname?:"(null)", port?:"(null)", strerror(get_socket_errno())); + if ((sock = connect_server(hostname, port)) == INVALID_SOCKET) { + fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)", + port ?: "(null)", strerror(get_socket_errno())); fprintf(stderr, "%d of %d tests failed\n", failed, total); return EXIT_FAILURE; } @@ -2255,12 +1935,9 @@ int main(int argc, char **argv) } closesocket(sock); - if (failed == 0) - { + if (failed == 0) { fprintf(stdout, "All tests passed\n"); - } - else - { + } else { fprintf(stderr, "%d of %d tests failed\n", failed, total); } diff --git a/src/bin/memcat.cc b/src/bin/memcat.cc index 269d1c1b..6ebcf7fd 100644 --- a/src/bin/memcat.cc +++ b/src/bin/memcat.cc @@ -1,14 +1,17 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -21,245 +24,195 @@ #include "utilities.h" -#define PROGRAM_NAME "memcat" +#define PROGRAM_NAME "memcat" #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout." - /* Prototypes */ void options_parse(int argc, char *argv[]); -static int opt_binary= 0; -static int opt_verbose= 0; -static int opt_displayflag= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; +static int opt_binary = 0; +static int opt_verbose = 0; +static int opt_displayflag = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; static char *opt_username; static char *opt_passwd; static char *opt_file; -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { char *string; size_t string_length; uint32_t flags; memcached_return_t rc; - int return_code= EXIT_SUCCESS; + int return_code = EXIT_SUCCESS; options_parse(argc, argv); initialize_sockets(); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - memcached_st* memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t)opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - while (optind < argc) - { - string= memcached_get(memc, argv[optind], strlen(argv[optind]), - &string_length, &flags, &rc); - if (rc == MEMCACHED_SUCCESS) - { - if (opt_displayflag) - { - if (opt_verbose) - { + while (optind < argc) { + string = memcached_get(memc, argv[optind], strlen(argv[optind]), &string_length, &flags, &rc); + if (rc == MEMCACHED_SUCCESS) { + if (opt_displayflag) { + if (opt_verbose) { std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl; } - } - else - { - if (opt_verbose) - { - std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl << "length: " << string_length << std::endl << "value: "; + } else { + if (opt_verbose) { + std::cout << "key: " << argv[optind] << std::endl + << "flags: " << flags << std::endl + << "length: " << string_length << std::endl + << "value: "; } - if (opt_file) - { - FILE *fp= fopen(opt_file, "w"); - if (fp == NULL) - { + if (opt_file) { + FILE *fp = fopen(opt_file, "w"); + if (fp == NULL) { perror("fopen"); - return_code= EXIT_FAILURE; + return_code = EXIT_FAILURE; break; } - size_t written= fwrite(string, 1, string_length, fp); - if (written != string_length) - { - std::cerr << "error writing file to file " << opt_file << " wrote " << written << ", should have written" << string_length << std::endl; - return_code= EXIT_FAILURE; + size_t written = fwrite(string, 1, string_length, fp); + if (written != string_length) { + std::cerr << "error writing file to file " << opt_file << " wrote " << written + << ", should have written" << string_length << std::endl; + return_code = EXIT_FAILURE; break; } - if (fclose(fp)) - { + if (fclose(fp)) { std::cerr << "error closing " << opt_file << std::endl; - return_code= EXIT_FAILURE; + return_code = EXIT_FAILURE; break; } - } - else - { + } else { std::cout.write(string, string_length); std::cout << std::endl; } free(string); } - } - else if (rc != MEMCACHED_NOTFOUND) - { - std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")"; - if (memcached_last_error_errno(memc)) - { - std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" << std::endl; + } else if (rc != MEMCACHED_NOTFOUND) { + std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")"; + if (memcached_last_error_errno(memc)) { + std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" + << std::endl; } std::cerr << std::endl; - return_code= EXIT_FAILURE; + return_code = EXIT_FAILURE; break; - } - else // Unknown Issue + } else // Unknown Issue { - std::cerr << "error on " << argv[optind] << "("<< memcached_strerror(NULL, rc) << ")" << std::endl; - return_code= EXIT_FAILURE; + std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(NULL, rc) << ")" + << std::endl; + return_code = EXIT_FAILURE; } optind++; } memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return return_code; } +void options_parse(int argc, char *argv[]) { + int option_index = 0; -void options_parse(int argc, char *argv[]) -{ - int option_index= 0; - - memcached_programs_help_st help_options[]= - { - {0}, + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {(OPTIONSTRING)"file", required_argument, NULL, OPT_FILE}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {(OPTIONSTRING) "file", required_argument, NULL, OPT_FILE}, {0, 0, 0, 0}, - }; + }; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; - case OPT_BINARY: - opt_binary = 1; - break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; - case OPT_VERSION: /* --version or -V */ - version_command(PROGRAM_NAME); + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { + case 0: break; + case OPT_BINARY: opt_binary = 1; break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; + case OPT_VERSION: /* --version or -V */ version_command(PROGRAM_NAME); break; case OPT_HELP: /* --help or -h */ help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; - case OPT_USERNAME: - opt_username= optarg; - break; - case OPT_PASSWD: - opt_passwd= optarg; - break; - case OPT_FILE: - opt_file= optarg; - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; + case OPT_HASH: opt_hash = strdup(optarg); break; + case OPT_USERNAME: opt_username = optarg; break; + case OPT_PASSWD: opt_passwd = optarg; break; + case OPT_FILE: opt_file = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_FAILURE); - default: - abort(); + default: abort(); } } } diff --git a/src/bin/memcp.cc b/src/bin/memcp.cc index 918ddac9..c0815dac 100644 --- a/src/bin/memcp.cc +++ b/src/bin/memcp.cc @@ -1,14 +1,17 @@ -/* LibMemcached - * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -21,102 +24,89 @@ #include #include #ifdef HAVE_STRINGS_H -#include +# include #endif #include #include #include - #include "libmemcached-1.0/memcached.h" #include "client_options.h" #include "utilities.h" -#define PROGRAM_NAME "memcp" +#define PROGRAM_NAME "memcp" #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster." /* Prototypes */ static void options_parse(int argc, char *argv[]); -static bool opt_binary= false; -static bool opt_udp= false; -static bool opt_buffer= false; -static int opt_verbose= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; -static int opt_method= OPT_SET; -static uint32_t opt_flags= 0; -static time_t opt_expires= 0; +static bool opt_binary = false; +static bool opt_udp = false; +static bool opt_buffer = false; +static int opt_verbose = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; +static int opt_method = OPT_SET; +static uint32_t opt_flags = 0; +static time_t opt_expires = 0; static char *opt_username; static char *opt_passwd; -static long strtol_wrapper(const char *nptr, int base, bool *error) -{ +static long strtol_wrapper(const char *nptr, int base, bool *error) { long val; char *endptr; - errno= 0; /* To distinguish success/failure after call */ - val= strtol(nptr, &endptr, base); + errno = 0; /* To distinguish success/failure after call */ + val = strtol(nptr, &endptr, base); /* Check for various possible errors */ - if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN)) - or (errno != 0 && val == 0)) - { - *error= true; + if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN)) or (errno != 0 && val == 0)) { + *error = true; return 0; } - if (endptr == nptr) - { - *error= true; + if (endptr == nptr) { + *error = true; return 0; } - *error= false; + *error = false; return val; } -int main(int argc, char *argv[]) -{ - +int main(int argc, char *argv[]) { options_parse(argc, argv); - if (optind >= argc) - { + if (optind >= argc) { fprintf(stderr, "Expected argument after options\n"); exit(EXIT_FAILURE); } initialize_sockets(); - memcached_st *memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); - if (opt_udp) - { - if (opt_verbose) - { + if (opt_udp) { + if (opt_verbose) { std::cout << "Enabling UDP" << std::endl; } - if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp))) - { + if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp))) { memcached_free(memc); std::cerr << "Could not enable UDP protocol." << std::endl; return EXIT_FAILURE; } } - if (opt_buffer) - { - if (opt_verbose) - { + if (opt_buffer) { + if (opt_verbose) { std::cout << "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl; } - if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer))) - { + if (memcached_failed( + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer))) { memcached_free(memc); std::cerr << "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl; return EXIT_FAILURE; @@ -125,13 +115,11 @@ int main(int argc, char *argv[]) process_hash_option(memc, opt_hash); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } #if 0 else if (argc >= 1 and argv[--argc]) @@ -140,16 +128,14 @@ int main(int argc, char *argv[]) } #endif - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } @@ -157,87 +143,76 @@ int main(int argc, char *argv[]) memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - int exit_code= EXIT_SUCCESS; - while (optind < argc) - { - int fd= open(argv[optind], O_RDONLY); - if (fd < 0) - { + int exit_code = EXIT_SUCCESS; + while (optind < argc) { + int fd = open(argv[optind], O_RDONLY); + if (fd < 0) { std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl; optind++; - exit_code= EXIT_FAILURE; + exit_code = EXIT_FAILURE; continue; } struct stat sbuf; - if (fstat(fd, &sbuf) == -1) - { + if (fstat(fd, &sbuf) == -1) { std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl; optind++; - exit_code= EXIT_FAILURE; + exit_code = EXIT_FAILURE; continue; } - char *ptr= rindex(argv[optind], '/'); - if (ptr) - { + char *ptr = rindex(argv[optind], '/'); + if (ptr) { ptr++; - } - else - { - ptr= argv[optind]; + } else { + ptr = argv[optind]; } - if (opt_verbose) - { - static const char *opstr[] = { "set", "add", "replace" }; + if (opt_verbose) { + static const char *opstr[] = {"set", "add", "replace"}; printf("op: %s\nsource file: %s\nlength: %lu\n" - "key: %s\nflags: %x\nexpires: %lu\n", - opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size, - ptr, opt_flags, (unsigned long)opt_expires); + "key: %s\nflags: %x\nexpires: %lu\n", + opstr[opt_method - OPT_SET], argv[optind], (unsigned long) sbuf.st_size, ptr, + opt_flags, (unsigned long) opt_expires); } // The file may be empty - char *file_buffer_ptr= NULL; - if (sbuf.st_size > 0) - { - if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL) - { + char *file_buffer_ptr = NULL; + if (sbuf.st_size > 0) { + if ((file_buffer_ptr = (char *) malloc(sizeof(char) * (size_t) sbuf.st_size)) == NULL) { std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl; close(fd); exit(EXIT_FAILURE); } ssize_t read_length; - if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1) - { - std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl; + if ((read_length = ::read(fd, file_buffer_ptr, (size_t) sbuf.st_size)) == -1) { + std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) + << ")" << std::endl; close(fd); free(file_buffer_ptr); exit(EXIT_FAILURE); } - if (read_length != sbuf.st_size) - { - std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl; + if (read_length != sbuf.st_size) { + std::cerr << "Failure while reading file. Read length was not equal to stat() length" + << std::endl; close(fd); free(file_buffer_ptr); exit(EXIT_FAILURE); @@ -245,29 +220,21 @@ int main(int argc, char *argv[]) } memcached_return_t rc; - if (opt_method == OPT_ADD) - { - rc= memcached_add(memc, ptr, strlen(ptr), - file_buffer_ptr, (size_t)sbuf.st_size, - opt_expires, opt_flags); - } - else if (opt_method == OPT_REPLACE) - { - rc= memcached_replace(memc, ptr, strlen(ptr), - file_buffer_ptr, (size_t)sbuf.st_size, - opt_expires, opt_flags); - } - else - { - rc= memcached_set(memc, ptr, strlen(ptr), - file_buffer_ptr, (size_t)sbuf.st_size, - opt_expires, opt_flags); + if (opt_method == OPT_ADD) { + rc = memcached_add(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t) sbuf.st_size, + opt_expires, opt_flags); + } else if (opt_method == OPT_REPLACE) { + rc = memcached_replace(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t) sbuf.st_size, + opt_expires, opt_flags); + } else { + rc = memcached_set(memc, ptr, strlen(ptr), file_buffer_ptr, (size_t) sbuf.st_size, + opt_expires, opt_flags); } - if (memcached_failed(rc)) - { - std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl; - exit_code= EXIT_FAILURE; + if (memcached_failed(rc)) { + std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) + << std::endl; + exit_code = EXIT_FAILURE; } ::free(file_buffer_ptr); @@ -275,171 +242,125 @@ int main(int argc, char *argv[]) optind++; } - if (opt_verbose) - { + if (opt_verbose) { std::cout << "Calling memcached_free()" << std::endl; } memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return exit_code; } -static void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +static void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP}, - {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG}, - {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, - {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET}, - {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD}, - {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "udp", no_argument, NULL, OPT_UDP}, + {(OPTIONSTRING) "buffer", no_argument, NULL, OPT_BUFFER}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "flag", required_argument, NULL, OPT_FLAG}, + {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING) "set", no_argument, NULL, OPT_SET}, + {(OPTIONSTRING) "add", no_argument, NULL, OPT_ADD}, + {(OPTIONSTRING) "replace", no_argument, NULL, OPT_REPLACE}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, {0, 0, 0, 0}, - }; + }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; + bool opt_version = false; + bool opt_help = false; + int option_index = 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_BINARY: - opt_binary= true; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose= OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose= OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; case OPT_FLAG: /* --flag */ - { - bool strtol_error; - opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error); - if (strtol_error == true) - { - fprintf(stderr, "Bad value passed via --flag\n"); - exit(1); - } + { + bool strtol_error; + opt_flags = (uint32_t) strtol_wrapper(optarg, 16, &strtol_error); + if (strtol_error == true) { + fprintf(stderr, "Bad value passed via --flag\n"); + exit(1); } - break; + } break; case OPT_EXPIRE: /* --expire */ - { - bool strtol_error; - opt_expires= (time_t)strtol_wrapper(optarg, 10, &strtol_error); - if (strtol_error == true) - { - fprintf(stderr, "Bad value passed via --expire\n"); - exit(1); - } + { + bool strtol_error; + opt_expires = (time_t) strtol_wrapper(optarg, 10, &strtol_error); + if (strtol_error == true) { + fprintf(stderr, "Bad value passed via --expire\n"); + exit(1); } - break; + } break; - case OPT_SET: - opt_method= OPT_SET; - break; + case OPT_SET: opt_method = OPT_SET; break; - case OPT_REPLACE: - opt_method= OPT_REPLACE; - break; + case OPT_REPLACE: opt_method = OPT_REPLACE; break; - case OPT_ADD: - opt_method= OPT_ADD; - break; + case OPT_ADD: opt_method = OPT_ADD; break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; + case OPT_HASH: opt_hash = strdup(optarg); break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; - case OPT_UDP: - opt_udp= true; - break; + case OPT_UDP: opt_udp = true; break; - case OPT_BUFFER: - opt_buffer= true; - break; + case OPT_BUFFER: opt_buffer = true; break; case '?': /* getopt_long already printed an error message. */ exit(1); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } diff --git a/src/bin/memdump.cc b/src/bin/memdump.cc index cf99027b..aa75f8a8 100644 --- a/src/bin/memdump.cc +++ b/src/bin/memdump.cc @@ -1,14 +1,17 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -30,68 +33,58 @@ #include "client_options.h" #include "utilities.h" -#define PROGRAM_NAME "memdump" +#define PROGRAM_NAME "memdump" #define PROGRAM_DESCRIPTION "Dump all values from one or many servers." /* Prototypes */ static void options_parse(int argc, char *argv[]); -static bool opt_binary=0; -static int opt_verbose= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; +static bool opt_binary = 0; +static int opt_verbose = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; static char *opt_username; static char *opt_passwd; /* Print the keys and counter how many were found */ -static memcached_return_t key_printer(const memcached_st *, - const char *key, size_t key_length, - void *) -{ +static memcached_return_t key_printer(const memcached_st *, const char *key, size_t key_length, + void *) { std::cout.write(key, key_length); std::cout << std::endl; return MEMCACHED_SUCCESS; } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { memcached_dump_fn callbacks[1]; - callbacks[0]= &key_printer; + callbacks[0] = &key_printer; options_parse(argc, argv); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); - } - else if (argc > 1 and argv[--argc]) - { - opt_servers= strdup(argv[argc]); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); + } else if (argc > 1 and argv[--argc]) { + opt_servers = strdup(argv[argc]); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - memcached_st *memc= memcached_create(NULL); - if (memc == NULL) - { + memcached_st *memc = memcached_create(NULL); + if (memc == NULL) { std::cerr << "Could not allocate a memcached_st structure.\n" << std::endl; return EXIT_FAILURE; } @@ -99,140 +92,105 @@ int main(int argc, char *argv[]) memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t)opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - memcached_return_t rc= memcached_dump(memc, callbacks, NULL, 1); + memcached_return_t rc = memcached_dump(memc, callbacks, NULL, 1); - int exit_code= EXIT_SUCCESS; - if (memcached_failed(rc)) - { - if (opt_verbose) - { + int exit_code = EXIT_SUCCESS; + if (memcached_failed(rc)) { + if (opt_verbose) { std::cerr << "Failed to dump keys: " << memcached_last_error_message(memc) << std::endl; } - exit_code= EXIT_FAILURE; + exit_code = EXIT_FAILURE; } memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return exit_code; } -static void options_parse(int argc, char *argv[]) -{ - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0} - }; - - int option_index= 0; - bool opt_version= false; - bool opt_help= false; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - - if (option_rv == -1) break; - - switch (option_rv) - { - case 0: +static void options_parse(int argc, char *argv[]) { + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}}; + + int option_index = 0; + bool opt_version = false; + bool opt_help = false; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + + if (option_rv == -1) break; - case OPT_BINARY: - opt_binary= true; - break; + switch (option_rv) { + case 0: break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose= OPT_VERBOSE; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose= OPT_DEBUG; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_VERSION: /* --version or -V */ - opt_verbose= true; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_VERSION: /* --version or -V */ opt_verbose = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_HASH: opt_hash = strdup(optarg); break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_PASSWD: opt_passwd = optarg; break; + + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(1); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL); exit(EXIT_SUCCESS); } diff --git a/src/bin/memerror.cc b/src/bin/memerror.cc index 96c068f5..c35684ad 100644 --- a/src/bin/memerror.cc +++ b/src/bin/memerror.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -25,43 +29,34 @@ #include "utilities.h" -#define PROGRAM_NAME "memerror" +#define PROGRAM_NAME "memerror" #define PROGRAM_DESCRIPTION "Translate a memcached errror code into a string." - /* Prototypes */ void options_parse(int argc, char *argv[]); -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); - if (argc < 2) - { + if (argc < 2) { return EXIT_FAILURE; } - while (optind < argc) - { - errno= 0; + while (optind < argc) { + errno = 0; char *nptr; - unsigned long value= strtoul(argv[optind], &nptr, 10); + unsigned long value = strtoul(argv[optind], &nptr, 10); - if ((errno != 0) or - (nptr == argv[optind] and value == 0) or - (value == ULONG_MAX and errno == ERANGE) or - (value == 0 and errno == EINVAL)) + if ((errno != 0) or (nptr == argv[optind] and value == 0) + or (value == ULONG_MAX and errno == ERANGE) or (value == 0 and errno == EINVAL)) { std::cerr << "strtoul() was unable to parse given value" << std::endl; return EXIT_FAILURE; } - if (value < MEMCACHED_MAXIMUM_RETURN) - { - std::cout << memcached_strerror(NULL, (memcached_return_t)value) << std::endl; - } - else - { + if (value < MEMCACHED_MAXIMUM_RETURN) { + std::cout << memcached_strerror(NULL, (memcached_return_t) value) << std::endl; + } else { std::cerr << memcached_strerror(NULL, MEMCACHED_MAXIMUM_RETURN) << std::endl; return EXIT_FAILURE; } @@ -72,57 +67,43 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } - -void options_parse(int argc, char *argv[]) -{ - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, +void options_parse(int argc, char *argv[]) { + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, {0, 0, 0, 0}, - }; - - bool opt_version= false; - bool opt_help= false; - int option_index= 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) - { + }; + + bool opt_version = false; + bool opt_help = false; + int option_index = 0; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) { break; } - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_FAILURE); - default: - exit(EXIT_FAILURE); + default: exit(EXIT_FAILURE); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL); exit(EXIT_SUCCESS); } diff --git a/src/bin/memexist.cc b/src/bin/memexist.cc index d08492ee..eec39abf 100644 --- a/src/bin/memexist.cc +++ b/src/bin/memexist.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -21,101 +25,86 @@ #include "client_options.h" #include "utilities.h" -static int opt_binary= 0; -static int opt_verbose= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; +static int opt_binary = 0; +static int opt_verbose = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; static char *opt_username; static char *opt_passwd; -#define PROGRAM_NAME "memexist" +#define PROGRAM_NAME "memexist" #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster." /* Prototypes */ static void options_parse(int argc, char *argv[]); -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); initialize_sockets(); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - memcached_st* memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t) opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - int return_code= EXIT_SUCCESS; + int return_code = EXIT_SUCCESS; - while (optind < argc) - { - memcached_return_t rc= memcached_exist(memc, argv[optind], strlen(argv[optind])); + while (optind < argc) { + memcached_return_t rc = memcached_exist(memc, argv[optind], strlen(argv[optind])); - if (rc == MEMCACHED_NOTFOUND) - { - if (opt_verbose) - { + if (rc == MEMCACHED_NOTFOUND) { + if (opt_verbose) { std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl; } - return_code= EXIT_FAILURE; - } - else if (memcached_failed(rc)) - { - if (opt_verbose) - { - std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl; + return_code = EXIT_FAILURE; + } else if (memcached_failed(rc)) { + if (opt_verbose) { + std::cerr << "Fatal error for key \"" << argv[optind] + << "\" :" << memcached_last_error_message(memc) << std::endl; } - return_code= EXIT_FAILURE; - } - else // success + return_code = EXIT_FAILURE; + } else // success { - if (opt_verbose) - { + if (opt_verbose) { std::cout << "Found key " << argv[optind] << std::endl; } } @@ -125,116 +114,83 @@ int main(int argc, char *argv[]) memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return return_code; } - -static void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +static void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; + bool opt_version = false; + bool opt_help = false; + int option_index = 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) - { + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) { break; } - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_BINARY: - opt_binary = 1; - break; + case OPT_BINARY: opt_binary = 1; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; + case OPT_HASH: opt_hash = strdup(optarg); break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_SUCCESS); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } diff --git a/src/bin/memflush.cc b/src/bin/memflush.cc index 49d691b8..baa552d1 100644 --- a/src/bin/memflush.cc +++ b/src/bin/memflush.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -22,42 +26,37 @@ #include "client_options.h" #include "utilities.h" -static int opt_binary= 0; -static int opt_verbose= 0; -static time_t opt_expire= 0; -static char *opt_servers= NULL; +static int opt_binary = 0; +static int opt_verbose = 0; +static time_t opt_expire = 0; +static char *opt_servers = NULL; static char *opt_username; static char *opt_passwd; -#define PROGRAM_NAME "memflush" +#define PROGRAM_NAME "memflush" #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers." /* Prototypes */ void options_parse(int argc, char *argv[]); -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; free(opt_servers); return EXIT_FAILURE; @@ -65,24 +64,21 @@ int main(int argc, char *argv[]) free(opt_servers); - memcached_st *memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t) opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; @@ -90,8 +86,7 @@ int main(int argc, char *argv[]) } memcached_return_t rc = memcached_flush(memc, opt_expire); - if (rc != MEMCACHED_SUCCESS) - { + if (rc != MEMCACHED_SUCCESS) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; @@ -101,99 +96,72 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } - -void options_parse(int argc, char *argv[]) -{ - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0}, +void options_parse(int argc, char *argv[]) { + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) break; - switch (option_rv) - { - case 0: + bool opt_version = false; + bool opt_help = false; + int option_index = 0; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { + case 0: break; - case OPT_BINARY: - opt_binary= true; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose= OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose= OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; case OPT_EXPIRE: /* --expire */ - errno= 0; - opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + opt_expire = (time_t) strtoll(optarg, (char **) NULL, 10); + if (errno != 0) { std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl; exit(EXIT_FAILURE); } break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_FAILURE); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL); exit(EXIT_SUCCESS); } diff --git a/src/bin/memparse.cc b/src/bin/memparse.cc index 341f5145..d08c9d99 100644 --- a/src/bin/memparse.cc +++ b/src/bin/memparse.cc @@ -1,39 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -43,23 +21,18 @@ #include "libmemcached-1.0/memcached.h" -int main(int argc, char *argv[]) -{ - - if (argc < 2) - { +int main(int argc, char *argv[]) { + if (argc < 2) { std::cerr << "No arguments provided." << std::endl; return EXIT_FAILURE; } - for (int x= 1; x < argc; x++) - { + for (int x = 1; x < argc; x++) { char buffer[BUFSIZ]; memcached_return_t rc; - rc= libmemcached_check_configuration(argv[x], strlen(argv[x]), buffer, sizeof(buffer)); + rc = libmemcached_check_configuration(argv[x], strlen(argv[x]), buffer, sizeof(buffer)); - if (rc != MEMCACHED_SUCCESS) - { + if (rc != MEMCACHED_SUCCESS) { std::cerr << "Failed to parse argument #" << x << " " << argv[x] << std::endl; std::cerr << buffer << std::endl; return EXIT_FAILURE; diff --git a/src/bin/memping.cc b/src/bin/memping.cc index f7962ae0..184647b2 100644 --- a/src/bin/memping.cc +++ b/src/bin/memping.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -24,63 +28,55 @@ #include -static bool opt_binary= false; -static int opt_verbose= 0; -static time_t opt_expire= 0; -static char *opt_servers= NULL; +static bool opt_binary = false; +static int opt_verbose = 0; +static time_t opt_expire = 0; +static char *opt_servers = NULL; static char *opt_username; static char *opt_passwd; -#define PROGRAM_NAME "memping" +#define PROGRAM_NAME "memping" #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive" /* Prototypes */ void options_parse(int argc, char *argv[]); -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - - if (opt_servers == NULL) - { + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - int exit_code= EXIT_SUCCESS; - memcached_server_st *servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + int exit_code = EXIT_SUCCESS; + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; - exit_code= EXIT_FAILURE; - } - else - { - for (uint32_t x= 0; x < memcached_server_list_count(servers); x++) - { + exit_code = EXIT_FAILURE; + } else { + for (uint32_t x = 0; x < memcached_server_list_count(servers); x++) { memcached_return_t instance_rc; - const char *hostname= servers[x].hostname; - in_port_t port= servers[x].port; + const char *hostname = servers[x].hostname; + in_port_t port = servers[x].port; - if (opt_verbose) - { + if (opt_verbose) { std::cout << "Trying to ping " << hostname << ":" << port << std::endl; } if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false) { - std::cerr << "Failed to ping " << hostname << ":" << port << " " << memcached_strerror(NULL, instance_rc) << std::endl; - exit_code= EXIT_FAILURE; + std::cerr << "Failed to ping " << hostname << ":" << port << " " + << memcached_strerror(NULL, instance_rc) << std::endl; + exit_code = EXIT_FAILURE; } } } @@ -91,106 +87,82 @@ int main(int argc, char *argv[]) return exit_code; } - -void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - - if (option_rv == -1) break; + bool opt_version = false; + bool opt_help = false; + int option_index = 0; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - switch (option_rv) - { - case 0: + if (option_rv == -1) break; - case OPT_BINARY: - opt_binary= true; - break; + switch (option_rv) { + case 0: break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_VERSION: /* --version or -V */ - version_command(PROGRAM_NAME); - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; + + case OPT_VERSION: /* --version or -V */ version_command(PROGRAM_NAME); break; case OPT_HELP: /* --help or -h */ help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; case OPT_EXPIRE: /* --expire */ - errno= 0; - opt_expire= time_t(strtoll(optarg, (char **)NULL, 10)); - if (errno != 0) - { + errno = 0; + opt_expire = time_t(strtoll(optarg, (char **) NULL, 10)); + if (errno != 0) { std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl; exit(EXIT_FAILURE); } break; case OPT_USERNAME: - opt_username= optarg; - opt_binary= true; + opt_username = optarg; + opt_binary = true; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(1); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } diff --git a/src/bin/memrm.cc b/src/bin/memrm.cc index fc6df6c0..7bd35132 100644 --- a/src/bin/memrm.cc +++ b/src/bin/memrm.cc @@ -1,14 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -22,103 +26,87 @@ #include "client_options.h" #include "utilities.h" -static int opt_binary= 0; -static int opt_verbose= 0; -static time_t opt_expire= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; +static int opt_binary = 0; +static int opt_verbose = 0; +static time_t opt_expire = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; static char *opt_username; static char *opt_passwd; -#define PROGRAM_NAME "memrm" +#define PROGRAM_NAME "memrm" #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster." /* Prototypes */ static void options_parse(int argc, char *argv[]); -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); initialize_sockets(); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - memcached_st* memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t) opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - int return_code= EXIT_SUCCESS; + int return_code = EXIT_SUCCESS; - while (optind < argc) - { - memcached_return_t rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); + while (optind < argc) { + memcached_return_t rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire); - if (rc == MEMCACHED_NOTFOUND) - { - if (opt_verbose) - { + if (rc == MEMCACHED_NOTFOUND) { + if (opt_verbose) { std::cerr << "Could not find key \"" << argv[optind] << "\"" << std::endl; } - } - else if (memcached_fatal(rc)) - { - if (opt_verbose) - { - std::cerr << "Failed to delete key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl; + } else if (memcached_fatal(rc)) { + if (opt_verbose) { + std::cerr << "Failed to delete key \"" << argv[optind] + << "\" :" << memcached_last_error_message(memc) << std::endl; } - return_code= EXIT_FAILURE; - } - else // success + return_code = EXIT_FAILURE; + } else // success { - if (opt_verbose) - { + if (opt_verbose) { std::cout << "Deleted key " << argv[optind]; - if (opt_expire) - { + if (opt_expire) { std::cout << " expires: " << opt_expire << std::endl; } std::cout << std::endl; @@ -130,127 +118,93 @@ int main(int argc, char *argv[]) memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return return_code; } - -static void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +static void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; + bool opt_version = false; + bool opt_help = false; + int option_index = 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) - { + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) { break; } - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_BINARY: - opt_binary = 1; - break; + case OPT_BINARY: opt_binary = 1; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; case OPT_EXPIRE: /* --expire */ - errno= 0; - opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + opt_expire = (time_t) strtoll(optarg, (char **) NULL, 10); + if (errno != 0) { std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl; exit(EXIT_FAILURE); } break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; + case OPT_HASH: opt_hash = strdup(optarg); break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_SUCCESS); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } diff --git a/src/bin/memslap.cc b/src/bin/memslap.cc index 59fef02a..b7b606e9 100644 --- a/src/bin/memslap.cc +++ b/src/bin/memslap.cc @@ -1,40 +1,17 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -62,13 +39,13 @@ #include "generator.h" #include "execute.h" -#define DEFAULT_INITIAL_LOAD 10000 +#define DEFAULT_INITIAL_LOAD 10000 #define DEFAULT_EXECUTE_NUMBER 10000 -#define DEFAULT_CONCURRENCY 1 +#define DEFAULT_CONCURRENCY 1 #define VALUE_BYTES 4096 -#define PROGRAM_NAME "memslap" +#define PROGRAM_NAME "memslap" #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers." /* Global Thread counter */ @@ -77,11 +54,7 @@ pthread_mutex_t sleeper_mutex; pthread_cond_t sleep_threshhold; /* Types */ -enum test_t { - SET_TEST, - GET_TEST, - MGET_TEST -}; +enum test_t { SET_TEST, GET_TEST, MGET_TEST }; struct thread_context_st { unsigned int key_count; @@ -93,31 +66,24 @@ struct thread_context_st { size_t *key_lengths; test_t test; memcached_st *memc; - const memcached_st* root; - - thread_context_st(const memcached_st* memc_arg, test_t test_arg) : - key_count(0), - initial_pairs(NULL), - initial_number(0), - execute_pairs(NULL), - execute_number(0), - keys(0), - key_lengths(NULL), - test(test_arg), - memc(NULL), - root(memc_arg) - { - } - - void init() - { - memc= memcached_clone(NULL, root); - } - - ~thread_context_st() - { - if (execute_pairs) - { + const memcached_st *root; + + thread_context_st(const memcached_st *memc_arg, test_t test_arg) + : key_count(0) + , initial_pairs(NULL) + , initial_number(0) + , execute_pairs(NULL) + , execute_number(0) + , keys(0) + , key_lengths(NULL) + , test(test_arg) + , memc(NULL) + , root(memc_arg) {} + + void init() { memc = memcached_clone(NULL, root); } + + ~thread_context_st() { + if (execute_pairs) { pairs_free(execute_pairs); } memcached_free(memc); @@ -130,64 +96,58 @@ struct conclusions_st { unsigned int rows_loaded; unsigned int rows_read; - conclusions_st() : - load_time(0), - read_time(0), - rows_loaded(0), - rows_read() - { } + conclusions_st() + : load_time(0) + , read_time(0) + , rows_loaded(0) + , rows_read() {} }; /* Prototypes */ void options_parse(int argc, char *argv[]); void conclusions_print(conclusions_st *conclusion); void scheduler(memcached_server_st *servers, conclusions_st *conclusion); -pairs_st *load_create_data(memcached_st *memc, unsigned int number_of, - unsigned int *actual_loaded); +pairs_st *load_create_data(memcached_st *memc, unsigned int number_of, unsigned int *actual_loaded); void flush_all(memcached_st *memc); -static bool opt_binary= 0; -static int opt_verbose= 0; -static int opt_flush= 0; -static int opt_non_blocking_io= 0; -static int opt_tcp_nodelay= 0; -static unsigned int opt_execute_number= 0; -static unsigned int opt_createial_load= 0; -static unsigned int opt_concurrency= 0; -static int opt_displayflag= 0; -static char *opt_servers= NULL; -static bool opt_udp_io= false; -test_t opt_test= SET_TEST; +static bool opt_binary = 0; +static int opt_verbose = 0; +static int opt_flush = 0; +static int opt_non_blocking_io = 0; +static int opt_tcp_nodelay = 0; +static unsigned int opt_execute_number = 0; +static unsigned int opt_createial_load = 0; +static unsigned int opt_concurrency = 0; +static int opt_displayflag = 0; +static char *opt_servers = NULL; +static bool opt_udp_io = false; +test_t opt_test = SET_TEST; extern "C" { -static __attribute__((noreturn)) void *run_task(void *p) -{ - thread_context_st *context= (thread_context_st *)p; +static __attribute__((noreturn)) void *run_task(void *p) { + thread_context_st *context = (thread_context_st *) p; context->init(); pthread_mutex_lock(&sleeper_mutex); - while (master_wakeup) - { + while (master_wakeup) { pthread_cond_wait(&sleep_threshhold, &sleeper_mutex); } pthread_mutex_unlock(&sleeper_mutex); /* Do Stuff */ - switch (context->test) - { + switch (context->test) { case SET_TEST: assert(context->execute_pairs); execute_set(context->memc, context->execute_pairs, context->execute_number); break; - case GET_TEST: - execute_get(context->memc, context->initial_pairs, context->initial_number); - break; + case GET_TEST: execute_get(context->memc, context->initial_pairs, context->initial_number); break; case MGET_TEST: - execute_mget(context->memc, (const char*const*)context->keys, context->key_lengths, context->initial_number); + execute_mget(context->memc, (const char *const *) context->keys, context->key_lengths, + context->initial_number); break; } @@ -195,36 +155,29 @@ static __attribute__((noreturn)) void *run_task(void *p) pthread_exit(0); } - } - -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { conclusions_st conclusion; - srandom((unsigned int)time(NULL)); + srandom((unsigned int) time(NULL)); options_parse(argc, argv); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st *servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } @@ -232,77 +185,65 @@ int main(int argc, char *argv[]) pthread_mutex_init(&sleeper_mutex, NULL); pthread_cond_init(&sleep_threshhold, NULL); - int error_code= EXIT_SUCCESS; + int error_code = EXIT_SUCCESS; try { scheduler(servers, &conclusion); - } - catch(std::exception& e) - { + } catch (std::exception &e) { std::cerr << "Died with exception: " << e.what() << std::endl; - error_code= EXIT_FAILURE; + error_code = EXIT_FAILURE; } free(opt_servers); - (void)pthread_mutex_destroy(&sleeper_mutex); - (void)pthread_cond_destroy(&sleep_threshhold); + (void) pthread_mutex_destroy(&sleeper_mutex); + (void) pthread_cond_destroy(&sleep_threshhold); conclusions_print(&conclusion); memcached_server_list_free(servers); return error_code; } -void scheduler(memcached_server_st *servers, conclusions_st *conclusion) -{ - unsigned int actual_loaded= 0; /* Fix warning */ +void scheduler(memcached_server_st *servers, conclusions_st *conclusion) { + unsigned int actual_loaded = 0; /* Fix warning */ struct timeval start_time, end_time; - pairs_st *pairs= NULL; + pairs_st *pairs = NULL; - memcached_st *memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); memcached_server_push(memc, servers); /* We need to set udp behavior before adding servers to the client */ - if (opt_udp_io) - { - if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp_io))) - { + if (opt_udp_io) { + if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp_io))) { std::cerr << "Failed to enable UDP." << std::endl; memcached_free(memc); exit(EXIT_FAILURE); } } - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t)opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_flush) - { + if (opt_flush) { flush_all(memc); } - if (opt_createial_load) - { - pairs= load_create_data(memc, opt_createial_load, &actual_loaded); + if (opt_createial_load) { + pairs = load_create_data(memc, opt_createial_load, &actual_loaded); } - char **keys= static_cast(calloc(actual_loaded, sizeof(char*))); - size_t *key_lengths= static_cast(calloc(actual_loaded, sizeof(size_t))); + char **keys = static_cast(calloc(actual_loaded, sizeof(char *))); + size_t *key_lengths = static_cast(calloc(actual_loaded, sizeof(size_t))); - if (keys == NULL or key_lengths == NULL) - { + if (keys == NULL or key_lengths == NULL) { free(keys); free(key_lengths); - keys= NULL; - key_lengths= NULL; - } - else - { - for (uint32_t x= 0; x < actual_loaded; ++x) - { - keys[x]= pairs[x].key; - key_lengths[x]= pairs[x].key_length; + keys = NULL; + key_lengths = NULL; + } else { + for (uint32_t x = 0; x < actual_loaded; ++x) { + keys[x] = pairs[x].key; + key_lengths[x] = pairs[x].key_length; } } @@ -316,233 +257,195 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion) } pthread_mutex_lock(&sleeper_mutex); - master_wakeup= 1; + master_wakeup = 1; pthread_mutex_unlock(&sleeper_mutex); - pthread_t *threads= new (std::nothrow) pthread_t[opt_concurrency]; + pthread_t *threads = new (std::nothrow) pthread_t[opt_concurrency]; - if (threads == NULL) - { + if (threads == NULL) { exit(EXIT_FAILURE); } - for (uint32_t x= 0; x < opt_concurrency; x++) - { - thread_context_st *context= new thread_context_st(memc, opt_test); - context->test= opt_test; - - context->initial_pairs= pairs; - context->initial_number= actual_loaded; - context->keys= keys; - context->key_lengths= key_lengths; - - if (opt_test == SET_TEST) - { - context->execute_pairs= pairs_generate(opt_execute_number, VALUE_BYTES); - context->execute_number= opt_execute_number; + for (uint32_t x = 0; x < opt_concurrency; x++) { + thread_context_st *context = new thread_context_st(memc, opt_test); + context->test = opt_test; + + context->initial_pairs = pairs; + context->initial_number = actual_loaded; + context->keys = keys; + context->key_lengths = key_lengths; + + if (opt_test == SET_TEST) { + context->execute_pairs = pairs_generate(opt_execute_number, VALUE_BYTES); + context->execute_number = opt_execute_number; } /* now you create the thread */ - if (pthread_create(threads +x, NULL, run_task, (void *)context) != 0) - { - fprintf(stderr,"Could not create thread\n"); + if (pthread_create(threads + x, NULL, run_task, (void *) context) != 0) { + fprintf(stderr, "Could not create thread\n"); exit(1); } } pthread_mutex_lock(&sleeper_mutex); - master_wakeup= 0; + master_wakeup = 0; pthread_mutex_unlock(&sleeper_mutex); pthread_cond_broadcast(&sleep_threshhold); gettimeofday(&start_time, NULL); - for (uint32_t x= 0; x < opt_concurrency; x++) - { + for (uint32_t x = 0; x < opt_concurrency; x++) { void *retval; pthread_join(threads[x], &retval); } - delete [] threads; + delete[] threads; gettimeofday(&end_time, NULL); - conclusion->load_time= timedif(end_time, start_time); - conclusion->read_time= timedif(end_time, start_time); + conclusion->load_time = timedif(end_time, start_time); + conclusion->read_time = timedif(end_time, start_time); free(keys); free(key_lengths); pairs_free(pairs); memcached_free(memc); } -void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER}, - {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG}, - {(OPTIONSTRING)"flush", no_argument, &opt_flush, OPT_FLUSH}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */ - {(OPTIONSTRING)"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY}, - {(OPTIONSTRING)"test", required_argument, NULL, OPT_SLAP_TEST}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP}, + static struct option long_options[] = { + {(OPTIONSTRING) "concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER}, + {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG}, + {(OPTIONSTRING) "flush", no_argument, &opt_flush, OPT_FLUSH}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "initial-load", required_argument, NULL, + OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */ + {(OPTIONSTRING) "non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY}, + {(OPTIONSTRING) "test", required_argument, NULL, OPT_SLAP_TEST}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "udp", no_argument, NULL, OPT_UDP}, {0, 0, 0, 0}, - }; - - bool opt_help= false; - bool opt_version= false; - int option_index= 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + }; - if (option_rv == -1) break; + bool opt_help = false; + bool opt_version = false; + int option_index = 0; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - switch (option_rv) - { - case 0: + if (option_rv == -1) break; + switch (option_rv) { + case 0: break; + case OPT_UDP: - if (opt_test == GET_TEST) - { - fprintf(stderr, "You can not run a get test in UDP mode. UDP mode " - "does not currently support get ops.\n"); + if (opt_test == GET_TEST) { + fprintf(stderr, + "You can not run a get test in UDP mode. UDP mode " + "does not currently support get ops.\n"); exit(1); } - opt_udp_io= true; + opt_udp_io = true; break; - case OPT_BINARY: - opt_binary= true; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose= OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose= OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; case OPT_SLAP_TEST: - if (strcmp(optarg, "get") == 0) - { - if (opt_udp_io == 1) - { - fprintf(stderr, "You can not run a get test in UDP mode. UDP mode " + if (strcmp(optarg, "get") == 0) { + if (opt_udp_io == 1) { + fprintf(stderr, + "You can not run a get test in UDP mode. UDP mode " "does not currently support get ops.\n"); exit(EXIT_FAILURE); } - opt_test= GET_TEST ; - } - else if (strcmp(optarg, "set") == 0) - { - opt_test= SET_TEST; - } - else if (strcmp(optarg, "mget") == 0) - { - opt_test= MGET_TEST; - } - else - { + opt_test = GET_TEST; + } else if (strcmp(optarg, "set") == 0) { + opt_test = SET_TEST; + } else if (strcmp(optarg, "mget") == 0) { + opt_test = MGET_TEST; + } else { fprintf(stderr, "Your test, %s, is not a known test\n", optarg); exit(EXIT_FAILURE); } break; case OPT_SLAP_CONCURRENCY: - errno= 0; - opt_concurrency= (unsigned int)strtoul(optarg, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + opt_concurrency = (unsigned int) strtoul(optarg, (char **) NULL, 10); + if (errno != 0) { fprintf(stderr, "Invalid value for concurrency: %s\n", optarg); exit(EXIT_FAILURE); } break; case OPT_SLAP_EXECUTE_NUMBER: - errno= 0; - opt_execute_number= (unsigned int)strtoul(optarg, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + opt_execute_number = (unsigned int) strtoul(optarg, (char **) NULL, 10); + if (errno != 0) { fprintf(stderr, "Invalid value for execute: %s\n", optarg); exit(EXIT_FAILURE); } break; case OPT_SLAP_INITIAL_LOAD: - errno= 0; - opt_createial_load= (unsigned int)strtoul(optarg, (char **)NULL, 10); - if (errno != 0) - { + errno = 0; + opt_createial_load = (unsigned int) strtoul(optarg, (char **) NULL, 10); + if (errno != 0) { fprintf(stderr, "Invalid value for initial load: %s\n", optarg); exit(EXIT_FAILURE); } break; - case OPT_QUIET: - close_stdio(); - break; - + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_FAILURE); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } if ((opt_test == GET_TEST or opt_test == MGET_TEST) and opt_createial_load == 0) - opt_createial_load= DEFAULT_INITIAL_LOAD; + opt_createial_load = DEFAULT_INITIAL_LOAD; if (opt_execute_number == 0) - opt_execute_number= DEFAULT_EXECUTE_NUMBER; + opt_execute_number = DEFAULT_EXECUTE_NUMBER; if (opt_concurrency == 0) - opt_concurrency= DEFAULT_CONCURRENCY; + opt_concurrency = DEFAULT_CONCURRENCY; } -void conclusions_print(conclusions_st *conclusion) -{ +void conclusions_print(conclusions_st *conclusion) { printf("\tThreads connecting to servers %u\n", opt_concurrency); #ifdef NOT_FINISHED printf("\tLoaded %u rows\n", conclusion->rows_loaded); @@ -556,20 +459,18 @@ void conclusions_print(conclusions_st *conclusion) conclusion->read_time % 1000); } -void flush_all(memcached_st *memc) -{ +void flush_all(memcached_st *memc) { memcached_flush(memc, 0); } pairs_st *load_create_data(memcached_st *memc, unsigned int number_of, - unsigned int *actual_loaded) -{ - memcached_st *memc_clone= memcached_clone(NULL, memc); + unsigned int *actual_loaded) { + memcached_st *memc_clone = memcached_clone(NULL, memc); /* We always used non-blocking IO for load since it is faster */ memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0); - pairs_st *pairs= pairs_generate(number_of, VALUE_BYTES); - *actual_loaded= execute_set(memc_clone, pairs, number_of); + pairs_st *pairs = pairs_generate(number_of, VALUE_BYTES); + *actual_loaded = execute_set(memc_clone, pairs, number_of); memcached_free(memc_clone); diff --git a/src/bin/memstat.cc b/src/bin/memstat.cc index 48ba3b6a..b7f75679 100644 --- a/src/bin/memstat.cc +++ b/src/bin/memstat.cc @@ -1,17 +1,18 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - * Authors: - * Brian Aker - * Toru Maesaka - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + #include "mem_config.h" #include @@ -31,156 +32,133 @@ #include "client_options.h" #include "utilities.h" -#define PROGRAM_NAME "memstat" +#define PROGRAM_NAME "memstat" #define PROGRAM_DESCRIPTION "Output the state of a memcached cluster." /* Prototypes */ static void options_parse(int argc, char *argv[]); static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat); -static void print_analysis_report(memcached_st *memc, - memcached_analysis_st *report); - -static bool opt_binary= false; -static bool opt_verbose= false; -static bool opt_server_version= false; -static bool opt_analyze= false; -static char *opt_servers= NULL; -static char *stat_args= NULL; -static char *analyze_mode= NULL; +static void print_analysis_report(memcached_st *memc, memcached_analysis_st *report); + +static bool opt_binary = false; +static bool opt_verbose = false; +static bool opt_server_version = false; +static bool opt_analyze = false; +static char *opt_servers = NULL; +static char *stat_args = NULL; +static char *analyze_mode = NULL; static char *opt_username; static char *opt_passwd; -static struct option long_options[]= -{ - {(OPTIONSTRING)"args", required_argument, NULL, OPT_STAT_ARGS}, - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, NULL, OPT_VERBOSE}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"debug", no_argument, NULL, OPT_DEBUG}, - {(OPTIONSTRING)"server-version", no_argument, NULL, OPT_SERVER_VERSION}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"analyze", optional_argument, NULL, OPT_ANALYZE}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {0, 0, 0, 0}, +static struct option long_options[] = { + {(OPTIONSTRING) "args", required_argument, NULL, OPT_STAT_ARGS}, + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, NULL, OPT_VERBOSE}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "debug", no_argument, NULL, OPT_DEBUG}, + {(OPTIONSTRING) "server-version", no_argument, NULL, OPT_SERVER_VERSION}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "analyze", optional_argument, NULL, OPT_ANALYZE}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, }; +static memcached_return_t stat_printer(const memcached_instance_st *instance, const char *key, + size_t key_length, const char *value, size_t value_length, + void *context) { + static const memcached_instance_st *last = NULL; + (void) context; -static memcached_return_t stat_printer(const memcached_instance_st * instance, - const char *key, size_t key_length, - const char *value, size_t value_length, - void *context) -{ - static const memcached_instance_st * last= NULL; - (void)context; - - if (last != instance) - { + if (last != instance) { printf("Server: %s (%u)\n", memcached_server_name(instance), - (uint32_t)memcached_server_port(instance)); - last= instance; + (uint32_t) memcached_server_port(instance)); + last = instance; } - printf("\t %.*s: %.*s\n", (int)key_length, key, (int)value_length, value); + printf("\t %.*s: %.*s\n", (int) key_length, key, (int) value_length, value); return MEMCACHED_SUCCESS; } static memcached_return_t server_print_callback(const memcached_st *, - const memcached_instance_st * instance, - void *) -{ - std::cerr << memcached_server_name(instance) << ":" << memcached_server_port(instance) << - " " << int(memcached_server_major_version(instance)) << - "." << int(memcached_server_minor_version(instance)) << - "." << int(memcached_server_micro_version(instance)) << std::endl; + const memcached_instance_st *instance, void *) { + std::cerr << memcached_server_name(instance) << ":" << memcached_server_port(instance) << " " + << int(memcached_server_major_version(instance)) << "." + << int(memcached_server_minor_version(instance)) << "." + << int(memcached_server_micro_version(instance)) << std::endl; return MEMCACHED_SUCCESS; } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { options_parse(argc, argv); initialize_sockets(); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; return EXIT_FAILURE; } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - - if (opt_servers) - { + + if (opt_servers) { free(opt_servers); } - memcached_st *memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary); - memcached_return_t rc= memcached_server_push(memc, servers); + memcached_return_t rc = memcached_server_push(memc, servers); memcached_server_list_free(servers); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_SOME_ERRORS) - { - printf("Failure to communicate with servers (%s)\n", - memcached_strerror(memc, rc)); + if (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_SOME_ERRORS) { + printf("Failure to communicate with servers (%s)\n", memcached_strerror(memc, rc)); exit(EXIT_FAILURE); } - if (opt_server_version) - { - if (memcached_failed(memcached_version(memc))) - { + if (opt_server_version) { + if (memcached_failed(memcached_version(memc))) { std::cerr << "Unable to obtain server version" << std::endl; exit(EXIT_FAILURE); } memcached_server_fn callbacks[1]; - callbacks[0]= server_print_callback; - memcached_server_cursor(memc, callbacks, NULL, 1); - } - else if (opt_analyze) - { - memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc); + callbacks[0] = server_print_callback; + memcached_server_cursor(memc, callbacks, NULL, 1); + } else if (opt_analyze) { + memcached_stat_st *memc_stat = memcached_stat(memc, NULL, &rc); - if (memc_stat == NULL || rc != MEMCACHED_SUCCESS) - { + if (memc_stat == NULL || rc != MEMCACHED_SUCCESS) { std::cerr << memcached_last_error_message(memc) << std::endl; exit(EXIT_FAILURE); } @@ -188,63 +166,50 @@ int main(int argc, char *argv[]) run_analyzer(memc, memc_stat); memcached_stat_free(memc, memc_stat); - } - else - { - rc= memcached_stat_execute(memc, stat_args, stat_printer, NULL); + } else { + rc = memcached_stat_execute(memc, stat_args, stat_printer, NULL); } memcached_free(memc); - return rc == MEMCACHED_SUCCESS ? EXIT_SUCCESS: EXIT_FAILURE; + return rc == MEMCACHED_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; } -static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) -{ +static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) { memcached_return_t rc; - if (analyze_mode == NULL) - { + if (analyze_mode == NULL) { memcached_analysis_st *report; - report= memcached_analyze(memc, memc_stat, &rc); - if (rc != MEMCACHED_SUCCESS || report == NULL) - { - printf("Failure to analyze servers (%s)\n", - memcached_strerror(memc, rc)); + report = memcached_analyze(memc, memc_stat, &rc); + if (rc != MEMCACHED_SUCCESS || report == NULL) { + printf("Failure to analyze servers (%s)\n", memcached_strerror(memc, rc)); exit(1); } print_analysis_report(memc, report); free(report); - } - else if (strcmp(analyze_mode, "latency") == 0) - { - uint32_t flags, server_count= memcached_server_count(memc); - uint32_t num_of_tests= 32; - const char *test_key= "libmemcached_test_key"; - - memcached_st **servers= static_cast(malloc(sizeof(memcached_st*) * server_count)); - if (servers == NULL) - { + } else if (strcmp(analyze_mode, "latency") == 0) { + uint32_t flags, server_count = memcached_server_count(memc); + uint32_t num_of_tests = 32; + const char *test_key = "libmemcached_test_key"; + + memcached_st **servers = + static_cast(malloc(sizeof(memcached_st *) * server_count)); + if (servers == NULL) { fprintf(stderr, "Failed to allocate memory\n"); return; } - for (uint32_t x= 0; x < server_count; x++) - { - const memcached_instance_st * instance= - memcached_server_instance_by_position(memc, x); + for (uint32_t x = 0; x < server_count; x++) { + const memcached_instance_st *instance = memcached_server_instance_by_position(memc, x); - if ((servers[x]= memcached_create(NULL)) == NULL) - { + if ((servers[x] = memcached_create(NULL)) == NULL) { fprintf(stderr, "Failed to memcached_create()\n"); - if (x > 0) - { + if (x > 0) { memcached_free(servers[0]); } x--; - for (; x > 0; x--) - { + for (; x > 0; x--) { memcached_free(servers[x]); } @@ -252,104 +217,85 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) return; } - memcached_server_add(servers[x], - memcached_server_name(instance), + memcached_server_add(servers[x], memcached_server_name(instance), memcached_server_port(instance)); } printf("Network Latency Test:\n\n"); struct timeval start_time, end_time; - uint32_t slowest_server= 0; - long elapsed_time, slowest_time= 0; + uint32_t slowest_server = 0; + long elapsed_time, slowest_time = 0; - for (uint32_t x= 0; x < server_count; x++) - { - const memcached_instance_st * instance= - memcached_server_instance_by_position(memc, x); + for (uint32_t x = 0; x < server_count; x++) { + const memcached_instance_st *instance = memcached_server_instance_by_position(memc, x); gettimeofday(&start_time, NULL); - for (uint32_t y= 0; y < num_of_tests; y++) - { + for (uint32_t y = 0; y < num_of_tests; y++) { size_t vlen; - char *val= memcached_get(servers[x], test_key, strlen(test_key), - &vlen, &flags, &rc); - if (rc != MEMCACHED_NOTFOUND and rc != MEMCACHED_SUCCESS) - { + char *val = memcached_get(servers[x], test_key, strlen(test_key), &vlen, &flags, &rc); + if (rc != MEMCACHED_NOTFOUND and rc != MEMCACHED_SUCCESS) { break; } free(val); } gettimeofday(&end_time, NULL); - elapsed_time= (long) timedif(end_time, start_time); + elapsed_time = (long) timedif(end_time, start_time); elapsed_time /= (long) num_of_tests; - if (elapsed_time > slowest_time) - { - slowest_server= x; - slowest_time= elapsed_time; + if (elapsed_time > slowest_time) { + slowest_server = x; + slowest_time = elapsed_time; } - if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS) - { - printf("\t %s (%d) => failed to reach the server\n", - memcached_server_name(instance), + if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS) { + printf("\t %s (%d) => failed to reach the server\n", memcached_server_name(instance), memcached_server_port(instance)); - } - else - { - printf("\t %s (%d) => %ld.%ld seconds\n", - memcached_server_name(instance), - memcached_server_port(instance), - elapsed_time / 1000, elapsed_time % 1000); + } else { + printf("\t %s (%d) => %ld.%ld seconds\n", memcached_server_name(instance), + memcached_server_port(instance), elapsed_time / 1000, elapsed_time % 1000); } } - if (server_count > 1 && slowest_time > 0) - { - const memcached_instance_st * slowest= - memcached_server_instance_by_position(memc, slowest_server); + if (server_count > 1 && slowest_time > 0) { + const memcached_instance_st *slowest = + memcached_server_instance_by_position(memc, slowest_server); printf("---\n"); - printf("Slowest Server: %s (%d) => %ld.%ld seconds\n", - memcached_server_name(slowest), - memcached_server_port(slowest), - slowest_time / 1000, slowest_time % 1000); + printf("Slowest Server: %s (%d) => %ld.%ld seconds\n", memcached_server_name(slowest), + memcached_server_port(slowest), slowest_time / 1000, slowest_time % 1000); } printf("\n"); - for (uint32_t x= 0; x < server_count; x++) - { + for (uint32_t x = 0; x < server_count; x++) { memcached_free(servers[x]); } free(servers); free(analyze_mode); - } - else - { + } else { fprintf(stderr, "Invalid Analyzer Option provided\n"); free(analyze_mode); } } -static void print_analysis_report(memcached_st *memc, - memcached_analysis_st *report) - +static void print_analysis_report(memcached_st *memc, memcached_analysis_st *report) + { - uint32_t server_count= memcached_server_count(memc); - const memcached_instance_st * most_consumed_server= memcached_server_instance_by_position(memc, report->most_consumed_server); - const memcached_instance_st * least_free_server= memcached_server_instance_by_position(memc, report->least_free_server); - const memcached_instance_st * oldest_server= memcached_server_instance_by_position(memc, report->oldest_server); + uint32_t server_count = memcached_server_count(memc); + const memcached_instance_st *most_consumed_server = + memcached_server_instance_by_position(memc, report->most_consumed_server); + const memcached_instance_st *least_free_server = + memcached_server_instance_by_position(memc, report->least_free_server); + const memcached_instance_st *oldest_server = + memcached_server_instance_by_position(memc, report->oldest_server); printf("Memcached Cluster Analysis Report\n\n"); printf("\tNumber of Servers Analyzed : %u\n", server_count); - printf("\tAverage Item Size (incl/overhead) : %u bytes\n", - report->average_item_size); + printf("\tAverage Item Size (incl/overhead) : %u bytes\n", report->average_item_size); - if (server_count == 1) - { + if (server_count == 1) { printf("\nFor a detailed report, you must supply multiple servers.\n"); return; } @@ -357,109 +303,80 @@ static void print_analysis_report(memcached_st *memc, printf("\n"); printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n", memcached_server_name(most_consumed_server), - (uint32_t)memcached_server_port(most_consumed_server), - (unsigned long long)report->most_used_bytes); + (uint32_t) memcached_server_port(most_consumed_server), + (unsigned long long) report->most_used_bytes); printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n", memcached_server_name(least_free_server), - (uint32_t)memcached_server_port(least_free_server), - (unsigned long long)report->least_remaining_bytes); + (uint32_t) memcached_server_port(least_free_server), + (unsigned long long) report->least_remaining_bytes); printf("\tNode with longest uptime : %s:%u (%us)\n", - memcached_server_name(oldest_server), - (uint32_t)memcached_server_port(oldest_server), + memcached_server_name(oldest_server), (uint32_t) memcached_server_port(oldest_server), report->longest_uptime); printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio); printf("\n"); } -static void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +static void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - int option_index= 0; + int option_index = 0; - bool opt_version= false; - bool opt_help= false; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:a", long_options, &option_index); + bool opt_version = false; + bool opt_help = false; + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:a", long_options, &option_index); if (option_rv == -1) break; - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose= true; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = true; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose= true; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = true; break; - case OPT_BINARY: - opt_binary= true; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_SERVER_VERSION: - opt_server_version= true; - break; + case OPT_SERVER_VERSION: opt_server_version = true; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; - case OPT_STAT_ARGS: - stat_args= strdup(optarg); - break; + case OPT_STAT_ARGS: stat_args = strdup(optarg); break; case OPT_ANALYZE: /* --analyze or -a */ - opt_analyze= true; - analyze_mode= (optarg) ? strdup(optarg) : NULL; + opt_analyze = true; + analyze_mode = (optarg) ? strdup(optarg) : NULL; break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case OPT_USERNAME: - opt_username= optarg; - opt_binary= true; + opt_username = optarg; + opt_binary = true; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; case '?': /* getopt_long already printed an error message. */ exit(1); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); } diff --git a/src/bin/memtouch.cc b/src/bin/memtouch.cc index b52ce9bd..1de4b92f 100644 --- a/src/bin/memtouch.cc +++ b/src/bin/memtouch.cc @@ -1,14 +1,17 @@ -/* LibMemcached - * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. - * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. - * - * Summary: - * - */ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ #include "mem_config.h" @@ -23,103 +26,87 @@ #include "utilities.h" -#define PROGRAM_NAME "memtouch" +#define PROGRAM_NAME "memtouch" #define PROGRAM_DESCRIPTION "Update the expiration value of an already existing value in the server" - /* Prototypes */ void options_parse(int argc, char *argv[]); -static int opt_binary= 0; -static int opt_verbose= 0; -static char *opt_servers= NULL; -static char *opt_hash= NULL; +static int opt_binary = 0; +static int opt_verbose = 0; +static char *opt_servers = NULL; +static char *opt_hash = NULL; static char *opt_username; static char *opt_passwd; -time_t expiration= 0; +time_t expiration = 0; -int main(int argc, char *argv[]) -{ - int return_code= EXIT_SUCCESS; +int main(int argc, char *argv[]) { + int return_code = EXIT_SUCCESS; options_parse(argc, argv); initialize_sockets(); - if (opt_servers == NULL) - { + if (opt_servers == NULL) { char *temp; - if ((temp= getenv("MEMCACHED_SERVERS"))) - { - opt_servers= strdup(temp); + if ((temp = getenv("MEMCACHED_SERVERS"))) { + opt_servers = strdup(temp); } - if (opt_servers == NULL) - { + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st* servers= memcached_servers_parse(opt_servers); - if (servers == NULL or memcached_server_list_count(servers) == 0) - { + memcached_server_st *servers = memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { std::cerr << "Invalid server list provided:" << opt_servers << std::endl; return EXIT_FAILURE; } - memcached_st *memc= memcached_create(NULL); + memcached_st *memc = memcached_create(NULL); process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, - (uint64_t)opt_binary); + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) - { + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { memcached_free(memc); - std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + std::cerr << "--username was supplied, but binary was not built with SASL support." + << std::endl; return EXIT_FAILURE; } - if (opt_username) - { + if (opt_username) { memcached_return_t ret; - if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) - { + if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) { std::cerr << memcached_last_error_message(memc) << std::endl; memcached_free(memc); return EXIT_FAILURE; } } - while (optind < argc) - { - memcached_return_t rc= memcached_touch(memc, argv[optind], strlen(argv[optind]), expiration); - if (rc == MEMCACHED_NOTFOUND) - { - if (opt_verbose) - { + while (optind < argc) { + memcached_return_t rc = memcached_touch(memc, argv[optind], strlen(argv[optind]), expiration); + if (rc == MEMCACHED_NOTFOUND) { + if (opt_verbose) { std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl; } - return_code= EXIT_FAILURE; - } - else if (memcached_failed(rc)) - { - if (opt_verbose) - { - std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl; + return_code = EXIT_FAILURE; + } else if (memcached_failed(rc)) { + if (opt_verbose) { + std::cerr << "Fatal error for key \"" << argv[optind] + << "\" :" << memcached_last_error_message(memc) << std::endl; } - return_code= EXIT_FAILURE; - } - else // success + return_code = EXIT_FAILURE; + } else // success { - if (opt_verbose) - { + if (opt_verbose) { std::cout << "Found key " << argv[optind] << std::endl; } } @@ -129,127 +116,93 @@ int main(int argc, char *argv[]) memcached_free(memc); - if (opt_servers) - { + if (opt_servers) { free(opt_servers); } - if (opt_hash) - { + if (opt_hash) { free(opt_hash); } return return_code; } - -void options_parse(int argc, char *argv[]) -{ - memcached_programs_help_st help_options[]= - { - {0}, +void options_parse(int argc, char *argv[]) { + memcached_programs_help_st help_options[] = { + {0}, }; - static struct option long_options[]= - { - {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, - {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, - {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET}, - {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, - {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, - {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, - {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH}, - {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, - {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, - {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, - {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, + static struct option long_options[] = { + {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET}, + {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH}, + {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD}, + {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE}, {0, 0, 0, 0}, - }; + }; - bool opt_version= false; - bool opt_help= false; - int option_index= 0; + bool opt_version = false; + bool opt_help = false; + int option_index = 0; - while (1) - { - int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); - if (option_rv == -1) - { + while (1) { + int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) { break; } - switch (option_rv) - { - case 0: - break; + switch (option_rv) { + case 0: break; - case OPT_BINARY: - opt_binary = true; - break; + case OPT_BINARY: opt_binary = true; break; - case OPT_VERBOSE: /* --verbose or -v */ - opt_verbose = OPT_VERBOSE; - break; + case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break; - case OPT_DEBUG: /* --debug or -d */ - opt_verbose = OPT_DEBUG; - break; + case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break; - case OPT_VERSION: /* --version or -V */ - opt_version= true; - break; + case OPT_VERSION: /* --version or -V */ opt_version = true; break; - case OPT_HELP: /* --help or -h */ - opt_help= true; - break; + case OPT_HELP: /* --help or -h */ opt_help = true; break; - case OPT_SERVERS: /* --servers or -s */ - opt_servers= strdup(optarg); - break; + case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break; - case OPT_HASH: - opt_hash= strdup(optarg); - break; + case OPT_HASH: opt_hash = strdup(optarg); break; - case OPT_USERNAME: - opt_username= optarg; - break; + case OPT_USERNAME: opt_username = optarg; break; - case OPT_PASSWD: - opt_passwd= optarg; - break; + case OPT_PASSWD: opt_passwd = optarg; break; case OPT_EXPIRE: - errno= 0; - expiration= time_t(strtoul(optarg, (char **)NULL, 10)); - if (errno != 0) - { + errno = 0; + expiration = time_t(strtoul(optarg, (char **) NULL, 10)); + if (errno != 0) { fprintf(stderr, "Invalid value for --expire: %s\n", optarg); exit(EXIT_FAILURE); } break; - case OPT_QUIET: - close_stdio(); - break; + case OPT_QUIET: close_stdio(); break; case '?': /* getopt_long already printed an error message. */ exit(EXIT_FAILURE); - default: - abort(); + default: abort(); } } - if (opt_version) - { + if (opt_version) { version_command(PROGRAM_NAME); exit(EXIT_SUCCESS); } - if (opt_help) - { + if (opt_help) { help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); exit(EXIT_SUCCESS); }