-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemo.hh
171 lines (150 loc) · 3.91 KB
/
memo.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef MEMCACHED_HH
#define MEMCACHED_HH
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cctype>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread>
#include <string>
#include <map>
#include <sstream>
#include <mutex>
#include <float.h>
#include <chrono>
#define VERSION_STR "0.0.1"
/* Max pending connections queue length*/
#define MAX_CONNECTIONS 25
#define CLIENT_BUFFER_SIZE 1024
#define MEMCACHED_PORT 11211
#define WHITESPACE " \t\n\v\f\r"
#define _WRITER(X){\
write(client_sockfd, X "\r\n", sizeof(X "\r\n"));\
process_stats->bytes_written += sizeof(X "\r\n");\
}
#define STAT_WRITER(X) write(client_sockfd, X, sizeof(X))
/*
* Replies
*/
#define STORED _WRITER("STORED")
#define NOT_STORED _WRITER("NOT_STORED")
#define EXISTS _WRITER("EXISTS")
#define NOT_FOUND _WRITER("NOT_FOUND")
#define DELETED _WRITER("DELETED")
#define TOUCHED _WRITER("TOUCHED")
#define END _WRITER("END")
#define OK _WRITER("OK")
#define VERSION _WRITER("VERSION " VERSION_STR)
#define STAT(x) STAT_WRITER(x)
/*
* Error strings
*/
#define ERROR _WRITER("ERROR")
#define CLIENT_ERROR(X) _WRITER("CLIENT_ERROR " X)
#define SERVER_ERROR(X) _WRITER("SERVER_ERROR " X)
/*
* This is the Cache Entry data structure stored as value in the map
*/
typedef struct {
std::string key;
uint16_t flags;
int64_t expiry;
/* not including the delimiting \r\n */
size_t bytes;
uint64_t cas_unique;
bool noreply;
char *data;
} cache_entry;
/*
* MCMap is the map data structure that stores the hash-table
*/
typedef std::map<std::string, cache_entry> MCMap;
static MCMap *map = new MCMap();
static std::mutex map_mutex;
static unsigned memory_counter = 0;
static size_t memory_limit;
static std::map<std::string, long int> *cache_miss_map = new std::map<std::string, long int>();
/*
* Type for replacement policies
*/
typedef enum {
LRU,
RANDOM,
LANDLORD
} policy_t;
typedef struct node_t node_t;
struct node_t {
cache_entry* entry;
node_t *prev;
node_t *next;
float cost; // for landlord credit
};
static node_t *head = NULL;
static node_t *tail = NULL;
static size_t list_size = 0;
static float delta = FLT_MAX; // for current min(credit(entry)/size(entry))
static std::mutex list_mutex;
/* default value is LRU */
static policy_t policy = LRU;
typedef struct{
int32_t pid = getpid();
int32_t start_time = std::time(NULL); //for calculation of uptime
int32_t uptime;
int32_t time;
const char *version = VERSION_STR;
int32_t pointer_size = sizeof(uintptr_t)*8;
int32_t rusage_user;
int32_t rusage_system;
int32_t curr_items = 0;
int32_t total_items = 0;
int64_t bytes = 0;
int32_t curr_connections = 0;
int32_t total_connections = 0;
int32_t connection_structures = 0;
int32_t reserved_fds = 0;
int64_t cmd_get = 0;
int64_t cmd_set = 0;
int64_t cmd_flush = 0;
int64_t get_hits = 0;
int64_t get_misses = 0;
int64_t delete_misses = 0;
int64_t delete_hits = 0;
int64_t incr_misses = 0;
int64_t incr_hits = 0;
int64_t decr_misses = 0;
int64_t decr_hits = 0;
int64_t cas_misses = 0;
int64_t cas_hits = 0;
int64_t cas_badval = 0;
int64_t evictions = 0;
int64_t reclaimed = 0;
int64_t bytes_read = 0;
int64_t bytes_written = 0;
int32_t limit_maxbytes = memory_limit;
int32_t threads = 0;
} stats;
stats *process_stats = new stats();
static void write_VALUE(int client_sockfd, cache_entry *entry, unsigned gets_flag)
{
std::ostringstream os;
os << "VALUE " << entry->key << " " << entry->flags << " " << entry->bytes;
if (gets_flag)
os << " " << entry->cas_unique;
os << "\r\n";
write(client_sockfd, os.str().c_str(), strlen(os.str().c_str()));
}
/*static void print_map(MCMap *map)
{
for (const auto &p : *map) {
std::cout << "map[" << p.first << "] = ";
std::cout << p.second.key << ", ";
std::cout << p.second.flags << ", ";
std::cout << p.second.expiry << ", ";
std::cout << p.second.bytes << '\n';
}
}*/
#endif