-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest.c
158 lines (116 loc) · 3.25 KB
/
test.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
#include <stdio.h>
#include "slab.h"
#define ITERATIONS 30000000
#define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
/* test approach based inspired by MinUnit
http://www.jera.com/techinfo/jtns/jtn002.html */
#define assertt(message, test) do { if (!(test)) return message; } while (0)
#define run_test(test) do { char *message = test(); tests_run++; \
if (message) return (char*)message; } while (0)
int tests_run = 0;
static char *
test_cache_create() {
kmem_cache_t cp = kmem_cache_create("test", 12, 0, NULL, NULL);
assertt("cache creation returned null?", cp);
assertt("effective size miscalculated", cp->effsize == 16);
kmem_cache_destroy(cp);
return 0;
}
static char *
test_cache_grow() {
kmem_cache_t cp = kmem_cache_create("test", 12, 0, NULL, NULL);
kmem_cache_grow(cp);
kmem_cache_destroy(cp);
return 0;
}
static char *
test_cache_alloc() {
// 12-byte struct
struct test {
int a, b, c;
};
struct test * obj;
kmem_cache_t cp = kmem_cache_create("test", sizeof(struct test), 0, NULL, NULL);
obj = (struct test *)kmem_cache_alloc(cp, KM_NOSLEEP);
obj->a=1;
obj->b=1;
obj->c=1;
obj = (struct test *)kmem_cache_alloc(cp, KM_NOSLEEP);
obj->a=1;
obj->b=1;
obj->c=1;
kmem_cache_destroy(cp);
return 0;
}
static char *
test_perf_cache_alloc() {
unsigned long long start, end;
int i;
// 12-byte struct
struct test {
int a, b, c;
};
struct test * obj;
kmem_cache_t cp = kmem_cache_create("test", sizeof(struct test), 0, NULL, NULL);
rdtscll(start);
for (i=0; i<ITERATIONS; i++)
obj = (struct test *)kmem_cache_alloc(cp, KM_NOSLEEP);
rdtscll(end);
printf("# %lld cycles for cache mem alloc\n", (end-start)/ITERATIONS);
obj->a = 1;
rdtscll(start);
for (i=0; i<ITERATIONS; i++)
obj = (struct test *)malloc(sizeof(struct test));
rdtscll(end);
printf("# %lld cycles for malloc\n", (end-start)/ITERATIONS);
kmem_cache_destroy(cp);
return 0;
}
static char *
test_cache_free() {
// 12-byte struct
struct test {
int a, b, c;
};
struct test * obj;
kmem_cache_t cp = kmem_cache_create("test", sizeof(struct test), 0, NULL, NULL);
obj = (struct test *)kmem_cache_alloc(cp, KM_NOSLEEP);
obj->a=1;
obj->b=1;
obj->c=1;
kmem_cache_free(cp, obj);
kmem_cache_destroy(cp);
return 0;
}
static char *
test_big_object() {
int i;
void * pos;
kmem_cache_t cp = kmem_cache_create("test", 1000, 0, NULL, NULL);
// alocating enough for two slabs (auto-growing)
for (i = 0; i < 9; i++) {
pos = kmem_cache_alloc(cp, KM_NOSLEEP);
}
kmem_cache_destroy(cp);
return 0;
}
static char *
test_all () {
run_test(test_cache_create);
run_test(test_cache_grow);
run_test(test_cache_alloc);
// run_test(test_perf_cache_alloc);
run_test(test_cache_free);
run_test(test_big_object);
return 0;
}
int
main(void) {
char * result = test_all();
if (result)
printf("Test failed: %s\n", result);
else
printf("ALL TESTS PASSED!\n");
printf("=====================\nTOTAL TESTS:\t%04d\n", tests_run);
return (result != 0);
}