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
|
implement Prof;
include "sys.m";
sys: Sys;
include "draw.m";
include "arg.m";
arg: Arg;
include "profile.m";
profile: Profile;
include "sh.m";
stderr: ref Sys->FD;
Prof: module {
init: fn(nil: ref Draw->Context, argv: list of string);
init0: fn(nil: ref Draw->Context, argv: list of string): Profile->Coverage;
};
exits(e: string)
{
if(profile != nil)
profile->end();
raise "fail:" + e;
}
pfatal(s: string)
{
sys->fprint(stderr, "cprof: %s: %s\n", s, profile->lasterror());
exits("error");
}
badmodule(p: string)
{
sys->fprint(stderr, "cprof: cannot load %s: %r\n", p);
exits("bad module");
}
usage(s: string)
{
sys->fprint(stderr, "cprof: %s\n", s);
sys->fprint(stderr, "usage: cprof [-fner] [-m modname]... cmd [arg ... ]\n");
exits("usage");
}
init(ctxt: ref Draw->Context, argv: list of string)
{
init0(ctxt, argv);
}
init0(ctxt: ref Draw->Context, argv: list of string): Profile->Coverage
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
arg = load Arg Arg->PATH;
if(arg == nil)
badmodule(Arg->PATH);
arg->init(argv);
profile = load Profile Profile->PATH;
if(profile == nil)
badmodule(Profile->PATH);
if(profile->init() < 0)
pfatal("cannot initialize profile device");
v := 0;
ep := 0;
rec := 0;
wm := 0;
exec, mods: list of string;
while((c := arg->opt()) != 0){
case c {
'n' => v |= profile->FULLHDR;
'f' => v |= profile->FREQUENCY;
'm' =>
if((s := arg->arg()) == nil)
usage("missing module/file");
mods = s :: mods;
'e' =>
ep = 1;
'r' =>
rec = 1;
'g' =>
wm = 1;
* =>
usage(sys->sprint("unknown option -%c", c));
}
}
exec = arg->argv();
# if(exec == nil)
# usage("nothing to execute");
for( ; mods != nil; mods = tl mods)
profile->profile(hd mods);
if(ep && exec != nil)
profile->profile(disname(hd exec));
if(exec != nil){
wfd := openwait(sys->pctl(0, nil));
ci := chan of int;
spawn execute(ctxt, hd exec, exec, ci);
epid := <- ci;
if(profile->cpstart(epid) < 0){
ci <-= 0;
pfatal("cannot start profiling");
}
ci <-= 1;
wait(wfd, epid);
if(profile->stop() < 0)
pfatal("cannot stop profiling");
}
if(exec == nil)
modl := profile->cpfstats(v);
else
modl = profile->cpstats(rec, v);
if(modl.mods == nil)
pfatal("no profile information");
if(wm){
cvr := profile->coverage(modl, v);
profile->end();
return cvr;
}
if(!rec && profile->cpshow(modl, v) < 0)
pfatal("cannot show profile");
profile->end();
return nil;
}
disname(cmd: string): string
{
file := cmd;
if(len file<4 || file[len file-4:]!=".dis")
file += ".dis";
if(exists(file))
return file;
if(file[0]!='/' && file[0:2]!="./")
file = "/dis/"+file;
# if(exists(file))
# return file;
return file;
}
execute(ctxt: ref Draw->Context, cmd : string, argl : list of string, ci: chan of int)
{
ci <-= sys->pctl(Sys->FORKNS|Sys->NEWFD|Sys->NEWPGRP, 0 :: 1 :: 2 :: stderr.fd :: nil);
file := cmd;
err := "";
if(len file<4 || file[len file-4:]!=".dis")
file += ".dis";
c := load Command file;
if(c == nil) {
err = sys->sprint("%r");
if(file[0]!='/' && file[0:2]!="./"){
c = load Command "/dis/"+file;
if(c == nil)
err = sys->sprint("%r");
}
}
if(<- ci){
if(c == nil)
sys->fprint(stderr, "cprof: %s: %s\n", cmd, err);
else
c->init(ctxt, argl);
}
}
openwait(pid : int) : ref Sys->FD
{
w := sys->sprint("#p/%d/wait", pid);
fd := sys->open(w, Sys->OREAD);
if (fd == nil)
pfatal("fd == nil in wait");
return fd;
}
wait(wfd : ref Sys->FD, wpid : int)
{
n : int;
buf := array[Sys->WAITLEN] of byte;
status := "";
for(;;) {
if ((n = sys->read(wfd, buf, len buf)) < 0)
pfatal("bad read in wait");
status = string buf[0:n];
if (int status == wpid)
break;
}
}
exists(f: string): int
{
return sys->open(f, Sys->OREAD) != nil;
}
|