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