Update.
[platform/upstream/glibc.git] / sysdeps / mach / hurd / profil.c
1 /* Low-level statistical profiling support function.  Mach/Hurd version.
2    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <hurd.h>
24 #include <mach/mach4.h>
25 #include <mach/pc_sample.h>
26 #include <cthreads.h>
27 #include <assert.h>
28
29 #define MAX_PC_SAMPLES  512     /* XXX ought to be exported in kernel hdr */
30
31 static thread_t profile_thread = MACH_PORT_NULL;
32 static u_short *samples;
33 static size_t maxsamples;
34 static size_t pc_offset;
35 static size_t sample_scale;
36 static sampled_pc_seqno_t seqno;
37 static spin_lock_t lock = SPIN_LOCK_INITIALIZER;
38 static mach_msg_timeout_t collector_timeout; /* ms between collections.  */
39 static int profile_tick;
40
41 /* Reply port used by profiler thread */
42 static mach_port_t profil_reply_port;
43
44 /* Forwards */
45 static kern_return_t profil_task_get_sampled_pcs (mach_port_t,
46                                                   sampled_pc_seqno_t *,
47                                                   sampled_pc_array_t,
48                                                   mach_msg_type_number_t *);
49 static void fetch_samples (void);
50
51 /* Enable statistical profiling, writing samples of the PC into at most
52    SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
53    is enabled, the system examines the user PC and increments
54    SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536].  If SCALE is zero,
55    disable profiling.  Returns zero on success, -1 on error.  */
56
57 static error_t
58 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
59 {
60   error_t err;
61
62   if (profile_thread == MACH_PORT_NULL)
63     {
64       /* Set up the profiling collector thread.  */
65       static void profile_waiter (void);
66       err = __thread_create (__mach_task_self (), &profile_thread);
67       if (! err)
68         err = __mach_setup_thread (__mach_task_self (), profile_thread,
69                                    &profile_waiter, NULL, NULL);
70     }
71   else
72     err = 0;
73
74   if (! err)
75     {
76       err = __task_enable_pc_sampling (__mach_task_self (), &profile_tick,
77                                        SAMPLED_PC_PERIODIC);
78       if (!err && sample_scale == 0)
79         /* Profiling was not turned on, so the collector thread was
80            suspended.  Resume it.  */
81         err = __thread_resume (profile_thread);
82       if (! err)
83         {
84           samples = sample_buffer;
85           maxsamples = size / sizeof *sample_buffer;
86           pc_offset = offset;
87           sample_scale = scale;
88           /* Calculate a good period for the collector thread.  From TICK
89              and the kernel buffer size we get the length of time it takes
90              to fill the buffer; translate that to milliseconds for
91              mach_msg, and chop it in half for general lag factor.  */
92           collector_timeout = MAX_PC_SAMPLES * profile_tick / 1000 / 2;
93         }
94     }
95
96   return err;
97 }
98
99 int
100 __profile_frequency (void)
101 {
102   return profile_tick;
103 }
104
105 int
106 __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
107 {
108   error_t err;
109
110   __spin_lock (&lock);
111
112   if (scale == 0)
113     {
114       /* Disable profiling.  */
115       int count;
116
117       if (profile_thread != MACH_PORT_NULL)
118         __thread_suspend (profile_thread);
119
120       /* Fetch the last set of samples */
121       if (sample_scale)
122         fetch_samples ();
123
124       err = __task_disable_pc_sampling (__mach_task_self (), &count);
125       sample_scale = 0;
126       seqno = 0;
127     }
128   else
129     err = update_waiter (sample_buffer, size, offset, scale);
130
131   __spin_unlock (&lock);
132
133   return err ? __hurd_fail (err) : 0;
134 }
135 weak_alias (__profil, profil)
136
137 /* Fetch PC samples.  This function must be very careful not to depend
138    on Hurd threadvar variables.  We arrange that by using a special
139    stub arranged for at the end of this file. */
140 static void
141 fetch_samples (void)
142 {
143   sampled_pc_t pc_samples[MAX_PC_SAMPLES];
144   mach_msg_type_number_t nsamples, i;
145   error_t err;
146
147   nsamples = MAX_PC_SAMPLES;
148
149   err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
150                                      pc_samples, &nsamples);
151   if (err)
152     {
153       static error_t special_profil_failure;
154       static volatile int a, b, c;
155
156       special_profil_failure = err;
157       a = 1;
158       b = 0;
159       while (1)
160         c = a / b;
161     }
162
163   for (i = 0; i < nsamples; ++i)
164     {
165       /* Do arithmetic in long long to avoid overflow problems. */
166       long long pc_difference = pc_samples[i].pc - pc_offset;
167       size_t idx = ((pc_difference / 2) * sample_scale) / 65536;
168       if (idx < maxsamples)
169         ++samples[idx];
170     }
171 }
172
173
174 /* This function must be very careful not to depend on Hurd threadvar
175    variables.  We arrange that by using special stubs arranged for at the
176    end of this file. */
177 static void
178 profile_waiter (void)
179 {
180   mach_msg_header_t msg;
181   mach_port_t timeout_reply_port;
182
183   profil_reply_port = __mach_reply_port ();
184   timeout_reply_port = __mach_reply_port ();
185
186   while (1)
187     {
188       __spin_lock (&lock);
189
190       fetch_samples ();
191
192       __spin_unlock (&lock);
193
194       __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
195                   timeout_reply_port, collector_timeout, MACH_PORT_NULL);
196     }
197 }
198 \f
199 /* Fork interaction */
200
201 /* Before fork, lock the interlock so that we are in a clean state. */
202 static void
203 fork_profil_prepare (void)
204 {
205   __spin_lock (&lock);
206 }
207 text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
208
209 /* In the parent, unlock the interlock once fork is complete. */
210 static void
211 fork_profil_parent (void)
212 {
213   __spin_unlock (&lock);
214 }
215 text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
216
217 /* In the childs, unlock the interlock, and start a profiling thread up
218    if necessary. */
219 static void
220 fork_profil_child (void)
221 {
222   u_short *sb;
223   size_t n, o, ss;
224   error_t err;
225
226   __spin_unlock (&lock);
227
228   if (profile_thread != MACH_PORT_NULL)
229     {
230       __mach_port_deallocate (__mach_task_self (), profile_thread);
231       profile_thread = MACH_PORT_NULL;
232     }
233
234   sb = samples;
235   samples = NULL;
236   n = maxsamples;
237   maxsamples = 0;
238   o = pc_offset;
239   pc_offset = 0;
240   ss = sample_scale;
241   sample_scale = 0;
242
243   if (ss != 0)
244     {
245       err = update_waiter (sb, n * sizeof *sb, o, ss);
246       assert_perror (err);
247     }
248 }
249 text_set_element (_hurd_fork_child_hook, fork_profil_child);
250
251
252 \f
253
254 /* Special RPC stubs for profile_waiter are made by including the normal
255    source code, with special CPP state to prevent it from doing the
256    usual thing. */
257
258 /* Include these first; then our #define's will take full effect, not
259    being overridden. */
260 #include <mach/mig_support.h>
261
262 /* This need not do anything; it is always associated with errors, which
263    are fatal in profile_waiter anyhow. */
264 #define __mig_put_reply_port(foo)
265
266 /* Use our static variable instead of the usual threadvar mechanism for
267    this. */
268 #define __mig_get_reply_port() profil_reply_port
269
270 /* Make the functions show up as static */
271 #define mig_external static
272
273 /* Turn off the attempt to generate ld aliasing records. */
274 #undef weak_alias
275 #define weak_alias(a,b)
276
277 /* And change their names to avoid confusing disasters. */
278 #define __vm_deallocate_rpc profil_vm_deallocate
279 #define __task_get_sampled_pcs profil_task_get_sampled_pcs
280
281 /* And include the source code */
282 #include <../mach/RPC_task_get_sampled_pcs.c>