Update.
[glibc.git] / sysdeps / mach / hurd / profil.c
blobd76cc25a3c74e47929da0984b74cb2593c246830
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.
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.
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.
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. */
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>
29 #define MAX_PC_SAMPLES 512 /* XXX ought to be exported in kernel hdr */
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;
41 /* Reply port used by profiler thread */
42 static mach_port_t profil_reply_port;
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);
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. */
57 static error_t
58 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
60 error_t err;
62 if (profile_thread == MACH_PORT_NULL)
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);
71 else
72 err = 0;
74 if (! err)
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)
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;
96 return err;
99 int
100 __profile_frequency (void)
102 return profile_tick;
106 profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
108 error_t err;
110 __spin_lock (&lock);
112 if (scale == 0)
114 /* Disable profiling. */
115 int count;
117 if (profile_thread != MACH_PORT_NULL)
118 __thread_suspend (profile_thread);
120 /* Fetch the last set of samples */
121 if (sample_scale)
122 fetch_samples ();
124 err = __task_disable_pc_sampling (__mach_task_self (), &count);
125 sample_scale = 0;
126 seqno = 0;
128 else
129 err = update_waiter (sample_buffer, size, offset, scale);
131 __spin_unlock (&lock);
133 return err ? __hurd_fail (err) : 0;
136 /* Fetch PC samples. This function must be very careful not to depend
137 on Hurd threadvar variables. We arrange that by using a special
138 stub arranged for at the end of this file. */
139 static void
140 fetch_samples (void)
142 sampled_pc_t pc_samples[MAX_PC_SAMPLES];
143 mach_msg_type_number_t nsamples, i;
144 error_t err;
146 nsamples = MAX_PC_SAMPLES;
148 err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
149 pc_samples, &nsamples);
150 if (err)
152 static error_t special_profil_failure;
153 static volatile int a, b, c;
155 special_profil_failure = err;
156 a = 1;
157 b = 0;
158 while (1)
159 c = a / b;
162 for (i = 0; i < nsamples; ++i)
164 /* Do arithmetic in long long to avoid overflow problems. */
165 long long pc_difference = pc_samples[i].pc - pc_offset;
166 size_t idx = ((pc_difference / 2) * sample_scale) / 65536;
167 if (idx < maxsamples)
168 ++samples[idx];
173 /* This function must be very careful not to depend on Hurd threadvar
174 variables. We arrange that by using special stubs arranged for at the
175 end of this file. */
176 static void
177 profile_waiter (void)
179 mach_msg_header_t msg;
180 mach_port_t timeout_reply_port;
182 profil_reply_port = __mach_reply_port ();
183 timeout_reply_port = __mach_reply_port ();
185 while (1)
187 __spin_lock (&lock);
189 fetch_samples ();
191 __spin_unlock (&lock);
193 __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
194 timeout_reply_port, collector_timeout, MACH_PORT_NULL);
198 /* Fork interaction */
200 /* Before fork, lock the interlock so that we are in a clean state. */
201 static void
202 fork_profil_prepare (void)
204 __spin_lock (&lock);
206 text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
208 /* In the parent, unlock the interlock once fork is complete. */
209 static void
210 fork_profil_parent (void)
212 __spin_unlock (&lock);
214 text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
216 /* In the childs, unlock the interlock, and start a profiling thread up
217 if necessary. */
218 static void
219 fork_profil_child (void)
221 u_short *sb;
222 size_t n, o, ss;
223 error_t err;
225 __spin_unlock (&lock);
227 if (profile_thread != MACH_PORT_NULL)
229 __mach_port_deallocate (__mach_task_self (), profile_thread);
230 profile_thread = MACH_PORT_NULL;
233 sb = samples;
234 samples = NULL;
235 n = maxsamples;
236 maxsamples = 0;
237 o = pc_offset;
238 pc_offset = 0;
239 ss = sample_scale;
240 sample_scale = 0;
242 if (ss != 0)
244 err = update_waiter (sb, n * sizeof *sb, o, ss);
245 assert_perror (err);
248 text_set_element (_hurd_fork_child_hook, fork_profil_child);
253 /* Special RPC stubs for profile_waiter are made by including the normal
254 source code, with special CPP state to prevent it from doing the
255 usual thing. */
257 /* Include these first; then our #define's will take full effect, not
258 being overridden. */
259 #include <mach/mig_support.h>
261 /* This need not do anything; it is always associated with errors, which
262 are fatal in profile_waiter anyhow. */
263 #define __mig_put_reply_port(foo)
265 /* Use our static variable instead of the usual threadvar mechanism for
266 this. */
267 #define __mig_get_reply_port() profil_reply_port
269 /* Make the functions show up as static */
270 #define mig_external static
272 /* Turn off the attempt to generate ld aliasing records. */
273 #undef weak_alias
274 #define weak_alias(a,b)
276 /* And change their names to avoid confusing disasters. */
277 #define __vm_deallocate_rpc profil_vm_deallocate
278 #define __task_get_sampled_pcs profil_task_get_sampled_pcs
280 /* And include the source code */
281 #include <../mach/RPC_task_get_sampled_pcs.c>