1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <mach.h>
5
6 #define PCRES 8
7
8 struct COUNTER
9 {
10 char *name; /* function name */
11 long time; /* ticks spent there */
12 };
13
14 void
error(int perr,char * s)15 error(int perr, char *s)
16 {
17 fprint(2, "kprof: %s", s);
18 if(perr){
19 fprint(2, ": ");
20 perror(0);
21 }else
22 fprint(2, "\n");
23 exits(s);
24 }
25
26 int
compar(void * va,void * vb)27 compar(void *va, void *vb)
28 {
29 struct COUNTER *a, *b;
30
31 a = va;
32 b = vb;
33 if(a->time < b->time)
34 return -1;
35 if(a->time == b->time)
36 return 0;
37 return 1;
38 }
39 void
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42 int fd;
43 long delta, i, j, k, n;
44 ulong sum;
45 ulong *data;
46 vlong tbase;
47 char *name;
48 Biobuf outbuf;
49 struct COUNTER *cp;
50 Fhdr f;
51 Dir *d;
52 Symbol s;
53
54 if(argc != 3)
55 error(0, "usage: kprof text data");
56 /*
57 * Read symbol table
58 */
59 fd = open(argv[1], OREAD);
60 if(fd < 0)
61 error(1, argv[1]);
62 if (!crackhdr(fd, &f))
63 error(1, "read text header");
64 if (f.type == FNONE)
65 error(0, "text file not an a.out");
66 if (syminit(fd, &f) < 0)
67 error(1, "syminit");
68 close(fd);
69 /*
70 * Read timing data
71 */
72 fd = open(argv[2], OREAD);
73 if(fd < 0)
74 error(1, argv[2]);
75 d = dirfstat(fd);
76 if(d == nil)
77 error(1, "stat");
78 n = d->length/sizeof(data[0]);
79 if(n < 2)
80 error(0, "data file too short");
81 data = malloc(d->length);
82 if(data == 0)
83 error(1, "malloc");
84 if(read(fd, data, d->length) < 0)
85 error(1, "text read");
86 close(fd);
87 for(i=0; i<n; i++)
88 data[i] = beswal(data[i]);
89
90 delta = data[0]-data[1];
91 print("total: %ld in kernel text: %ld outside kernel text: %ld\n",
92 data[0], delta, data[1]);
93 if(data[0] == 0)
94 exits(0);
95 /* get text symbol with lowest address */
96 if (!textsym(&s, 0))
97 error(0, "no text symbols");
98
99 tbase = mach->kbase;
100 if(tbase != (s.value & ~0xFFF)) {
101 print("warning: kbase %.8llux != tbase %.8llux\n",
102 tbase, s.value&~0xFFF);
103 tbase = s.value;
104 }
105 print("KTZERO %.8llux PGSIZE %dKb\n", tbase, mach->pgsize/1024);
106 /*
107 * Accumulate counts for each function
108 */
109 cp = 0;
110 k = 0;
111 for (i = 0, j = 2; j < n; i++) {
112 name = s.name; /* save name */
113 if (!textsym(&s, i)) /* get next symbol */
114 break;
115 s.value -= tbase;
116 s.value /= PCRES;
117 sum = 0;
118 while (j < n && j < s.value)
119 sum += data[j++];
120 if (sum) {
121 cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
122 if (cp == 0)
123 error(1, "realloc");
124 cp[k].name = name;
125 cp[k].time = sum;
126 k++;
127 }
128 }
129 if (!k)
130 error(0, "no counts");
131 cp[k].time = 0; /* "etext" can take no time */
132 /*
133 * Sort by time and print
134 */
135 qsort(cp, k, sizeof(struct COUNTER), compar);
136 Binit(&outbuf, 1, OWRITE);
137 Bprint(&outbuf, "ms %% sym\n");
138 while(--k>=0)
139 Bprint(&outbuf, "%ld\t%3lld.%lld\t%s\n",
140 cp[k].time,
141 100LL*cp[k].time/delta,
142 (1000LL*cp[k].time/delta)%10,
143 cp[k].name);
144 exits(0);
145 }
146