1
1
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* $Id: memcached.h,v 1.21 2004/02/24 23:42:02 bradfitz Exp $ */
2
/* $Id: memcached.h 450 2006-11-26 21:33:47Z sgrimm $ */
4
3
#define DATA_BUFFER_SIZE 2048
6
#if defined(TCP_CORK) && !defined(TCP_NOPUSH)
7
#define TCP_NOPUSH TCP_CORK
4
#define UDP_READ_BUFFER_SIZE 65536
5
#define UDP_MAX_PAYLOAD_SIZE 1400
6
#define UDP_HEADER_SIZE 8
7
#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
9
/* Initial size of list of items being returned by "get". */
10
#define ITEM_LIST_INITIAL 200
12
/* Initial size of the sendmsg() scatter/gather array. */
13
#define IOV_LIST_INITIAL 400
15
/* Initial number of sendmsg() argument structures to allocate. */
16
#define MSG_LIST_INITIAL 10
18
/* High water marks for buffer shrinking */
19
#define READ_BUFFER_HIGHWAT 8192
20
#define ITEM_LIST_HIGHWAT 400
21
#define IOV_LIST_HIGHWAT 600
22
#define MSG_LIST_HIGHWAT 100
24
/* Time relative to server start. Smaller than time_t on 64-bit systems. */
25
typedef unsigned int rel_time_t;
11
28
unsigned int curr_items;
12
29
unsigned int total_items;
13
unsigned long long curr_bytes;
30
unsigned long long curr_bytes;
14
31
unsigned int curr_conns;
15
32
unsigned int total_conns;
16
33
unsigned int conn_structs;
17
unsigned int get_cmds;
18
unsigned int set_cmds;
19
unsigned int get_hits;
20
unsigned int get_misses;
34
unsigned long long get_cmds;
35
unsigned long long set_cmds;
36
unsigned long long get_hits;
37
unsigned long long get_misses;
21
38
time_t started; /* when the process was started */
22
39
unsigned long long bytes_read;
23
40
unsigned long long bytes_written;
27
unsigned int maxbytes;
30
48
struct in_addr interface;
32
time_t oldest_live; /* ignore existing items older than this */
50
rel_time_t oldest_live; /* ignore existing items older than this */
51
int managed; /* if 1, a tracker manages virtual buckets */
53
char *socketpath; /* path to unix socket if using local socket */
54
double factor; /* chunk size growth factor */
36
58
extern struct stats stats;
45
67
typedef struct _stritem {
46
68
struct _stritem *next;
47
69
struct _stritem *prev;
48
struct _stritem *h_next; /* hash chain next */
49
unsigned short refcount;
51
int nbytes; /* size of data */
52
time_t time; /* least recent access */
53
time_t exptime; /* expire time */
54
unsigned char it_flags; /* ITEM_* above */
55
unsigned char slabs_clsid;
56
unsigned char nkey; /* key length, with terminating null and padding */
70
struct _stritem *h_next; /* hash chain next */
71
rel_time_t time; /* least recent access */
72
rel_time_t exptime; /* expire time */
73
int nbytes; /* size of data */
74
unsigned short refcount;
75
unsigned char nsuffix; /* length of flags-and-length string */
76
unsigned char it_flags; /* ITEM_* above */
77
unsigned char slabs_clsid;/* which slab class we're in */
78
unsigned char nkey; /* key length, w/terminating null and padding */
80
/* then null-terminated key */
81
/* then " flags length\r\n" (no terminating null) */
82
/* then data with terminating \r\n (no terminating null; it's binary!) */
61
85
#define ITEM_key(item) ((char*)&((item)->end[0]))
63
87
/* warning: don't use these macros with a function, as it evals its arg twice */
64
#define ITEM_data(item) ((char*) &((item)->end[0]) + (item)->nkey)
65
#define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + (item)->nbytes)
88
#define ITEM_suffix(item) ((char*) &((item)->end[0]) + (item)->nkey + 1)
89
#define ITEM_data(item) ((char*) &((item)->end[0]) + (item)->nkey + 1 + (item)->nsuffix)
90
#define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + 1 + (item)->nsuffix + (item)->nbytes)
68
93
conn_listening, /* the socket which listens for connections */
84
109
struct event event;
86
short which; /* which events were just triggered */
111
short which; /* which events were just triggered */
113
char *rbuf; /* buffer to read commands into */
114
char *rcurr; /* but if we parsed some already, this is where we stopped */
115
int rsize; /* total allocated size of rbuf */
116
int rbytes; /* how much data, starting from rcur, do we have unparsed */
96
122
int write_and_go; /* which state to go into after finishing current write */
97
123
void *write_and_free; /* free this memory after finishing writing */
98
char is_corked; /* boolean, connection is corked */
125
char *ritem; /* when we read in an item's value, it goes here */
103
128
/* data for the nread state */
106
131
* item is used to hold an item structure created after reading the command
107
* line of set/add/replace commands, but before we finished reading the actual
132
* line of set/add/replace commands, but before we finished reading the actual
108
133
* data. The data is read into ITEM_data(item) to avoid extra copying.
115
140
int sbytes; /* how many bytes to swallow */
117
142
/* data for the mwrite state */
144
int iovsize; /* number of elements allocated in iov[] */
145
int iovused; /* number of elements used in iov[] */
147
struct msghdr *msglist;
148
int msgsize; /* number of elements allocated in msglist[] */
149
int msgused; /* number of elements used in msglist[] */
150
int msgcurr; /* element in msglist[] being transmitted now */
151
int msgbytes; /* number of bytes in current msg */
118
153
item **ilist; /* list of items to write out */
122
int ipart; /* 1 if we're writing a VALUE line, 2 if we're writing data */
123
char ibuf[300]; /* for VALUE lines */
158
/* data for UDP clients */
159
int udp; /* 1 if this is a UDP "connection" */
160
int request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
161
struct sockaddr request_addr; /* Who sent the most recent request */
162
socklen_t request_addr_size;
163
unsigned char *hdrbuf; /* udp packet headers */
164
int hdrsize; /* number of headers' worth of space is allocated */
166
int binary; /* are we in binary mode */
167
int bucket; /* bucket number for the next command, if running as
168
a managed instance. -1 (_not_ 0) means invalid. */
169
int gen; /* generation requested for the bucket */
172
/* number of virtual buckets for a managed instance */
173
#define MAX_BUCKETS 32768
129
175
/* listening socket */
130
176
extern int l_socket;
181
/* current time of day (updated periodically) */
182
extern volatile rel_time_t current_time;
132
184
/* temporary hack */
133
185
/* #define assert(x) if(!(x)) { printf("assert failure: %s\n", #x); pre_gdb(); }
134
186
void pre_gdb (); */
141
193
* given time value that's either unix time or delta from current unix time, return
142
* unix time. Use the fact that delta can't exceed one month (and real time value can't
194
* unix time. Use the fact that delta can't exceed one month (and real time value can't
146
time_t realtime(time_t exptime);
198
rel_time_t realtime(time_t exptime);
148
200
/* slabs memory allocation */
150
/* Init the subsystem. The argument is the limit on no. of bytes to allocate, 0 if no limit */
151
void slabs_init(unsigned int limit);
202
/* Init the subsystem. 1st argument is the limit on no. of bytes to allocate,
203
0 if no limit. 2nd argument is the growth factor; each slab will use a chunk
204
size equal to the previous slab's chunk size times this factor. */
205
void slabs_init(size_t limit, double factor);
207
/* Preallocate as many slab pages as possible (called from slabs_init)
208
on start-up, so users don't get confused out-of-memory errors when
209
they do have free (in-slab) space, but no space to make new slabs.
210
if maxslabs is 18 (POWER_LARGEST - POWER_SMALLEST + 1), then all
211
slab types can be made. if max memory is less than 18 MB, only the
212
smaller ones will be made. */
213
void slabs_preallocate (unsigned int maxslabs);
153
215
/* Given object size, return id to use when allocating/freeing memory for object */
154
216
/* 0 means error: can't store such a large object */
155
unsigned int slabs_clsid(unsigned int size);
217
unsigned int slabs_clsid(size_t size);
157
219
/* Allocate object of given length. 0 on error */
158
void *slabs_alloc(unsigned int size);
220
void *slabs_alloc(size_t size);
160
222
/* Free previously allocated object */
161
void slabs_free(void *ptr, unsigned int size);
223
void slabs_free(void *ptr, size_t size);
163
225
/* Fill buffer with stats */
164
226
char* slabs_stats(int *buflen);
169
231
-1 = tried. busy. send again shortly. */
170
232
int slabs_reassign(unsigned char srcid, unsigned char dstid);
233
int slabs_newslab(unsigned int id);
172
235
/* event handling, network IO */
173
236
void event_handler(int fd, short which, void *arg);
174
conn *conn_new(int sfd, int init_state, int event_flags);
237
conn *conn_new(int sfd, int init_state, int event_flags, int read_buffer_size, int is_udp);
175
238
void conn_close(conn *c);
176
239
void conn_init(void);
240
void accept_new_conns(int do_accept);
177
241
void drive_machine(conn *c);
178
int new_socket(void);
179
int server_socket(int port);
242
int new_socket(int isUdp);
243
int server_socket(int port, int isUdp);
180
244
int update_event(conn *c, int new_flags);
181
245
int try_read_command(conn *c);
182
246
int try_read_network(conn *c);
247
int try_read_udp(conn *c);
183
248
void complete_nread(conn *c);
184
249
void process_command(conn *c, char *command);
250
int transmit(conn *c);
251
int ensure_iov_space(conn *c);
252
int add_iov(conn *c, const void *buf, int len);
253
int add_msghdr(conn *c);
187
255
void stats_reset(void);
188
256
void stats_init(void);
191
258
void settings_init(void);
193
259
/* associative array */
194
260
void assoc_init(void);
195
item *assoc_find(char *key);
196
int assoc_insert(char *key, item *item);
197
void assoc_delete(char *key);
261
item *assoc_find(const char *key, size_t nkey);
262
int assoc_insert(item *item);
263
void assoc_delete(const char *key, size_t nkey);
264
void assoc_move_next_bucket(void);
200
266
void item_init(void);
201
item *item_alloc(char *key, int flags, time_t exptime, int nbytes);
267
item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
202
268
void item_free(item *it);
269
int item_size_ok(char *key, size_t nkey, int flags, int nbytes);
204
271
int item_link(item *it); /* may fail if transgresses limits */
205
272
void item_unlink(item *it);
206
273
void item_remove(item *it);
208
274
void item_update(item *it); /* update LRU time to current and reposition */
209
275
int item_replace(item *it, item *new_it);
210
276
char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes);
211
277
char *item_stats_sizes(int *bytes);
212
278
void item_stats(char *buffer, int buflen);
279
void item_flush_expired(void);
282
void set_current_time (); /* update the global variable holding
283
global 32-bit seconds-since-start time
284
(to avoid 64 bit time_t) */