OSDN Git Service

* select.cc (select_stuff::wait): Temporarily disallow APCS.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / gmon.c
1 /*-
2  * Copyright (c) 1983, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #if !defined(lint) && defined(LIBC_SCCS)
35 static char rcsid[] = "$OpenBSD: gmon.c,v 1.8 1997/07/23 21:11:27 kstailey Exp $";
36 #endif
37
38 #include "winlean.h"
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <gmon.h>
43 #include <stdlib.h>
44
45 #include <profil.h>
46
47 /* XXX needed? */
48 //extern char *minbrk __asm ("minbrk");
49
50 struct gmonparam _gmonparam = { GMON_PROF_OFF };
51
52 static int      s_scale;
53 /* see profil(2) where this is describe (incorrectly) */
54 #define         SCALE_1_TO_1    0x10000L
55
56 #define ERR(s) write(2, s, sizeof(s))
57
58 void    moncontrol __P((int));
59
60 static void *
61 fake_sbrk(int size)
62 {
63     void *rv = malloc(size);
64     if (rv)
65       return rv;
66     else
67       return (void *) -1;
68 }
69
70 void
71 monstartup(lowpc, highpc)
72         u_long lowpc;
73         u_long highpc;
74 {
75         register int o;
76         char *cp;
77         struct gmonparam *p = &_gmonparam;
78
79         /*
80          * round lowpc and highpc to multiples of the density we're using
81          * so the rest of the scaling (here and in gprof) stays in ints.
82          */
83         p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
84         p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
85         p->textsize = p->highpc - p->lowpc;
86         p->kcountsize = p->textsize / HISTFRACTION;
87         p->hashfraction = HASHFRACTION;
88         p->fromssize = p->textsize / p->hashfraction;
89         p->tolimit = p->textsize * ARCDENSITY / 100;
90         if (p->tolimit < MINARCS)
91                 p->tolimit = MINARCS;
92         else if (p->tolimit > MAXARCS)
93                 p->tolimit = MAXARCS;
94         p->tossize = p->tolimit * sizeof(struct tostruct);
95
96         cp = fake_sbrk(p->kcountsize + p->fromssize + p->tossize);
97         if (cp == (char *)-1) {
98                 ERR("monstartup: out of memory\n");
99                 return;
100         }
101 #ifdef notdef
102         bzero(cp, p->kcountsize + p->fromssize + p->tossize);
103 #endif
104         p->tos = (struct tostruct *)cp;
105         cp += p->tossize;
106         p->kcount = (u_short *)cp;
107         cp += p->kcountsize;
108         p->froms = (u_short *)cp;
109
110         /* XXX minbrk needed? */
111         //minbrk = fake_sbrk(0);
112         p->tos[0].link = 0;
113
114         o = p->highpc - p->lowpc;
115         if (p->kcountsize < o) {
116 #ifndef notdef
117                 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
118 #else /* avoid floating point */
119                 int quot = o / p->kcountsize;
120
121                 if (quot >= 0x10000)
122                         s_scale = 1;
123                 else if (quot >= 0x100)
124                         s_scale = 0x10000 / quot;
125                 else if (o >= 0x800000)
126                         s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
127                 else
128                         s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
129 #endif
130         } else
131                 s_scale = SCALE_1_TO_1;
132
133         moncontrol(1);
134 }
135
136 void
137 _mcleanup()
138 {
139         int fd;
140         int hz;
141         int fromindex;
142         int endfrom;
143         u_long frompc;
144         int toindex;
145         struct rawarc rawarc;
146         struct gmonparam *p = &_gmonparam;
147         struct gmonhdr gmonhdr, *hdr;
148         char *proffile;
149 #ifdef DEBUG
150         int log, len;
151         char dbuf[200];
152 #endif
153
154         if (p->state == GMON_PROF_ERROR)
155                 ERR("_mcleanup: tos overflow\n");
156
157         hz = PROF_HZ;
158         moncontrol(0);
159
160 #ifdef nope
161         if ((profdir = getenv("PROFDIR")) != NULL) {
162                 extern char *__progname;
163                 char *s, *t, *limit;
164                 pid_t pid;
165                 long divisor;
166
167                 /* If PROFDIR contains a null value, no profiling
168                    output is produced */
169                 if (*profdir == '\0') {
170                         return;
171                 }
172
173                 limit = buf + sizeof buf - 1 - 10 - 1 -
174                     strlen(__progname) - 1;
175                 t = buf;
176                 s = profdir;
177                 while((*t = *s) != '\0' && t < limit) {
178                         t++;
179                         s++;
180                 }
181                 *t++ = '/';
182
183                 /*
184                  * Copy and convert pid from a pid_t to a string.  For
185                  * best performance, divisor should be initialized to
186                  * the largest power of 10 less than PID_MAX.
187                  */
188                 pid = getpid();
189                 divisor=10000;
190                 while (divisor > pid) divisor /= 10;    /* skip leading zeros */
191                 do {
192                         *t++ = (pid/divisor) + '0';
193                         pid %= divisor;
194                 } while (divisor /= 10);
195                 *t++ = '.';
196
197                 s = __progname;
198                 while ((*t++ = *s++) != '\0')
199                         ;
200
201                 proffile = buf;
202         } else {
203                 proffile = "gmon.out";
204         }
205 #else
206         {
207           char gmon_out[] = "gmon.out";
208           proffile = gmon_out;
209         }
210 #endif
211
212         fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);
213         if (fd < 0) {
214                 perror( proffile );
215                 return;
216         }
217 #ifdef DEBUG
218         log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
219         if (log < 0) {
220                 perror("mcount: gmon.log");
221                 return;
222         }
223         len = sprintf(dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
224             p->kcount, p->kcountsize);
225         write(log, dbuf, len);
226 #endif
227         hdr = (struct gmonhdr *)&gmonhdr;
228         hdr->lpc = p->lowpc;
229         hdr->hpc = p->highpc;
230         hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
231         hdr->version = GMONVERSION;
232         hdr->profrate = hz;
233         write(fd, (char *)hdr, sizeof *hdr);
234         write(fd, p->kcount, p->kcountsize);
235         endfrom = p->fromssize / sizeof(*p->froms);
236         for (fromindex = 0; fromindex < endfrom; fromindex++) {
237                 if (p->froms[fromindex] == 0)
238                         continue;
239
240                 frompc = p->lowpc;
241                 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
242                 for (toindex = p->froms[fromindex]; toindex != 0;
243                      toindex = p->tos[toindex].link) {
244 #ifdef DEBUG
245                         len = sprintf(dbuf,
246                         "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
247                                 frompc, p->tos[toindex].selfpc,
248                                 p->tos[toindex].count);
249                         write(log, dbuf, len);
250 #endif
251                         rawarc.raw_frompc = frompc;
252                         rawarc.raw_selfpc = p->tos[toindex].selfpc;
253                         rawarc.raw_count = p->tos[toindex].count;
254                         write(fd, &rawarc, sizeof rawarc);
255                 }
256         }
257         close(fd);
258 }
259
260 /*
261  * Control profiling
262  *      profiling is what mcount checks to see if
263  *      all the data structures are ready.
264  */
265 void
266 moncontrol(mode)
267         int mode;
268 {
269         struct gmonparam *p = &_gmonparam;
270
271         if (mode) {
272                 /* start */
273                 profil((char *)p->kcount, p->kcountsize, p->lowpc,
274                     s_scale);
275                 p->state = GMON_PROF_ON;
276         } else {
277                 /* stop */
278                 profil((char *)0, 0, 0, 0);
279                 p->state = GMON_PROF_OFF;
280         }
281 }
282
283