2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 #include <mach_prof.h>
58 #include <mach/task_server.h>
59 #include <mach/thread_act_server.h>
63 #include <kern/thread.h>
64 #include <kern/thread_swap.h>
65 #include <kern/queue.h>
66 #include <kern/profile.h>
67 #include <kern/sched_prim.h>
69 #include <kern/misc_protos.h>
70 #include <ipc/ipc_space.h>
71 #include <machine/machparam.h>
72 #include <mach/prof.h>
74 thread_t profile_thread_id
= THREAD_NULL
;
75 int profile_sample_count
= 0; /* Provided for looking at from kdb. */
76 extern kern_return_t
task_suspend(task_t task
); /* ack */
79 prof_data_t
pbuf_alloc(void);
82 void profile_thread(void);
83 void send_last_sample_buf(
87 *****************************************************************************
88 * profile_thread is the profile/trace kernel support thread. It is started
89 * by a server/user request through task_sample, or thread_sample. The profile
90 * thread dequeues messages and sends them to the receive_prof thread, in the
91 * server, via the send_samples and send_notices mig interface functions. If
92 * there are no messages in the queue profile thread blocks until wakened by
93 * profile (called in from mach_clock), or last_sample (called by thread/task_
102 queue_entry_t prof_queue_entry
;
107 thread_swappable(current_act(), FALSE
);
109 /* Initialise the queue header for the prof_queue */
110 mpqueue_init(&prof_queue
);
114 /* Dequeue the first buffer. */
116 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
119 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
) {
120 assert_wait((event_t
) profile_thread
, THREAD_UNINT
);
121 thread_block(THREAD_CONTINUE_NULL
);
122 if (current_thread()->wait_result
!= THREAD_AWAKENED
)
128 pbuf
= buf_entry
->p_prof
;
129 kr
= send_samples(pbuf
->prof_port
, (void *)buf_entry
->p_zone
,
130 (mach_msg_type_number_t
)buf_entry
->p_index
);
131 profile_sample_count
+= buf_entry
->p_index
;
132 if (kr
!= KERN_SUCCESS
)
133 printf("send_samples(%x, %x, %d) error %x\n",
134 pbuf
->prof_port
, buf_entry
->p_zone
, buf_entry
->p_index
, kr
);
135 dropped
= buf_entry
->p_dropped
;
137 printf("kernel: profile dropped %d sample%s\n", dropped
,
138 dropped
== 1 ? "" : "s");
139 buf_entry
->p_dropped
= 0;
142 /* Indicate you've finished the dirty job */
143 buf_entry
->p_full
= FALSE
;
144 if (buf_entry
->p_wakeme
)
145 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
149 /* The profile thread has been signalled to exit. Any threads waiting
150 for the last buffer of samples to be acknowledged should be woken
152 profile_thread_id
= THREAD_NULL
;
155 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
157 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
)
159 if (buf_entry
->p_wakeme
)
160 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
165 panic("profile_thread(): halt_self");
170 *****************************************************************************
171 * send_last_sample is the drain mechanism to allow partial profiled buffers
172 * to be sent to the receive_prof thread in the server.
173 *****************************************************************************
177 send_last_sample_buf(prof_data_t pbuf
)
182 if (pbuf
== NULLPROFDATA
)
185 /* Ask for the sending of the last PC buffer.
186 * Make a request to the profile_thread by inserting
187 * the buffer in the send queue, and wake it up.
188 * The last buffer must be inserted at the head of the
189 * send queue, so the profile_thread handles it immediatly.
191 buf_entry
= pbuf
->prof_area
+ pbuf
->prof_index
;
192 buf_entry
->p_prof
= pbuf
;
195 Watch out in case profile thread exits while we are about to
199 if (profile_thread_id
== THREAD_NULL
)
202 buf_entry
->p_wakeme
= 1;
203 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
204 thread_wakeup((event_t
) profile_thread
);
205 assert_wait((event_t
) &buf_entry
->p_wakeme
, THREAD_ABORTSAFE
);
207 thread_block(THREAD_CONTINUE_NULL
);
213 *****************************************************************************
214 * add clock tick parameters to profile/trace buffers. Called from the mach_
215 * clock heritz_tick function. DCI version stores thread, sp, and pc values
216 * into the profile/trace buffers. MACH_PROF version just stores pc values.
217 *****************************************************************************
221 profile(natural_t pc
,
224 natural_t inout_val
= pc
;
227 if (pbuf
== NULLPROFDATA
)
230 /* Inserts the PC value in the buffer of the thread */
231 set_pbuf_value(pbuf
, &inout_val
);
232 switch((int)inout_val
) {
234 if (profile_thread_id
== THREAD_NULL
) {
235 reset_pbuf_area(pbuf
);
239 /* Normal case, value successfully inserted */
243 * The value we have just inserted caused the
244 * buffer to be full, and ready to be sent.
245 * If profile_thread_id is null, the profile
246 * thread has been killed. Since this generally
247 * happens only when the O/S server task of which
248 * it is a part is killed, it is not a great loss
249 * to throw away the data.
251 if (profile_thread_id
== THREAD_NULL
) {
252 reset_pbuf_area(pbuf
);
256 buf_entry
= (buffer_t
) &pbuf
->prof_area
[pbuf
->prof_index
];
257 buf_entry
->p_prof
= pbuf
;
258 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
260 /* Switch to another buffer */
261 reset_pbuf_area(pbuf
);
263 /* Wake up the profile thread */
264 if (profile_thread_id
!= THREAD_NULL
)
265 thread_wakeup((event_t
) profile_thread
);
269 printf("profile : unexpected case\n");
274 *****************************************************************************
275 * pbuf_alloc creates a profile/trace buffer and assoc. zones for storing
277 *****************************************************************************
283 register prof_data_t pbuf
;
285 register natural_t
*zone
;
287 pbuf
= (prof_data_t
)kalloc(sizeof(struct prof_data
));
289 return(NULLPROFDATA
);
290 pbuf
->prof_port
= MACH_PORT_NULL
;
291 for (i
=0; i
< NB_PROF_BUFFER
; i
++) {
292 zone
= (natural_t
*)kalloc(SIZE_PROF_BUFFER
*sizeof(natural_t
));
296 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
297 SIZE_PROF_BUFFER
*sizeof(natural_t
));
298 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
299 return(NULLPROFDATA
);
301 pbuf
->prof_area
[i
].p_zone
= zone
;
302 pbuf
->prof_area
[i
].p_full
= FALSE
;
304 pbuf
->prof_port
= MACH_PORT_NULL
;
309 *****************************************************************************
310 * pbuf_free free memory allocated for storing profile/trace items. Called
311 * when a task is no longer profiled/traced. Pbuf_free tears down the memory
312 * alloced in pbuf_alloc. It does not check to see if the structures are valid
313 * since it is only called by functions in this file.
314 *****************************************************************************
323 ipc_port_release_send(pbuf
->prof_port
);
325 for(i
=0; i
< NB_PROF_BUFFER
; i
++)
326 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
327 SIZE_PROF_BUFFER
*sizeof(natural_t
));
328 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
331 #endif /* MACH_PROF */
334 *****************************************************************************
335 * Thread_sample is used by MACH_PROF to profile a single thread, and is only
337 *****************************************************************************
342 thread_act_t thr_act
,
346 * This routine is called every time that a new thread has made
347 * a request for the sampling service. We must keep track of the
348 * correspondance between its identity (thread) and the port
349 * we are going to use as a reply port to send out the samples resulting
350 * from its execution.
358 if (reply
!= MACH_PORT_NULL
) {
359 if (thr_act
->act_profiled
) /* yuck! */
360 return KERN_INVALID_ARGUMENT
;
361 /* Start profiling this activation, do the initialization. */
363 if ((thr_act
->profil_buffer
= pbuf
) == NULLPROFDATA
) {
364 printf("thread_sample: cannot allocate pbuf\n");
365 return KERN_RESOURCE_SHORTAGE
;
368 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
369 printf("mach_sample_thread: cannot set pbuf_nb\n");
372 reset_pbuf_area(pbuf
);
374 pbuf
->prof_port
= reply
;
375 thr_act
->act_profiled
= TRUE
;
376 thr_act
->act_profiled_own
= TRUE
;
377 if (profile_thread_id
== THREAD_NULL
)
378 profile_thread_id
= kernel_thread(kernel_task
, profile_thread
);
380 if (!thr_act
->act_profiled
)
381 return(KERN_INVALID_ARGUMENT
);
383 thr_act
->act_profiled
= FALSE
;
384 /* do not stop sampling if thread is not profiled by its own */
386 if (!thr_act
->act_profiled_own
)
389 thr_act
->act_profiled_own
= FALSE
;
391 send_last_sample_buf(thr_act
->profil_buffer
);
392 pbuf_free(thr_act
->profil_buffer
);
393 thr_act
->profil_buffer
= NULLPROFDATA
;
396 #endif /* MACH_PROF */
400 *****************************************************************************
401 * Task_sample is used to profile/trace tasks - all thread within a task using
402 * a common profile buffer to collect items generated by the hertz_tick. For
403 * each task profiled a profile buffer is created that associates a reply port
404 * (used to send the data to a server thread), task (used for throttling), and
405 * a zone area (used to store profiled/traced items).
406 *****************************************************************************
417 prof_data_t pbuf
=task
->profil_buffer
;
419 boolean_t turnon
= (reply
!= MACH_PORT_NULL
);
421 if (task
== TASK_NULL
)
422 return KERN_INVALID_ARGUMENT
;
423 if (turnon
) /* Do we want to profile this task? */
425 pbuf
= pbuf_alloc(); /* allocate a profile buffer */
427 if (task
->task_profiled
) { /* if it is already profiled return so */
429 if (pbuf
!= NULLPROFDATA
)
431 return(KERN_INVALID_ARGUMENT
);
433 if (pbuf
== NULLPROFDATA
) {
435 return KERN_RESOURCE_SHORTAGE
; /* can't allocate a buffer, quit */
437 task
->profil_buffer
= pbuf
;
439 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
444 reset_pbuf_area(pbuf
);
445 pbuf
->prof_port
= reply
; /* assoc. buffer with reply port */
446 } else { /* We want to stop profiling/tracing */
448 if (!task
->task_profiled
) { /* but this task is not being profiled */
450 return(KERN_INVALID_ARGUMENT
);
455 * turnon = FALSE && task_profile = TRUE ||
456 * turnon = TRUE && task_profile = FALSE
459 if (turnon
!= task
->task_profiled
) {
461 thread_act_t thr_act
;
463 if (turnon
&& profile_thread_id
== THREAD_NULL
) /* 1st time thru? */
464 profile_thread_id
= /* then start profile thread. */
465 kernel_thread(kernel_task
, profile_thread
);
466 task
->task_profiled
= turnon
;
467 actual
= task
->thr_act_count
;
468 for (i
= 0, thr_act
= (thread_act_t
)queue_first(&task
->thr_acts
);
470 i
++, thr_act
= (thread_act_t
)queue_next(&thr_act
->thr_acts
)) {
471 if (!thr_act
->act_profiled_own
) {
472 thr_act
->act_profiled
= turnon
;
474 thr_act
->profil_buffer
= task
->profil_buffer
;
475 thr_act
->act_profiled
= TRUE
;
477 thr_act
->act_profiled
= FALSE
;
478 thr_act
->profil_buffer
= NULLPROFDATA
;
482 if (!turnon
) { /* drain buffers and clean-up */
483 send_last_sample_buf(task
->profil_buffer
);
484 pbuf_free(task
->profil_buffer
);
485 task
->profil_buffer
= NULLPROFDATA
;
491 #endif /* MACH_PROF */