xref: /memcached-1.4.29/memcached.h (revision bed5f9bb)
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 <stdbool.h>
19 #include <stdint.h>
20 #include <pthread.h>
21 
22 #include "protocol_binary.h"
23 #include "cache.h"
24 
25 /** Maximum length of a key. */
26 #define KEY_MAX_LENGTH 250
27 
28 /** Size of an incr buf. */
29 #define INCR_MAX_STORAGE_LEN 24
30 
31 #define DATA_BUFFER_SIZE 2048
32 #define UDP_READ_BUFFER_SIZE 65536
33 #define UDP_MAX_PAYLOAD_SIZE 1400
34 #define UDP_HEADER_SIZE 8
35 #define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
36 /* I'm told the max length of a 64-bit num converted to string is 20 bytes.
37  * Plus a few for spaces, \r\n, \0 */
38 #define SUFFIX_SIZE 24
39 
40 /** Initial size of list of items being returned by "get". */
41 #define ITEM_LIST_INITIAL 200
42 
43 /** Initial size of list of CAS suffixes appended to "gets" lines. */
44 #define SUFFIX_LIST_INITIAL 20
45 
46 /** Initial size of the sendmsg() scatter/gather array. */
47 #define IOV_LIST_INITIAL 400
48 
49 /** Initial number of sendmsg() argument structures to allocate. */
50 #define MSG_LIST_INITIAL 10
51 
52 /** High water marks for buffer shrinking */
53 #define READ_BUFFER_HIGHWAT 8192
54 #define ITEM_LIST_HIGHWAT 400
55 #define IOV_LIST_HIGHWAT 600
56 #define MSG_LIST_HIGHWAT 100
57 
58 /* Binary protocol stuff */
59 #define MIN_BIN_PKT_LENGTH 16
60 #define BIN_PKT_HDR_WORDS (MIN_BIN_PKT_LENGTH/sizeof(uint32_t))
61 
62 /* unistd.h is here */
63 #if HAVE_UNISTD_H
64 # include <unistd.h>
65 #endif
66 
67 /* Slab sizing definitions. */
68 #define POWER_SMALLEST 1
69 #define POWER_LARGEST  200
70 #define CHUNK_ALIGN_BYTES 8
71 #define DONT_PREALLOC_SLABS
72 #define MAX_NUMBER_OF_SLAB_CLASSES (POWER_LARGEST + 1)
73 
74 /** How long an object can reasonably be assumed to be locked before
75     harvesting it on a low memory condition. */
76 #define TAIL_REPAIR_TIME (3 * 3600)
77 
78 /* warning: don't use these macros with a function, as it evals its arg twice */
79 #define ITEM_get_cas(i) ((uint64_t)(((i)->it_flags & ITEM_CAS) ? \
80                                     *(uint64_t*)&((i)->end[0]) : 0x0))
81 #define ITEM_set_cas(i,v) { if ((i)->it_flags & ITEM_CAS) { \
82                           *(uint64_t*)&((i)->end[0]) = v; } }
83 
84 #define ITEM_key(item) (((char*)&((item)->end[0])) \
85          + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0))
86 
87 #define ITEM_suffix(item) ((char*) &((item)->end[0]) + (item)->nkey + 1 \
88          + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0))
89 
90 #define ITEM_data(item) ((char*) &((item)->end[0]) + (item)->nkey + 1 \
91          + (item)->nsuffix \
92          + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0))
93 
94 #define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + 1 \
95          + (item)->nsuffix + (item)->nbytes \
96          + (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0))
97 
98 #define STAT_KEY_LEN 128
99 #define STAT_VAL_LEN 128
100 
101 /** Append a simple stat with a stat name, value format and value */
102 #define APPEND_STAT(name, fmt, val) \
103     append_stat(name, add_stats, c, fmt, val);
104 
105 /** Append an indexed stat with a stat name (with format), value format
106     and value */
107 #define APPEND_NUM_FMT_STAT(name_fmt, num, name, fmt, val)          \
108     klen = snprintf(key_str, STAT_KEY_LEN, name_fmt, num, name);    \
109     vlen = snprintf(val_str, STAT_VAL_LEN, fmt, val);               \
110     add_stats(key_str, klen, val_str, vlen, c);
111 
112 /** Common APPEND_NUM_FMT_STAT format. */
113 #define APPEND_NUM_STAT(num, name, fmt, val) \
114     APPEND_NUM_FMT_STAT("%d:%s", num, name, fmt, val)
115 
116 /**
117  * Callback for any function producing stats.
118  *
119  * @param key the stat's key
120  * @param klen length of the key
121  * @param val the stat's value in an ascii form (e.g. text form of a number)
122  * @param vlen length of the value
123  * @parm cookie magic callback cookie
124  */
125 typedef void (*ADD_STAT)(const char *key, const uint16_t klen,
126                          const char *val, const uint32_t vlen,
127                          const void *cookie);
128 
129 /*
130  * NOTE: If you modify this table you _MUST_ update the function state_text
131  */
132 /**
133  * Possible states of a connection.
134  */
135 enum conn_states {
136     conn_listening,  /**< the socket which listens for connections */
137     conn_new_cmd,    /**< Prepare connection for next command */
138     conn_waiting,    /**< waiting for a readable socket */
139     conn_read,       /**< reading in a command line */
140     conn_parse_cmd,  /**< try to parse a command from the input buffer */
141     conn_write,      /**< writing out a simple response */
142     conn_nread,      /**< reading in a fixed number of bytes */
143     conn_swallow,    /**< swallowing unnecessary bytes w/o storing */
144     conn_closing,    /**< closing this connection */
145     conn_mwrite,     /**< writing out many items sequentially */
146     conn_max_state   /**< Max state value (used for assertion) */
147 };
148 
149 enum bin_substates {
150     bin_no_state,
151     bin_reading_set_header,
152     bin_reading_cas_header,
153     bin_read_set_value,
154     bin_reading_get_key,
155     bin_reading_stat,
156     bin_reading_del_header,
157     bin_reading_incr_header,
158     bin_read_flush_exptime
159 };
160 
161 enum protocol {
162     ascii_prot = 3, /* arbitrary value. */
163     binary_prot,
164     negotiating_prot /* Discovering the protocol */
165 };
166 
167 enum network_transport {
168     local_transport, /* Unix sockets*/
169     tcp_transport,
170     udp_transport
171 };
172 
173 #define IS_UDP(x) (x == udp_transport)
174 
175 #define NREAD_ADD 1
176 #define NREAD_SET 2
177 #define NREAD_REPLACE 3
178 #define NREAD_APPEND 4
179 #define NREAD_PREPEND 5
180 #define NREAD_CAS 6
181 
182 enum store_item_type {
183     NOT_STORED=0, STORED, EXISTS, NOT_FOUND
184 };
185 
186 enum delta_result_type {
187     OK, NON_NUMERIC, EOM
188 };
189 
190 /** Time relative to server start. Smaller than time_t on 64-bit systems. */
191 typedef unsigned int rel_time_t;
192 
193 /** Stats stored per slab (and per thread). */
194 struct slab_stats {
195     uint64_t  set_cmds;
196     uint64_t  get_hits;
197     uint64_t  delete_hits;
198     uint64_t  cas_hits;
199     uint64_t  cas_badval;
200     uint64_t  incr_hits;
201     uint64_t  decr_hits;
202 };
203 
204 /**
205  * Stats stored per-thread.
206  */
207 struct thread_stats {
208     pthread_mutex_t   mutex;
209     uint64_t          get_cmds;
210     uint64_t          get_misses;
211     uint64_t          delete_misses;
212     uint64_t          incr_misses;
213     uint64_t          decr_misses;
214     uint64_t          cas_misses;
215     uint64_t          bytes_read;
216     uint64_t          bytes_written;
217     uint64_t          flush_cmds;
218     uint64_t          conn_yields; /* # of yields for connections (-R option)*/
219     struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES];
220 };
221 
222 /**
223  * Global stats.
224  */
225 struct stats {
226     pthread_mutex_t mutex;
227     unsigned int  curr_items;
228     unsigned int  total_items;
229     uint64_t      curr_bytes;
230     unsigned int  curr_conns;
231     unsigned int  total_conns;
232     unsigned int  conn_structs;
233     uint64_t      get_cmds;
234     uint64_t      set_cmds;
235     uint64_t      get_hits;
236     uint64_t      get_misses;
237     uint64_t      evictions;
238     time_t        started;          /* when the process was started */
239     bool          accepting_conns;  /* whether we are currently accepting */
240     uint64_t      listen_disabled_num;
241 };
242 
243 #define MAX_VERBOSITY_LEVEL 2
244 
245 /* When adding a setting, be sure to update process_stat_settings */
246 /**
247  * Globally accessible settings as derived from the commandline.
248  */
249 struct settings {
250     size_t maxbytes;
251     int maxconns;
252     int port;
253     int udpport;
254     char *inter;
255     int verbose;
256     rel_time_t oldest_live; /* ignore existing items older than this */
257     int evict_to_free;
258     char *socketpath;   /* path to unix socket if using local socket */
259     int access;  /* access mask (a la chmod) for unix domain socket */
260     double factor;          /* chunk size growth factor */
261     int chunk_size;
262     int num_threads;        /* number of worker (without dispatcher) libevent threads to run */
263     char prefix_delimiter;  /* character that marks a key prefix (for stats) */
264     int detail_enabled;     /* nonzero if we're collecting detailed stats */
265     int reqs_per_event;     /* Maximum number of io to process on each
266                                io-event. */
267     bool use_cas;
268     enum protocol binding_protocol;
269     int backlog;
270     int item_size_max;        /* Maximum item size, and upper end for slabs */
271 };
272 
273 extern struct stats stats;
274 extern time_t process_started;
275 extern struct settings settings;
276 
277 #define ITEM_LINKED 1
278 #define ITEM_CAS 2
279 
280 /* temp */
281 #define ITEM_SLABBED 4
282 
283 /**
284  * Structure for storing items within memcached.
285  */
286 typedef struct _stritem {
287     struct _stritem *next;
288     struct _stritem *prev;
289     struct _stritem *h_next;    /* hash chain next */
290     rel_time_t      time;       /* least recent access */
291     rel_time_t      exptime;    /* expire time */
292     int             nbytes;     /* size of data */
293     unsigned short  refcount;
294     uint8_t         nsuffix;    /* length of flags-and-length string */
295     uint8_t         it_flags;   /* ITEM_* above */
296     uint8_t         slabs_clsid;/* which slab class we're in */
297     uint8_t         nkey;       /* key length, w/terminating null and padding */
298     void * end[];
299     /* if it_flags & ITEM_CAS we have 8 bytes CAS */
300     /* then null-terminated key */
301     /* then " flags length\r\n" (no terminating null) */
302     /* then data with terminating \r\n (no terminating null; it's binary!) */
303 } item;
304 
305 typedef struct {
306     pthread_t thread_id;        /* unique ID of this thread */
307     struct event_base *base;    /* libevent handle this thread uses */
308     struct event notify_event;  /* listen event for notify pipe */
309     int notify_receive_fd;      /* receiving end of notify pipe */
310     int notify_send_fd;         /* sending end of notify pipe */
311     struct thread_stats stats;  /* Stats generated by this thread */
312     struct conn_queue *new_conn_queue; /* queue of new connections to handle */
313     cache_t *suffix_cache;      /* suffix cache */
314 } LIBEVENT_THREAD;
315 
316 typedef struct {
317     pthread_t thread_id;        /* unique ID of this thread */
318     struct event_base *base;    /* libevent handle this thread uses */
319 } LIBEVENT_DISPATCHER_THREAD;
320 
321 /**
322  * The structure representing a connection into memcached.
323  */
324 typedef struct conn conn;
325 struct conn {
326     int    sfd;
327     enum conn_states  state;
328     enum bin_substates substate;
329     struct event event;
330     short  ev_flags;
331     short  which;   /** which events were just triggered */
332 
333     char   *rbuf;   /** buffer to read commands into */
334     char   *rcurr;  /** but if we parsed some already, this is where we stopped */
335     int    rsize;   /** total allocated size of rbuf */
336     int    rbytes;  /** how much data, starting from rcur, do we have unparsed */
337 
338     char   *wbuf;
339     char   *wcurr;
340     int    wsize;
341     int    wbytes;
342     /** which state to go into after finishing current write */
343     enum conn_states  write_and_go;
344     void   *write_and_free; /** free this memory after finishing writing */
345 
346     char   *ritem;  /** when we read in an item's value, it goes here */
347     int    rlbytes;
348 
349     /* data for the nread state */
350 
351     /**
352      * item is used to hold an item structure created after reading the command
353      * line of set/add/replace commands, but before we finished reading the actual
354      * data. The data is read into ITEM_data(item) to avoid extra copying.
355      */
356 
357     void   *item;     /* for commands set/add/replace  */
358 
359     /* data for the swallow state */
360     int    sbytes;    /* how many bytes to swallow */
361 
362     /* data for the mwrite state */
363     struct iovec *iov;
364     int    iovsize;   /* number of elements allocated in iov[] */
365     int    iovused;   /* number of elements used in iov[] */
366 
367     struct msghdr *msglist;
368     int    msgsize;   /* number of elements allocated in msglist[] */
369     int    msgused;   /* number of elements used in msglist[] */
370     int    msgcurr;   /* element in msglist[] being transmitted now */
371     int    msgbytes;  /* number of bytes in current msg */
372 
373     item   **ilist;   /* list of items to write out */
374     int    isize;
375     item   **icurr;
376     int    ileft;
377 
378     char   **suffixlist;
379     int    suffixsize;
380     char   **suffixcurr;
381     int    suffixleft;
382 
383     enum protocol protocol;   /* which protocol this connection speaks */
384     enum network_transport transport; /* what transport is used by this connection */
385 
386     /* data for UDP clients */
387     int    request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
388     struct sockaddr request_addr; /* Who sent the most recent request */
389     socklen_t request_addr_size;
390     unsigned char *hdrbuf; /* udp packet headers */
391     int    hdrsize;   /* number of headers' worth of space is allocated */
392 
393     bool   noreply;   /* True if the reply should not be sent. */
394     /* current stats command */
395     struct {
396         char *buffer;
397         size_t size;
398         size_t offset;
399     } stats;
400 
401     /* Binary protocol stuff */
402     /* This is where the binary header goes */
403     protocol_binary_request_header binary_header;
404     uint64_t cas; /* the cas to return */
405     short cmd; /* current command being processed */
406     int opaque;
407     int keylen;
408     conn   *next;     /* Used for generating a list of conn structures */
409     LIBEVENT_THREAD *thread; /* Pointer to the thread object serving this connection */
410 };
411 
412 
413 /* current time of day (updated periodically) */
414 extern volatile rel_time_t current_time;
415 
416 /*
417  * Functions
418  */
419 void do_accept_new_conns(const bool do_accept);
420 enum delta_result_type do_add_delta(conn *c, item *item, const bool incr,
421                                     const int64_t delta, char *buf);
422 enum store_item_type do_store_item(item *item, int comm, conn* c);
423 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);
424 extern int daemonize(int nochdir, int noclose);
425 
426 
427 #include "stats.h"
428 #include "slabs.h"
429 #include "assoc.h"
430 #include "items.h"
431 #include "trace.h"
432 #include "hash.h"
433 #include "util.h"
434 
435 /*
436  * Functions such as the libevent-related calls that need to do cross-thread
437  * communication in multithreaded mode (rather than actually doing the work
438  * in the current thread) are called via "dispatch_" frontends, which are
439  * also #define-d to directly call the underlying code in singlethreaded mode.
440  */
441 
442 void thread_init(int nthreads, struct event_base *main_base);
443 int  dispatch_event_add(int thread, conn *c);
444 void dispatch_conn_new(int sfd, enum conn_states init_state, int event_flags, int read_buffer_size, enum network_transport transport);
445 
446 /* Lock wrappers for cache functions that are called from main loop. */
447 enum delta_result_type add_delta(conn *c, item *item, const int incr,
448                                  const int64_t delta, char *buf);
449 void accept_new_conns(const bool do_accept);
450 conn *conn_from_freelist(void);
451 bool  conn_add_to_freelist(conn *c);
452 int   is_listen_thread(void);
453 item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
454 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes);
455 void  item_flush_expired(void);
456 item *item_get(const char *key, const size_t nkey);
457 int   item_link(item *it);
458 void  item_remove(item *it);
459 int   item_replace(item *it, item *new_it);
460 void  item_stats(ADD_STAT add_stats, void *c);
461 void  item_stats_sizes(ADD_STAT add_stats, void *c);
462 void  item_unlink(item *it);
463 void  item_update(item *it);
464 
465 void STATS_LOCK(void);
466 void STATS_UNLOCK(void);
467 void threadlocal_stats_reset(void);
468 void threadlocal_stats_aggregate(struct thread_stats *stats);
469 void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out);
470 
471 /* Stat processing functions */
472 void append_stat(const char *name, ADD_STAT add_stats, conn *c,
473                  const char *fmt, ...);
474 
475 enum store_item_type store_item(item *item, int comm, conn *c);
476 
477 #if HAVE_DROP_PRIVILEGES
478 extern void drop_privileges(void);
479 #else
480 #define drop_privileges()
481 #endif
482 
483 /* If supported, give compiler hints for branch prediction. */
484 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
485 #define __builtin_expect(x, expected_value) (x)
486 #endif
487 
488 #define likely(x)       __builtin_expect((x),1)
489 #define unlikely(x)     __builtin_expect((x),0)
490