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>
24 #include <mach/mach4.h>
25 #include <mach/pc_sample.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;
45 static kern_return_t profil_task_get_sampled_pcs (mach_port_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. */
58 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
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);
68 err = __mach_setup_thread (__mach_task_self (), profile_thread,
69 &profile_waiter, NULL, NULL);
76 err = __task_enable_pc_sampling (__mach_task_self (), &profile_tick,
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);
84 samples = sample_buffer;
85 maxsamples = size / sizeof *sample_buffer;
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;
100 __profile_frequency (void)
106 __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
114 /* Disable profiling. */
117 if (profile_thread != MACH_PORT_NULL)
118 __thread_suspend (profile_thread);
120 /* Fetch the last set of samples */
124 err = __task_disable_pc_sampling (__mach_task_self (), &count);
129 err = update_waiter (sample_buffer, size, offset, scale);
131 __spin_unlock (&lock);
133 return err ? __hurd_fail (err) : 0;
135 weak_alias (__profil, profil)
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. */
143 sampled_pc_t pc_samples[MAX_PC_SAMPLES];
144 mach_msg_type_number_t nsamples, i;
147 nsamples = MAX_PC_SAMPLES;
149 err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
150 pc_samples, &nsamples);
153 static error_t special_profil_failure;
154 static volatile int a, b, c;
156 special_profil_failure = err;
163 for (i = 0; i < nsamples; ++i)
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)
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
178 profile_waiter (void)
180 mach_msg_header_t msg;
181 mach_port_t timeout_reply_port;
183 profil_reply_port = __mach_reply_port ();
184 timeout_reply_port = __mach_reply_port ();
192 __spin_unlock (&lock);
194 __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
195 timeout_reply_port, collector_timeout, MACH_PORT_NULL);
199 /* Fork interaction */
201 /* Before fork, lock the interlock so that we are in a clean state. */
203 fork_profil_prepare (void)
207 text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
209 /* In the parent, unlock the interlock once fork is complete. */
211 fork_profil_parent (void)
213 __spin_unlock (&lock);
215 text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
217 /* In the childs, unlock the interlock, and start a profiling thread up
220 fork_profil_child (void)
226 __spin_unlock (&lock);
228 if (profile_thread != MACH_PORT_NULL)
230 __mach_port_deallocate (__mach_task_self (), profile_thread);
231 profile_thread = MACH_PORT_NULL;
245 err = update_waiter (sb, n * sizeof *sb, o, ss);
249 text_set_element (_hurd_fork_child_hook, fork_profil_child);
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
258 /* Include these first; then our #define's will take full effect, not
260 #include <mach/mig_support.h>
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)
266 /* Use our static variable instead of the usual threadvar mechanism for
268 #define __mig_get_reply_port() profil_reply_port
270 /* Make the functions show up as static */
271 #define mig_external static
273 /* Turn off the attempt to generate ld aliasing records. */
275 #define weak_alias(a,b)
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
281 /* And include the source code */
282 #include <../mach/RPC_task_get_sampled_pcs.c>