#include "config.h"
#include <stdio.h>
+#include <inttypes.h>
#include <limits.h>
#include <sys/uio.h>
#include <event.h>
/* 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);
*/
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(
*/
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;
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,
*/
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=
{
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)
*/
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)
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 */
*/
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 */
pthread_mutex_unlock(&ms_global.run_lock.lock);
}
- if (ms_thread.nactive_conn == 0)
+ if (ms_thread->nactive_conn == 0)
{
pthread_exit(NULL);
}
*/
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;
}
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;
}
*/
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;
}
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;
}
"\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"
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"
*/
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;
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;
}
#include "config.h"
#include <ctype.h>
+#include <inttypes.h>
#include <strings.h>
#include "ms_setting.h"
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 <limits.h>
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
}
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);
#include "config.h"
+#include <inttypes.h>
#include "ms_stats.h"
#define array_size(x) (sizeof(x) / sizeof((x)[0]))
{
printf("\n %2d:", (int)i);
}
- printf(" %6ld", stat->dist[i]);
+ printf(" %6" PRIu64 , stat->dist[i]);
}
printf("\n\n");
#include "config.h"
+#include <inttypes.h>
+
#include "ms_thread.h"
#include "ms_setting.h"
#include "ms_atomic.h"
* 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);
}
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);
}
}
"\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"
{
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",
"\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"
{
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",
#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;
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 */
*/
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)
/* 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 */
*/
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
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 */
*/
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)
{
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)
{
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)
*/
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;
/**
/* 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 */
/ 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++)
{
{
free(ms_thread_ctx);
}
+ pthread_key_delete(ms_thread_key);
} /* ms_thread_cleanup */
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