-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
126 lines (106 loc) · 2.82 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cache.h"
// represents expected command line arguments
typedef struct {
int c; // cache size in number of pages
char fname[1024];
FILE *trace; // trace file
} input;
// represents a line in a trace file
// fields are self explanatory, see description.pdf
// for more info.
typedef struct {
int starting_block;
int number_of_blocks;
int ignore;
int request_number;
} trace_line;
// display usage info
int usage (char *bin) {
printf(
"USAGE: %s <c> <trace>\n"
" c - cache size in number of pages\n"
" trace - location of trace file\n"
, bin);
return 2;
}
// parse command line arguments
// argc - argument count
// argv - array of string arguments
// return 0 if successful
int parse_args (int argc, char **argv, input* in) {
// check correct number of parameters are passed
if (argc != 3) {
printf("Error: not enough parameters passed\n");
return usage(argv[0]);
}
// check first parameter is an integer
if (sscanf(argv[1], "%d", &in->c) != 1) {
printf("Error: c in not an integer\n");
return usage(argv[0]);
}
// check trace file by opening it
in->trace = fopen(argv[2], "r");
if (in->trace == NULL) {
printf("Error: \"%s\" is not a valid path\n", argv[2]);
return usage(argv[0]);
}
strcpy(in->fname, argv[2]);
// made it this far, congrats!
return 0;
}
// parse out the next line of the trace file
// f - open trace file pointer
// l - line struct pointer to populate line entries
// return 0 when done
// NOTE: assuming the trace file is similar to
// the description specification
int next_line (FILE *f, trace_line *l) {
if (fscanf(f,
"%d %d %d %d\n",
&l->starting_block,
&l->number_of_blocks,
&l->ignore,
&l->request_number
) == EOF) {
return 0;
}
return 1;
}
// run trace through various algorithm
// NOTE: assume that the cache is fully-associative
// - any item can be in any cache location
void trace (input* in) {
trace_line l;
int lines = 0;
cache *lru = cache_create(LRU, in->c);
cache *arc = cache_create(ARC, in->c);
while (next_line(in->trace, &l)) {
int n = l.starting_block + l.number_of_blocks;
for (int i = l.starting_block; i < n; i++) {
cache_get(lru, i);
cache_get(arc, i);
}
lines++;
if (lines % 100000 == 0) {
fprintf(stderr, "%s(c=%d): processed %d lines\r", in->fname, in->c, lines);
fflush(stderr);
}
}
printf("file: %17s, capacity: %6d, algo: lru, ", in->fname, in->c);
cache_print_stats(lru);
printf("file: %17s, capacity: %6d, algo: arc, ", in->fname, in->c);
cache_print_stats(arc);
cache_free(lru);
cache_free(arc);
}
int main (int argc, char **argv) {
input in;
if (parse_args(argc, argv, &in)) {
return 1;
}
trace(&in);
fclose(in.trace);
}