2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 #include <mach_prof.h>
61 #include <mach/task_server.h>
62 #include <mach/thread_act_server.h>
65 #include <kern/thread.h>
66 #include <kern/queue.h>
67 #include <kern/profile.h>
68 #include <kern/sched_prim.h>
70 #include <kern/misc_protos.h>
71 #include <ipc/ipc_space.h>
72 #include <machine/machparam.h>
73 #include <mach/prof.h>
75 thread_t profile_thread_id
= THREAD_NULL
;
76 int profile_sample_count
= 0; /* Provided for looking at from kdb. */
77 extern kern_return_t
task_suspend(task_t task
); /* ack */
80 prof_data_t
pbuf_alloc(void);
83 void profile_thread(void);
84 void send_last_sample_buf(
88 *****************************************************************************
89 * profile_thread is the profile/trace kernel support thread. It is started
90 * by a server/user request through task_sample, or thread_sample. The profile
91 * thread dequeues messages and sends them to the receive_prof thread, in the
92 * server, via the send_samples and send_notices mig interface functions. If
93 * there are no messages in the queue profile thread blocks until wakened by
94 * profile (called in from mach_clock), or last_sample (called by thread/task_
103 queue_entry_t prof_queue_entry
;
108 /* Initialise the queue header for the prof_queue */
109 mpqueue_init(&prof_queue
);
113 /* Dequeue the first buffer. */
115 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
118 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
) {
119 assert_wait((event_t
) profile_thread
, THREAD_UNINT
);
120 thread_block(THREAD_CONTINUE_NULL
);
121 if (current_thread()->wait_result
!= THREAD_AWAKENED
)
127 pbuf
= buf_entry
->p_prof
;
128 kr
= send_samples(pbuf
->prof_port
, (void *)buf_entry
->p_zone
,
129 (mach_msg_type_number_t
)buf_entry
->p_index
);
130 profile_sample_count
+= buf_entry
->p_index
;
131 if (kr
!= KERN_SUCCESS
)
132 printf("send_samples(%x, %x, %d) error %x\n",
133 pbuf
->prof_port
, buf_entry
->p_zone
, buf_entry
->p_index
, kr
);
134 dropped
= buf_entry
->p_dropped
;
136 printf("kernel: profile dropped %d sample%s\n", dropped
,
137 dropped
== 1 ? "" : "s");
138 buf_entry
->p_dropped
= 0;
141 /* Indicate you've finished the dirty job */
142 buf_entry
->p_full
= FALSE
;
143 if (buf_entry
->p_wakeme
)
144 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
148 /* The profile thread has been signalled to exit. Any threads waiting
149 for the last buffer of samples to be acknowledged should be woken
151 profile_thread_id
= THREAD_NULL
;
154 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
156 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
)
158 if (buf_entry
->p_wakeme
)
159 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
164 panic("profile_thread(): halt_self");
169 *****************************************************************************
170 * send_last_sample is the drain mechanism to allow partial profiled buffers
171 * to be sent to the receive_prof thread in the server.
172 *****************************************************************************
176 send_last_sample_buf(prof_data_t pbuf
)
181 if (pbuf
== NULLPROFDATA
)
184 /* Ask for the sending of the last PC buffer.
185 * Make a request to the profile_thread by inserting
186 * the buffer in the send queue, and wake it up.
187 * The last buffer must be inserted at the head of the
188 * send queue, so the profile_thread handles it immediatly.
190 buf_entry
= pbuf
->prof_area
+ pbuf
->prof_index
;
191 buf_entry
->p_prof
= pbuf
;
194 Watch out in case profile thread exits while we are about to
198 if (profile_thread_id
== THREAD_NULL
)
201 buf_entry
->p_wakeme
= 1;
202 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
203 thread_wakeup((event_t
) profile_thread
);
204 assert_wait((event_t
) &buf_entry
->p_wakeme
, THREAD_ABORTSAFE
);
206 thread_block(THREAD_CONTINUE_NULL
);
212 *****************************************************************************
213 * add clock tick parameters to profile/trace buffers. Called from the mach_
214 * clock heritz_tick function. DCI version stores thread, sp, and pc values
215 * into the profile/trace buffers. MACH_PROF version just stores pc values.
216 *****************************************************************************
220 profile(natural_t pc
,
223 natural_t inout_val
= pc
;
226 if (pbuf
== NULLPROFDATA
)
229 /* Inserts the PC value in the buffer of the thread */
230 set_pbuf_value(pbuf
, &inout_val
);
231 switch((int)inout_val
) {
233 if (profile_thread_id
== THREAD_NULL
) {
234 reset_pbuf_area(pbuf
);
238 /* Normal case, value successfully inserted */
242 * The value we have just inserted caused the
243 * buffer to be full, and ready to be sent.
244 * If profile_thread_id is null, the profile
245 * thread has been killed. Since this generally
246 * happens only when the O/S server task of which
247 * it is a part is killed, it is not a great loss
248 * to throw away the data.
250 if (profile_thread_id
== THREAD_NULL
) {
251 reset_pbuf_area(pbuf
);
255 buf_entry
= (buffer_t
) &pbuf
->prof_area
[pbuf
->prof_index
];
256 buf_entry
->p_prof
= pbuf
;
257 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
259 /* Switch to another buffer */
260 reset_pbuf_area(pbuf
);
262 /* Wake up the profile thread */
263 if (profile_thread_id
!= THREAD_NULL
)
264 thread_wakeup((event_t
) profile_thread
);
268 printf("profile : unexpected case\n");
273 *****************************************************************************
274 * pbuf_alloc creates a profile/trace buffer and assoc. zones for storing
276 *****************************************************************************
282 register prof_data_t pbuf
;
284 register natural_t
*zone
;
286 pbuf
= (prof_data_t
)kalloc(sizeof(struct prof_data
));
288 return(NULLPROFDATA
);
289 pbuf
->prof_port
= MACH_PORT_NULL
;
290 for (i
=0; i
< NB_PROF_BUFFER
; i
++) {
291 zone
= (natural_t
*)kalloc(SIZE_PROF_BUFFER
*sizeof(natural_t
));
295 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
296 SIZE_PROF_BUFFER
*sizeof(natural_t
));
297 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
298 return(NULLPROFDATA
);
300 pbuf
->prof_area
[i
].p_zone
= zone
;
301 pbuf
->prof_area
[i
].p_full
= FALSE
;
303 pbuf
->prof_port
= MACH_PORT_NULL
;
308 *****************************************************************************
309 * pbuf_free free memory allocated for storing profile/trace items. Called
310 * when a task is no longer profiled/traced. Pbuf_free tears down the memory
311 * alloced in pbuf_alloc. It does not check to see if the structures are valid
312 * since it is only called by functions in this file.
313 *****************************************************************************
322 ipc_port_release_send(pbuf
->prof_port
);
324 for(i
=0; i
< NB_PROF_BUFFER
; i
++)
325 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
326 SIZE_PROF_BUFFER
*sizeof(natural_t
));
327 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
330 #endif /* MACH_PROF */
333 *****************************************************************************
334 * Thread_sample is used by MACH_PROF to profile a single thread, and is only
336 *****************************************************************************
342 __unused thread_t thread
,
343 __unused ipc_port_t reply
)
354 * This routine is called every time that a new thread has made
355 * a request for the sampling service. We must keep track of the
356 * correspondance between its identity (thread) and the port
357 * we are going to use as a reply port to send out the samples resulting
358 * from its execution.
363 if (reply
!= MACH_PORT_NULL
) {
364 if (thread
->profiled
) /* yuck! */
365 return KERN_INVALID_ARGUMENT
;
366 /* Start profiling this activation, do the initialization. */
368 if ((thread
->profil_buffer
= pbuf
) == NULLPROFDATA
) {
369 printf("thread_sample: cannot allocate pbuf\n");
370 return KERN_RESOURCE_SHORTAGE
;
373 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
374 printf("mach_sample_thread: cannot set pbuf_nb\n");
377 reset_pbuf_area(pbuf
);
379 pbuf
->prof_port
= reply
;
380 thread
->profiled
= TRUE
;
381 thread
->profiled_own
= TRUE
;
382 if (profile_thread_id
== THREAD_NULL
)
383 profile_thread_id
= kernel_thread(kernel_task
, profile_thread
);
385 if (!thread
->profiled
)
386 return(KERN_INVALID_ARGUMENT
);
388 thread
->profiled
= FALSE
;
389 /* do not stop sampling if thread is not profiled by its own */
391 if (!thread
->profiled_own
)
394 thread
->profiled_own
= FALSE
;
396 send_last_sample_buf(thread
->profil_buffer
);
397 pbuf_free(thread
->profil_buffer
);
398 thread
->profil_buffer
= NULLPROFDATA
;
402 #endif /* MACH_PROF */
405 *****************************************************************************
406 * Task_sample is used to profile/trace tasks - all thread within a task using
407 * a common profile buffer to collect items generated by the hertz_tick. For
408 * each task profiled a profile buffer is created that associates a reply port
409 * (used to send the data to a server thread), task (used for throttling), and
410 * a zone area (used to store profiled/traced items).
411 *****************************************************************************
417 __unused task_t task
,
418 __unused ipc_port_t reply
)
428 prof_data_t pbuf
=task
->profil_buffer
;
430 boolean_t turnon
= (reply
!= MACH_PORT_NULL
);
432 if (task
== TASK_NULL
)
433 return KERN_INVALID_ARGUMENT
;
434 if (turnon
) /* Do we want to profile this task? */
436 pbuf
= pbuf_alloc(); /* allocate a profile buffer */
438 if (task
->task_profiled
) { /* if it is already profiled return so */
440 if (pbuf
!= NULLPROFDATA
)
442 return(KERN_INVALID_ARGUMENT
);
444 if (pbuf
== NULLPROFDATA
) {
446 return KERN_RESOURCE_SHORTAGE
; /* can't allocate a buffer, quit */
448 task
->profil_buffer
= pbuf
;
450 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
455 reset_pbuf_area(pbuf
);
456 pbuf
->prof_port
= reply
; /* assoc. buffer with reply port */
457 } else { /* We want to stop profiling/tracing */
459 if (!task
->task_profiled
) { /* but this task is not being profiled */
461 return(KERN_INVALID_ARGUMENT
);
466 * turnon = FALSE && task_profile = TRUE ||
467 * turnon = TRUE && task_profile = FALSE
470 if (turnon
!= task
->task_profiled
) {
474 if (turnon
&& profile_thread_id
== THREAD_NULL
) /* 1st time thru? */
475 profile_thread_id
= /* then start profile thread. */
476 kernel_thread(kernel_task
, profile_thread
);
477 task
->task_profiled
= turnon
;
478 actual
= task
->thread_count
;
479 for (i
= 0, thread
= (thread_t
)queue_first(&task
->threads
);
481 i
++, thread
= (thread_t
)queue_next(&thr_act
->task_threads
)) {
482 if (!thread
->profiled_own
) {
483 threadt
->profiled
= turnon
;
485 threadt
->profil_buffer
= task
->profil_buffer
;
486 thread
->profiled
= TRUE
;
488 thread
->profiled
= FALSE
;
489 thread
->profil_buffer
= NULLPROFDATA
;
493 if (!turnon
) { /* drain buffers and clean-up */
494 send_last_sample_buf(task
->profil_buffer
);
495 pbuf_free(task
->profil_buffer
);
496 task
->profil_buffer
= NULLPROFDATA
;
503 #endif /* MACH_PROF */