-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkma.c
363 lines (287 loc) · 8.29 KB
/
kma.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/***************************************************************************
* Title: Kernel Memory Allocator
* -------------------------------------------------------------------------
* Purpose: Test suite for the kernel memory allocator
* Author: Stefan Birrer
* Version: $Revision: 1.3 $
* Last Modification: $Date: 2009/10/31 21:28:52 $
* File: $RCSfile: kma.c,v $
* Copyright: 2004 Northwestern University
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* $Log: kma.c,v $
* Revision 1.3 2009/10/31 21:28:52 jot836
* This is the current version of KMA project 3.
* It includes:
* - the most up-to-date handout (F'09)
* - updated skeleton including
* file-driven test harness,
* trace generator script,
* support for evaluating efficiency of algorithm (wasted memory),
* gnuplot support for plotting allocation and waste,
* set of traces for all students to use (including a makefile and README of the settings),
* - different version of the testsuite for use on the submission site, including:
* scoreboard Python scripts, which posts the top 5 scores on the course webpage
*
* Revision 1.2 2009/10/21 07:06:46 npb853
* New test framework in place. Also adding a new sample testcase file
*
* Revision 1.1 2005/10/24 16:07:09 sbirrer
* - skeleton
*
* Revision 1.4 2004/11/30 22:11:42 sbirrer
* - assure always one allocation pending during test
*
* Revision 1.3 2004/11/16 19:33:50 sbirrer
* - increased the request size
*
* Revision 1.2 2004/11/05 15:45:56 sbirrer
* - added size as a parameter to kma_free
*
* Revision 1.1 2004/11/03 23:04:03 sbirrer
* - initial version for the kernel memory allocator project
*
* Revision 1.1 2004/11/03 18:34:52 sbirrer
* - initial version of the kernel memory project
*
***************************************************************************/
#define __KMA_TEST_IMPL__
/************System include***********************************************/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/************Private include**********************************************/
#include "kpage.h"
#include "kma.h"
/************Defines and Typedefs*****************************************/
/* #defines and typedefs should have their names in all caps.
* Global variables begin with g. Global constants with k. Local
* variables should be in all lower case. When initializing
* structures and arrays, line everything up in neat columns.
*/
enum REQ_STATE
{
FREE,
USED
};
typedef struct mem
{
int size;
void* ptr;
void* value; // to check correctness
enum REQ_STATE state;
} mem_t;
/************Global Variables*********************************************/
static int val = 0;
/************Function Prototypes******************************************/
void allocate();
void deallocate();
void fill(char*, int);
void check(char*, char*, int);
void usage();
void error(char*, char*);
void pass();
void fail();
/************External Declaration*****************************************/
/**************Implementation***********************************************/
int anyMismatches = 0;
int currentAllocBytes = 0;
char *name = NULL;
int
main(int argc, char* argv[])
{
name = argv[0];
#ifdef COMPETITION
printf("%s: Running in competition mode\n", name);
#endif
#ifndef COMPETITION
printf("%s: Running in correctness mode\n", name);
#endif
int n_req = 0, n_alloc=0, n_dealloc=0;
kpage_stat_t* stat;
#ifdef COMPETITION
double ratioSum = 0.0;
int ratioCount = 0;
#endif
#ifndef COMPETITION
FILE* allocTrace = fopen("kma_output.dat", "w");
if (allocTrace == NULL)
{
error("unable to open allocation output file", "kma_output.dat");
}
fprintf(allocTrace, "0 0 0\n");
#endif
if (argc != 2)
{
usage();
}
FILE* f_test = fopen(argv[1], "r");
if (f_test == NULL)
{
error("unable to open input test file", argv[1]);
}
// Get the number of requests in the trace file
// Allocate some memory...
int status = fscanf(f_test, "%d\n", &n_req);
if(status != 1)
error("Couldn't read number of requests at head of file", "");
mem_t* requests = malloc((n_req + 1)*sizeof(mem_t));
memset(requests, 0, (n_req + 1)*sizeof(mem_t));
char command[16];
int req_id, req_size, index = 1;
// Parse the lines in the file, and call allocate or
// deallocate accordingly.
while (fscanf(f_test, "%10s", command) == 1)
{
if (strcmp(command, "REQUEST") == 0)
{
if (fscanf(f_test, "%d %d", &req_id, &req_size) != 2)
error("Not enough arguments to REQUEST", "");
assert(req_id >= 0 && req_id < n_req);
allocate(requests, req_id, req_size);
n_alloc++;
}
else if (strcmp(command, "FREE") == 0)
{
if (fscanf(f_test, "%d", &req_id) != 1)
error("Not enough arguments to FREE", "");
assert(req_id >= 0 && req_id < n_req);
deallocate(requests, req_id);
n_dealloc++;
}
else
{
error("unknown command type:", command);
}
stat = page_stats();
int totalBytes = stat->num_in_use * stat->page_size;
#ifdef COMPETITION
if(req_id < n_req && n_alloc != n_dealloc)
{
// We can calculate the ratio of wasted to used memory here.
int wastedBytes = totalBytes - currentAllocBytes;
ratioSum += ((double) wastedBytes) / currentAllocBytes;
ratioCount += 1;
}
#endif
#ifndef COMPETITION
fprintf(allocTrace, "%d %d %d\n", index, currentAllocBytes, totalBytes);
#endif
index += 1;
}
#ifndef COMPETITION
fclose(allocTrace);
#endif
stat = page_stats();
printf("Page Requested/Freed/In Use: %5d/%5d/%5d\n",
stat->num_requested, stat->num_freed, stat->num_in_use);
if (stat->num_requested != stat->num_freed || stat->num_in_use != 0)
{
error("not all pages freed", "");
}
if(anyMismatches)
{
error("there were memory mismatches", "");
}
#ifdef COMPETITION
printf("Competition average ratio: %f\n", ratioSum / ratioCount);
#endif
pass();
return 0;
}
void
fail()
{
printf("Test: FAILED\n");
exit(-1);
}
void
pass()
{
printf("Test: PASS\n");
exit(0);
}
void
usage() {
printf("Usage: %s traceFile\n", name);
exit(0);
}
void
error(char* message, char* arg ) {
fprintf(stderr, "ERROR: %s: %s.\n", message, arg);
fail();
}
void
allocate(mem_t* requests, int req_id, int req_size)
{
mem_t* new = &requests[req_id];
assert(new->state == FREE);
new->size = req_size;
new->ptr = kma_malloc(new->size);
// Accept a NULL response in some cases...
if(!(((new->ptr != NULL) && (new->size <= (PAGESIZE - sizeof(void*))))
|| ((new->ptr == NULL) && (new->size > (PAGESIZE - sizeof(void*))))))
{
error("got NULL from kma_malloc for alloc'able request", "");
}
if (new->ptr == NULL)
{
return;
}
currentAllocBytes += req_size;
#ifndef COMPETITION
// Only run the actual memory accesses/copies/checks if we're
// testing for correctness.
new->value = malloc(new->size);
assert(new->value != NULL);
// initialize memory
fill((char*)new->ptr, new->size);
// copy the value for further reference
bcopy(new->ptr, new->value, new->size);
check((char*)new->ptr, (char*)new->value, new->size);
#endif
new->state = USED;
}
void
deallocate(mem_t* requests, int req_id)
{
mem_t* cur = &requests[req_id];
assert(cur->state == USED);
assert(cur->size > 0);
#ifndef COMPETITION
// Only run the memory checks if we're testing for correctness.
// check memory
check((char*)cur->ptr, (char*)cur->value, cur->size);
// free memory
free(cur->value);
#endif
kma_free(cur->ptr, cur->size);
currentAllocBytes -= cur->size;
cur->state = FREE;
}
void
fill(char* ptr, int size)
{
int i;
for (i = 0; i < size; i++)
{
ptr[i] = (char) val++;
}
}
void
check(char* lhs, char* rhs, int size)
{
int i;
for (i = 0; i < size; i++)
{
if (lhs[i] != rhs[i])
{
fprintf(stderr, "memory mismatch at position %d (%3d!=%3d)\n",
i, lhs[i], rhs[i]);
anyMismatches = 1;
}
}
}