X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=clients%2Fms_conn.c;h=85e84739835b0c3af8b8709a189f5eefa438c79d;hb=398f48445cacf12679248142f0c86d3a0d6caab6;hp=82016e1fef60c3c557dfd45e4cb0827ebac10cc4;hpb=36583b08f3d20bebd7d9874dd08131e1b3c6ffaf;p=awesomized%2Flibmemcached diff --git a/clients/ms_conn.c b/clients/ms_conn.c index 82016e1f..85e84739 100644 --- a/clients/ms_conn.c +++ b/clients/ms_conn.c @@ -9,7 +9,7 @@ * */ -#include "config.h" +#include "mem_config.h" #include #include @@ -18,17 +18,17 @@ #include #include #include +#include #include -#if TIME_WITH_SYS_TIME + +#if defined(HAVE_SYS_TIME_H) # include +#endif + +#if defined(HAVE_TIME_H) # include -#else -# if HAVE_SYS_TIME_H -# include -# else -# include -# endif #endif + #include "ms_setting.h" #include "ms_thread.h" #include "ms_atomic.h" @@ -223,7 +223,7 @@ static void ms_task_init(ms_conn_t *c) * @param c, pointer of the concurrency * @param is_udp, whether it's udp * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) { @@ -259,7 +259,7 @@ static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) memset(c->udppkt, 0, MAX_UDP_PACKET * sizeof(ms_udppkt_t)); } - return 0; + return EXIT_SUCCESS; } /* ms_conn_udp_init */ @@ -271,7 +271,7 @@ static int ms_conn_udp_init(ms_conn_t *c, const bool is_udp) * @param read_buffer_size * @param is_udp, whether it's udp * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_conn_init(ms_conn_t *c, const int init_state, @@ -357,7 +357,7 @@ static int ms_conn_init(ms_conn_t *c, c->mlget_task.mlget_num= 0; c->mlget_task.value_index= -1; /* default invalid value */ - if (ms_setting.binary_prot) + if (ms_setting.binary_prot_) { c->protocol= binary_prot; } @@ -380,7 +380,7 @@ static int ms_conn_init(ms_conn_t *c, atomic_add_32(&ms_stats.active_conns, 1); } - return 0; + return EXIT_SUCCESS; } /* ms_conn_init */ @@ -413,7 +413,7 @@ static void ms_warmup_num_init(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_item_win_init(ms_conn_t *c) { @@ -457,7 +457,7 @@ static int ms_item_win_init(ms_conn_t *c) ms_warmup_num_init(c); - return 0; + return EXIT_SUCCESS; } /* ms_item_win_init */ @@ -468,7 +468,7 @@ static int ms_item_win_init(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_conn_sock_init(ms_conn_t *c) { @@ -552,7 +552,7 @@ static int ms_conn_sock_init(ms_conn_t *c) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_conn_sock_init */ @@ -562,7 +562,7 @@ static int ms_conn_sock_init(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_conn_event_init(ms_conn_t *c) { @@ -578,7 +578,7 @@ static int ms_conn_event_init(ms_conn_t *c) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_conn_event_init */ @@ -588,7 +588,7 @@ static int ms_conn_event_init(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ int ms_setup_conn(ms_conn_t *c) { @@ -612,7 +612,7 @@ int ms_setup_conn(ms_conn_t *c) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_setup_conn */ @@ -706,7 +706,7 @@ static void ms_conn_close(ms_conn_t *c) * * @param ai, server address information * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_new_socket(struct addrinfo *ai) { @@ -758,6 +758,7 @@ static void ms_maximize_sndbuf(const int sfd) max= avg - 1; } } + (void)last_good; } /* ms_maximize_sndbuf */ @@ -770,7 +771,7 @@ static void ms_maximize_sndbuf(const int sfd) * @param is_udp, whether it's udp * @param ret_sfd, the connected socket file descriptor * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_network_connect(ms_conn_t *c, char *srv_host_name, @@ -893,7 +894,7 @@ static int ms_network_connect(ms_conn_t *c, * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_reconn(ms_conn_t *c) { @@ -983,7 +984,7 @@ static int ms_reconn(ms_conn_t *c) c->alive_sfds--; } - return 0; + return EXIT_SUCCESS; } /* ms_reconn */ @@ -995,7 +996,7 @@ static int ms_reconn(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ int ms_reconn_socks(ms_conn_t *c) { @@ -1009,7 +1010,7 @@ int ms_reconn_socks(ms_conn_t *c) if ((c->total_sfds == 1) || (c->total_sfds == c->alive_sfds)) { - return 0; + return EXIT_SUCCESS; } for (uint32_t i= 0; i < c->total_sfds; i++) @@ -1064,7 +1065,7 @@ int ms_reconn_socks(ms_conn_t *c) } } - return 0; + return EXIT_SUCCESS; } /* ms_reconn_socks */ @@ -1136,7 +1137,7 @@ static int ms_tokenize_command(char *command, * @param c, pointer of the concurrency * @param command, the string responded by server * - * @return int, if the command completed return 0, else return + * @return int, if the command completed return EXIT_SUCCESS, else return * -1 */ static int ms_ascii_process_line(ms_conn_t *c, char *command) @@ -1159,7 +1160,12 @@ static int ms_ascii_process_line(ms_conn_t *c, char *command) { 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) + { + printf("<%d ERROR %s\n", c->sfd, strerror(errno)); + } c->currcmd.key_prefix= *(uint64_t *)tokens[KEY_TOKEN].value; /* @@ -1178,6 +1184,7 @@ static int ms_ascii_process_line(ms_conn_t *c, char *command) case 'O': /* OK */ c->currcmd.retstat= MCD_SUCCESS; + break; case 'S': /* STORED STATS SERVER_ERROR */ if (buffer[2] == 'A') /* STORED STATS */ @@ -1311,7 +1318,7 @@ void ms_reset_conn(ms_conn_t *c, bool timeout) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_try_read_line(ms_conn_t *c) { @@ -1321,7 +1328,7 @@ static int ms_try_read_line(ms_conn_t *c) if ((uint64_t)c->rbytes < sizeof(c->binary_header)) { /* need more data! */ - return 0; + return EXIT_SUCCESS; } else { @@ -1351,7 +1358,7 @@ static int ms_try_read_line(ms_conn_t *c) fprintf(stderr, "Invalid magic: %x\n", c->binary_header.response.magic); ms_conn_set_state(c, conn_closing); - return 0; + return EXIT_SUCCESS; } /* process this complete response */ @@ -1376,11 +1383,11 @@ static int ms_try_read_line(ms_conn_t *c) assert(c->rcurr <= (c->rbuf + c->rsize)); if (c->rbytes == 0) - return 0; + return EXIT_SUCCESS; el= memchr(c->rcurr, '\n', (size_t)c->rbytes); if (! el) - return 0; + return EXIT_SUCCESS; cont= el + 1; if (((el - c->rcurr) > 1) && (*(el - 1) == '\r')) @@ -1543,6 +1550,7 @@ static int ms_sort_udp_packet(ms_conn_t *c, char *buf, int rbytes) break; } } + (void)packets; return wbytes == 0 ? -1 : wbytes; } /* ms_sort_udp_packet */ @@ -1634,7 +1642,7 @@ static int ms_udp_read(ms_conn_t *c, char *buf, int len) * close. * before reading, move the remaining incomplete fragment of a command * (if any) to the beginning of the buffer. - * return 0 if there's nothing to read on the first read. + * return EXIT_SUCCESS if there's nothing to read on the first read. */ /** @@ -1645,8 +1653,8 @@ static int ms_udp_read(ms_conn_t *c, char *buf, int len) * @param c, pointer of the concurrency * * @return int, - * return 0 if there's nothing to read on the first read. - * return 1 if get data + * return EXIT_SUCCESS if there's nothing to read on the first read. + * return EXIT_FAILURE if get data * return -1 if error happens */ static int ms_try_read_network(ms_conn_t *c) @@ -2012,7 +2020,7 @@ static void ms_complete_nread(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_add_msghdr(ms_conn_t *c) { @@ -2056,7 +2064,7 @@ static int ms_add_msghdr(ms_conn_t *c) return ms_add_iov(c, NULL, UDP_HEADER_SIZE); } - return 0; + return EXIT_SUCCESS; } /* ms_add_msghdr */ @@ -2066,7 +2074,7 @@ static int ms_add_msghdr(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_ensure_iov_space(ms_conn_t *c) { @@ -2093,7 +2101,7 @@ static int ms_ensure_iov_space(ms_conn_t *c) } } - return 0; + return EXIT_SUCCESS; } /* ms_ensure_iov_space */ @@ -2105,7 +2113,7 @@ static int ms_ensure_iov_space(ms_conn_t *c) * @param buf, the buffer includes data to send * @param len, the data length in the buffer * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) { @@ -2124,6 +2132,7 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) */ 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))) @@ -2131,6 +2140,7 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) ms_add_msghdr(c); m= &c->msglist[c->msgused - 1]; } +#endif if (ms_ensure_iov_space(c) != 0) return -1; @@ -2159,7 +2169,7 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) } while (leftover > 0); - return 0; + return EXIT_SUCCESS; } /* ms_add_iov */ @@ -2168,7 +2178,7 @@ static int ms_add_iov(ms_conn_t *c, const void *buf, int len) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_build_udp_headers(ms_conn_t *c) { @@ -2219,7 +2229,7 @@ static int ms_build_udp_headers(ms_conn_t *c) + UDP_HEADER_SIZE)); } - return 0; + return EXIT_SUCCESS; } /* ms_build_udp_headers */ @@ -2335,7 +2345,7 @@ static void ms_conn_shrink(ms_conn_t *c) && (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) + if (new_rbuf) { c->rudpbuf= new_rbuf; c->rudpsize= UDP_DATA_BUFFER_SIZE; @@ -2464,9 +2474,7 @@ static bool ms_need_yield(ms_conn_t *c) { 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)); + 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) @@ -2724,7 +2732,7 @@ void ms_event_handler(const int fd, const short which, void *arg) * @param c, pointer of the concurrency * @param cmd, command(get or set ) * - * @return int, if success, return the index, else return 0 + * @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) { @@ -2733,7 +2741,7 @@ static uint32_t ms_get_rep_sock_index(ms_conn_t *c, int cmd) if (c->total_sfds == 1) { - return 0; + return EXIT_SUCCESS; } if (ms_setting.rep_write_srv == 0) @@ -2802,7 +2810,7 @@ static uint32_t ms_get_next_sock_index(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_update_conn_sock_event(ms_conn_t *c) { @@ -2866,7 +2874,7 @@ static int ms_update_conn_sock_event(ms_conn_t *c) } } - return 0; + return EXIT_SUCCESS; } /* ms_update_conn_sock_event */ @@ -2878,7 +2886,7 @@ static int ms_update_conn_sock_event(ms_conn_t *c) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @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) { @@ -2886,13 +2894,14 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) int write_len; char *buffer= c->wbuf; - write_len= sprintf(buffer, - " %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) + 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"); @@ -2922,7 +2931,7 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_set */ @@ -2933,7 +2942,7 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) { @@ -2977,7 +2986,7 @@ int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) item->key_size + item->value_size); atomic_add_size(&ms_stats.cmd_set, 1); - return 0; + return EXIT_SUCCESS; } /* ms_mcd_set */ @@ -2989,7 +2998,7 @@ int ms_mcd_set(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @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) { @@ -3004,7 +3013,7 @@ static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_get */ @@ -3015,7 +3024,7 @@ static int ms_build_ascii_write_buf_get(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) { @@ -3057,7 +3066,7 @@ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) atomic_add_size(&ms_stats.cmd_get, 1); - return 0; + return EXIT_SUCCESS; } /* ms_mcd_get */ @@ -3067,7 +3076,7 @@ int ms_mcd_get(ms_conn_t *c, ms_task_item_t *item) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) { @@ -3098,7 +3107,7 @@ static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) return -1; } - return 0; + return EXIT_SUCCESS; } /* ms_build_ascii_write_buf_mlget */ @@ -3107,7 +3116,7 @@ static int ms_build_ascii_write_buf_mlget(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ int ms_mcd_mlget(ms_conn_t *c) { @@ -3157,7 +3166,9 @@ int ms_mcd_mlget(ms_conn_t *c) atomic_add_size(&ms_stats.cmd_get, 1); } - return 0; + (void)item; + + return EXIT_SUCCESS; } /* ms_mcd_mlget */ @@ -3170,7 +3181,7 @@ int ms_mcd_mlget(ms_conn_t *c) * * @param c, pointer of the concurrency * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_bin_process_response(ms_conn_t *c) { @@ -3186,7 +3197,7 @@ static int ms_bin_process_response(ms_conn_t *c) { c->rvbytes= (int32_t)bodylen; c->readval= true; - return 1; + return EXIT_FAILURE; } else { @@ -3253,7 +3264,7 @@ static int ms_bin_process_response(ms_conn_t *c) } } - return 0; + return EXIT_SUCCESS; } /* ms_bin_process_response */ @@ -3286,7 +3297,7 @@ static void ms_add_bin_header(ms_conn_t *c, header->request.extlen= (uint8_t)hdr_len; header->request.datatype= (uint8_t)PROTOCOL_BINARY_RAW_BYTES; - header->request.reserved= 0; + header->request.vbucket= 0; header->request.bodylen= htonl(body_len); header->request.opaque= 0; @@ -3319,7 +3330,7 @@ static void ms_add_key_to_iov(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @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) { @@ -3351,7 +3362,7 @@ static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item) } ms_add_iov(c, &ms_setting.char_block[value_offset], item->value_size); - return 0; + return EXIT_SUCCESS; } /* ms_build_bin_write_buf_set */ @@ -3363,7 +3374,7 @@ static int ms_build_bin_write_buf_set(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @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) { @@ -3373,7 +3384,7 @@ static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item) (uint32_t)item->key_size); ms_add_key_to_iov(c, item); - return 0; + return EXIT_SUCCESS; } /* ms_build_bin_write_buf_get */ @@ -3385,7 +3396,7 @@ static int ms_build_bin_write_buf_get(ms_conn_t *c, ms_task_item_t *item) * @param item, pointer of task item which includes the object * information * - * @return int, if success, return 0, else return -1 + * @return int, if success, return EXIT_SUCCESS, else return -1 */ static int ms_build_bin_write_buf_mlget(ms_conn_t *c) { @@ -3409,5 +3420,5 @@ static int ms_build_bin_write_buf_mlget(ms_conn_t *c) c->wcurr= c->wbuf; - return 0; + return EXIT_SUCCESS; } /* ms_build_bin_write_buf_mlget */