Skip to content
Newer
Older
100644 845 lines (720 sloc) 23.2 KB
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Thread management for memcached.
4 */
5 #include "memcached.h"
869f186 @dormando Changelog update for 1.2.6 release and Patch originally from the Face…
dormando authored Sep 29, 2008
6 #include <assert.h>
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <errno.h>
d9b97d8 cleanup unistd.h, better spec file, remove warnings from threads.c
Paul Lindner authored May 7, 2007
11 #include <string.h>
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
12 #include <pthread.h>
13
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
14 #ifdef __sun
15 #include <atomic.h>
16 #endif
17
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
18 #define ITEMS_PER_ALLOC 64
19
20 /* An item in the connection queue. */
21 typedef struct conn_queue_item CQ_ITEM;
22 struct conn_queue_item {
f20c484 @dustin Fixed data types for init_state and protocol in CQ_ITEM.
dustin authored Feb 2, 2009
23 int sfd;
24 enum conn_states init_state;
25 int event_flags;
26 int read_buffer_size;
15ace4b fix and test for issue 38 (server does not respond to binary requests)
Eric Lambert authored Apr 8, 2009
27 enum network_transport transport;
f20c484 @dustin Fixed data types for init_state and protocol in CQ_ITEM.
dustin authored Feb 3, 2009
28 CQ_ITEM *next;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
29 };
30
31 /* A connection queue. */
32 typedef struct conn_queue CQ;
33 struct conn_queue {
34 CQ_ITEM *head;
35 CQ_ITEM *tail;
36 pthread_mutex_t lock;
37 pthread_cond_t cond;
38 };
39
40 /* Lock for cache operations (item_*, assoc_*) */
7f09e20 Do hash expansion in it's own thread
Trond Norbye authored Jan 27, 2009
41 pthread_mutex_t cache_lock;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
42
a0e4a75 @dormando fix resumption of accept loop under multithread
dormando authored Apr 10, 2009
43 /* Connection lock around accepting new connections */
44 pthread_mutex_t conn_lock = PTHREAD_MUTEX_INITIALIZER;
45
de1f30c @dormando properly detect GCC atomics
dormando authored Jan 25, 2012
46 #if !defined(HAVE_GCC_ATOMICS) && !defined(__sun)
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
47 pthread_mutex_t atomics_mutex = PTHREAD_MUTEX_INITIALIZER;
48 #endif
49
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
50 /* Lock for global stats */
51 static pthread_mutex_t stats_lock;
52
53 /* Free list of CQ_ITEM structs */
54 static CQ_ITEM *cqi_freelist;
55 static pthread_mutex_t cqi_freelist_lock;
56
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
57 static pthread_mutex_t *item_locks;
97409b3 @dormando Use a proper hash mask for item lock table
dormando authored Oct 12, 2011
58 /* size of the item lock hash table */
59 static uint32_t item_lock_count;
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
60 #define hashsize(n) ((unsigned long int)1<<(n))
61 #define hashmask(n) (hashsize(n)-1)
62 /* this lock is temporarily engaged during a hash table expansion */
63 static pthread_mutex_t item_global_lock;
64 /* thread-specific variable for deeply finding the item lock type */
65 static pthread_key_t item_lock_type_key;
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
66
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
67 static LIBEVENT_DISPATCHER_THREAD dispatcher_thread;
68
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
69 /*
70 * Each libevent instance has a wakeup pipe, which other threads
71 * can use to signal that they've put a new connection on its queue.
72 */
73 static LIBEVENT_THREAD *threads;
74
75 /*
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
76 * Number of worker threads that have finished setting themselves up.
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
77 */
78 static int init_count = 0;
79 static pthread_mutex_t init_lock;
80 static pthread_cond_t init_cond;
81
82
83 static void thread_libevent_process(int fd, short which, void *arg);
84
b19b41d @swills - Fix inline issue with older compilers (gcc 4.2.2)
swills authored Feb 2, 2012
85 unsigned short refcount_incr(unsigned short *refcount) {
de1f30c @dormando properly detect GCC atomics
dormando authored Jan 26, 2012
86 #ifdef HAVE_GCC_ATOMICS
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
87 return __sync_add_and_fetch(refcount, 1);
88 #elif defined(__sun)
89 return atomic_inc_ushort_nv(refcount);
90 #else
91 unsigned short res;
92 mutex_lock(&atomics_mutex);
de1f30c @dormando properly detect GCC atomics
dormando authored Jan 26, 2012
93 (*refcount)++;
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
94 res = *refcount;
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
95 mutex_unlock(&atomics_mutex);
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
96 return res;
97 #endif
98 }
99
b19b41d @swills - Fix inline issue with older compilers (gcc 4.2.2)
swills authored Feb 2, 2012
100 unsigned short refcount_decr(unsigned short *refcount) {
de1f30c @dormando properly detect GCC atomics
dormando authored Jan 26, 2012
101 #ifdef HAVE_GCC_ATOMICS
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
102 return __sync_sub_and_fetch(refcount, 1);
103 #elif defined(__sun)
104 return atomic_dec_ushort_nv(refcount);
105 #else
106 unsigned short res;
107 mutex_lock(&atomics_mutex);
de1f30c @dormando properly detect GCC atomics
dormando authored Jan 26, 2012
108 (*refcount)--;
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
109 res = *refcount;
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
110 mutex_unlock(&atomics_mutex);
3b96138 @dormando more portable refcount atomics
dormando authored Jan 10, 2012
111 return res;
112 #endif
113 }
114
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
115 /* Convenience functions for calling *only* when in ITEM_LOCK_GLOBAL mode */
116 void item_lock_global(void) {
117 mutex_lock(&item_global_lock);
118 }
119
120 void item_unlock_global(void) {
121 mutex_unlock(&item_global_lock);
122 }
123
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
124 void item_lock(uint32_t hv) {
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
125 uint8_t *lock_type = pthread_getspecific(item_lock_type_key);
126 if (likely(*lock_type == ITEM_LOCK_GRANULAR)) {
127 mutex_lock(&item_locks[(hv & hashmask(hashpower)) % item_lock_count]);
128 } else {
129 mutex_lock(&item_global_lock);
130 }
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
131 }
132
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
133 /* Special case. When ITEM_LOCK_GLOBAL mode is enabled, this should become a
134 * no-op, as it's only called from within the item lock if necessary.
135 * However, we can't mix a no-op and threads which are still synchronizing to
136 * GLOBAL. So instead we just always try to lock. When in GLOBAL mode this
137 * turns into an effective no-op. Threads re-synchronize after the power level
138 * switch so it should stay safe.
139 */
140 void *item_trylock(uint32_t hv) {
141 pthread_mutex_t *lock = &item_locks[(hv & hashmask(hashpower)) % item_lock_count];
142 if (pthread_mutex_trylock(lock) == 0) {
143 return lock;
144 }
145 return NULL;
146 }
147
148 void item_trylock_unlock(void *lock) {
149 mutex_unlock((pthread_mutex_t *) lock);
2db1bf4 @dormando alloc loop now attempts an item_lock
dormando authored Aug 17, 2012
150 }
151
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
152 void item_unlock(uint32_t hv) {
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
153 uint8_t *lock_type = pthread_getspecific(item_lock_type_key);
154 if (likely(*lock_type == ITEM_LOCK_GRANULAR)) {
155 mutex_unlock(&item_locks[(hv & hashmask(hashpower)) % item_lock_count]);
156 } else {
157 mutex_unlock(&item_global_lock);
158 }
159 }
160
161 static void wait_for_thread_registration(int nthreads) {
162 while (init_count < nthreads) {
163 pthread_cond_wait(&init_cond, &init_lock);
164 }
165 }
166
167 static void register_thread_initialized(void) {
168 pthread_mutex_lock(&init_lock);
169 init_count++;
170 pthread_cond_signal(&init_cond);
171 pthread_mutex_unlock(&init_lock);
172 }
173
174 void switch_item_lock_type(enum item_lock_types type) {
175 char buf[1];
176 int i;
177
178 switch (type) {
179 case ITEM_LOCK_GRANULAR:
180 buf[0] = 'l';
181 break;
182 case ITEM_LOCK_GLOBAL:
183 buf[0] = 'g';
184 break;
185 default:
186 fprintf(stderr, "Unknown lock type: %d\n", type);
187 assert(1 == 0);
188 break;
189 }
190
191 pthread_mutex_lock(&init_lock);
192 init_count = 0;
193 for (i = 0; i < settings.num_threads; i++) {
194 if (write(threads[i].notify_send_fd, buf, 1) != 1) {
195 perror("Failed writing to notify pipe");
196 /* TODO: This is a fatal problem. Can it ever happen temporarily? */
197 }
198 }
199 wait_for_thread_registration(settings.num_threads);
200 pthread_mutex_unlock(&init_lock);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
201 }
202
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
203 /*
204 * Initializes a connection queue.
205 */
206 static void cq_init(CQ *cq) {
207 pthread_mutex_init(&cq->lock, NULL);
208 pthread_cond_init(&cq->cond, NULL);
209 cq->head = NULL;
210 cq->tail = NULL;
211 }
212
213 /*
214 * Looks for an item on a connection queue, but doesn't block if there isn't
215 * one.
d9b97d8 cleanup unistd.h, better spec file, remove warnings from threads.c
Paul Lindner authored May 7, 2007
216 * Returns the item, or NULL if no item is available
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
217 */
b2b4942 Removed dead code and renamed cq_peek to cq_pop
Toru Maesaka authored Oct 15, 2008
218 static CQ_ITEM *cq_pop(CQ *cq) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
219 CQ_ITEM *item;
220
221 pthread_mutex_lock(&cq->lock);
222 item = cq->head;
223 if (NULL != item) {
224 cq->head = item->next;
225 if (NULL == cq->head)
226 cq->tail = NULL;
227 }
228 pthread_mutex_unlock(&cq->lock);
229
230 return item;
231 }
232
233 /*
234 * Adds an item to a connection queue.
235 */
236 static void cq_push(CQ *cq, CQ_ITEM *item) {
237 item->next = NULL;
238
239 pthread_mutex_lock(&cq->lock);
240 if (NULL == cq->tail)
241 cq->head = item;
242 else
243 cq->tail->next = item;
244 cq->tail = item;
245 pthread_cond_signal(&cq->cond);
246 pthread_mutex_unlock(&cq->lock);
247 }
248
249 /*
250 * Returns a fresh connection queue item.
251 */
df1b7e4 Removed compiler warnings and increased warning level
Trond Norbye authored Oct 6, 2008
252 static CQ_ITEM *cqi_new(void) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
253 CQ_ITEM *item = NULL;
254 pthread_mutex_lock(&cqi_freelist_lock);
255 if (cqi_freelist) {
256 item = cqi_freelist;
257 cqi_freelist = item->next;
258 }
259 pthread_mutex_unlock(&cqi_freelist_lock);
260
261 if (NULL == item) {
262 int i;
263
264 /* Allocate a bunch of items at once to reduce fragmentation */
265 item = malloc(sizeof(CQ_ITEM) * ITEMS_PER_ALLOC);
266 if (NULL == item)
267 return NULL;
268
269 /*
270 * Link together all the new items except the first one
271 * (which we'll return to the caller) for placement on
272 * the freelist.
273 */
274 for (i = 2; i < ITEMS_PER_ALLOC; i++)
275 item[i - 1].next = &item[i];
276
277 pthread_mutex_lock(&cqi_freelist_lock);
278 item[ITEMS_PER_ALLOC - 1].next = cqi_freelist;
279 cqi_freelist = &item[1];
280 pthread_mutex_unlock(&cqi_freelist_lock);
281 }
282
283 return item;
284 }
285
286
287 /*
288 * Frees a connection queue item (adds it to the freelist.)
289 */
290 static void cqi_free(CQ_ITEM *item) {
291 pthread_mutex_lock(&cqi_freelist_lock);
292 item->next = cqi_freelist;
293 cqi_freelist = item;
294 pthread_mutex_unlock(&cqi_freelist_lock);
295 }
296
297
298 /*
299 * Creates a worker thread.
300 */
301 static void create_worker(void *(*func)(void *), void *arg) {
302 pthread_t thread;
303 pthread_attr_t attr;
304 int ret;
305
306 pthread_attr_init(&attr);
307
d9b97d8 cleanup unistd.h, better spec file, remove warnings from threads.c
Paul Lindner authored May 7, 2007
308 if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
309 fprintf(stderr, "Can't create thread: %s\n",
310 strerror(ret));
311 exit(1);
312 }
313 }
314
a0e4a75 @dormando fix resumption of accept loop under multithread
dormando authored Apr 11, 2009
315 /*
316 * Sets whether or not we accept new connections.
317 */
318 void accept_new_conns(const bool do_accept) {
319 pthread_mutex_lock(&conn_lock);
320 do_accept_new_conns(do_accept);
321 pthread_mutex_unlock(&conn_lock);
322 }
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
323 /****************************** LIBEVENT THREADS *****************************/
324
325 /*
326 * Set up a thread's information.
327 */
328 static void setup_thread(LIBEVENT_THREAD *me) {
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
329 me->base = event_init();
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
330 if (! me->base) {
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
331 fprintf(stderr, "Can't allocate event base\n");
332 exit(1);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
333 }
334
335 /* Listen for notifications from other threads */
336 event_set(&me->notify_event, me->notify_receive_fd,
337 EV_READ | EV_PERSIST, thread_libevent_process, me);
338 event_base_set(me->base, &me->notify_event);
339
340 if (event_add(&me->notify_event, 0) == -1) {
341 fprintf(stderr, "Can't monitor libevent notify pipe\n");
342 exit(1);
343 }
344
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
345 me->new_conn_queue = malloc(sizeof(struct conn_queue));
346 if (me->new_conn_queue == NULL) {
347 perror("Failed to allocate memory for connection queue");
348 exit(EXIT_FAILURE);
349 }
350 cq_init(me->new_conn_queue);
351
352 if (pthread_mutex_init(&me->stats.mutex, NULL) != 0) {
353 perror("Failed to initialize mutex");
354 exit(EXIT_FAILURE);
355 }
4c86fa5 Create a generic cache for objects of same size
Trond Norbye authored Mar 27, 2009
356
357 me->suffix_cache = cache_create("suffix", SUFFIX_SIZE, sizeof(char*),
358 NULL, NULL);
359 if (me->suffix_cache == NULL) {
360 fprintf(stderr, "Failed to create suffix cache\n");
361 exit(EXIT_FAILURE);
362 }
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
363 }
364
365 /*
366 * Worker thread: main event loop
367 */
368 static void *worker_libevent(void *arg) {
369 LIBEVENT_THREAD *me = arg;
370
371 /* Any per-thread setup can happen here; thread_init() will block until
372 * all threads have finished initializing.
373 */
374
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
375 /* set an indexable thread-specific memory item for the lock type.
376 * this could be unnecessary if we pass the conn *c struct through
377 * all item_lock calls...
378 */
379 me->item_lock_type = ITEM_LOCK_GRANULAR;
380 pthread_setspecific(item_lock_type_key, &me->item_lock_type);
381
382 register_thread_initialized();
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
383
df1b7e4 Removed compiler warnings and increased warning level
Trond Norbye authored Oct 6, 2008
384 event_base_loop(me->base, 0);
385 return NULL;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
386 }
387
388
389 /*
390 * Processes an incoming "handle a new connection" item. This is called when
391 * input arrives on the libevent wakeup pipe.
392 */
393 static void thread_libevent_process(int fd, short which, void *arg) {
394 LIBEVENT_THREAD *me = arg;
395 CQ_ITEM *item;
396 char buf[1];
397
398 if (read(fd, buf, 1) != 1)
399 if (settings.verbose > 0)
400 fprintf(stderr, "Can't read from libevent pipe\n");
401
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
402 switch (buf[0]) {
403 case 'c':
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
404 item = cq_pop(me->new_conn_queue);
d9b97d8 cleanup unistd.h, better spec file, remove warnings from threads.c
Paul Lindner authored May 7, 2007
405
406 if (NULL != item) {
407 conn *c = conn_new(item->sfd, item->init_state, item->event_flags,
15ace4b fix and test for issue 38 (server does not respond to binary requests)
Eric Lambert authored Apr 9, 2009
408 item->read_buffer_size, item->transport, me->base);
0b211d4 Fix minor style inconsistencies and add some missing error checking.
Steven Grimm authored Jul 8, 2007
409 if (c == NULL) {
15ace4b fix and test for issue 38 (server does not respond to binary requests)
Eric Lambert authored Apr 9, 2009
410 if (IS_UDP(item->transport)) {
d9b97d8 cleanup unistd.h, better spec file, remove warnings from threads.c
Paul Lindner authored May 7, 2007
411 fprintf(stderr, "Can't listen for events on UDP socket\n");
412 exit(1);
413 } else {
414 if (settings.verbose > 0) {
415 fprintf(stderr, "Can't listen for events on fd %d\n",
416 item->sfd);
417 }
418 close(item->sfd);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
419 }
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
420 } else {
421 c->thread = me;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
422 }
423 cqi_free(item);
424 }
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
425 break;
426 /* we were told to flip the lock type and report in */
427 case 'l':
428 me->item_lock_type = ITEM_LOCK_GRANULAR;
429 register_thread_initialized();
430 break;
431 case 'g':
432 me->item_lock_type = ITEM_LOCK_GLOBAL;
433 register_thread_initialized();
434 break;
435 }
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
436 }
437
438 /* Which thread we assigned a connection to most recently. */
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
439 static int last_thread = -1;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
440
441 /*
442 * Dispatches a new connection to another thread. This is only ever called
443 * from the main thread, either during initialization (for UDP) or because
444 * of an incoming connection.
445 */
1437fe9 1) I don't have any prototype for daemon 2) The signature for dispatc…
Trond Norbye authored Mar 25, 2008
446 void dispatch_conn_new(int sfd, enum conn_states init_state, int event_flags,
15ace4b fix and test for issue 38 (server does not respond to binary requests)
Eric Lambert authored Apr 9, 2009
447 int read_buffer_size, enum network_transport transport) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
448 CQ_ITEM *item = cqi_new();
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
449 char buf[1];
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
450 int tid = (last_thread + 1) % settings.num_threads;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
451
869f186 @dormando Changelog update for 1.2.6 release and Patch originally from the Face…
dormando authored Sep 29, 2008
452 LIBEVENT_THREAD *thread = threads + tid;
453
454 last_thread = tid;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
455
456 item->sfd = sfd;
457 item->init_state = init_state;
458 item->event_flags = event_flags;
459 item->read_buffer_size = read_buffer_size;
15ace4b fix and test for issue 38 (server does not respond to binary requests)
Eric Lambert authored Apr 9, 2009
460 item->transport = transport;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
461
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
462 cq_push(thread->new_conn_queue, item);
6895721 Add DTrace probes for Solaris/etc.
Trond Norbye authored Jul 23, 2008
463
b2e7e90 DTrace probe fix, Fix typo in configure script, Use calloc(3) instead…
Ricky Zhou authored Sep 29, 2008
464 MEMCACHED_CONN_DISPATCH(sfd, thread->thread_id);
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
465 buf[0] = 'c';
466 if (write(thread->notify_send_fd, buf, 1) != 1) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
467 perror("Writing to thread notify pipe");
468 }
469 }
470
471 /*
472 * Returns true if this is the thread that listens for new TCP connections.
473 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
474 int is_listen_thread() {
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
475 return pthread_self() == dispatcher_thread.thread_id;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
476 }
477
478 /********************************* ITEM ACCESS *******************************/
479
480 /*
481 * Allocates a new item.
482 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
483 item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
484 item *it;
09f54cb @dormando push cache_lock deeper into item_alloc
dormando authored Oct 3, 2011
485 /* do_item_alloc handles its own locks */
2db1bf4 @dormando alloc loop now attempts an item_lock
dormando authored Aug 18, 2012
486 it = do_item_alloc(key, nkey, flags, exptime, nbytes, 0);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
487 return it;
488 }
489
490 /*
5da8dba Merged Trond Norbye's protocol patch.
Trond Norbye authored Jul 28, 2008
491 * Returns an item if it hasn't been marked as expired,
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
492 * lazy-expiring as needed.
493 */
5da8dba Merged Trond Norbye's protocol patch.
Trond Norbye authored Jul 28, 2008
494 item *item_get(const char *key, const size_t nkey) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
495 item *it;
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
496 uint32_t hv;
497 hv = hash(key, nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
498 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
499 it = do_item_get(key, nkey, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
500 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
501 return it;
502 }
503
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
504 item *item_touch(const char *key, size_t nkey, uint32_t exptime) {
505 item *it;
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
506 uint32_t hv;
507 hv = hash(key, nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
508 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
509 it = do_item_touch(key, nkey, exptime, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
510 item_unlock(hv);
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
511 return it;
512 }
513
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
514 /*
515 * Links an item into the LRU and hashtable.
516 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
517 int item_link(item *item) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
518 int ret;
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
519 uint32_t hv;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
520
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
521 hv = hash(ITEM_key(item), item->nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
522 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
523 ret = do_item_link(item, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
524 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
525 return ret;
526 }
527
528 /*
529 * Decrements the reference count on an item and adds it to the freelist if
530 * needed.
531 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
532 void item_remove(item *item) {
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
533 uint32_t hv;
534 hv = hash(ITEM_key(item), item->nkey, 0);
535
536 item_lock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
537 do_item_remove(item);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
538 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
539 }
540
541 /*
542 * Replaces one item with another in the hashtable.
d5618da Code cleaned by running devtools/clean-whitespace.pl script.
Toru Maesaka authored Aug 20, 2008
543 * Unprotected by a mutex lock since the core server does not require
600ec99 The core server never called the mutex wrapper for do_item_replace()
Toru Maesaka authored May 9, 2008
544 * it to be thread-safe.
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
545 */
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
546 int item_replace(item *old_it, item *new_it, const uint32_t hv) {
547 return do_item_replace(old_it, new_it, hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
548 }
549
550 /*
551 * Unlinks an item from the LRU and hashtable.
552 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
553 void item_unlink(item *item) {
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
554 uint32_t hv;
555 hv = hash(ITEM_key(item), item->nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
556 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
557 do_item_unlink(item, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
558 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
559 }
560
561 /*
562 * Moves an item to the back of the LRU queue.
563 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
564 void item_update(item *item) {
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
565 uint32_t hv;
566 hv = hash(ITEM_key(item), item->nkey, 0);
567
568 item_lock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
569 do_item_update(item);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
570 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
571 }
572
573 /*
574 * Does arithmetic on a numeric item value.
575 */
cbcd387 @dormando fix incr/decr race conditions for ASCII prot
dormando authored Jul 11, 2011
576 enum delta_result_type add_delta(conn *c, const char *key,
577 const size_t nkey, int incr,
ea2d42a @dormando fix incr/decr race conditions for binary prot
dormando authored Jul 11, 2011
578 const int64_t delta, char *buf,
579 uint64_t *cas) {
d044acb @dustin add_delta should return a proper status indicator.
dustin authored Jun 27, 2009
580 enum delta_result_type ret;
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
581 uint32_t hv;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
582
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
583 hv = hash(key, nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
584 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
585 ret = do_add_delta(c, key, nkey, incr, delta, buf, cas, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
586 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
587 return ret;
588 }
589
590 /*
591 * Stores an item in the cache (high level, obeys set/add/replace semantics)
592 */
e5d053c @dustin Magic number cleanup in do_store_item.
dustin authored Feb 24, 2009
593 enum store_item_type store_item(item *item, int comm, conn* c) {
594 enum store_item_type ret;
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
595 uint32_t hv;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
596
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
597 hv = hash(ITEM_key(item), item->nkey, 0);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
598 item_lock(hv);
bab9acd @dormando move hash calls outside of cache_lock
dormando authored Oct 2, 2011
599 ret = do_store_item(item, comm, c, hv);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
600 item_unlock(hv);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
601 return ret;
602 }
603
604 /*
605 * Flushes expired items after a flush_all call
606 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
607 void item_flush_expired() {
45e0e95 @dormando Use spinlocks for main cache lock
dormando authored Oct 2, 2011
608 mutex_lock(&cache_lock);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
609 do_item_flush_expired();
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
610 mutex_unlock(&cache_lock);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
611 }
612
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
613 /*
614 * Dumps part of the cache
615 */
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
616 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) {
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
617 char *ret;
618
45e0e95 @dormando Use spinlocks for main cache lock
dormando authored Oct 2, 2011
619 mutex_lock(&cache_lock);
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
620 ret = do_item_cachedump(slabs_clsid, limit, bytes);
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
621 mutex_unlock(&cache_lock);
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
622 return ret;
623 }
624
625 /*
626 * Dumps statistics about slab classes
627 */
17df5c0 Don't expose the protocol used to the client api of the stats
Trond Norbye authored Apr 2, 2009
628 void item_stats(ADD_STAT add_stats, void *c) {
45e0e95 @dormando Use spinlocks for main cache lock
dormando authored Oct 2, 2011
629 mutex_lock(&cache_lock);
17df5c0 Don't expose the protocol used to the client api of the stats
Trond Norbye authored Apr 2, 2009
630 do_item_stats(add_stats, c);
67d7cfc @dormando remove global stats lock from item allocation
dormando authored Aug 31, 2012
631 mutex_unlock(&cache_lock);
632 }
633
634 void item_stats_totals(ADD_STAT add_stats, void *c) {
635 mutex_lock(&cache_lock);
636 do_item_stats_totals(add_stats, c);
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
637 mutex_unlock(&cache_lock);
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
638 }
639
640 /*
641 * Dumps a list of objects of each size in 32-byte increments
642 */
17df5c0 Don't expose the protocol used to the client api of the stats
Trond Norbye authored Apr 2, 2009
643 void item_stats_sizes(ADD_STAT add_stats, void *c) {
45e0e95 @dormando Use spinlocks for main cache lock
dormando authored Oct 2, 2011
644 mutex_lock(&cache_lock);
17df5c0 Don't expose the protocol used to the client api of the stats
Trond Norbye authored Apr 2, 2009
645 do_item_stats_sizes(add_stats, c);
890dfb7 @dormando call mutex_unlock() when we use mutex_lock()
dormando authored Jul 30, 2012
646 mutex_unlock(&cache_lock);
6f60aac Make item stats commands thread-safe
Steven Grimm authored Jul 8, 2007
647 }
648
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
649 /******************************* GLOBAL STATS ******************************/
650
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
651 void STATS_LOCK() {
4be8a5a Fixed bad patch: stats locks shouldn't be commented out
Trond Norbye authored Jan 28, 2009
652 pthread_mutex_lock(&stats_lock);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
653 }
654
a9dcd9a Code modified to only build and run under multi-threaded mode.
Toru Maesaka authored Apr 28, 2008
655 void STATS_UNLOCK() {
4be8a5a Fixed bad patch: stats locks shouldn't be commented out
Trond Norbye authored Jan 28, 2009
656 pthread_mutex_unlock(&stats_lock);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
657 }
658
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
659 void threadlocal_stats_reset(void) {
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 6, 2009
660 int ii, sid;
effae30 Revert to pre-c99 style in for loops due to compiler problems on icc
Trond Norbye authored Mar 4, 2009
661 for (ii = 0; ii < settings.num_threads; ++ii) {
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
662 pthread_mutex_lock(&threads[ii].stats.mutex);
663
664 threads[ii].stats.get_cmds = 0;
665 threads[ii].stats.get_misses = 0;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
666 threads[ii].stats.touch_cmds = 0;
667 threads[ii].stats.touch_misses = 0;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 6, 2009
668 threads[ii].stats.delete_misses = 0;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
669 threads[ii].stats.incr_misses = 0;
670 threads[ii].stats.decr_misses = 0;
7dda2c4 @steveyen stats reset also clears cas_hits/badval/misses
steveyen authored Mar 25, 2009
671 threads[ii].stats.cas_misses = 0;
e2da378 Moved bytes_read and bytes_written to threadlocal stats
Trond Norbye authored Mar 6, 2009
672 threads[ii].stats.bytes_read = 0;
673 threads[ii].stats.bytes_written = 0;
534466e @dormando add a cmd_flush stat
dormando authored Mar 25, 2009
674 threads[ii].stats.flush_cmds = 0;
a21f819 Issue 61: reqs_per_event handling (-R) is incorrect leading to client…
Trond Norbye authored Jun 30, 2009
675 threads[ii].stats.conn_yields = 0;
5100e7a @ingenthr Added new stats to track sasl authentication.
ingenthr authored Nov 24, 2009
676 threads[ii].stats.auth_cmds = 0;
677 threads[ii].stats.auth_errors = 0;
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
678
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
679 for(sid = 0; sid < MAX_NUMBER_OF_SLAB_CLASSES; sid++) {
680 threads[ii].stats.slab_stats[sid].set_cmds = 0;
681 threads[ii].stats.slab_stats[sid].get_hits = 0;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
682 threads[ii].stats.slab_stats[sid].touch_hits = 0;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 7, 2009
683 threads[ii].stats.slab_stats[sid].delete_hits = 0;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
684 threads[ii].stats.slab_stats[sid].incr_hits = 0;
685 threads[ii].stats.slab_stats[sid].decr_hits = 0;
7dda2c4 @steveyen stats reset also clears cas_hits/badval/misses
steveyen authored Mar 25, 2009
686 threads[ii].stats.slab_stats[sid].cas_hits = 0;
687 threads[ii].stats.slab_stats[sid].cas_badval = 0;
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
688 }
689
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
690 pthread_mutex_unlock(&threads[ii].stats.mutex);
691 }
692 }
693
694 void threadlocal_stats_aggregate(struct thread_stats *stats) {
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
695 int ii, sid;
92d6e12 @toofishes Simplify stats aggregation code
toofishes authored Nov 2, 2010
696
697 /* The struct has a mutex, but we can safely set the whole thing
698 * to zero since it is unused when aggregating. */
699 memset(stats, 0, sizeof(*stats));
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
700
effae30 Revert to pre-c99 style in for loops due to compiler problems on icc
Trond Norbye authored Mar 4, 2009
701 for (ii = 0; ii < settings.num_threads; ++ii) {
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
702 pthread_mutex_lock(&threads[ii].stats.mutex);
703
704 stats->get_cmds += threads[ii].stats.get_cmds;
705 stats->get_misses += threads[ii].stats.get_misses;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
706 stats->touch_cmds += threads[ii].stats.touch_cmds;
707 stats->touch_misses += threads[ii].stats.touch_misses;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 7, 2009
708 stats->delete_misses += threads[ii].stats.delete_misses;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
709 stats->decr_misses += threads[ii].stats.decr_misses;
710 stats->incr_misses += threads[ii].stats.incr_misses;
15e6462 @dustin Added CAS stats.
dustin authored Mar 7, 2009
711 stats->cas_misses += threads[ii].stats.cas_misses;
e2da378 Moved bytes_read and bytes_written to threadlocal stats
Trond Norbye authored Mar 6, 2009
712 stats->bytes_read += threads[ii].stats.bytes_read;
713 stats->bytes_written += threads[ii].stats.bytes_written;
534466e @dormando add a cmd_flush stat
dormando authored Mar 25, 2009
714 stats->flush_cmds += threads[ii].stats.flush_cmds;
a21f819 Issue 61: reqs_per_event handling (-R) is incorrect leading to client…
Trond Norbye authored Jun 30, 2009
715 stats->conn_yields += threads[ii].stats.conn_yields;
5100e7a @ingenthr Added new stats to track sasl authentication.
ingenthr authored Nov 25, 2009
716 stats->auth_cmds += threads[ii].stats.auth_cmds;
717 stats->auth_errors += threads[ii].stats.auth_errors;
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
718
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
719 for (sid = 0; sid < MAX_NUMBER_OF_SLAB_CLASSES; sid++) {
720 stats->slab_stats[sid].set_cmds +=
721 threads[ii].stats.slab_stats[sid].set_cmds;
722 stats->slab_stats[sid].get_hits +=
723 threads[ii].stats.slab_stats[sid].get_hits;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
724 stats->slab_stats[sid].touch_hits +=
725 threads[ii].stats.slab_stats[sid].touch_hits;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 7, 2009
726 stats->slab_stats[sid].delete_hits +=
727 threads[ii].stats.slab_stats[sid].delete_hits;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
728 stats->slab_stats[sid].decr_hits +=
729 threads[ii].stats.slab_stats[sid].decr_hits;
730 stats->slab_stats[sid].incr_hits +=
731 threads[ii].stats.slab_stats[sid].incr_hits;
15e6462 @dustin Added CAS stats.
dustin authored Mar 7, 2009
732 stats->slab_stats[sid].cas_hits +=
733 threads[ii].stats.slab_stats[sid].cas_hits;
734 stats->slab_stats[sid].cas_badval +=
735 threads[ii].stats.slab_stats[sid].cas_badval;
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
736 }
737
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
738 pthread_mutex_unlock(&threads[ii].stats.mutex);
739 }
740 }
741
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
742 void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out) {
743 int sid;
744
745 out->set_cmds = 0;
746 out->get_hits = 0;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
747 out->touch_hits = 0;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 7, 2009
748 out->delete_hits = 0;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
749 out->incr_hits = 0;
750 out->decr_hits = 0;
15e6462 @dustin Added CAS stats.
dustin authored Mar 7, 2009
751 out->cas_hits = 0;
752 out->cas_badval = 0;
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
753
754 for (sid = 0; sid < MAX_NUMBER_OF_SLAB_CLASSES; sid++) {
755 out->set_cmds += stats->slab_stats[sid].set_cmds;
756 out->get_hits += stats->slab_stats[sid].get_hits;
d87f568 @dormando Backport binary TOUCH/GAT/GATQ commands
dormando authored Sep 27, 2011
757 out->touch_hits += stats->slab_stats[sid].touch_hits;
a77d12b @dustin Added stats for delete commands.
dustin authored Mar 7, 2009
758 out->delete_hits += stats->slab_stats[sid].delete_hits;
3e03078 @dustin incr/decr stats
dustin authored Mar 7, 2009
759 out->decr_hits += stats->slab_stats[sid].decr_hits;
760 out->incr_hits += stats->slab_stats[sid].incr_hits;
15e6462 @dustin Added CAS stats.
dustin authored Mar 7, 2009
761 out->cas_hits += stats->slab_stats[sid].cas_hits;
762 out->cas_badval += stats->slab_stats[sid].cas_badval;
25b5189 @dustin Push hit and set stats down into per-slab stats.
dustin authored Mar 7, 2009
763 }
764 }
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
765
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
766 /*
767 * Initializes the thread subsystem, creating various worker threads.
768 *
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
769 * nthreads Number of worker event handler threads to spawn
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
770 * main_base Event base for main thread
771 */
772 void thread_init(int nthreads, struct event_base *main_base) {
773 int i;
97409b3 @dormando Use a proper hash mask for item lock table
dormando authored Oct 13, 2011
774 int power;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
775
776 pthread_mutex_init(&cache_lock, NULL);
777 pthread_mutex_init(&stats_lock, NULL);
778
779 pthread_mutex_init(&init_lock, NULL);
780 pthread_cond_init(&init_cond, NULL);
781
782 pthread_mutex_init(&cqi_freelist_lock, NULL);
783 cqi_freelist = NULL;
784
97409b3 @dormando Use a proper hash mask for item lock table
dormando authored Oct 13, 2011
785 /* Want a wide lock table, but don't waste memory */
786 if (nthreads < 3) {
787 power = 10;
788 } else if (nthreads < 4) {
789 power = 11;
790 } else if (nthreads < 5) {
791 power = 12;
792 } else {
793 /* 8192 buckets, and central locks don't scale much past 5 threads */
794 power = 13;
795 }
796
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
797 item_lock_count = hashsize(power);
97409b3 @dormando Use a proper hash mask for item lock table
dormando authored Oct 13, 2011
798
799 item_locks = calloc(item_lock_count, sizeof(pthread_mutex_t));
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
800 if (! item_locks) {
801 perror("Can't allocate item locks");
802 exit(1);
803 }
97409b3 @dormando Use a proper hash mask for item lock table
dormando authored Oct 13, 2011
804 for (i = 0; i < item_lock_count; i++) {
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
805 pthread_mutex_init(&item_locks[i], NULL);
806 }
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
807 pthread_key_create(&item_lock_type_key, NULL);
808 pthread_mutex_init(&item_global_lock, NULL);
8fe5bf1 @dormando use item partitioned lock for as much as possible
dormando authored Oct 3, 2011
809
b2e7e90 DTrace probe fix, Fix typo in configure script, Use calloc(3) instead…
Ricky Zhou authored Sep 29, 2008
810 threads = calloc(nthreads, sizeof(LIBEVENT_THREAD));
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
811 if (! threads) {
812 perror("Can't allocate thread descriptors");
813 exit(1);
814 }
815
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
816 dispatcher_thread.base = main_base;
817 dispatcher_thread.thread_id = pthread_self();
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
818
819 for (i = 0; i < nthreads; i++) {
820 int fds[2];
821 if (pipe(fds)) {
822 perror("Can't create notify pipe");
823 exit(1);
824 }
825
826 threads[i].notify_receive_fd = fds[0];
827 threads[i].notify_send_fd = fds[1];
828
1fdfb7e Use threadlocal stats to count the commands
Trond Norbye authored Mar 4, 2009
829 setup_thread(&threads[i]);
d1f9d99 @dormando experimental maxconns_fast option
dormando authored Sep 27, 2011
830 /* Reserve three fds for the libevent base, and two for the pipe */
831 stats.reserved_fds += 5;
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
832 }
833
834 /* Create threads after we've done all the libevent setup. */
2fe44f1 Cleanup of number of threads declarations (issue 91)
Dmitry Isaykin authored Sep 18, 2009
835 for (i = 0; i < nthreads; i++) {
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
836 create_worker(worker_libevent, &threads[i]);
837 }
838
839 /* Wait for all the threads to set themselves up before returning. */
840 pthread_mutex_lock(&init_lock);
1c94e12 @dormando item locks now lock hash table buckets
dormando authored Aug 18, 2012
841 wait_for_thread_registration(nthreads);
5b30499 Merge multithreaded branch into trunk.
Steven Grimm authored Apr 16, 2007
842 pthread_mutex_unlock(&init_lock);
843 }
844
Something went wrong with that request. Please try again.