From: Monty Taylor Date: Thu, 3 Dec 2009 20:16:27 +0000 (-0800) Subject: Fixed for OSX. X-Git-Tag: 0.37~81^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=1cb7ba391e84882407f3268dbae0a945214be12e;p=awesomized%2Flibmemcached Fixed for OSX. --- diff --git a/clients/ms_conn.c b/clients/ms_conn.c index 86e2c68a..17553022 100644 --- a/clients/ms_conn.c +++ b/clients/ms_conn.c @@ -12,6 +12,7 @@ #include "config.h" #include +#include #include #include #include @@ -42,7 +43,7 @@ static uint64_t key_prefix_seq= KEY_PREFIX_BASE; /* global increasing counter, generating request id for UDP */ static volatile uint32_t udp_request_id= 0; -extern __thread ms_thread_t ms_thread; +extern pthread_key_t ms_thread_key; /* generate upd request id */ static uint32_t ms_get_udp_request_id(void); @@ -398,11 +399,12 @@ static void ms_warmup_num_init(ms_conn_t *c) */ 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->exec_num= ms_thread->thread_ctx->exec_num_perconn; c->remain_exec_num= c->exec_num; c->item_win= (ms_task_item_t *)malloc( @@ -452,6 +454,7 @@ static int ms_item_win_init(ms_conn_t *c) */ static int ms_conn_sock_init(ms_conn_t *c) { + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); int i; int ret_sfd; int srv_idx= 0; @@ -470,7 +473,7 @@ static int ms_conn_sock_init(ms_conn_t *c) 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, @@ -545,6 +548,7 @@ static int ms_conn_sock_init(ms_conn_t *c) */ static int ms_conn_event_init(ms_conn_t *c) { + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); /* default event timeout 10 seconds */ struct timeval t= { @@ -553,7 +557,7 @@ static int ms_conn_event_init(ms_conn_t *c) short event_flags= EV_WRITE | EV_PERSIST; event_set(&c->event, c->sfd, event_flags, ms_event_handler, (void *)c); - event_base_set(ms_thread.base, &c->event); + event_base_set(ms_thread->base, &c->event); c->ev_flags= event_flags; if (c->total_sfds == 1) @@ -616,6 +620,7 @@ int ms_setup_conn(ms_conn_t *c) */ 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) @@ -639,9 +644,9 @@ 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); + free(ms_thread->conn); } } } /* ms_conn_free */ @@ -654,6 +659,7 @@ void ms_conn_free(ms_conn_t *c) */ 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 */ @@ -685,7 +691,7 @@ static void ms_conn_close(ms_conn_t *c) pthread_mutex_unlock(&ms_global.run_lock.lock); } - if (ms_thread.nactive_conn == 0) + if (ms_thread->nactive_conn == 0) { pthread_exit(NULL); } @@ -884,6 +890,7 @@ static int ms_network_connect(ms_conn_t *c, */ static int ms_reconn(ms_conn_t *c) { + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); int srv_idx= 0; int32_t srv_conn_cnt= 0; @@ -894,7 +901,7 @@ static int ms_reconn(ms_conn_t *c) } else { - srv_idx= ms_thread.thread_ctx->srv_idx; + srv_idx= ms_thread->thread_ctx->srv_idx; srv_conn_cnt= ms_setting.nconns / ms_setting.srv_cnt; } @@ -984,6 +991,7 @@ static int ms_reconn(ms_conn_t *c) */ int ms_reconn_socks(ms_conn_t *c) { + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); int srv_idx= 0; int ret_sfd= 0; int srv_conn_cnt= 0; @@ -1021,7 +1029,7 @@ int ms_reconn_socks(ms_conn_t *c) } else { - srv_idx= ms_thread.thread_ctx->srv_idx; + srv_idx= ms_thread->thread_ctx->srv_idx; srv_conn_cnt= ms_setting.nconns / ms_setting.srv_cnt; } @@ -1759,7 +1767,7 @@ static void ms_verify_value(ms_conn_t *c, "\n<%d expire time verification failed, " "object expired but get it now\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\tset time: %s current time: %s " "diff time: %d expire time: %d\n" "\texpected data: \n" @@ -1793,7 +1801,7 @@ static void ms_verify_value(ms_conn_t *c, fprintf(stderr, "\n<%d data verification failed\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64" %.*s\n" "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data len: %d\n" @@ -2452,6 +2460,7 @@ static bool ms_update_event(ms_conn_t *c, const int new_flags) */ 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; @@ -2460,13 +2469,13 @@ static bool ms_need_yield(ms_conn_t *c) if (ms_setting.expected_tps > 0) { gettimeofday(&curr_time, NULL); - time_diff= ms_time_diff(&ms_thread.startup_time, &curr_time); + 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; } diff --git a/clients/ms_setting.c b/clients/ms_setting.c index 4791c479..52e4670f 100644 --- a/clients/ms_setting.c +++ b/clients/ms_setting.c @@ -12,6 +12,7 @@ #include "config.h" #include +#include #include #include "ms_setting.h" @@ -59,7 +60,7 @@ static void ms_print_setting(void); static void ms_setting_slapmode_init_pre(void); static void ms_setting_slapmode_init_post(void); -#if defined(__SUNPRO_C) +#if !defined(HAVE_GETLINE) #include static ssize_t getline (char **line, size_t *line_size, FILE *fp) { @@ -132,8 +133,8 @@ static ssize_t getline (char **line, size_t *line_size, FILE *fp) break; } (*line)[cur_len] = '\0'; - result= cur_len ? cur_len : result; - + if (cur_len != 0) + return (ssize_t)cur_len; return result; } #endif @@ -858,9 +859,9 @@ static void ms_print_setting() } else { - fprintf(stdout, "execute number: %ld\n", ms_setting.exec_num); + fprintf(stdout, "execute number: %" PRId64 "\n", ms_setting.exec_num); } - fprintf(stdout, "windows size: %ldk\n", + 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); diff --git a/clients/ms_stats.c b/clients/ms_stats.c index 7fad7fb8..49a5ab6e 100644 --- a/clients/ms_stats.c +++ b/clients/ms_stats.c @@ -11,6 +11,7 @@ #include "config.h" +#include #include "ms_stats.h" #define array_size(x) (sizeof(x) / sizeof((x)[0])) @@ -191,7 +192,7 @@ void ms_dump_stats(ms_stat_t *stat) { printf("\n %2d:", (int)i); } - printf(" %6ld", stat->dist[i]); + printf(" %6" PRIu64 , stat->dist[i]); } printf("\n\n"); diff --git a/clients/ms_task.c b/clients/ms_task.c index c99f67cc..7a427332 100644 --- a/clients/ms_task.c +++ b/clients/ms_task.c @@ -11,6 +11,8 @@ #include "config.h" +#include + #include "ms_thread.h" #include "ms_setting.h" #include "ms_atomic.h" @@ -24,8 +26,6 @@ * item in the window. This factor shows it. */ -extern __thread ms_thread_t ms_thread; - /* get item from task window */ static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c); static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c); @@ -571,7 +571,7 @@ static void ms_warmup_server(ms_conn_t *c) } else if (c->precmd.cmd == CMD_SET && c->precmd.retstat != MCD_STORED) { - printf("key: %lx didn't set success\n", item->key_prefix); + printf("key: %" PRIx64 " didn't set success\n", item->key_prefix); } } @@ -755,7 +755,7 @@ static void ms_update_multi_get_result(ms_conn_t *c) "\n\t<%d expire time verification failed, object " "doesn't expire but can't get it now\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\tset time: %s current time: %s " "diff time: %d expire time: %d\n" "\texpected data len: %d\n" @@ -785,7 +785,7 @@ static void ms_update_multi_get_result(ms_conn_t *c) { fprintf(stderr, "\n<%d data verification failed\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data: \n", @@ -860,7 +860,7 @@ static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) "\n\t<%d expire time verification failed, object " "doesn't expire but can't get it now\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\tset time: %s current time: %s " "diff time: %d expire time: %d\n" "\texpected data len: %d\n" @@ -890,7 +890,7 @@ static void ms_update_single_get_result(ms_conn_t *c, ms_task_item_t *item) { fprintf(stderr, "\n<%d data verification failed\n" "\tkey len: %d\n" - "\tkey: %lx %.*s\n" + "\tkey: %" PRIx64 " %.*s\n" "\texpected data len: %d\n" "\texpected data: %.*s\n" "\treceived data: \n", diff --git a/clients/ms_thread.c b/clients/ms_thread.c index 4db08750..af856366 100644 --- a/clients/ms_thread.c +++ b/clients/ms_thread.c @@ -16,7 +16,7 @@ #include "ms_atomic.h" /* global variable */ -__thread ms_thread_t ms_thread; /* each thread with a private ms_thread structure */ +pthread_key_t ms_thread_key; /* array of thread context structure, each thread has a thread context structure */ static ms_thread_ctx_t *ms_thread_ctx; @@ -38,9 +38,10 @@ static void ms_create_worker(void *(*func)(void *), void *arg); static void ms_set_current_time() { struct timeval timer; + 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 */ @@ -50,16 +51,17 @@ static void ms_set_current_time() */ 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 (int i= 0; i < ms_thread.thread_ctx->nconns; i++) + for (int i= 0; i < ms_thread->thread_ctx->nconns; i++) { - c= &ms_thread.conn[i]; + c= &ms_thread->conn[i]; if (c->udp) { - time_diff= (int)(ms_thread.curr_time - c->start_time.tv_sec); + time_diff= (int)(ms_thread->curr_time - c->start_time.tv_sec); /* wait time out */ if (time_diff > SOCK_WAIT_TIMEOUT) @@ -81,9 +83,10 @@ static void ms_check_sock_timeout(void) /* if disconnect, the ever-1-second timer will call this function to reconnect */ static void ms_reconn_thread_socks(void) { - for (int i= 0; i < ms_thread.thread_ctx->nconns; i++) + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); + for (int i= 0; i < ms_thread->thread_ctx->nconns; i++) { - ms_reconn_socks(&ms_thread.conn[i]); + ms_reconn_socks(&ms_thread->conn[i]); } } /* ms_reconn_thread_socks */ @@ -97,6 +100,7 @@ static void ms_reconn_thread_socks(void) */ 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 @@ -108,22 +112,22 @@ 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); + evtimer_del(&ms_thread->clock_event); ms_check_sock_timeout(); } else { - ms_thread.initialized= true; + ms_thread->initialized= true; } ms_reconn_thread_socks(); - evtimer_set(&ms_thread.clock_event, ms_clock_handler, 0); - event_base_set(ms_thread.base, &ms_thread.clock_event); - evtimer_add(&ms_thread.clock_event, &t); + evtimer_set(&ms_thread->clock_event, ms_clock_handler, 0); + event_base_set(ms_thread->base, &ms_thread->clock_event); + evtimer_add(&ms_thread->clock_event, &t); } /* ms_clock_handler */ @@ -165,15 +169,19 @@ static int ms_set_thread_cpu_affinity(int cpu) */ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) { - ms_thread.thread_ctx= thread_ctx; - ms_thread.nactive_conn= thread_ctx->nconns; - ms_thread.initialized= false; + + 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 volatile uint32_t cnt= 0; - gettimeofday(&ms_thread.startup_time, NULL); + gettimeofday(&ms_thread->startup_time, NULL); - ms_thread.base= event_init(); - if (ms_thread.base == NULL) + ms_thread->base= event_init(); + if (ms_thread->base == NULL) { if (atomic_add_32_nv(&cnt, 1) == 0) { @@ -183,9 +191,9 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) return -1; } - ms_thread.conn= + ms_thread->conn= (ms_conn_t *)malloc((size_t)thread_ctx->nconns * sizeof(ms_conn_t)); - if (ms_thread.conn == NULL) + if (ms_thread->conn == NULL) { if (atomic_add_32_nv(&cnt, 1) == 0) { @@ -196,12 +204,12 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) 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 (int i= 0; i < thread_ctx->nconns; i++) { - ms_thread.conn[i].conn_idx= i; - if (ms_setup_conn(&ms_thread.conn[i]) != 0) + 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) @@ -226,6 +234,7 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) */ static void *ms_worker_libevent(void *arg) { + ms_thread_t *ms_thread= pthread_getspecific(ms_thread_key); ms_thread_ctx_t *thread_ctx= (ms_thread_ctx_t *)arg; /** @@ -245,7 +254,7 @@ static void *ms_worker_libevent(void *arg) /* each thread with a timer */ ms_clock_handler(0, 0, 0); - event_base_loop(ms_thread.base, 0); + event_base_loop(ms_thread->base, 0); return NULL; } /* ms_worker_libevent */ @@ -302,6 +311,11 @@ void ms_thread_init() / ms_setting.nconns; } + 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 (int i= 0; i < ms_setting.nthreads; i++) { @@ -317,4 +331,5 @@ void ms_thread_cleanup() { free(ms_thread_ctx); } + pthread_key_delete(ms_thread_key); } /* ms_thread_cleanup */ diff --git a/configure.ac b/configure.ac index c594d67f..aa051dd2 100644 --- a/configure.ac +++ b/configure.ac @@ -17,6 +17,9 @@ PANDORA_CANONICAL_TARGET AC_SEARCH_LIBS(getopt_long, gnugetopt) AC_SEARCH_LIBS(gethostbyname, nsl) +AC_CHECK_FUNCS_ONCE([getline]) + + PANDORA_HAVE_LIBEVENT PANDORA_REQUIRE_PTHREAD PANDORA_CXX_DEMANGLE