report.c
7.26 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*
*
* report - print profile report
*
*/
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __sgi
#include <a.out.h>
typedef SYMR SYMENT;
#include <ldfcn.h>
#else
#include "libsym.h"
#endif
#include "gperf.h"
#ifdef __sgi
extern LDFILE *Ldptr;
#endif
/*
* S t r u c t u r e s
*/
typedef struct {
char *name;
symaddr_t addr;
unsigned cnt;
} syment_t;
/*
* G l o b a l s
*/
static int Dump = 1; /* if set then dump symbols used */
static int symcnt; /* number of symbols */
static syment_t *symEntTbl = NULL; /* list of addresses */
/*
* F o w a r d R e f e r e n c e s
*/
static syment_t *rdsymtab(char *namelist);
static syment_t * bsearch_sym( symaddr_t addr, syment_t *symtab, int ntab);
static int compar_addr();
static int compar_cnt();
static float sum = 0.0;
/*
* F u n c t i o n s
*/
void
report( int numSegs, gPerfHisto *PerfHists, int tp, int overflow )
{
register syment_t *symEnt;
int i, p;
float freq;
char fmt_string[50];
/*
* If you haven't already, read symbol table
*/
if(symEntTbl == NULL)
symEntTbl = rdsymtab(Namelist);
/*
* sort the symbol entries by address
*/
qsort(symEntTbl, symcnt, sizeof (syment_t), compar_addr);
/*
* Clear all the counts
*/
for(i = 0; i < symcnt; i++)
symEntTbl[i].cnt = 0;
/*
* Sum counts for each function
*/
sum = overflow;
/*
* Get counts per symbol
*/
for (p = 0; p < numSegs; p++) /* for each profiled section */
{
for (i = 0; i < PerfHists[p].numEntrs; i++) /* for each possible entry */
{
if (PerfHists[p].histo[i]) /* if you got hits */
{
/* update the number of hits in the symEntTbl */
(bsearch_sym(i*4+PerfHists[p].start, symEntTbl, symcnt))->cnt +=
PerfHists[p].histo[i];
/* update the total number of hits */
sum += (float)PerfHists[p].histo[i];
}
}
}
/*
* Sort by count
*/
qsort(symEntTbl, symcnt, sizeof (syment_t), compar_cnt);
/*
* Print Report
*/
printf("-------------------------------------------------------------\n");
printf("Profile Report for %s\n", Namelist);
printf("Number of text symbols: %d\n", symcnt);
printf("Sample period: %d microseconds\n", tp);
printf("Number of profiled segments: %d\n", numSegs);
for (i = 0; i < numSegs; i++) {
printf(" Segment %d, start = %08x, end = %08x, size = %08x\n",
i, PerfHists[i].start, (int)PerfHists[i].start + 2*PerfHists[i].numEntrs - 4, PerfHists[i].numEntrs);
}
printf("-------------------------------------------------------------\n");
printf("\n");
for (symEnt = symEntTbl; symEnt < (symEntTbl+symcnt); symEnt++)
{
if (PrintCounts)
freq = (float)symEnt->cnt;
else
freq = (float)symEnt->cnt / sum * 100.0;
if (freq >= Threshold)
{
sprintf(fmt_string,"...........................................");
for (i = 0; i < 40; i++) {
if (!symEnt->name || symEnt->name[i] == '\0')
break;
fmt_string[i] = symEnt->name[i];
}
printf("%08x %s %8.4f %% ", symEnt->addr, fmt_string,
(float)symEnt->cnt / sum * 100.0);
if (PrintCounts)
printf("%5d ", symEnt->cnt);
if (PrintRealTime)
printf("%10.6f sec", (float)symEnt->cnt * (float)tp / 1000000.0 );
printf("\n");
}
}
printf("Overflow ");
sprintf(fmt_string,"...........................................");
printf("%s %8.4f %% ", fmt_string, (float)overflow/sum *100.0);
if (PrintCounts)
printf("%5d ", overflow);
if (PrintRealTime)
printf("%10.6f sec", (float)overflow * (float)tp / 1000000.0 );
printf("\n");
if (ServerMode)
return;
else
exit(0);
}
/*
* Compare function used by bsort
* sort by count
*/
static int
compar_cnt(x, y)
syment_t *x, *y;
{
if (x->cnt > y->cnt)
return(1);
else if (x->cnt == y->cnt)
return(0);
return(-1);
}
/*
* Compare function used by bsort
*/
static int
compar_addr(x, y)
syment_t *x, *y;
{
if (x->addr > y->addr)
return(1);
else if (x->addr == y->addr)
return(0);
return(-1);
}
#ifndef __sgi
static int maxsym;
static int
scanfunc(const Elf32_Sym* sym, const char* name, void* ctx) {
syment_t** base = ctx;
maxsym++;
if (strcmp(name, "gcc2_compiled.") == 0 ||
strcmp(name, "__gnu_compiled_c") == 0) return 0;
if(ELF32_ST_TYPE(sym->st_info) == STT_FUNC ||
(ELF32_ST_BIND(sym->st_info) == STB_LOCAL &&
ELF32_ST_TYPE(sym->st_info) == STT_NOTYPE)) {
syment_t *ip;
*base = realloc(*base, (symcnt+1)*sizeof *ip);
if (!*base) {
error("cannot allocate space for namelist");
exit(1);
}
ip = *base+symcnt;
ip->addr = sym->st_value;
ip->name = strdup(name);
ip->cnt = 0;
symcnt++;
}
return 0;
}
#endif
/*
* Read symbol table and addresses of each symbol
*/
static syment_t *
rdsymtab( char *namelist)
{
#ifdef __sgi
register int i;
register int maxsym;
register syment_t *ip;
SYMR ts;
syment_t *addrbase;
/* external symbols start at index isymMax and are iextMax long */
maxsym = SYMHEADER(Ldptr).isymMax + SYMHEADER(Ldptr).iextMax;
symcnt = 0;
if((addrbase = (syment_t *) malloc (maxsym * sizeof(syment_t *) + 1))
== NULL) {
error("cannot allocate space for namelist");
exit(1);
}
ip = addrbase;
for(i = 0; i < maxsym; i++) {
if(ldtbread(Ldptr, i, &ts) != SUCCESS)
error("cannot read symbol from namelist");
if (i < SYMHEADER(Ldptr).isymMax && ts.st != stStatic &&
ts.st != stStaticProc)
/* statics allowed, throw away other locals */
continue;
if(ts.sc == scText && (ts.st == stProc || ts.st == stStaticProc)) {
ip->addr = ts.value;
ip->name = strdup(ldgetname(Ldptr, &ts));
ip->cnt = 0;
ip++;
symcnt++;
}
}
if (Dump) {
printf("%d text symbols out of %d total read from %s\n",
symcnt, maxsym, namelist);
}
if(ldclose(Ldptr) != SUCCESS) {
error("cannot ldclose namelist file");
exit(1);
}
#else
syment_t* addrbase = 0;
symcnt = 0;
sym_scan(namelist, scanfunc, &addrbase);
if (Dump) {
printf("%d text symbols out of %d total read from %s\n",
symcnt, maxsym, namelist);
}
#endif
return(addrbase);
}
/*
* return symbol entry for given address, finds address range
* that symbol lies within and return address of that function.
*/
static syment_t *
bsearch_sym( symaddr_t addr, syment_t *symtab, int ntab)
{
int hi, lo, mid;
lo = 0;
hi = ntab - 1;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if (addr == symtab[mid].addr)
return(&symtab[mid]);
if (addr < symtab[mid].addr)
hi = mid - 1;
else
lo = mid + 1;
}
if(symtab[mid].addr > addr)
return(&symtab[mid-1]);
else
return(&symtab[mid]);
}