~yolanda.robla/ubuntu/trusty/memcached/add_distribution

« back to all changes in this revision

Viewing changes to items.c

  • Committer: Bazaar Package Importer
  • Author(s): Jay Bonci
  • Date: 2004-05-05 17:25:25 UTC
  • Revision ID: james.westby@ubuntu.com-20040505172525-ullh634q1xce88jl
Tags: upstream-1.1.11
ImportĀ upstreamĀ versionĀ 1.1.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 
2
/* $Id: items.c,v 1.21 2004/03/31 05:01:51 avva Exp $ */
 
3
 
 
4
#include <sys/types.h>
 
5
#include <sys/stat.h>
 
6
#include <sys/time.h>
 
7
#include <sys/socket.h>
 
8
#include <sys/signal.h>
 
9
#include <sys/resource.h>
 
10
#include <fcntl.h>
 
11
#include <stdlib.h>
 
12
#include <stdio.h>
 
13
#include <string.h>
 
14
#include <unistd.h>
 
15
#include <netinet/in.h>
 
16
#include <errno.h>
 
17
#include <time.h>
 
18
#include <event.h>
 
19
#include <assert.h>
 
20
 
 
21
#include "memcached.h"
 
22
 
 
23
 
 
24
/* 
 
25
 * NOTE: we assume here for simplicity that slab ids are <=32. That's true in 
 
26
 * the powers-of-2 implementation, but if that changes this should be changed too
 
27
 */
 
28
 
 
29
#define LARGEST_ID 32
 
30
static item *heads[LARGEST_ID];
 
31
static item *tails[LARGEST_ID];
 
32
unsigned int sizes[LARGEST_ID];
 
33
 
 
34
void item_init(void) {
 
35
    int i;
 
36
    for(i=0; i<LARGEST_ID; i++) {
 
37
        heads[i]=0;
 
38
        tails[i]=0;
 
39
        sizes[i]=0;
 
40
    }
 
41
}
 
42
 
 
43
 
 
44
item *item_alloc(char *key, int flags, time_t exptime, int nbytes) {
 
45
    int ntotal, len;
 
46
    item *it;
 
47
    unsigned int id;
 
48
 
 
49
    len = strlen(key) + 1; if(len % 4) len += 4 - (len % 4);
 
50
    ntotal = sizeof(item) + len + nbytes;
 
51
    
 
52
    id = slabs_clsid(ntotal);
 
53
    if (id == 0)
 
54
        return 0;
 
55
 
 
56
    it = slabs_alloc(ntotal);
 
57
    if (it == 0) {
 
58
        int tries = 50;
 
59
        item *search;
 
60
 
 
61
        /* If requested to not push old items out of cache when memory runs out,
 
62
         * we're out of luck at this point...
 
63
         */
 
64
 
 
65
        if (!settings.evict_to_free) return 0;
 
66
 
 
67
        /* 
 
68
         * try to get one off the right LRU 
 
69
         * don't necessariuly unlink the tail because it may be locked: refcount>0
 
70
         * search up from tail an item with refcount==0 and unlink it; give up after 50
 
71
         * tries
 
72
         */
 
73
 
 
74
        if (id > LARGEST_ID) return 0;
 
75
        if (tails[id]==0) return 0;
 
76
 
 
77
        for (search = tails[id]; tries>0 && search; tries--, search=search->prev) {
 
78
            if (search->refcount==0) {
 
79
                item_unlink(search);
 
80
                break;
 
81
            }
 
82
        }
 
83
        it = slabs_alloc(ntotal);
 
84
        if (it==0) return 0;
 
85
    }
 
86
 
 
87
    assert(it->slabs_clsid == 0);
 
88
 
 
89
    it->slabs_clsid = id;
 
90
 
 
91
    assert(it != heads[it->slabs_clsid]);
 
92
 
 
93
    it->next = it->prev = it->h_next = 0;
 
94
    it->refcount = 0;
 
95
    it->it_flags = 0;
 
96
    it->nkey = len;
 
97
    it->nbytes = nbytes;
 
98
    strcpy(ITEM_key(it), key);
 
99
    it->exptime = exptime;
 
100
    it->flags = flags;
 
101
    return it;
 
102
}
 
103
 
 
104
void item_free(item *it) {
 
105
    unsigned int ntotal = ITEM_ntotal(it);
 
106
    assert((it->it_flags & ITEM_LINKED) == 0);
 
107
    assert(it != heads[it->slabs_clsid]);
 
108
    assert(it != tails[it->slabs_clsid]);
 
109
    assert(it->refcount == 0);    
 
110
 
 
111
    /* so slab size changer can tell later if item is already free or not */
 
112
    it->slabs_clsid = 0;
 
113
    it->it_flags |= ITEM_SLABBED;
 
114
    slabs_free(it, ntotal);
 
115
}
 
116
 
 
117
void item_link_q(item *it) { /* item is the new head */
 
118
    item **head, **tail;
 
119
    assert(it->slabs_clsid <= LARGEST_ID);
 
120
    assert((it->it_flags & ITEM_SLABBED) == 0);
 
121
 
 
122
    head = &heads[it->slabs_clsid];
 
123
    tail = &tails[it->slabs_clsid];
 
124
    assert(it != *head);
 
125
    assert((*head && *tail) || (*head == 0 && *tail == 0));
 
126
    it->prev = 0;
 
127
    it->next = *head;
 
128
    if (it->next) it->next->prev = it;
 
129
    *head = it;
 
130
    if (*tail == 0) *tail = it;
 
131
    sizes[it->slabs_clsid]++;
 
132
    return;
 
133
}
 
134
 
 
135
void item_unlink_q(item *it) {
 
136
    item **head, **tail;
 
137
    assert(it->slabs_clsid <= LARGEST_ID);
 
138
    head = &heads[it->slabs_clsid];
 
139
    tail = &tails[it->slabs_clsid];
 
140
    
 
141
    if (*head == it) {
 
142
        assert(it->prev == 0);
 
143
        *head = it->next;
 
144
    }
 
145
    if (*tail == it) {
 
146
        assert(it->next == 0);
 
147
        *tail = it->prev;
 
148
    }
 
149
    assert(it->next != it);
 
150
    assert(it->prev != it);
 
151
 
 
152
    if (it->next) it->next->prev = it->prev;
 
153
    if (it->prev) it->prev->next = it->next;
 
154
    sizes[it->slabs_clsid]--;
 
155
    return;
 
156
}
 
157
 
 
158
int item_link(item *it) {
 
159
    assert((it->it_flags & (ITEM_LINKED|ITEM_SLABBED)) == 0);
 
160
    assert(it->nbytes < 1048576);
 
161
    it->it_flags |= ITEM_LINKED;
 
162
    it->time = time(0);
 
163
    assoc_insert(ITEM_key(it), it);
 
164
 
 
165
    stats.curr_bytes += ITEM_ntotal(it);
 
166
    stats.curr_items += 1;
 
167
    stats.total_items += 1;
 
168
 
 
169
    item_link_q(it);
 
170
 
 
171
    return 1;
 
172
}
 
173
 
 
174
void item_unlink(item *it) {
 
175
    if (it->it_flags & ITEM_LINKED) {
 
176
        it->it_flags &= ~ITEM_LINKED;
 
177
        stats.curr_bytes -= ITEM_ntotal(it);
 
178
        stats.curr_items -= 1;
 
179
        assoc_delete(ITEM_key(it));
 
180
        item_unlink_q(it);
 
181
    }
 
182
    if (it->refcount == 0) item_free(it);
 
183
}
 
184
 
 
185
void item_remove(item *it) {
 
186
    assert((it->it_flags & ITEM_SLABBED) == 0);
 
187
    if (it->refcount) it->refcount--;
 
188
    assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount);
 
189
    if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) {
 
190
        item_free(it);
 
191
    }
 
192
}
 
193
 
 
194
void item_update(item *it) {
 
195
    assert((it->it_flags & ITEM_SLABBED) == 0);
 
196
 
 
197
    item_unlink_q(it);
 
198
    it->time = time(0);
 
199
    item_link_q(it);
 
200
}
 
201
 
 
202
int item_replace(item *it, item *new_it) {
 
203
    assert((it->it_flags & ITEM_SLABBED) == 0);
 
204
 
 
205
    item_unlink(it);
 
206
    return item_link(new_it);
 
207
}
 
208
 
 
209
char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) {
 
210
    
 
211
    int memlimit = 2*1024*1024;
 
212
    char *buffer;
 
213
    int bufcurr;
 
214
    item *it;
 
215
    int len;
 
216
    int shown = 0;
 
217
    char temp[256];
 
218
    
 
219
    if (slabs_clsid > LARGEST_ID) return 0;
 
220
    it = heads[slabs_clsid];
 
221
 
 
222
    buffer = malloc(memlimit);
 
223
    if (buffer == 0) return 0;
 
224
    bufcurr = 0;
 
225
 
 
226
    while(1) {
 
227
        if(limit && shown >=limit)
 
228
            break;
 
229
        if (!it)
 
230
            break;
 
231
        sprintf(temp, "ITEM %s [%u b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time);
 
232
        len = strlen(temp);
 
233
        if (bufcurr + len +5 > memlimit)  /* 5 is END\r\n */
 
234
            break;
 
235
        strcpy(buffer + bufcurr, temp);
 
236
        bufcurr+=len;
 
237
        shown++;
 
238
        it = it->next;
 
239
    }
 
240
 
 
241
    strcpy(buffer+bufcurr, "END\r\n");
 
242
    bufcurr+=5;
 
243
    
 
244
    *bytes = bufcurr;
 
245
    return buffer;
 
246
}
 
247
 
 
248
void item_stats(char *buffer, int buflen) {
 
249
    int i;
 
250
    char *bufcurr = buffer;
 
251
    time_t now = time(0);
 
252
 
 
253
    if (buflen < 4096) {
 
254
        strcpy(buffer, "SERVER_ERROR out of memory");
 
255
        return;
 
256
    }
 
257
 
 
258
    for (i=0; i<LARGEST_ID; i++) {
 
259
        if (tails[i])
 
260
            bufcurr += sprintf(bufcurr, "STAT items:%u:number %u\r\nSTAT items:%u:age %lu\r\n", 
 
261
                               i, sizes[i], i, now - tails[i]->time);
 
262
    }
 
263
    strcpy(bufcurr, "END");
 
264
    return;
 
265
}
 
266
 
 
267
/* dumps out a list of objects of each size, with granularity of 32 bytes */
 
268
char* item_stats_sizes(int *bytes) {
 
269
    int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */
 
270
    unsigned int *histogram = (int*) malloc(num_buckets * sizeof(int));
 
271
    char *buf = (char*) malloc(1024*1024*2*sizeof(char));
 
272
    int i;
 
273
 
 
274
    if (histogram == 0 || buf == 0) {
 
275
        if (histogram) free(histogram);
 
276
        if (buf) free(buf);
 
277
        return 0;
 
278
    }
 
279
 
 
280
    /* build the histogram */
 
281
    memset(buf, 0, num_buckets * sizeof(int));
 
282
    for (i=0; i<LARGEST_ID; i++) {
 
283
        item *iter = heads[i];
 
284
        while (iter) {
 
285
            int ntotal = ITEM_ntotal(iter);
 
286
            int bucket = ntotal / 32;
 
287
            if (ntotal % 32) bucket++;
 
288
            if (bucket < num_buckets) histogram[bucket]++;
 
289
            iter = iter->next;
 
290
        }
 
291
    }
 
292
 
 
293
    /* write the buffer */
 
294
    *bytes = 0;
 
295
    for (i=0; i<num_buckets; i++) {
 
296
        if (histogram[i]) {
 
297
            *bytes += sprintf(&buf[*bytes], "%u %u\r\n", i*32, histogram[i]);
 
298
        }
 
299
    }
 
300
    *bytes += sprintf(&buf[*bytes], "END\r\n");
 
301
 
 
302
    free(histogram);
 
303
    return buf;
 
304
}