comparison 3rdparty/packages/cc/sources/prof.c @ 867:0198655f2552

Added sources
author boisy
date Thu, 16 Jan 2003 19:54:21 +0000
parents
children
comparison
equal deleted inserted replaced
866:157ddcf642b2 867:0198655f2552
1 /* C program profiler
2
3 The -P option of the compiler generates code at the beginning
4 of each function that calls _prof with the function's absolute
5 address and a pointer to the name of the function. This version
6 of _prof just tallies the number of times a function is called.
7 It could possibly be modified to trace the execution of functions.
8 */
9
10 #include <stdio.h>
11
12 #define MAXFUNC 63 /* maximum number of unique functions */
13
14 typedef struct profstr {
15 int (*func)();
16 char *fname;
17 long count;
18 } prof;
19
20 static prof pfs[MAXFUNC];
21 static prof *pfree=pfs;
22 static prof *pmax=&pfs[MAXFUNC];
23
24 _prof(afunc,name)
25 int (*afunc)();
26 char *name;
27 {
28 register prof *p;
29
30 /* linear search for the function addr (not quick, but easy) */
31 for(p=pfs; p<pfree; ++p)
32 if(p->func == afunc) { /* function found, bump count */
33 ++p->count;
34 return;
35 }
36
37 if(pfree > pmax) { /* no more room? */
38 p=pfree-1;
39 p->fname = p->fname ? "????????" : name; /* put it here */
40 ++p->count;
41 }
42 else { /* enter the function in table */
43 p=pfree++;
44 p->fname=name; /* pointer to function */
45 p->func = afunc; /* pointer to function name */
46 p->count = 1;
47 }
48 }
49
50 /* _dumprof() is called by exit() at program end.
51 It may be called at any time to get a count */
52 _dumprof()
53 {
54 register prof *p;
55
56 pflinit();
57 fflush(stdout); /* flush stdout first */
58 for(p=pfs; p<pfree; ++p) /* dump names and counts */
59 fprintf(stderr," %8s() %ld\n",p->fname,p->count);
60 }