hurd: Define EXEC_PAGESIZE
[glibc.git] / sysdeps / mach / hurd / profil.c
blobb3f201b016364c6f08d0f2cfb2d034900e05c51a
1 /* Low-level statistical profiling support function. Mach/Hurd version.
2 Copyright (C) 1995-2018 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <hurd.h>
23 #include <mach/mach4.h>
24 #include <mach/pc_sample.h>
25 #include <cthreads.h>
26 #include <assert.h>
27 #include <libc-internal.h>
30 #define MAX_PC_SAMPLES 512 /* XXX ought to be exported in kernel hdr */
32 static thread_t profile_thread = MACH_PORT_NULL;
33 static u_short *samples;
34 static size_t maxsamples;
35 static size_t pc_offset;
36 static size_t sample_scale;
37 static sampled_pc_seqno_t seqno;
38 static spin_lock_t lock = SPIN_LOCK_INITIALIZER;
39 static mach_msg_timeout_t collector_timeout; /* ms between collections. */
40 static int profile_tick;
42 /* Reply port used by profiler thread */
43 static mach_port_t profil_reply_port = MACH_PORT_NULL;
45 /* Forwards */
46 static kern_return_t profil_task_get_sampled_pcs (mach_port_t,
47 sampled_pc_seqno_t *,
48 sampled_pc_array_t,
49 mach_msg_type_number_t *);
50 static void fetch_samples (void);
51 static void profile_waiter (void);
53 /* Enable statistical profiling, writing samples of the PC into at most
54 SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
55 is enabled, the system examines the user PC and increments
56 SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
57 disable profiling. Returns zero on success, -1 on error. */
59 static error_t
60 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
62 error_t err;
64 if (profile_thread == MACH_PORT_NULL)
66 if (profil_reply_port == MACH_PORT_NULL)
67 profil_reply_port = __mach_reply_port ();
68 /* Set up the profiling collector thread. */
69 err = __thread_create (__mach_task_self (), &profile_thread);
70 if (! err)
71 err = __mach_setup_thread (__mach_task_self (), profile_thread,
72 &profile_waiter, NULL, NULL);
74 else
75 err = 0;
77 if (! err)
79 err = __task_enable_pc_sampling (__mach_task_self (), &profile_tick,
80 SAMPLED_PC_PERIODIC);
81 if (!err && sample_scale == 0)
82 /* Profiling was not turned on, so the collector thread was
83 suspended. Resume it. */
84 err = __thread_resume (profile_thread);
85 if (! err)
87 samples = sample_buffer;
88 maxsamples = size / sizeof *sample_buffer;
89 pc_offset = offset;
90 sample_scale = scale;
91 /* Calculate a good period for the collector thread. From TICK
92 and the kernel buffer size we get the length of time it takes
93 to fill the buffer; translate that to milliseconds for
94 mach_msg, and chop it in half for general lag factor. */
95 collector_timeout = MAX_PC_SAMPLES * profile_tick / 1000 / 2;
99 return err;
103 __profile_frequency (void)
105 return 1000000 / profile_tick;
107 libc_hidden_def (__profile_frequency)
110 __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
112 error_t err;
114 __spin_lock (&lock);
116 if (scale == 0)
118 /* Disable profiling. */
119 int count;
121 if (profile_thread != MACH_PORT_NULL)
122 __thread_suspend (profile_thread);
124 /* Fetch the last set of samples */
125 if (sample_scale)
126 fetch_samples ();
128 err = __task_disable_pc_sampling (__mach_task_self (), &count);
129 sample_scale = 0;
130 seqno = 0;
132 else
133 err = update_waiter (sample_buffer, size, offset, scale);
135 __spin_unlock (&lock);
137 return err ? __hurd_fail (err) : 0;
139 weak_alias (__profil, profil)
141 static volatile error_t special_profil_failure;
143 /* Fetch PC samples. This function must be very careful not to depend
144 on Hurd threadvar variables. We arrange that by using a special
145 stub arranged for at the end of this file. */
146 static void
147 fetch_samples (void)
149 sampled_pc_t pc_samples[MAX_PC_SAMPLES];
150 mach_msg_type_number_t nsamples, i;
151 error_t err;
153 nsamples = MAX_PC_SAMPLES;
155 err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
156 pc_samples, &nsamples);
157 if (err)
159 static volatile int a, b;
161 special_profil_failure = err;
162 a = 1;
163 b = 0;
164 while (1)
165 a = a / b;
168 for (i = 0; i < nsamples; ++i)
170 /* Do arithmetic in long long to avoid overflow problems. */
171 long long pc_difference = pc_samples[i].pc - pc_offset;
172 size_t idx = ((pc_difference / 2) * sample_scale) / 65536;
173 if (idx < maxsamples)
174 ++samples[idx];
179 /* This function must be very careful not to depend on Hurd threadvar
180 variables. We arrange that by using special stubs arranged for at the
181 end of this file. */
182 static void
183 profile_waiter (void)
185 mach_msg_header_t msg;
186 mach_port_t timeout_reply_port;
188 timeout_reply_port = __mach_reply_port ();
190 while (1)
192 __spin_lock (&lock);
194 fetch_samples ();
196 __spin_unlock (&lock);
198 __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
199 timeout_reply_port, collector_timeout, MACH_PORT_NULL);
203 /* Fork interaction */
205 /* Before fork, lock the interlock so that we are in a clean state. */
206 static void
207 fork_profil_prepare (void)
209 __spin_lock (&lock);
211 text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
213 /* In the parent, unlock the interlock once fork is complete. */
214 static void
215 fork_profil_parent (void)
217 __spin_unlock (&lock);
219 text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
221 /* In the child, unlock the interlock, and start a profiling thread up
222 if necessary. */
223 static void
224 fork_profil_child (void)
226 u_short *sb;
227 size_t n, o, ss;
228 error_t err;
230 __spin_unlock (&lock);
232 if (profile_thread != MACH_PORT_NULL)
234 __mach_port_deallocate (__mach_task_self (), profile_thread);
235 profile_thread = MACH_PORT_NULL;
238 sb = samples;
239 samples = NULL;
240 n = maxsamples;
241 maxsamples = 0;
242 o = pc_offset;
243 pc_offset = 0;
244 ss = sample_scale;
245 sample_scale = 0;
247 if (ss != 0)
249 err = update_waiter (sb, n * sizeof *sb, o, ss);
250 assert_perror (err);
253 text_set_element (_hurd_fork_child_hook, fork_profil_child);
258 /* Special RPC stubs for profile_waiter are made by including the normal
259 source code, with special CPP state to prevent it from doing the
260 usual thing. */
262 /* Include these first; then our #define's will take full effect, not
263 being overridden. */
264 #include <mach/mig_support.h>
266 /* This need not do anything; it is always associated with errors, which
267 are fatal in profile_waiter anyhow. */
268 #define __mig_put_reply_port(foo)
270 /* Use our static variable instead of the usual threadvar mechanism for
271 this. */
272 #define __mig_get_reply_port() profil_reply_port
274 /* Make the functions show up as static */
275 #define mig_external static
277 /* Turn off the attempt to generate ld aliasing records. */
278 #undef weak_alias
279 #define weak_alias(a,b)
281 /* And change their names to avoid confusing disasters. */
282 #define __vm_deallocate_rpc profil_vm_deallocate
283 #define __task_get_sampled_pcs profil_task_get_sampled_pcs
285 /* And include the source code */
286 #include <../mach/RPC_task_get_sampled_pcs.c>