1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 3 /** \file 4 * The main memcached header holding commonly used data 5 * structures and function prototypes. 6 */ 7 8 #ifdef HAVE_CONFIG_H 9 #include "config.h" 10 #endif 11 12 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <sys/time.h> 15 #include <netinet/in.h> 16 #include <event.h> 17 #include <netdb.h> 18 #include <pthread.h> 19 #include <unistd.h> 20 #include <assert.h> 21 22 #include "protocol_binary.h" 23 #include "cache.h" 24 #include "logger.h" 25 26 #include "sasl_defs.h" 27 28 /** Maximum length of a key. */ 29 #define KEY_MAX_LENGTH 250 30 31 /** Size of an incr buf. */ 32 #define INCR_MAX_STORAGE_LEN 24 33 34 #define DATA_BUFFER_SIZE 2048 35 #define UDP_READ_BUFFER_SIZE 65536 36 #define UDP_MAX_PAYLOAD_SIZE 1400 37 #define UDP_HEADER_SIZE 8 38 #define MAX_SENDBUF_SIZE (256 * 1024 * 1024) 39 /* I'm told the max length of a 64-bit num converted to string is 20 bytes. 40 * Plus a few for spaces, \r\n, \0 */ 41 #define SUFFIX_SIZE 24 42 43 /** Initial size of list of items being returned by "get". */ 44 #define ITEM_LIST_INITIAL 200 45 46 /** Initial size of list of CAS suffixes appended to "gets" lines. */ 47 #define SUFFIX_LIST_INITIAL 20 48 49 /** Initial size of the sendmsg() scatter/gather array. */ 50 #define IOV_LIST_INITIAL 400 51 52 /** Initial number of sendmsg() argument structures to allocate. */ 53 #define MSG_LIST_INITIAL 10 54 55 /** High water marks for buffer shrinking */ 56 #define READ_BUFFER_HIGHWAT 8192 57 #define ITEM_LIST_HIGHWAT 400 58 #define IOV_LIST_HIGHWAT 600 59 #define MSG_LIST_HIGHWAT 100 60 61 /* Binary protocol stuff */ 62 #define MIN_BIN_PKT_LENGTH 16 63 #define BIN_PKT_HDR_WORDS (MIN_BIN_PKT_LENGTH/sizeof(uint32_t)) 64 65 /* Initial power multiplier for the hash table */ 66 #define HASHPOWER_DEFAULT 16 67 68 /* 69 * We only reposition items in the LRU queue if they haven't been repositioned 70 * in this many seconds. That saves us from churning on frequently-accessed 71 * items. 72 */ 73 #define ITEM_UPDATE_INTERVAL 60 74 75 /* unistd.h is here */ 76 #if HAVE_UNISTD_H 77 # include <unistd.h> 78 #endif 79 80 /* Slab sizing definitions. */ 81 #define POWER_SMALLEST 1 82 #define POWER_LARGEST 256 /* actual cap is 255 */ 83 #define SLAB_GLOBAL_PAGE_POOL 0 /* magic slab class for storing pages for reassignment */ 84 #define CHUNK_ALIGN_BYTES 8 85 /* slab class max is a 6-bit number, -1. */ 86 #define MAX_NUMBER_OF_SLAB_CLASSES (63 + 1) 87 88 /** How long an object can reasonably be assumed to be locked before 89 harvesting it on a low memory condition. Default: disabled. */ 90 #define TAIL_REPAIR_TIME_DEFAULT 0 91 92 /* warning: don't use these macros with a function, as it evals its arg twice */ 93 #define ITEM_get_cas(i) (((i)->it_flags & ITEM_CAS) ? \ 94 (i)->data->cas : (uint64_t)0) 95 96 #define ITEM_set_cas(i,v) { \ 97 if ((i)->it_flags & ITEM_CAS) { \ 98 (i)->data->cas = v; \ 99 } \ 100 } 101 102 #define ITEM_key(item) (((char*)&((item)->data)) \ 103 + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0)) 104 105 #define ITEM_suffix(item) ((char*) &((item)->data) + (item)->nkey + 1 \ 106 + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0)) 107 108 #define ITEM_data(item) ((char*) &((item)->data) + (item)->nkey + 1 \ 109 + (item)->nsuffix \ 110 + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0)) 111 112 #define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + 1 \ 113 + (item)->nsuffix + (item)->nbytes \ 114 + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0)) 115 116 #define ITEM_clsid(item) ((item)->slabs_clsid & ~(3<<6)) 117 118 #define STAT_KEY_LEN 128 119 #define STAT_VAL_LEN 128 120 121 /** Append a simple stat with a stat name, value format and value */ 122 #define APPEND_STAT(name, fmt, val) \ 123 append_stat(name, add_stats, c, fmt, val); 124 125 /** Append an indexed stat with a stat name (with format), value format 126 and value */ 127 #define APPEND_NUM_FMT_STAT(name_fmt, num, name, fmt, val) \ 128 klen = snprintf(key_str, STAT_KEY_LEN, name_fmt, num, name); \ 129 vlen = snprintf(val_str, STAT_VAL_LEN, fmt, val); \ 130 add_stats(key_str, klen, val_str, vlen, c); 131 132 /** Common APPEND_NUM_FMT_STAT format. */ 133 #define APPEND_NUM_STAT(num, name, fmt, val) \ 134 APPEND_NUM_FMT_STAT("%d:%s", num, name, fmt, val) 135 136 /** 137 * Callback for any function producing stats. 138 * 139 * @param key the stat's key 140 * @param klen length of the key 141 * @param val the stat's value in an ascii form (e.g. text form of a number) 142 * @param vlen length of the value 143 * @parm cookie magic callback cookie 144 */ 145 typedef void (*ADD_STAT)(const char *key, const uint16_t klen, 146 const char *val, const uint32_t vlen, 147 const void *cookie); 148 149 /* 150 * NOTE: If you modify this table you _MUST_ update the function state_text 151 */ 152 /** 153 * Possible states of a connection. 154 */ 155 enum conn_states { 156 conn_listening, /**< the socket which listens for connections */ 157 conn_new_cmd, /**< Prepare connection for next command */ 158 conn_waiting, /**< waiting for a readable socket */ 159 conn_read, /**< reading in a command line */ 160 conn_parse_cmd, /**< try to parse a command from the input buffer */ 161 conn_write, /**< writing out a simple response */ 162 conn_nread, /**< reading in a fixed number of bytes */ 163 conn_swallow, /**< swallowing unnecessary bytes w/o storing */ 164 conn_closing, /**< closing this connection */ 165 conn_mwrite, /**< writing out many items sequentially */ 166 conn_closed, /**< connection is closed */ 167 conn_watch, /**< held by the logger thread as a watcher */ 168 conn_max_state /**< Max state value (used for assertion) */ 169 }; 170 171 enum bin_substates { 172 bin_no_state, 173 bin_reading_set_header, 174 bin_reading_cas_header, 175 bin_read_set_value, 176 bin_reading_get_key, 177 bin_reading_stat, 178 bin_reading_del_header, 179 bin_reading_incr_header, 180 bin_read_flush_exptime, 181 bin_reading_sasl_auth, 182 bin_reading_sasl_auth_data, 183 bin_reading_touch_key, 184 }; 185 186 enum protocol { 187 ascii_prot = 3, /* arbitrary value. */ 188 binary_prot, 189 negotiating_prot /* Discovering the protocol */ 190 }; 191 192 enum network_transport { 193 local_transport, /* Unix sockets*/ 194 tcp_transport, 195 udp_transport 196 }; 197 198 enum pause_thread_types { 199 PAUSE_WORKER_THREADS = 0, 200 PAUSE_ALL_THREADS, 201 RESUME_ALL_THREADS, 202 RESUME_WORKER_THREADS 203 }; 204 205 #define IS_TCP(x) (x == tcp_transport) 206 #define IS_UDP(x) (x == udp_transport) 207 208 #define NREAD_ADD 1 209 #define NREAD_SET 2 210 #define NREAD_REPLACE 3 211 #define NREAD_APPEND 4 212 #define NREAD_PREPEND 5 213 #define NREAD_CAS 6 214 215 enum store_item_type { 216 NOT_STORED=0, STORED, EXISTS, NOT_FOUND, TOO_LARGE, NO_MEMORY 217 }; 218 219 enum delta_result_type { 220 OK, NON_NUMERIC, EOM, DELTA_ITEM_NOT_FOUND, DELTA_ITEM_CAS_MISMATCH 221 }; 222 223 /** Time relative to server start. Smaller than time_t on 64-bit systems. */ 224 typedef unsigned int rel_time_t; 225 226 /** Use X macros to avoid iterating over the stats fields during reset and 227 * aggregation. No longer have to add new stats in 3+ places. 228 */ 229 230 #define SLAB_STATS_FIELDS \ 231 X(set_cmds) \ 232 X(get_hits) \ 233 X(touch_hits) \ 234 X(delete_hits) \ 235 X(cas_hits) \ 236 X(cas_badval) \ 237 X(incr_hits) \ 238 X(decr_hits) 239 240 /** Stats stored per slab (and per thread). */ 241 struct slab_stats { 242 #define X(name) uint64_t name; 243 SLAB_STATS_FIELDS 244 #undef X 245 }; 246 247 #define THREAD_STATS_FIELDS \ 248 X(get_cmds) \ 249 X(get_misses) \ 250 X(get_expired) \ 251 X(get_flushed) \ 252 X(touch_cmds) \ 253 X(touch_misses) \ 254 X(delete_misses) \ 255 X(incr_misses) \ 256 X(decr_misses) \ 257 X(cas_misses) \ 258 X(bytes_read) \ 259 X(bytes_written) \ 260 X(flush_cmds) \ 261 X(conn_yields) /* # of yields for connections (-R option)*/ \ 262 X(auth_cmds) \ 263 X(auth_errors) \ 264 X(idle_kicks) /* idle connections killed */ 265 266 /** 267 * Stats stored per-thread. 268 */ 269 struct thread_stats { 270 pthread_mutex_t mutex; 271 #define X(name) uint64_t name; 272 THREAD_STATS_FIELDS 273 #undef X 274 struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES]; 275 }; 276 277 /** 278 * Global stats. Only resettable stats should go into this structure. 279 */ 280 struct stats { 281 uint64_t total_items; 282 uint64_t total_conns; 283 uint64_t rejected_conns; 284 uint64_t malloc_fails; 285 uint64_t listen_disabled_num; 286 uint64_t slabs_moved; /* times slabs were moved around */ 287 uint64_t slab_reassign_rescues; /* items rescued during slab move */ 288 uint64_t slab_reassign_evictions_nomem; /* valid items lost during slab move */ 289 uint64_t slab_reassign_inline_reclaim; /* valid items lost during slab move */ 290 uint64_t slab_reassign_chunk_rescues; /* chunked-item chunks recovered */ 291 uint64_t slab_reassign_busy_items; /* valid temporarily unmovable */ 292 uint64_t lru_crawler_starts; /* Number of item crawlers kicked off */ 293 uint64_t lru_maintainer_juggles; /* number of LRU bg pokes */ 294 uint64_t time_in_listen_disabled_us; /* elapsed time in microseconds while server unable to process new connections */ 295 uint64_t log_worker_dropped; /* logs dropped by worker threads */ 296 uint64_t log_worker_written; /* logs written by worker threads */ 297 uint64_t log_watcher_skipped; /* logs watchers missed */ 298 uint64_t log_watcher_sent; /* logs sent to watcher buffers */ 299 struct timeval maxconns_entered; /* last time maxconns entered */ 300 }; 301 302 /** 303 * Global "state" stats. Reflects state that shouldn't be wiped ever. 304 * Ordered for some cache line locality for commonly updated counters. 305 */ 306 struct stats_state { 307 uint64_t curr_items; 308 uint64_t curr_bytes; 309 uint64_t curr_conns; 310 uint64_t hash_bytes; /* size used for hash tables */ 311 unsigned int conn_structs; 312 unsigned int reserved_fds; 313 unsigned int hash_power_level; /* Better hope it's not over 9000 */ 314 bool hash_is_expanding; /* If the hash table is being expanded */ 315 bool accepting_conns; /* whether we are currently accepting */ 316 bool slab_reassign_running; /* slab reassign in progress */ 317 bool lru_crawler_running; /* crawl in progress */ 318 }; 319 320 #define MAX_VERBOSITY_LEVEL 2 321 322 /* When adding a setting, be sure to update process_stat_settings */ 323 /** 324 * Globally accessible settings as derived from the commandline. 325 */ 326 struct settings { 327 size_t maxbytes; 328 int maxconns; 329 int port; 330 int udpport; 331 char *inter; 332 int verbose; 333 rel_time_t oldest_live; /* ignore existing items older than this */ 334 uint64_t oldest_cas; /* ignore existing items with CAS values lower than this */ 335 int evict_to_free; 336 char *socketpath; /* path to unix socket if using local socket */ 337 int access; /* access mask (a la chmod) for unix domain socket */ 338 double factor; /* chunk size growth factor */ 339 int chunk_size; 340 int num_threads; /* number of worker (without dispatcher) libevent threads to run */ 341 int num_threads_per_udp; /* number of worker threads serving each udp socket */ 342 char prefix_delimiter; /* character that marks a key prefix (for stats) */ 343 int detail_enabled; /* nonzero if we're collecting detailed stats */ 344 int reqs_per_event; /* Maximum number of io to process on each 345 io-event. */ 346 bool use_cas; 347 enum protocol binding_protocol; 348 int backlog; 349 int item_size_max; /* Maximum item size */ 350 int slab_chunk_size_max; /* Upper end for chunks within slab pages. */ 351 int slab_page_size; /* Slab's page units. */ 352 bool sasl; /* SASL on/off */ 353 bool maxconns_fast; /* Whether or not to early close connections */ 354 bool lru_crawler; /* Whether or not to enable the autocrawler thread */ 355 bool lru_maintainer_thread; /* LRU maintainer background thread */ 356 bool slab_reassign; /* Whether or not slab reassignment is allowed */ 357 int slab_automove; /* Whether or not to automatically move slabs */ 358 int hashpower_init; /* Starting hash power level */ 359 bool shutdown_command; /* allow shutdown command */ 360 int tail_repair_time; /* LRU tail refcount leak repair time */ 361 bool flush_enabled; /* flush_all enabled */ 362 char *hash_algorithm; /* Hash algorithm in use */ 363 int lru_crawler_sleep; /* Microsecond sleep between items */ 364 uint32_t lru_crawler_tocrawl; /* Number of items to crawl per run */ 365 int hot_lru_pct; /* percentage of slab space for HOT_LRU */ 366 int warm_lru_pct; /* percentage of slab space for WARM_LRU */ 367 int crawls_persleep; /* Number of LRU crawls to run before sleeping */ 368 bool expirezero_does_not_evict; /* exptime == 0 goes into NOEXP_LRU */ 369 int idle_timeout; /* Number of seconds to let connections idle */ 370 unsigned int logger_watcher_buf_size; /* size of logger's per-watcher buffer */ 371 unsigned int logger_buf_size; /* size of per-thread logger buffer */ 372 }; 373 374 extern struct stats stats; 375 extern struct stats_state stats_state; 376 extern time_t process_started; 377 extern struct settings settings; 378 379 #define ITEM_LINKED 1 380 #define ITEM_CAS 2 381 382 /* temp */ 383 #define ITEM_SLABBED 4 384 385 /* Item was fetched at least once in its lifetime */ 386 #define ITEM_FETCHED 8 387 /* Appended on fetch, removed on LRU shuffling */ 388 #define ITEM_ACTIVE 16 389 /* If an item's storage are chained chunks. */ 390 #define ITEM_CHUNKED 32 391 #define ITEM_CHUNK 64 392 393 /** 394 * Structure for storing items within memcached. 395 */ 396 typedef struct _stritem { 397 /* Protected by LRU locks */ 398 struct _stritem *next; 399 struct _stritem *prev; 400 /* Rest are protected by an item lock */ 401 struct _stritem *h_next; /* hash chain next */ 402 rel_time_t time; /* least recent access */ 403 rel_time_t exptime; /* expire time */ 404 int nbytes; /* size of data */ 405 unsigned short refcount; 406 uint8_t nsuffix; /* length of flags-and-length string */ 407 uint8_t it_flags; /* ITEM_* above */ 408 uint8_t slabs_clsid;/* which slab class we're in */ 409 uint8_t nkey; /* key length, w/terminating null and padding */ 410 /* this odd type prevents type-punning issues when we do 411 * the little shuffle to save space when not using CAS. */ 412 union { 413 uint64_t cas; 414 char end; 415 } data[]; 416 /* if it_flags & ITEM_CAS we have 8 bytes CAS */ 417 /* then null-terminated key */ 418 /* then " flags length\r\n" (no terminating null) */ 419 /* then data with terminating \r\n (no terminating null; it's binary!) */ 420 } item; 421 422 typedef struct { 423 struct _stritem *next; 424 struct _stritem *prev; 425 struct _stritem *h_next; /* hash chain next */ 426 rel_time_t time; /* least recent access */ 427 rel_time_t exptime; /* expire time */ 428 int nbytes; /* size of data */ 429 unsigned short refcount; 430 uint8_t nsuffix; /* length of flags-and-length string */ 431 uint8_t it_flags; /* ITEM_* above */ 432 uint8_t slabs_clsid;/* which slab class we're in */ 433 uint8_t nkey; /* key length, w/terminating null and padding */ 434 uint32_t remaining; /* Max keys to crawl per slab per invocation */ 435 } crawler; 436 437 /* Header when an item is actually a chunk of another item. */ 438 typedef struct _strchunk { 439 struct _strchunk *next; /* points within its own chain. */ 440 struct _strchunk *prev; /* can potentially point to the head. */ 441 struct _stritem *head; /* always points to the owner chunk */ 442 int size; /* available chunk space in bytes */ 443 int used; /* chunk space used */ 444 int nbytes; /* used. */ 445 unsigned short refcount; /* used? */ 446 uint8_t nsuffix; /* unused */ 447 uint8_t it_flags; /* ITEM_* above. */ 448 uint8_t slabs_clsid; /* Same as above. */ 449 char data[]; 450 } item_chunk; 451 452 typedef struct { 453 pthread_t thread_id; /* unique ID of this thread */ 454 struct event_base *base; /* libevent handle this thread uses */ 455 struct event notify_event; /* listen event for notify pipe */ 456 int notify_receive_fd; /* receiving end of notify pipe */ 457 int notify_send_fd; /* sending end of notify pipe */ 458 struct thread_stats stats; /* Stats generated by this thread */ 459 struct conn_queue *new_conn_queue; /* queue of new connections to handle */ 460 cache_t *suffix_cache; /* suffix cache */ 461 logger *l; /* logger buffer */ 462 } LIBEVENT_THREAD; 463 464 typedef struct { 465 pthread_t thread_id; /* unique ID of this thread */ 466 struct event_base *base; /* libevent handle this thread uses */ 467 } LIBEVENT_DISPATCHER_THREAD; 468 469 /** 470 * The structure representing a connection into memcached. 471 */ 472 typedef struct conn conn; 473 struct conn { 474 int sfd; 475 sasl_conn_t *sasl_conn; 476 bool authenticated; 477 enum conn_states state; 478 enum bin_substates substate; 479 rel_time_t last_cmd_time; 480 struct event event; 481 short ev_flags; 482 short which; /** which events were just triggered */ 483 484 char *rbuf; /** buffer to read commands into */ 485 char *rcurr; /** but if we parsed some already, this is where we stopped */ 486 int rsize; /** total allocated size of rbuf */ 487 int rbytes; /** how much data, starting from rcur, do we have unparsed */ 488 489 char *wbuf; 490 char *wcurr; 491 int wsize; 492 int wbytes; 493 /** which state to go into after finishing current write */ 494 enum conn_states write_and_go; 495 void *write_and_free; /** free this memory after finishing writing */ 496 497 char *ritem; /** when we read in an item's value, it goes here */ 498 int rlbytes; 499 500 /* data for the nread state */ 501 502 /** 503 * item is used to hold an item structure created after reading the command 504 * line of set/add/replace commands, but before we finished reading the actual 505 * data. The data is read into ITEM_data(item) to avoid extra copying. 506 */ 507 508 void *item; /* for commands set/add/replace */ 509 510 /* data for the swallow state */ 511 int sbytes; /* how many bytes to swallow */ 512 513 /* data for the mwrite state */ 514 struct iovec *iov; 515 int iovsize; /* number of elements allocated in iov[] */ 516 int iovused; /* number of elements used in iov[] */ 517 518 struct msghdr *msglist; 519 int msgsize; /* number of elements allocated in msglist[] */ 520 int msgused; /* number of elements used in msglist[] */ 521 int msgcurr; /* element in msglist[] being transmitted now */ 522 int msgbytes; /* number of bytes in current msg */ 523 524 item **ilist; /* list of items to write out */ 525 int isize; 526 item **icurr; 527 int ileft; 528 529 char **suffixlist; 530 int suffixsize; 531 char **suffixcurr; 532 int suffixleft; 533 534 enum protocol protocol; /* which protocol this connection speaks */ 535 enum network_transport transport; /* what transport is used by this connection */ 536 537 /* data for UDP clients */ 538 int request_id; /* Incoming UDP request ID, if this is a UDP "connection" */ 539 struct sockaddr_in6 request_addr; /* udp: Who sent the most recent request */ 540 socklen_t request_addr_size; 541 unsigned char *hdrbuf; /* udp packet headers */ 542 int hdrsize; /* number of headers' worth of space is allocated */ 543 544 bool noreply; /* True if the reply should not be sent. */ 545 /* current stats command */ 546 struct { 547 char *buffer; 548 size_t size; 549 size_t offset; 550 } stats; 551 552 /* Binary protocol stuff */ 553 /* This is where the binary header goes */ 554 protocol_binary_request_header binary_header; 555 uint64_t cas; /* the cas to return */ 556 short cmd; /* current command being processed */ 557 int opaque; 558 int keylen; 559 conn *next; /* Used for generating a list of conn structures */ 560 LIBEVENT_THREAD *thread; /* Pointer to the thread object serving this connection */ 561 }; 562 563 /* array of conn structures, indexed by file descriptor */ 564 extern conn **conns; 565 566 /* current time of day (updated periodically) */ 567 extern volatile rel_time_t current_time; 568 569 /* TODO: Move to slabs.h? */ 570 extern volatile int slab_rebalance_signal; 571 572 struct slab_rebalance { 573 void *slab_start; 574 void *slab_end; 575 void *slab_pos; 576 int s_clsid; 577 int d_clsid; 578 uint32_t busy_items; 579 uint32_t rescues; 580 uint32_t evictions_nomem; 581 uint32_t inline_reclaim; 582 uint32_t chunk_rescues; 583 uint8_t done; 584 }; 585 586 extern struct slab_rebalance slab_rebal; 587 588 /* 589 * Functions 590 */ 591 void do_accept_new_conns(const bool do_accept); 592 enum delta_result_type do_add_delta(conn *c, const char *key, 593 const size_t nkey, const bool incr, 594 const int64_t delta, char *buf, 595 uint64_t *cas, const uint32_t hv); 596 enum store_item_type do_store_item(item *item, int comm, conn* c, const uint32_t hv); 597 conn *conn_new(const int sfd, const enum conn_states init_state, const int event_flags, const int read_buffer_size, enum network_transport transport, struct event_base *base); 598 extern int daemonize(int nochdir, int noclose); 599 600 #define mutex_lock(x) pthread_mutex_lock(x) 601 #define mutex_unlock(x) pthread_mutex_unlock(x) 602 603 #include "stats.h" 604 #include "slabs.h" 605 #include "assoc.h" 606 #include "items.h" 607 #include "trace.h" 608 #include "hash.h" 609 #include "util.h" 610 611 /* 612 * Functions such as the libevent-related calls that need to do cross-thread 613 * communication in multithreaded mode (rather than actually doing the work 614 * in the current thread) are called via "dispatch_" frontends, which are 615 * also #define-d to directly call the underlying code in singlethreaded mode. 616 */ 617 618 void memcached_thread_init(int nthreads, struct event_base *main_base); 619 int dispatch_event_add(int thread, conn *c); 620 void dispatch_conn_new(int sfd, enum conn_states init_state, int event_flags, int read_buffer_size, enum network_transport transport); 621 void sidethread_conn_close(conn *c); 622 623 /* Lock wrappers for cache functions that are called from main loop. */ 624 enum delta_result_type add_delta(conn *c, const char *key, 625 const size_t nkey, const int incr, 626 const int64_t delta, char *buf, 627 uint64_t *cas); 628 void accept_new_conns(const bool do_accept); 629 conn *conn_from_freelist(void); 630 bool conn_add_to_freelist(conn *c); 631 void conn_close_idle(conn *c); 632 int is_listen_thread(void); 633 item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes); 634 item *item_get(const char *key, const size_t nkey, conn *c); 635 item *item_touch(const char *key, const size_t nkey, uint32_t exptime, conn *c); 636 int item_link(item *it); 637 void item_remove(item *it); 638 int item_replace(item *it, item *new_it, const uint32_t hv); 639 void item_unlink(item *it); 640 void item_update(item *it); 641 642 void item_lock(uint32_t hv); 643 void *item_trylock(uint32_t hv); 644 void item_trylock_unlock(void *arg); 645 void item_unlock(uint32_t hv); 646 void pause_threads(enum pause_thread_types type); 647 unsigned short refcount_incr(unsigned short *refcount); 648 unsigned short refcount_decr(unsigned short *refcount); 649 void STATS_LOCK(void); 650 void STATS_UNLOCK(void); 651 void threadlocal_stats_reset(void); 652 void threadlocal_stats_aggregate(struct thread_stats *stats); 653 void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out); 654 655 /* Stat processing functions */ 656 void append_stat(const char *name, ADD_STAT add_stats, conn *c, 657 const char *fmt, ...); 658 659 enum store_item_type store_item(item *item, int comm, conn *c); 660 661 #if HAVE_DROP_PRIVILEGES 662 extern void drop_privileges(void); 663 #else 664 #define drop_privileges() 665 #endif 666 667 /* If supported, give compiler hints for branch prediction. */ 668 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) 669 #define __builtin_expect(x, expected_value) (x) 670 #endif 671 672 #define likely(x) __builtin_expect((x),1) 673 #define unlikely(x) __builtin_expect((x),0) 674