Schooner memslap changes
[m6w6/libmemcached] / clients / ms_thread.h
1 /*
2 * File: ms_thread.h
3 * Author: Mingqiang Zhuang
4 *
5 * Created on February 10, 2009
6 *
7 * (c) Copyright 2009, Schooner Information Technology, Inc.
8 * http://www.schoonerinfotech.com/
9 *
10 */
11
12 /**
13 * Asynchronous memslap has the similar implementation of
14 * multi-threads with memcached. Asynchronous memslap creates
15 * one or more self-governed threads; each thread is bound with
16 * one CPU core if the system supports setting CPU core
17 * affinity. And every thread has private variables. There is
18 * less communication or some shared resources among all the
19 * threads. It can improve the performance because there are
20 * fewer locks and competition. In addition, each thread has a
21 * libevent to manage the events of network. Each thread has one
22 * or more self-governed concurrencies; each concurrency has one
23 * or more socket connections. All the concurrencies don't
24 * communicate with each other even though they are in the same
25 * thread.
26 */
27 #ifndef MS_THREAD_H
28 #define MS_THREAD_H
29
30 #include <sched.h>
31 #include "ms_conn.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** Time relative to server start. Smaller than time_t on 64-bit systems. */
38 typedef unsigned int rel_time_t;
39
40 /* Used to store the context of each thread */
41 typedef struct thread_ctx {
42 int thd_idx; /* the thread index */
43 int nconns; /* how many connections included by the thread */
44 int srv_idx; /* index of the thread */
45 int tps_perconn; /* expected throughput per connection */
46 int64_t exec_num_perconn; /* execute number per connection */
47 } ms_thread_ctx_t;
48
49 /* Used to store the private variables of each thread */
50 typedef struct thread {
51 ms_conn_t *conn; /* conn array to store all the conn in the thread */
52 int nactive_conn; /* how many connects are active */
53
54 ms_thread_ctx_t *thread_ctx; /* thread context from the caller */
55 struct event_base *base; /* libevent handler created by this thread */
56
57 rel_time_t curr_time; /* current time */
58 struct event clock_event; /* clock event to time each one second */
59 bool initialized; /* whether clock_event has been initialized */
60
61 struct timeval startup_time; /* start time of the thread */
62 } ms_thread_t;
63
64 /* initialize threads */
65 void ms_thread_init(void);
66
67 /* cleanup some resource of threads when all the threads exit */
68 void ms_thread_cleanup(void);
69
70 #ifdef __cplusplus
71 }
72 #endif
73
74 #endif /* end of MS_THREAD_H */