~ubuntu-branches/ubuntu/jaunty/memcached/jaunty

« back to all changes in this revision

Viewing changes to memcached.h

  • Committer: Bazaar Package Importer
  • Author(s): Jay Bonci
  • Date: 2007-05-02 11:35:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502113542-qpoxsq28fjb7s9wc
Tags: 1.2.1-1
* New upstream release (Closes: #405054)
* Fix to logfile output so logrotate will work (Closes: #417941)
* Listen in on localhost by default (Closes: #383660)
* Default configuration suggests nobody by default (Closes: #391351)
* Bumped policy version to 3.7.2.2 (No other changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 $ */
3
 
 
 
2
/* $Id: memcached.h 450 2006-11-26 21:33:47Z sgrimm $ */
4
3
#define DATA_BUFFER_SIZE 2048
5
 
 
6
 
#if defined(TCP_CORK) && !defined(TCP_NOPUSH)
7
 
#define TCP_NOPUSH TCP_CORK
8
 
#endif
 
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)
 
8
 
 
9
/* Initial size of list of items being returned by "get". */
 
10
#define ITEM_LIST_INITIAL 200
 
11
 
 
12
/* Initial size of the sendmsg() scatter/gather array. */
 
13
#define IOV_LIST_INITIAL 400
 
14
 
 
15
/* Initial number of sendmsg() argument structures to allocate. */
 
16
#define MSG_LIST_INITIAL 10
 
17
 
 
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
 
23
 
 
24
/* Time relative to server start. Smaller than time_t on 64-bit systems. */
 
25
typedef unsigned int rel_time_t;
9
26
 
10
27
struct stats {
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;
24
41
};
25
42
 
26
43
struct settings {
27
 
    unsigned int maxbytes;
 
44
    size_t maxbytes;
28
45
    int maxconns;
29
46
    int port;
 
47
    int udpport;
30
48
    struct in_addr interface;
31
49
    int verbose;
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 */
33
52
    int evict_to_free;
 
53
    char *socketpath;   /* path to unix socket if using local socket */
 
54
    double factor;          /* chunk size growth factor */
 
55
    int chunk_size;
34
56
};
35
57
 
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; 
50
 
    unsigned short  flags;
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 */
57
 
    unsigned char dummy1;
 
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 */
58
79
    void * end[0];
 
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!) */
59
83
} item;
60
84
 
61
85
#define ITEM_key(item) ((char*)&((item)->end[0]))
62
86
 
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)
66
91
 
67
92
enum conn_states {
68
93
    conn_listening,  /* the socket which listens for connections */
83
108
    int    state;
84
109
    struct event event;
85
110
    short  ev_flags;
86
 
    short  which;  /* which events were just triggered */
 
111
    short  which;   /* which events were just triggered */
87
112
 
88
 
    char   *rbuf;  
89
 
    int    rsize;  
90
 
    int    rbytes;
 
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 */
91
117
 
92
118
    char   *wbuf;
93
119
    char   *wcurr;
94
120
    int    wsize;
95
 
    int    wbytes; 
 
121
    int    wbytes;
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 */
99
124
 
100
 
    char   *rcurr;
 
125
    char   *ritem;  /* when we read in an item's value, it goes here */
101
126
    int    rlbytes;
102
 
    
 
127
 
103
128
    /* data for the nread state */
104
129
 
105
 
    /* 
 
130
    /*
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.
109
134
     */
110
135
 
115
140
    int    sbytes;    /* how many bytes to swallow */
116
141
 
117
142
    /* data for the mwrite state */
 
143
    struct iovec *iov;
 
144
    int    iovsize;   /* number of elements allocated in iov[] */
 
145
    int    iovused;   /* number of elements used in iov[] */
 
146
 
 
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 */
 
152
 
118
153
    item   **ilist;   /* list of items to write out */
119
154
    int    isize;
120
155
    item   **icurr;
121
156
    int    ileft;
122
 
    int    ipart;     /* 1 if we're writing a VALUE line, 2 if we're writing data */
123
 
    char   ibuf[300]; /* for VALUE lines */
124
 
    char   *iptr;
125
 
    int    ibytes;
126
 
                         
 
157
 
 
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 */
 
165
 
 
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 */
127
170
} conn;
128
171
 
 
172
/* number of virtual buckets for a managed instance */
 
173
#define MAX_BUCKETS 32768
 
174
 
129
175
/* listening socket */
130
176
extern int l_socket;
131
177
 
 
178
/* udp socket */
 
179
extern int u_socket;
 
180
 
 
181
/* current time of day (updated periodically) */
 
182
extern volatile rel_time_t current_time;
 
183
 
132
184
/* temporary hack */
133
185
/* #define assert(x) if(!(x)) { printf("assert failure: %s\n", #x); pre_gdb(); }
134
186
   void pre_gdb (); */
137
189
 * Functions
138
190
 */
139
191
 
140
 
/* 
 
192
/*
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
143
195
 * be that low).
144
196
 */
145
197
 
146
 
time_t realtime(time_t exptime);
 
198
rel_time_t realtime(time_t exptime);
147
199
 
148
200
/* slabs memory allocation */
149
201
 
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);
 
206
 
 
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);
152
214
 
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);
156
218
 
157
219
/* Allocate object of given length. 0 on error */
158
 
void *slabs_alloc(unsigned int size);
 
220
void *slabs_alloc(size_t size);
159
221
 
160
222
/* Free previously allocated object */
161
 
void slabs_free(void *ptr, unsigned int size);
162
 
    
 
223
void slabs_free(void *ptr, size_t size);
 
224
 
163
225
/* Fill buffer with stats */
164
226
char* slabs_stats(int *buflen);
165
227
 
168
230
   0 = fail
169
231
   -1 = tried. busy. send again shortly. */
170
232
int slabs_reassign(unsigned char srcid, unsigned char dstid);
171
 
    
 
233
int slabs_newslab(unsigned int id);
 
234
 
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);
185
 
 
 
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);
186
254
/* stats */
187
255
void stats_reset(void);
188
256
void stats_init(void);
189
 
 
190
257
/* defaults */
191
258
void settings_init(void);
192
 
 
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);
198
 
 
 
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);
199
265
 
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);
203
270
 
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);
207
 
 
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);
 
280
 
 
281
/* time handling */
 
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) */