1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <sys/types.h> |
4 |
#include <sys/time.h> |
5 |
#include <sys/resource.h> |
6 |
|
7 |
#define N_CALLS_PER_PAGE 1024 |
8 |
|
9 |
#define MHZ 533 |
10 |
|
11 |
struct cpu; |
12 |
|
13 |
struct instr_call { |
14 |
void (*f)(struct cpu *cpu, struct instr_call *ic); |
15 |
int instr_len; |
16 |
void *arg[3]; |
17 |
}; |
18 |
|
19 |
struct cpu { |
20 |
void *curpage; |
21 |
int orig_nloops; |
22 |
int nloops; |
23 |
|
24 |
struct instr_call *next_instr_call; |
25 |
}; |
26 |
|
27 |
void r(struct cpu *cpu) |
28 |
{ |
29 |
struct instr_call *ic; |
30 |
|
31 |
for (;;) { |
32 |
ic = cpu->next_instr_call++; |
33 |
ic->f(cpu, ic); |
34 |
|
35 |
ic = cpu->next_instr_call++; |
36 |
ic->f(cpu, ic); |
37 |
|
38 |
ic = cpu->next_instr_call++; |
39 |
ic->f(cpu, ic); |
40 |
|
41 |
ic = cpu->next_instr_call++; |
42 |
ic->f(cpu, ic); |
43 |
|
44 |
ic = cpu->next_instr_call++; |
45 |
ic->f(cpu, ic); |
46 |
|
47 |
ic = cpu->next_instr_call++; |
48 |
ic->f(cpu, ic); |
49 |
|
50 |
ic = cpu->next_instr_call++; |
51 |
ic->f(cpu, ic); |
52 |
|
53 |
ic = cpu->next_instr_call++; |
54 |
ic->f(cpu, ic); |
55 |
} |
56 |
} |
57 |
|
58 |
|
59 |
void f_add(struct cpu *cpu, struct instr_call *ic) |
60 |
{ |
61 |
int *a = (int *) ic->arg[0]; |
62 |
int *b = (int *) ic->arg[1]; |
63 |
int *c = (int *) ic->arg[2]; |
64 |
|
65 |
*a = (*b) + (*c); |
66 |
|
67 |
#if 0 |
68 |
ic = cpu->next_instr_call++; |
69 |
|
70 |
a = (int *) ic->arg[0]; |
71 |
b = (int *) ic->arg[1]; |
72 |
c = (int *) ic->arg[2]; |
73 |
|
74 |
*a = (*b) + (*c); |
75 |
#endif |
76 |
} |
77 |
|
78 |
|
79 |
void f_sub(struct cpu *cpu, struct instr_call *ic) |
80 |
{ |
81 |
int *a = (int *) ic->arg[0]; |
82 |
int *b = (int *) ic->arg[1]; |
83 |
int *c = (int *) ic->arg[2]; |
84 |
*a = (*b) - (*c); |
85 |
} |
86 |
|
87 |
|
88 |
void f_and(struct cpu *cpu, struct instr_call *ic) |
89 |
{ |
90 |
int *a = (int *) ic->arg[0]; |
91 |
int *b = (int *) ic->arg[1]; |
92 |
int *c = (int *) ic->arg[2]; |
93 |
*a = (*b) & (*c); |
94 |
} |
95 |
|
96 |
|
97 |
void f_or(struct cpu *cpu, struct instr_call *ic) |
98 |
{ |
99 |
int *a = (int *) ic->arg[0]; |
100 |
int *b = (int *) ic->arg[1]; |
101 |
int *c = (int *) ic->arg[2]; |
102 |
*a = (*b) | (*c); |
103 |
} |
104 |
|
105 |
|
106 |
void f_end(struct cpu *cpu, struct instr_call *ic) |
107 |
{ |
108 |
struct rusage rusage; |
109 |
double t, mips; |
110 |
|
111 |
cpu->nloops--; |
112 |
if (cpu->nloops > 0) { |
113 |
cpu->next_instr_call = cpu->curpage; |
114 |
return; |
115 |
} |
116 |
|
117 |
getrusage(RUSAGE_SELF, &rusage); |
118 |
t = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec / 1000000.0; |
119 |
mips = (float)(N_CALLS_PER_PAGE * cpu->orig_nloops) / t |
120 |
/ 1000000.0; |
121 |
|
122 |
printf("%.2f user seconds = %.1f MIPS. %.1f cycles/emulated " |
123 |
"instruction on a %i MHz machine\n", (float)t, (float)mips, |
124 |
(float)MHZ/mips, MHZ); |
125 |
exit(1); |
126 |
} |
127 |
|
128 |
|
129 |
int main(int argc, char *argv[]) |
130 |
{ |
131 |
int32_t tmp_a, tmp_b, tmp_c; |
132 |
struct instr_call *call_array; |
133 |
int i, ncalls; |
134 |
struct cpu *cpu = malloc(sizeof(struct cpu)); |
135 |
|
136 |
if (argc <= 1) { |
137 |
fprintf(stderr, "usage: %s n\n", argv[0]); |
138 |
exit(1); |
139 |
} |
140 |
|
141 |
cpu->orig_nloops = cpu->nloops = atoi(argv[1]); |
142 |
ncalls = N_CALLS_PER_PAGE + 1; |
143 |
|
144 |
/* Fill a range of nonsense calls: */ |
145 |
call_array = malloc(sizeof(struct instr_call) * ncalls); |
146 |
cpu->curpage = call_array; |
147 |
|
148 |
printf("ncalls = %i\n", ncalls); |
149 |
for (i=0; i<ncalls; i++) { |
150 |
if (i == ncalls-1) { |
151 |
call_array[i].f = f_end; |
152 |
} else { |
153 |
switch (i & 3) { |
154 |
case 0: call_array[i].f = f_add; |
155 |
case 1: call_array[i].f = f_sub; |
156 |
case 2: call_array[i].f = f_and; |
157 |
case 3: call_array[i].f = f_or; |
158 |
} |
159 |
|
160 |
call_array[i].arg[0] = &tmp_a; |
161 |
call_array[i].arg[1] = &tmp_b; |
162 |
call_array[i].arg[2] = &tmp_c; |
163 |
} |
164 |
} |
165 |
|
166 |
printf("running...\n"); |
167 |
cpu->next_instr_call = &call_array[0]; |
168 |
r(cpu); |
169 |
|
170 |
printf("ERROR!\n"); |
171 |
return 0; |
172 |
} |
173 |
|