10 char *name; /* function name */
11 long time; /* ticks spent there */
15 error(int perr, char *s)
17 fprint(2, "tprof: %s", s);
27 compar(void *va, void *vb)
29 struct COUNTER *a, *b;
35 if(a->time == b->time)
40 main(int argc, char *argv[])
53 char filebuf[128], *file;
55 if(argc != 2 && argc != 3)
56 error(0, "usage: tprof pid [binary]");
62 snprint(filebuf, sizeof filebuf, "/proc/%s/text", argv[1]);
66 fd = open(file, OREAD);
70 if (!crackhdr(fd, &f))
71 error(1, "read text header");
73 error(0, "text file not an a.out");
75 if (syminit(fd, &f) < 0)
81 file = smprint("/proc/%s/profile", argv[1]);
82 fd = open(file, OREAD);
89 n = d->length/sizeof(data[0]);
91 error(0, "data file too short");
92 data = malloc(d->length);
95 if(read(fd, data, d->length) < 0)
96 error(1, "text read");
100 data[i] = machdata->swal(data[i]);
102 delta = data[0]-data[1];
103 print("total: %ld\n", data[0]);
107 error(0, "no text symbols");
108 tbase = s.value & ~(mach->pgsize-1); /* align down to page */
109 print("TEXT %.8lux\n", tbase);
111 * Accumulate counts for each function
115 for (i = 0, j = (s.value-tbase)/PCRES+2; j < n; i++) {
116 name = s.name; /* save name */
117 if (!textsym(&s, i)) /* get next symbol */
120 while (j < n && j*PCRES < s.value-tbase)
123 cp = realloc(cp, (k+1)*sizeof(struct COUNTER));
132 error(0, "no counts");
133 cp[k].time = 0; /* "etext" can take no time */
135 * Sort by time and print
137 qsort(cp, k, sizeof(struct COUNTER), compar);
138 Binit(&outbuf, 1, OWRITE);
139 Bprint(&outbuf, " ms %% sym\n");
141 Bprint(&outbuf, "%6ld\t%3lld.%lld\t%s\n",
143 100LL*cp[k].time/delta,
144 (1000LL*cp[k].time/delta)%10,