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
|
#include <u.h>
#include <libc.h>
#include <tos.h>
extern uintptr _callpc(void**);
extern uintptr _saveret(void);
extern uintptr _savearg(void);
static ulong khz;
static ulong perr;
static int havecycles;
typedef struct Plink Plink;
struct Plink
{
Plink *old;
Plink *down;
Plink *link;
long pc;
long count;
vlong time;
};
#pragma profile off
static uintptr
_restore(uintptr, uintptr ret)
{
return ret;
}
uintptr
_profin(void)
{
void *dummy;
long pc;
Plink *pp, *p;
uintptr arg, ret;
vlong t;
ret = _saveret();
arg = _savearg();
pc = _callpc(&dummy);
pp = _tos->prof.pp;
if(pp == 0 || (_tos->prof.pid && _tos->pid != _tos->prof.pid))
return _restore(arg, ret);
for(p=pp->down; p; p=p->link)
if(p->pc == pc)
goto out;
p = _tos->prof.next + 1;
if(p >= _tos->prof.last) {
_tos->prof.pp = 0;
perr++;
return _restore(arg, ret);
}
_tos->prof.next = p;
p->link = pp->down;
pp->down = p;
p->pc = pc;
p->old = pp;
p->down = 0;
p->count = 0;
p->time = 0LL;
out:
_tos->prof.pp = p;
p->count++;
switch(_tos->prof.what){
case Profkernel:
p->time = p->time - _tos->pcycles;
goto proftime;
case Profuser:
/* Add kernel cycles on proc entry */
p->time = p->time + _tos->kcycles;
/* fall through */
case Proftime:
proftime: /* Subtract cycle counter on proc entry */
cycles((uvlong*)&t);
p->time = p->time - t;
break;
case Profsample:
p->time = p->time - _tos->clock;
break;
}
return _restore(arg, ret);
}
uintptr
_profout(void)
{
Plink *p;
uintptr ret, arg;
vlong t;
ret = _saveret();
arg = _savearg();
p = _tos->prof.pp;
if (p == nil || (_tos->prof.pid != 0 && _tos->pid != _tos->prof.pid))
return _restore(arg, ret); /* Not our process */
switch(_tos->prof.what){
case Profkernel: /* Add proc cycles on proc entry */
p->time = p->time + _tos->pcycles;
goto proftime;
case Profuser: /* Subtract kernel cycles on proc entry */
p->time = p->time - _tos->kcycles;
/* fall through */
case Proftime:
proftime: /* Add cycle counter on proc entry */
cycles((uvlong*)&t);
p->time = p->time + t;
break;
case Profsample:
p->time = p->time + _tos->clock;
break;
}
_tos->prof.pp = p->old;
return _restore(arg, ret);
}
void
_profdump(void)
{
int f;
long n;
Plink *p;
char *vp;
char filename[64];
if (_tos->prof.what == 0)
return; /* No profiling */
if (_tos->prof.pid != 0 && _tos->pid != _tos->prof.pid)
return; /* Not our process */
if(perr)
fprint(2, "%lud Prof errors\n", perr);
_tos->prof.pp = nil;
if (_tos->prof.pid)
snprint(filename, sizeof filename - 1, "prof.%ld", _tos->prof.pid);
else
snprint(filename, sizeof filename - 1, "prof.out");
f = create(filename, OWRITE|OCEXEC, 0666);
if(f < 0) {
perror("create prof.out");
return;
}
_tos->prof.pid = ~0; /* make sure data gets dumped once */
switch(_tos->prof.what){
case Profkernel:
cycles((uvlong*)&_tos->prof.first->time);
_tos->prof.first->time = _tos->prof.first->time + _tos->pcycles;
break;
case Profuser:
cycles((uvlong*)&_tos->prof.first->time);
_tos->prof.first->time = _tos->prof.first->time - _tos->kcycles;
break;
case Proftime:
cycles((uvlong*)&_tos->prof.first->time);
break;
case Profsample:
_tos->prof.first->time = _tos->clock;
break;
}
vp = (char*)_tos->prof.first;
for(p = _tos->prof.first; p <= _tos->prof.next; p++) {
/*
* short down
*/
n = 0xffff;
if(p->down)
n = p->down - _tos->prof.first;
vp[0] = n>>8;
vp[1] = n;
/*
* short right
*/
n = 0xffff;
if(p->link)
n = p->link - _tos->prof.first;
vp[2] = n>>8;
vp[3] = n;
vp += 4;
/*
* long pc
*/
n = p->pc;
vp[0] = n>>24;
vp[1] = n>>16;
vp[2] = n>>8;
vp[3] = n;
vp += 4;
/*
* long count
*/
n = p->count;
vp[0] = n>>24;
vp[1] = n>>16;
vp[2] = n>>8;
vp[3] = n;
vp += 4;
/*
* vlong time
*/
if (havecycles){
n = (vlong)(p->time / (vlong)khz);
}else
n = p->time;
vp[0] = n>>24;
vp[1] = n>>16;
vp[2] = n>>8;
vp[3] = n;
vp += 4;
}
write(f, (char*)_tos->prof.first, vp - (char*)_tos->prof.first);
close(f);
}
void
_profinit(int entries, int what)
{
if (_tos->prof.what == 0)
return; /* Profiling not linked in */
_tos->prof.pp = nil;
_tos->prof.first = mallocz(entries*sizeof(Plink),1);
_tos->prof.last = _tos->prof.first + entries;
_tos->prof.next = _tos->prof.first;
_tos->prof.pid = _tos->pid;
_tos->prof.what = what;
_tos->clock = 1;
}
void
_profmain(void)
{
char ename[50];
int n, f;
n = 2000;
if (_tos->cyclefreq != 0LL){
khz = _tos->cyclefreq / 1000; /* Report times in milliseconds */
havecycles = 1;
}
f = open("/env/profsize", OREAD|OCEXEC);
if(f >= 0) {
memset(ename, 0, sizeof(ename));
read(f, ename, sizeof(ename)-1);
close(f);
n = atol(ename);
}
_tos->prof.what = Profuser;
f = open("/env/proftype", OREAD|OCEXEC);
if(f >= 0) {
memset(ename, 0, sizeof(ename));
read(f, ename, sizeof(ename)-1);
close(f);
if (strcmp(ename, "user") == 0)
_tos->prof.what = Profuser;
else if (strcmp(ename, "kernel") == 0)
_tos->prof.what = Profkernel;
else if (strcmp(ename, "elapsed") == 0 || strcmp(ename, "time") == 0)
_tos->prof.what = Proftime;
else if (strcmp(ename, "sample") == 0)
_tos->prof.what = Profsample;
}
_tos->prof.first = sbrk(n*sizeof(Plink));
_tos->prof.last = sbrk(0);
_tos->prof.next = _tos->prof.first;
_tos->prof.pp = nil;
_tos->prof.pid = _tos->pid;
atexit(_profdump);
_tos->clock = 1;
}
void prof(void (*fn)(void*), void *arg, int entries, int what)
{
_profinit(entries, what);
_tos->prof.pp = _tos->prof.next;
fn(arg);
_profdump();
}
#pragma profile on
|