1 /*
2  * Summary: Specification of the storage engine interface.
3  *
4  * Copy: See Copyright for the status of this software.
5  *
6  * Author: Trond Norbye <trond.norbye@sun.com>
7  */
8 #ifndef MEMCACHED_DEFAULT_ENGINE_H
9 #define MEMCACHED_DEFAULT_ENGINE_H
10 
11 #include "config.h"
12 
13 #include <pthread.h>
14 
15 #include <memcached/engine.h>
16 #include <memcached/util.h>
17 #include <memcached/visibility.h>
18 
19 /* Slab sizing definitions. */
20 #define POWER_SMALLEST 1
21 #define POWER_LARGEST  200
22 #define CHUNK_ALIGN_BYTES 8
23 #define DONT_PREALLOC_SLABS
24 #define MAX_NUMBER_OF_SLAB_CLASSES (POWER_LARGEST + 1)
25 
26 /** How long an object can reasonably be assumed to be locked before
27     harvesting it on a low memory condition. */
28 #define TAIL_REPAIR_TIME (3 * 3600)
29 
30 
31 /* Forward decl */
32 struct default_engine;
33 
34 #include "trace.h"
35 #include "items.h"
36 #include "assoc.h"
37 #include "hash.h"
38 #include "slabs.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44    /* Flags */
45 #define ITEM_WITH_CAS 1
46 
47 #define ITEM_LINKED (1<<8)
48 
49 /* temp */
50 #define ITEM_SLABBED (2<<8)
51 
52 struct config {
53    bool use_cas;
54    size_t verbose;
55    rel_time_t oldest_live;
56    bool evict_to_free;
57    size_t maxbytes;
58    bool preallocate;
59    float factor;
60    size_t chunk_size;
61    size_t item_size_max;
62    bool ignore_vbucket;
63    bool vb0;
64 };
65 
66 MEMCACHED_PUBLIC_API
67 ENGINE_ERROR_CODE create_instance(uint64_t interface,
68                                   GET_SERVER_API get_server_api,
69                                   ENGINE_HANDLE **handle);
70 
71 /**
72  * Statistic information collected by the default engine
73  */
74 struct engine_stats {
75    pthread_mutex_t lock;
76    uint64_t evictions;
77    uint64_t reclaimed;
78    uint64_t curr_bytes;
79    uint64_t curr_items;
80    uint64_t total_items;
81 };
82 
83 struct engine_scrubber {
84    pthread_mutex_t lock;
85    bool running;
86    uint64_t visited;
87    uint64_t cleaned;
88    time_t started;
89    time_t stopped;
90 };
91 
92 enum vbucket_state {
93     VBUCKET_STATE_DEAD    = 0,
94     VBUCKET_STATE_ACTIVE  = 1,
95     VBUCKET_STATE_REPLICA = 2,
96     VBUCKET_STATE_PENDING = 3
97 };
98 
99 struct vbucket_info {
100     int state : 2;
101 };
102 
103 #define NUM_VBUCKETS 65536
104 
105 /**
106  * Definition of the private instance data used by the default engine.
107  *
108  * This is currently "work in progress" so it is not as clean as it should be.
109  */
110 struct default_engine {
111    ENGINE_HANDLE_V1 engine;
112    SERVER_HANDLE_V1 server;
113    GET_SERVER_API get_server_api;
114 
115    /**
116     * Is the engine initalized or not
117     */
118    bool initialized;
119 
120    struct assoc assoc;
121    struct slabs slabs;
122    struct items items;
123 
124    /**
125     * The cache layer (item_* and assoc_*) is currently protected by
126     * this single mutex
127     */
128    pthread_mutex_t cache_lock;
129 
130    struct config config;
131    struct engine_stats stats;
132    struct engine_scrubber scrubber;
133    union {
134        engine_info engine_info;
135        char buffer[sizeof(engine_info) +
136                    (sizeof(feature_info) * LAST_REGISTERED_ENGINE_FEATURE)];
137    } info;
138 
139    char vbucket_infos[NUM_VBUCKETS];
140 };
141 
142 char* item_get_data(const hash_item* item);
143 const void* item_get_key(const hash_item* item);
144 void item_set_cas(ENGINE_HANDLE *handle, const void *cookie,
145                   item* item, uint64_t val);
146 uint64_t item_get_cas(const hash_item* item);
147 uint8_t item_get_clsid(const hash_item* item);
148 #endif
149