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
|
implement Wmprof;
include "sys.m";
sys: Sys;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "draw.m";
draw: Draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "arg.m";
arg: Arg;
include "profile.m";
Prof: module{
init0: fn(ctxt: ref Draw->Context, argv: list of string): Profile->Prof;
};
prof: Prof;
Wmprof: module{
init: fn(ctxt: ref Draw->Context, argl: list of string);
};
usage(s: string)
{
sys->fprint(sys->fildes(2), "wm/prof: %s\n", s);
sys->fprint(sys->fildes(2), "usage: wm/prof [-e] [-m modname]... cmd [arg ... ]\n");
exit;
}
TXTBEGIN: con 3;
init(ctxt: ref Draw->Context, argl: list of string)
{
sys = load Sys Sys->PATH;
bufio = load Bufio Bufio->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
arg = load Arg Arg->PATH;
if(ctxt == nil)
fatal("wm not running");
sys->pctl(Sys->NEWPGRP, nil);
arg->init(argl);
while((o := arg->opt()) != 0){
case(o){
'e' => ;
'm' =>
if(arg->arg() == nil)
usage("missing module/file");
's' =>
if(arg->arg() == nil)
usage("missing sample rate");
* =>
usage(sys->sprint("unknown option -%c", o));
}
}
stats := execprof(ctxt, argl);
if(stats.mods == nil)
exit;
tkclient->init();
(win, wmc) := tkclient->toplevel(ctxt, nil, hd argl, Tkclient->Resize|Tkclient->Hide);
tkc := chan of string;
tk->namechan(win, tkc, "tkc");
for(i := 0; i < len wincfg; i++)
cmd(win, wincfg[i]);
tkclient->onscreen(win, nil);
tkclient->startinput(win, "kbd"::"ptr"::nil);
createmenu(win, stats);
curc := 0;
cura := newprint(win, stats, curc);
for(;;){
alt{
c := <-win.ctxt.kbd =>
tk->keyboard(win, c);
c := <-win.ctxt.ptr =>
tk->pointer(win, *c);
c := <-win.ctxt.ctl or
c = <-win.wreq or
c = <-wmc =>
tkclient->wmctl(win, c);
c := <- tkc =>
(nil, toks) := sys->tokenize(c, " ");
case(hd toks){
"b" =>
if(curc > 0)
cura = newprint(win, stats, --curc);
"f" =>
if(curc < len stats.mods - 1)
cura = newprint(win, stats, ++curc);
"s" =>
if(cura != nil)
scroll(win, cura);
"m" =>
x := cmd(win, ".f cget actx");
y := cmd(win, ".f cget acty");
cmd(win, ".f.menu post " + x + " " + y);
* =>
curc = int hd toks;
cura = newprint(win, stats, curc);
}
}
}
}
execprof(ctxt: ref Draw->Context, argl: list of string): Profile->Prof
{
{
prof = load Prof "/dis/prof.dis";
if(prof == nil)
fatal("cannot load profiler");
return prof->init0(ctxt, hd argl :: "-g" :: tl argl);
}
exception{
"fail:*" =>
return (nil, 0, nil);
}
return (nil, 0, nil);
}
newprint(win: ref Tk->Toplevel, p: Profile->Prof, i: int): array of int
{
cmd(win, ".f.t delete 1.0 end");
cmd(win, "update");
m0, m1: list of Profile->Modprof;
for(m := p.mods; m != nil && --i >= 0; m = tl m)
m0 = m;
if(m == nil)
return nil;
m1 = tl m;
(name, nil, spath, nil, line, nil, nil, tot, nil, nil) := hd m;
name0 := name1 := "nil";
if(m0 != nil)
name0 = (hd m0).name;
if(m1 != nil)
name1 = (hd m1).name;
a := len name;
name += sys->sprint(" (%d%%) ", percent(tot, p.total));
cmd(win, ".f.t insert end {" + name + " <- " + name0 + " -> " + name1 + "}");
tag := gettag(win, tot, p.total);
cmd(win, ".f.t tag add " + tag + " " + "1.0" + " " + "1." + string a);
cmd(win, ".f.t insert end \n\n");
cmd(win, "update");
lineno := TXTBEGIN;
bio := bufio->open(spath, Bufio->OREAD);
if(bio == nil)
return nil;
i = 1;
ll := len line;
while((s := bio.gets('\n')) != nil){
f := 0;
if(i < ll)
f = line[i];
a = len s;
if(f > 0)
s = sys->sprint("%d%%\t%s", percent(f, tot), s);
else
s = sys->sprint("- \t%s", s);
b := len s;
cmd(win, ".f.t insert end " + tk->quote(s));
tag = gettag(win, f, tot);
cmd(win, ".f.t tag add " + tag + " " + string lineno + "." + string (b-a) + " " + string lineno + "." + string (b-1));
cmd(win, "update");
lineno++;
i++;
}
return line;
}
index(win: ref Tk->Toplevel, x: int, y: int): int
{
t := cmd(win, ".f.t index @" + string x + "," + string y);
(nil, l) := sys->tokenize(t, ".");
# sys->print("%d,%d -> %s\n", x, y, t);
return int hd l;
}
winextent(win: ref Tk->Toplevel): (int, int)
{
w := int cmd(win, ".f.t cget -actwidth");
h := int cmd(win, ".f.t cget -actheight");
lw := index(win, 0, 0);
uw := index(win, w-1, h-1);
return (lw, uw);
}
see(win: ref Tk->Toplevel, line: int)
{
cmd(win, ".f.t see " + string line + ".0");
cmd(win, "update");
}
scroll(win: ref Tk->Toplevel, line: array of int)
{
(nil, uw) := winextent(win);
lno := TXTBEGIN;
ll := len line;
for(i := 1; i < ll; i++){
n := line[i];
if(n > 0 && lno > uw){
see(win, lno);
return;
}
lno++;
}
lno = TXTBEGIN;
ll = len line;
for(i = 1; i < ll; i++){
n := line[i];
if(n > 0){
see(win, lno);
return;
}
lno++;
}
}
cmd(top: ref Tk->Toplevel, s: string): string
{
# sys->print("%s\n", s);
e := tk->cmd(top, s);
if (e != nil && e[0] == '!')
sys->fprint(sys->fildes(2), "tk error on '%s': %s\n", s, e);
return e;
}
fatal(s: string)
{
sys->fprint(sys->fildes(2), "%s\n", s);
exit;
}
MENUMAX: con 20;
createmenu(top: ref Tk->Toplevel, p: Profile->Prof )
{
mn := ".f.menu";
cmd(top, "menu " + mn);
i := j := 0;
for(m := p.mods; m != nil; m = tl m){
name := (hd m).name;
cmd(top, mn + " add command -label " + name + " -command {send tkc " + string i + "}");
i++;
j++;
if(j == MENUMAX && tl m != nil){
cmd(top, mn + " add cascade -label MORE -menu " + mn + ".menu");
mn += ".menu";
cmd(top, "menu " + mn);
j = 0;
}
}
}
tags := array[256] of { * => byte 0 };
gettag(win: ref Tk->Toplevel, n: int, d: int): string
{
i := int ((real n/real d) * real 15);
if(i < 0 || i > 15)
i = 0;
s := "tag" + string i;
if(tags[i] == byte 0){
rgb := "#" + hex2(255-64*0)+hex2(255-64*(i/4))+hex2(255-64*(i%4));
cmd(win, ".f.t tag configure " + s + " -fg black -bg " + rgb);
tags[i] = byte 1;
}
return s;
}
percent(n: int, d: int): int
{
return int ((real n/real d) * real 100);
}
hex(i: int): int
{
if(i < 10)
return i+'0';
else
return i-10+'A';
}
hex2(i: int): string
{
s := "00";
s[0] = hex(i/16);
s[1] = hex(i%16);
return s;
}
wincfg := array[] of {
"frame .f",
"text .f.t -width 809 -height 500 -state disabled -wrap char -bg white -yscrollcommand {.f.s set}",
"scrollbar .f.s -orient vertical -command {.f.t yview}",
"frame .i",
"button .i.b -bitmap small_color_left.bit -command {send tkc b}",
"button .i.f -bitmap small_color_right.bit -command {send tkc f}",
"button .i.s -bitmap small_find.bit -command {send tkc s}",
"button .i.m -bitmap small_reload.bit -command {send tkc m}",
"pack .i.b -side left",
"pack .i.f -side left",
"pack .i.s -side left",
"pack .i.m -side left",
"pack .f.s -fill y -side left",
"pack .f.t -fill both -expand 1",
"pack .i -fill x",
"pack .f -fill both -expand 1",
"pack propagate . 0",
"update",
};
|