-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcd.c
192 lines (146 loc) · 3.93 KB
/
mcd.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#if HAVE_MCD
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>
#include <linux/mcd.h>
#include <errno.h>
#include "libmcd.h"
ssize_t mcd_getsize(const void *key, size_t key_size)
{
int ret_val = 0;
void *cast_key = (void *) key;
mcd_verbose(XENMCD_cache_check, MCDOPT_shared, key_size, cast_key, 0 /* val_size */ , &ret_val, NULL);
return ret_val;
}
int mcd_get(const void* key, size_t key_size, void **p_value)
{
void *cast_key = (void *) key;
ssize_t r_val_size = mcd_getsize(key, key_size);
int rc;
if (r_val_size < 0) {
if (r_val_size == -2000) {
errno = ENOENT;
}
return r_val_size;
}
void *p = malloc(rc);
if (!p) {
return -1;
}
mcd_verbose(XENMCD_cache_get, MCDOPT_shared, key_size, cast_key, r_val_size, &rc, p);
if (rc < 0) {
free(p);
p = NULL;
}
*p_value = p;
return rc;
}
int mcd_get_fill(const void* key, size_t key_size, void *buf, size_t buf_size)
{
void *cast_key = (void *) key;
int rc;
#if 0
/* Check first to avoid a memory leak in the ioctl implementation. */
mcd_verbose(XENMCD_cache_check, MCDOPT_shared, key_size, cast_key, 0, &rc, NULL);
if (rc > 0) /* > 0 means exists */
{
mcd_verbose(XENMCD_cache_get, MCDOPT_shared, key_size, cast_key, buf_size, &rc, buf);
}
#else
mcd_verbose(XENMCD_cache_get, MCDOPT_shared, key_size, cast_key, buf_size, &rc, buf);
#endif
return rc;
}
int mcd_put(const void *key, size_t key_size, const void *val, size_t val_size)
{
int ret_val;
void *cast_key = (void *) key;
void *cast_val = (void *) val;
mcd_verbose(XENMCD_cache_put, MCDOPT_shared, key_size, cast_key, val_size, &ret_val, cast_val);
return ret_val;
}
int mcd_remove(const void *key, size_t key_size)
{
int ret_val;
void *cast_key = (void *) key;
mcd_verbose(XENMCD_cache_remove, MCDOPT_shared, key_size, cast_key,
0 /* val_size */, &ret_val, NULL);
return ret_val;
}
int mcd_flush()
{
int ret_val = 0;
mcd_verbose(XENMCD_cache_flush, MCDOPT_shared, 0, NULL, 0, &ret_val, NULL);
return ret_val;
}
int mcd_stat()
{
char buf[1024];
int r_val_size = 0;
unsigned long cache_size, cache_free, cache_used;
memset(buf, 0, sizeof(buf));
mcd_verbose(XENMCD_stat_get, MCDOPT_shared, 0, 0, sizeof(buf), &r_val_size, buf);
printf("r_val_size = %d\n", r_val_size);
if (r_val_size > 0) {
sscanf(buf, "%lu,%lu\n", &cache_size, &cache_free);
cache_used = cache_size - cache_free;
double used = (double) cache_used / cache_size;
printf("cache_size = %lu cache_free = %lu cache_used = %lu used = %lf\n", cache_size, cache_free, cache_used, used);
return 0;
}
return -1;
}
#ifdef BUILD_MCD_MAIN
void usage(int exit_code)
{
fprintf(stderr, "Usage: mcd (get|put|remove|flush) [value]\n");
exit(exit_code);
}
int main(int argc, char *argv[])
{
int rc;
if (argc < 2) {
usage(1);
}
char *cmd = argv[1];
char *key = argv[2];
printf("mcd cmd is %s\n", cmd);
if (!strcmp(cmd, "get")) {
void *buf;
rc = mcd_get(key, strlen(key), &buf);
printf("rc = %d\n", rc);
if (rc > 0) {
printf("%s\n", (char *) buf);
free(buf);
}
}
else if (!strcmp(cmd, "put")) {
if (argc < 4) {
usage(1);
}
char *val = argv[3];
rc = mcd_put(key, strlen(key), val, strlen(val));
}
else if (!strcmp(cmd, "remove") || !strcmp(cmd, "rm")) {
rc = mcd_remove(key, strlen(key));
}
else if (!strcmp(cmd, "flush")) {
rc = mcd_flush();
}
else if (!strcmp(cmd, "stat")) {
rc = mcd_stat();
}
else {
usage(1);
}
if (rc < 0) {
perror("mcd");
exit(1);
}
return 0;
}
#endif
#endif /* HAVE_MCD */