comparison memcached.h @ 0:30782bb1fc04 MEMCACHED_1_2_3

memcached-1.2.3
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 23 Sep 2007 03:58:34 +0400
parents
children e28ab6bd21fa
comparison
equal deleted inserted replaced
-1:000000000000 0:30782bb1fc04
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* $Id: memcached.h 570 2007-06-20 01:21:45Z plindner $ */
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. */
51 typedef unsigned int rel_time_t;
52
53 struct 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
72 struct 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
90 extern struct stats stats;
91 extern struct settings settings;
92
93 #define ITEM_LINKED 1
94 #define ITEM_DELETED 2
95
96 /* temp */
97 #define ITEM_SLABBED 4
98
99 typedef 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
124 enum 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
138 typedef 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) */
208 extern 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
218 conn *do_conn_from_freelist();
219 int do_conn_add_to_freelist(conn *c);
220 char *do_defer_delete(item *item, time_t exptime);
221 void do_run_deferred_deletes(void);
222 char *do_add_delta(item *item, int incr, const unsigned int delta, char *buf);
223 int do_store_item(item *item, int comm);
224 conn *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
248 void thread_init(int nthreads, struct event_base *main_base);
249 int dispatch_event_add(int thread, conn *c);
250 void 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. */
253 char *mt_add_delta(item *item, const int incr, const unsigned int delta, char *buf);
254 void mt_assoc_move_next_bucket(void);
255 conn *mt_conn_from_freelist(void);
256 int mt_conn_add_to_freelist(conn *c);
257 char *mt_defer_delete(item *it, time_t exptime);
258 int mt_is_listen_thread(void);
259 item *mt_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
260 void mt_item_flush_expired(void);
261 item *mt_item_get_notedeleted(const char *key, const size_t nkey, bool *delete_locked);
262 int mt_item_link(item *it);
263 void mt_item_remove(item *it);
264 int mt_item_replace(item *it, item *new_it);
265 void mt_item_unlink(item *it);
266 void mt_item_update(item *it);
267 void mt_run_deferred_deletes(void);
268 void *mt_slabs_alloc(size_t size);
269 void mt_slabs_free(void *ptr, size_t size);
270 int mt_slabs_reassign(unsigned char srcid, unsigned char dstid);
271 char *mt_slabs_stats(int *buflen);
272 void mt_stats_lock(void);
273 void mt_stats_unlock(void);
274 int mt_store_item(item *item, int comm);
275
276
277 # define add_delta(x,y,z,a) mt_add_delta(x,y,z,a)
278 # define assoc_move_next_bucket() mt_assoc_move_next_bucket()
279 # define conn_from_freelist() mt_conn_from_freelist()
280 # define conn_add_to_freelist(x) mt_conn_add_to_freelist(x)
281 # define defer_delete(x,y) mt_defer_delete(x,y)
282 # define is_listen_thread() mt_is_listen_thread()
283 # define item_alloc(x,y,z,a,b) mt_item_alloc(x,y,z,a,b)
284 # define item_flush_expired() mt_item_flush_expired()
285 # define item_get_notedeleted(x,y,z) mt_item_get_notedeleted(x,y,z)
286 # define item_link(x) mt_item_link(x)
287 # define item_remove(x) mt_item_remove(x)
288 # define item_replace(x,y) mt_item_replace(x,y)
289 # define item_update(x) mt_item_update(x)
290 # define item_unlink(x) mt_item_unlink(x)
291 # define run_deferred_deletes() mt_run_deferred_deletes()
292 # define slabs_alloc(x) mt_slabs_alloc(x)
293 # define slabs_free(x,y) mt_slabs_free(x,y)
294 # define slabs_reassign(x,y) mt_slabs_reassign(x,y)
295 # define slabs_stats(x) mt_slabs_stats(x)
296 # define store_item(x,y) mt_store_item(x,y)
297
298 # define STATS_LOCK() mt_stats_lock()
299 # define STATS_UNLOCK() mt_stats_unlock()
300
301 #else /* !USE_THREADS */
302
303 # define add_delta(x,y,z,a) do_add_delta(x,y,z,a)
304 # define assoc_move_next_bucket() do_assoc_move_next_bucket()
305 # define conn_from_freelist() do_conn_from_freelist()
306 # define conn_add_to_freelist(x) do_conn_add_to_freelist(x)
307 # define defer_delete(x,y) do_defer_delete(x,y)
308 # define dispatch_conn_new(x,y,z,a,b) conn_new(x,y,z,a,b,main_base)
309 # define dispatch_event_add(t,c) event_add(&(c)->event, 0)
310 # define is_listen_thread() 1
311 # define item_alloc(x,y,z,a,b) do_item_alloc(x,y,z,a,b)
312 # define item_flush_expired() do_item_flush_expired()
313 # define item_get_notedeleted(x,y,z) do_item_get_notedeleted(x,y,z)
314 # define item_link(x) do_item_link(x)
315 # define item_remove(x) do_item_remove(x)
316 # define item_replace(x,y) do_item_replace(x,y)
317 # define item_unlink(x) do_item_unlink(x)
318 # define item_update(x) do_item_update(x)
319 # define run_deferred_deletes() do_run_deferred_deletes()
320 # define slabs_alloc(x) do_slabs_alloc(x)
321 # define slabs_free(x,y) do_slabs_free(x,y)
322 # define slabs_reassign(x,y) do_slabs_reassign(x,y)
323 # define slabs_stats(x) do_slabs_stats(x)
324 # define store_item(x,y) do_store_item(x,y)
325 # define thread_init(x,y) 0
326
327 # define STATS_LOCK() /**/
328 # define STATS_UNLOCK() /**/
329
330 #endif /* !USE_THREADS */
331
332