1: /*
2: * Copyright (c) 1987 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #if defined(LIBC_SCCS) && !defined(lint)
8: static char sccsid[] = "@(#)mon.c 5.5 (GTE) 3/23/92";
9: #endif LIBC_SCCS and not lint
10:
11: #define ARCDENSITY 1 /* density of routines per 100 bytes */
12: #define MINARCS 50 /* minimum number of counters */
13: #define HISTFRACTION 8 /* fraction of text space for histograms */
14:
15:
16: struct phdr { /* mon.out header */
17: int *lpc; /* low pc of histogramed text space */
18: int *hpc; /* high pc */
19: int ncnt; /* number of functions counted */
20: };
21:
22: struct cnt { /* function entry count structure */
23: int (*pc)(); /* address of profiled function */
24: long ncall; /* number of time function called */
25: };
26:
27: static struct cnt *countbase; /* next free cnt struct */
28: static struct cnt *countend; /* first address past cnt structs */
29:
30: static short *s_sbuf; /* start of histogram buffer */
31: static unsigned s_bufsize; /* size of histogram buffer (in chars) */
32: static char *s_lowpc; /* low pc for histgram recording */
33: static unsigned s_scale; /* histogram scale */
34:
35: #define PERROR(s) write(2, s, sizeof(s)-1)
36:
37: monstartup(lowpc, highpc)
38: char *lowpc;
39: char *highpc;
40: {
41: unsigned int cntsize, monsize;
42: char *buffer;
43: extern char *sbrk();
44: extern char *minbrk;
45:
46: cntsize = (unsigned)(highpc - lowpc) * ARCDENSITY / 100;
47: if (cntsize < MINARCS)
48: cntsize = MINARCS;
49: monsize = (unsigned)(highpc - lowpc + HISTFRACTION - 1) / HISTFRACTION
50: + sizeof(struct phdr) + cntsize * sizeof(struct cnt);
51: monsize = (monsize + 1) & ~1;
52: buffer = sbrk(monsize);
53: if (buffer == (char *)-1) {
54: PERROR("monstartup: no space for monitor buffer(s)\n");
55: return;
56: }
57: minbrk = sbrk(0);
58: monitor(lowpc, highpc, buffer, monsize>>1, cntsize);
59: }
60:
61: monitor(lowpc, highpc, buf, bufsize, cntsize)
62: char *lowpc, *highpc;
63: char *buf; /* really (short *) but easier this way */
64: unsigned bufsize, cntsize;
65: {
66: register unsigned o;
67: register struct phdr *php;
68: static char *sbuf; /* saved base of profiling buffer */
69: static unsigned ssize; /* saved buffer size */
70:
71: if (lowpc == 0) {
72: moncontrol(0);
73: o = creat("mon.out", 0666);
74: write(o, sbuf, ssize);
75: close(o);
76: return;
77: }
78: bufsize *= sizeof(short);
79: if (bufsize < sizeof(struct phdr)+sizeof(struct cnt)+sizeof(short)) {
80: PERROR("monitor: buffer too small");
81: return;
82: }
83: sbuf = buf;
84: ssize = bufsize;
85:
86: countbase = (struct cnt *)(buf + sizeof(struct phdr));
87: o = sizeof(struct phdr) + cntsize * sizeof(struct cnt);
88: if (o > bufsize) {
89: cntsize = (bufsize - sizeof(struct phdr))/sizeof(struct cnt);
90: o = sizeof(struct phdr) + cntsize * sizeof(struct cnt);
91: }
92: countend = (struct cnt *)(buf + o);
93:
94: php = (struct phdr *)buf;
95: php->lpc = (int *)lowpc;
96: php->hpc = (int *)highpc;
97: php->ncnt = cntsize;
98:
99: s_sbuf = (short *)countend;
100: s_bufsize = bufsize - o;
101: s_lowpc = lowpc;
102: o = highpc - lowpc;
103: if(s_bufsize < o)
104: o = ((long)s_bufsize << 16) / o;
105: else
106: o = 0xffff;
107: s_scale = o;
108: moncontrol(1);
109: }
110:
111: /*
112: * Control profiling
113: */
114: moncontrol(mode)
115: int mode;
116: {
117: if (mode) {
118: /* start */
119: profil(s_sbuf, s_bufsize, s_lowpc, s_scale);
120: } else {
121: /* stop */
122: profil((char *)0, 0, 0, 0);
123: }
124: }
Defined functions
Defined variables
sccsid
defined in line
8;
never used
Defined struct's
cnt
defined in line
22; used 18 times
phdr
defined in line
16; used 16 times
Defined macros