~ubuntu-branches/ubuntu/precise/memcached/precise

« back to all changes in this revision

Viewing changes to stats.c

  • Committer: Bazaar Package Importer
  • Author(s): David Martínez Moreno
  • Date: 2009-10-16 15:09:43 UTC
  • mfrom: (1.3.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20091016150943-l96biwf7siwdt1ci
Tags: upstream-1.4.2
Import upstream version 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 *
7
7
 * Author:
8
8
 *   Steven Grimm <sgrimm@facebook.com>
9
 
 *
10
 
 * $Id$
11
9
 */
12
10
#include "memcached.h"
13
11
#include <stdio.h>
66
64
 * in the list.
67
65
 */
68
66
/*@null@*/
69
 
static PREFIX_STATS *stats_prefix_find(const char *key) {
 
67
static PREFIX_STATS *stats_prefix_find(const char *key, const size_t nkey) {
70
68
    PREFIX_STATS *pfs;
71
69
    uint32_t hashval;
72
70
    size_t length;
73
71
 
74
72
    assert(key != NULL);
75
73
 
76
 
    for (length = 0; key[length] != '\0'; length++)
 
74
    for (length = 0; length < nkey && key[length] != '\0'; length++)
77
75
        if (key[length] == settings.prefix_delimiter)
78
76
            break;
79
77
 
113
111
/*
114
112
 * Records a "get" of a key.
115
113
 */
116
 
void stats_prefix_record_get(const char *key, const bool is_hit) {
 
114
void stats_prefix_record_get(const char *key, const size_t nkey, const bool is_hit) {
117
115
    PREFIX_STATS *pfs;
118
116
 
119
117
    STATS_LOCK();
120
 
    pfs = stats_prefix_find(key);
 
118
    pfs = stats_prefix_find(key, nkey);
121
119
    if (NULL != pfs) {
122
120
        pfs->num_gets++;
123
121
        if (is_hit) {
130
128
/*
131
129
 * Records a "delete" of a key.
132
130
 */
133
 
void stats_prefix_record_delete(const char *key) {
 
131
void stats_prefix_record_delete(const char *key, const size_t nkey) {
134
132
    PREFIX_STATS *pfs;
135
133
 
136
134
    STATS_LOCK();
137
 
    pfs = stats_prefix_find(key);
 
135
    pfs = stats_prefix_find(key, nkey);
138
136
    if (NULL != pfs) {
139
137
        pfs->num_deletes++;
140
138
    }
144
142
/*
145
143
 * Records a "set" of a key.
146
144
 */
147
 
void stats_prefix_record_set(const char *key) {
 
145
void stats_prefix_record_set(const char *key, const size_t nkey) {
148
146
    PREFIX_STATS *pfs;
149
147
 
150
148
    STATS_LOCK();
151
 
    pfs = stats_prefix_find(key);
 
149
    pfs = stats_prefix_find(key, nkey);
152
150
    if (NULL != pfs) {
153
151
        pfs->num_sets++;
154
152
    }
164
162
    PREFIX_STATS *pfs;
165
163
    char *buf;
166
164
    int i, pos;
167
 
    size_t size;
 
165
    size_t size = 0, written = 0, total_written = 0;
168
166
 
169
167
    /*
170
168
     * Figure out how big the buffer needs to be. This is the sum of the
187
185
    pos = 0;
188
186
    for (i = 0; i < PREFIX_HASH_SIZE; i++) {
189
187
        for (pfs = prefix_stats[i]; NULL != pfs; pfs = pfs->next) {
190
 
            pos += snprintf(buf + pos, size-pos, format,
 
188
            written = snprintf(buf + pos, size-pos, format,
191
189
                           pfs->prefix, pfs->num_gets, pfs->num_hits,
192
190
                           pfs->num_sets, pfs->num_deletes);
 
191
            pos += written;
 
192
            total_written += written;
 
193
            assert(total_written < size);
193
194
        }
194
195
    }
195
196
 
321
322
 
322
323
    /* Find a key that hashes to the same bucket as "abc" */
323
324
    for (keynum = 0; keynum < PREFIX_HASH_SIZE * 100; keynum++) {
324
 
        sprintf(tmp, "%d", keynum);
 
325
        snprintf(tmp, sizeof(tmp), "%d", keynum);
325
326
        if (hashval == hash(tmp, strlen(tmp), 0) % PREFIX_HASH_SIZE) {
326
327
            break;
327
328
        }
328
329
    }
329
330
    stats_prefix_record_set(tmp);
330
 
    sprintf(tmp, "PREFIX %d get 0 hit 0 set 1 del 0\r\n"
331
 
                 "PREFIX abc get 2 hit 1 set 1 del 1\r\n"
332
 
                 "PREFIX def get 0 hit 0 set 0 del 1\r\n"
333
 
                 "END\r\n", keynum);
 
331
    snprintf(tmp, sizeof(tmp),
 
332
             "PREFIX %d get 0 hit 0 set 1 del 0\r\n"
 
333
             "PREFIX abc get 2 hit 1 set 1 del 1\r\n"
 
334
             "PREFIX def get 0 hit 0 set 0 del 1\r\n"
 
335
             "END\r\n", keynum);
334
336
    test_equals_str("stats with two stats in one bucket",
335
337
                    tmp, stats_prefix_dump(&length));
336
338
    test_equals_int("stats length with two stats in one bucket",