1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* File: ms_thread.c
* Author: Mingqiang Zhuang
*
* Created on February 10, 2009
*
* (c) Copyright 2009, Schooner Information Technology, Inc.
* http://www.schoonerinfotech.com/
*
*/
#include "config.h"
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include "ms_thread.h"
#include "ms_setting.h"
#include "ms_atomic.h"
/* global variable */
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;
/* functions */
static void ms_set_current_time(void);
static void ms_check_sock_timeout(void);
static void ms_clock_handler(const int fd, const short which, void *arg);
static uint32_t ms_set_thread_cpu_affinity(uint32_t cpu);
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()
{
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_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;
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);
/* wait time out */
if (time_diff > SOCK_WAIT_TIMEOUT)
{
/* calculate dropped packets count */
if (c->recvpkt > 0)
{
atomic_add_size(&ms_stats.pkt_drop, c->packets - c->recvpkt);
}
atomic_add_size(&ms_stats.udp_timeout, 1);
ms_reset_conn(c, true);
}
}
}
} /* 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++)
{
ms_reconn_socks(&ms_thread->conn[i]);
}
} /* ms_reconn_thread_socks */
/**
* the handler of the ever-1-second timer
*
* @param fd, the descriptors of the socket
* @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
};
UNUSED_ARGUMENT(fd);
UNUSED_ARGUMENT(which);
UNUSED_ARGUMENT(arg);
ms_set_current_time();
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;
}
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);
} /* ms_clock_handler */
/**
* used to bind thread to CPU if the system supports
*
* @param cpu, cpu index
*
* @return if success, return EXIT_SUCCESS, else return -1
*/
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)
{
fprintf(stderr, "WARNING: Could not set CPU Affinity, continuing...\n");
ret= 1;
}
#else
UNUSED_ARGUMENT(cpu);
#endif
return ret;
} /* ms_set_thread_cpu_affinity */
/**
* Set up a thread's information.
*
* @param thread_ctx, pointer of the thread context structure
*
* @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);
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);
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.");
}
return -1;
}
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)
{
/* only output this error once */
if (atomic_add_32_nv(&cnt, 1) == 0)
{
fprintf(stderr, "Initializing connection failed.\n");
}
return -1;
}
}
return EXIT_SUCCESS;
} /* ms_setup_thread */
/**
* Worker thread: main event loop
*
* @param arg, the pointer of argument
*
* @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;
/**
* 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)
{
ms_set_thread_cpu_affinity(thread_ctx->thd_idx % ms_setting.ncpu);
}
if (ms_setup_thread(thread_ctx) != 0)
{
exit(1);
}
/* each thread with a timer */
ms_clock_handler(0, 0, 0);
pthread_mutex_lock(&ms_global.init_lock.lock);
ms_global.init_lock.count++;
pthread_cond_signal(&ms_global.init_lock.cond);
pthread_mutex_unlock(&ms_global.init_lock.lock);
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)
{
pthread_t thread;
pthread_attr_t attr;
int ret;
pthread_attr_init(&attr);
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)
{
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;
/**
* 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;
}
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]);
}
} /* ms_thread_init */
/* cleanup some resource of threads when all the threads exit */
void ms_thread_cleanup()
{
if (ms_thread_ctx != NULL)
{
free(ms_thread_ctx);
}
pthread_key_delete(ms_thread_key);
} /* ms_thread_cleanup */
|