root/branches/append/server/memcached.h

Revision 590, 12.6 kB (checked in by sgrimm, 2 years ago)

Fix the "stats items" command; the powers-of-N chunk size change broke it

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* $Id$ */
3
4#include "config.h"
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <sys/time.h>
8#include <netinet/in.h>
9#include <event.h>
10
11#define DATA_BUFFER_SIZE 2048
12#define UDP_READ_BUFFER_SIZE 65536
13#define UDP_MAX_PAYLOAD_SIZE 1400
14#define UDP_HEADER_SIZE 8
15#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
16
17/* Initial size of list of items being returned by "get". */
18#define ITEM_LIST_INITIAL 200
19
20/* Initial size of the sendmsg() scatter/gather array. */
21#define IOV_LIST_INITIAL 400
22
23/* Initial number of sendmsg() argument structures to allocate. */
24#define MSG_LIST_INITIAL 10
25
26/* High water marks for buffer shrinking */
27#define READ_BUFFER_HIGHWAT 8192
28#define ITEM_LIST_HIGHWAT 400
29#define IOV_LIST_HIGHWAT 600
30#define MSG_LIST_HIGHWAT 100
31
32/* Get a consistent bool type */
33#if HAVE_STDBOOL_H
34# include <stdbool.h>
35#else
36  typedef enum {false = 0, true = 1} bool;
37#endif
38
39#if HAVE_STDINT_H
40# include <stdint.h>
41#else
42 typedef unsigned char             uint8_t;
43#endif
44
45/* unistd.h is here */
46#if HAVE_UNISTD_H
47# include <unistd.h>
48#endif
49
50/* Time relative to server start. Smaller than time_t on 64-bit systems. */
51typedef unsigned int rel_time_t;
52
53struct stats {
54    unsigned int  curr_items;
55    unsigned int  total_items;
56    uint64_t      curr_bytes;
57    unsigned int  curr_conns;
58    unsigned int  total_conns;
59    unsigned int  conn_structs;
60    uint64_t      get_cmds;
61    uint64_t      set_cmds;
62    uint64_t      get_hits;
63    uint64_t      get_misses;
64    uint64_t      evictions;
65    time_t        started;          /* when the process was started */
66    uint64_t      bytes_read;
67    uint64_t      bytes_written;
68};
69
70#define MAX_VERBOSITY_LEVEL 2
71
72struct settings {
73    size_t maxbytes;
74    int maxconns;
75    int port;
76    int udpport;
77    struct in_addr interf;
78    int verbose;
79    rel_time_t oldest_live; /* ignore existing items older than this */
80    bool managed;          /* if 1, a tracker manages virtual buckets */
81    int evict_to_free;
82    char *socketpath;   /* path to unix socket if using local socket */
83    double factor;          /* chunk size growth factor */
84    int chunk_size;
85    int num_threads;        /* number of libevent threads to run */
86    char prefix_delimiter;  /* character that marks a key prefix (for stats) */
87    int detail_enabled;     /* nonzero if we're collecting detailed stats */
88};
89
90extern struct stats stats;
91extern struct settings settings;
92
93#define ITEM_LINKED 1
94#define ITEM_DELETED 2
95
96/* temp */
97#define ITEM_SLABBED 4
98
99typedef struct _stritem {
100    struct _stritem *next;
101    struct _stritem *prev;
102    struct _stritem *h_next;    /* hash chain next */
103    rel_time_t      time;       /* least recent access */
104    rel_time_t      exptime;    /* expire time */
105    int             nbytes;     /* size of data */
106    unsigned short  refcount;
107    uint8_t         nsuffix;    /* length of flags-and-length string */
108    uint8_t         it_flags;   /* ITEM_* above */
109    uint8_t         slabs_clsid;/* which slab class we're in */
110    uint8_t         nkey;       /* key length, w/terminating null and padding */
111    void * end[];
112    /* then null-terminated key */
113    /* then " flags length\r\n" (no terminating null) */
114    /* then data with terminating \r\n (no terminating null; it's binary!) */
115} item;
116
117#define ITEM_key(item) ((char*)&((item)->end[0]))
118
119/* warning: don't use these macros with a function, as it evals its arg twice */
120#define ITEM_suffix(item) ((char*) &((item)->end[0]) + (item)->nkey + 1)
121#define ITEM_data(item) ((char*) &((item)->end[0]) + (item)->nkey + 1 + (item)->nsuffix)
122#define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + 1 + (item)->nsuffix + (item)->nbytes)
123
124enum conn_states {
125    conn_listening,  /* the socket which listens for connections */
126    conn_read,       /* reading in a command line */
127    conn_write,      /* writing out a simple response */
128    conn_nread,      /* reading in a fixed number of bytes */
129    conn_swallow,    /* swallowing unnecessary bytes w/o storing */
130    conn_closing,    /* closing this connection */
131    conn_mwrite      /* writing out many items sequentially */
132};
133
134#define NREAD_ADD 1
135#define NREAD_SET 2
136#define NREAD_REPLACE 3
137
138typedef struct {
139    int    sfd;
140    int    state;
141    struct event event;
142    short  ev_flags;
143    short  which;   /* which events were just triggered */
144
145    char   *rbuf;   /* buffer to read commands into */
146    char   *rcurr;  /* but if we parsed some already, this is where we stopped */
147    int    rsize;   /* total allocated size of rbuf */
148    int    rbytes;  /* how much data, starting from rcur, do we have unparsed */
149
150    char   *wbuf;
151    char   *wcurr;
152    int    wsize;
153    int    wbytes;
154    int    write_and_go; /* which state to go into after finishing current write */
155    void   *write_and_free; /* free this memory after finishing writing */
156
157    char   *ritem;  /* when we read in an item's value, it goes here */
158    int    rlbytes;
159
160    /* data for the nread state */
161
162    /*
163     * item is used to hold an item structure created after reading the command
164     * line of set/add/replace commands, but before we finished reading the actual
165     * data. The data is read into ITEM_data(item) to avoid extra copying.
166     */
167
168    void   *item;     /* for commands set/add/replace  */
169    int    item_comm; /* which one is it: set/add/replace */
170
171    /* data for the swallow state */
172    int    sbytes;    /* how many bytes to swallow */
173
174    /* data for the mwrite state */
175    struct iovec *iov;
176    int    iovsize;   /* number of elements allocated in iov[] */
177    int    iovused;   /* number of elements used in iov[] */
178
179    struct msghdr *msglist;
180    int    msgsize;   /* number of elements allocated in msglist[] */
181    int    msgused;   /* number of elements used in msglist[] */
182    int    msgcurr;   /* element in msglist[] being transmitted now */
183    int    msgbytes;  /* number of bytes in current msg */
184
185    item   **ilist;   /* list of items to write out */
186    int    isize;
187    item   **icurr;
188    int    ileft;
189
190    /* data for UDP clients */
191    bool   udp;       /* is this is a UDP "connection" */
192    int    request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
193    struct sockaddr request_addr; /* Who sent the most recent request */
194    socklen_t request_addr_size;
195    unsigned char *hdrbuf; /* udp packet headers */
196    int    hdrsize;   /* number of headers' worth of space is allocated */
197
198    int    binary;    /* are we in binary mode */
199    int    bucket;    /* bucket number for the next command, if running as
200                         a managed instance. -1 (_not_ 0) means invalid. */
201    int    gen;       /* generation requested for the bucket */
202} conn;
203
204/* number of virtual buckets for a managed instance */
205#define MAX_BUCKETS 32768
206
207/* current time of day (updated periodically) */
208extern volatile rel_time_t current_time;
209
210/* temporary hack */
211/* #define assert(x) if(!(x)) { printf("assert failure: %s\n", #x); pre_gdb(); }
212   void pre_gdb (); */
213
214/*
215 * Functions
216 */
217
218conn *do_conn_from_freelist();
219int do_conn_add_to_freelist(conn *c);
220char *do_defer_delete(item *item, time_t exptime);
221void do_run_deferred_deletes(void);
222char *do_add_delta(item *item, int incr, const unsigned int delta, char *buf);
223int do_store_item(item *item, int comm);
224conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp, struct event_base *base);
225
226
227#include "stats.h"
228#include "slabs.h"
229#include "assoc.h"
230#include "items.h"
231
232
233/*
234 * In multithreaded mode, we wrap certain functions with lock management and
235 * replace the logic of some other functions. All wrapped functions have
236 * "mt_" and "do_" variants. In multithreaded mode, the plain version of a
237 * function is #define-d to the "mt_" variant, which often just grabs a
238 * lock and calls the "do_" function. In singlethreaded mode, the "do_"
239 * function is called directly.
240 *
241 * Functions such as the libevent-related calls that need to do cross-thread
242 * communication in multithreaded mode (rather than actually doing the work
243 * in the current thread) are called via "dispatch_" frontends, which are
244 * also #define-d to directly call the underlying code in singlethreaded mode.
245 */
246#ifdef USE_THREADS
247
248void thread_init(int nthreads, struct event_base *main_base);
249int  dispatch_event_add(int thread, conn *c);
250void dispatch_conn_new(int sfd, int init_state, int event_flags, int read_buffer_size, int is_udp);
251
252/* Lock wrappers for cache functions that are called from main loop. */
253char *mt_add_delta(item *item, const int incr, const unsigned int delta, char *buf);
254void mt_assoc_move_next_bucket(void);
255conn *mt_conn_from_freelist(void);
256int   mt_conn_add_to_freelist(conn *c);
257char *mt_defer_delete(item *it, time_t exptime);
258int   mt_is_listen_thread(void);
259item *mt_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
260char *mt_item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes);
261void  mt_item_flush_expired(void);
262item *mt_item_get_notedeleted(const char *key, const size_t nkey, bool *delete_locked);
263int   mt_item_link(item *it);
264void  mt_item_remove(item *it);
265int   mt_item_replace(item *it, item *new_it);
266char *mt_item_stats(int *bytes);
267char *mt_item_stats_sizes(int *bytes);
268void  mt_item_unlink(item *it);
269void  mt_item_update(item *it);
270void  mt_run_deferred_deletes(void);
271void *mt_slabs_alloc(size_t size);
272void  mt_slabs_free(void *ptr, size_t size);
273int   mt_slabs_reassign(unsigned char srcid, unsigned char dstid);
274char *mt_slabs_stats(int *buflen);
275void  mt_stats_lock(void);
276void  mt_stats_unlock(void);
277int   mt_store_item(item *item, int comm);
278
279
280# define add_delta(x,y,z,a)          mt_add_delta(x,y,z,a)
281# define assoc_move_next_bucket()    mt_assoc_move_next_bucket()
282# define conn_from_freelist()        mt_conn_from_freelist()
283# define conn_add_to_freelist(x)     mt_conn_add_to_freelist(x)
284# define defer_delete(x,y)           mt_defer_delete(x,y)
285# define is_listen_thread()          mt_is_listen_thread()
286# define item_alloc(x,y,z,a,b)       mt_item_alloc(x,y,z,a,b)
287# define item_cachedump(x,y,z)       mt_item_cachedump(x,y,z)
288# define item_flush_expired()        mt_item_flush_expired()
289# define item_get_notedeleted(x,y,z) mt_item_get_notedeleted(x,y,z)
290# define item_link(x)                mt_item_link(x)
291# define item_remove(x)              mt_item_remove(x)
292# define item_replace(x,y)           mt_item_replace(x,y)
293# define item_stats(x)               mt_item_stats(x)
294# define item_stats_sizes(x)         mt_item_stats_sizes(x)
295# define item_update(x)              mt_item_update(x)
296# define item_unlink(x)              mt_item_unlink(x)
297# define run_deferred_deletes()      mt_run_deferred_deletes()
298# define slabs_alloc(x)              mt_slabs_alloc(x)
299# define slabs_free(x,y)             mt_slabs_free(x,y)
300# define slabs_reassign(x,y)         mt_slabs_reassign(x,y)
301# define slabs_stats(x)              mt_slabs_stats(x)
302# define store_item(x,y)             mt_store_item(x,y)
303
304# define STATS_LOCK()                mt_stats_lock()
305# define STATS_UNLOCK()              mt_stats_unlock()
306
307#else /* !USE_THREADS */
308
309# define add_delta(x,y,z,a)          do_add_delta(x,y,z,a)
310# define assoc_move_next_bucket()    do_assoc_move_next_bucket()
311# define conn_from_freelist()        do_conn_from_freelist()
312# define conn_add_to_freelist(x)     do_conn_add_to_freelist(x)
313# define defer_delete(x,y)           do_defer_delete(x,y)
314# define dispatch_conn_new(x,y,z,a,b) conn_new(x,y,z,a,b,main_base)
315# define dispatch_event_add(t,c)     event_add(&(c)->event, 0)
316# define is_listen_thread()          1
317# define item_alloc(x,y,z,a,b)       do_item_alloc(x,y,z,a,b)
318# define item_cachedump(x,y,z)       do_item_cachedump(x,y,z)
319# define item_flush_expired()        do_item_flush_expired()
320# define item_get_notedeleted(x,y,z) do_item_get_notedeleted(x,y,z)
321# define item_link(x)                do_item_link(x)
322# define item_remove(x)              do_item_remove(x)
323# define item_replace(x,y)           do_item_replace(x,y)
324# define item_stats(x)               do_item_stats(x)
325# define item_stats_sizes(x)         do_item_stats_sizes(x)
326# define item_unlink(x)              do_item_unlink(x)
327# define item_update(x)              do_item_update(x)
328# define run_deferred_deletes()      do_run_deferred_deletes()
329# define slabs_alloc(x)              do_slabs_alloc(x)
330# define slabs_free(x,y)             do_slabs_free(x,y)
331# define slabs_reassign(x,y)         do_slabs_reassign(x,y)
332# define slabs_stats(x)              do_slabs_stats(x)
333# define store_item(x,y)             do_store_item(x,y)
334# define thread_init(x,y)            0
335
336# define STATS_LOCK()                /**/
337# define STATS_UNLOCK()              /**/
338
339#endif /* !USE_THREADS */
340
341
Note: See TracBrowser for help on using the browser.