Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * @OSF_COPYRIGHT@
 */
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

/*
 */
#include	<mach_prof.h>

#include	<mach/task_server.h>
#include	<mach/thread_act_server.h>

#if MACH_PROF
#include        <cpus.h>
#include	<kern/thread.h>
#include	<kern/thread_swap.h>
#include	<kern/queue.h>
#include	<kern/profile.h>
#include	<kern/sched_prim.h>
#include	<kern/spl.h>
#include	<kern/misc_protos.h>
#include	<ipc/ipc_space.h>
#include        <machine/machparam.h>
#include	<mach/prof.h>

thread_t profile_thread_id = THREAD_NULL;
int profile_sample_count = 0;	/* Provided for looking at from kdb. */
extern kern_return_t task_suspend(task_t task);	/* ack */

/* Forwards */
prof_data_t	pbuf_alloc(void);
void		pbuf_free(
			prof_data_t	pbuf);
void		profile_thread(void);
void		send_last_sample_buf(
			prof_data_t	pbuf);

/*
 *****************************************************************************
 * profile_thread is the profile/trace kernel support thread. It is started
 * by a server/user request through task_sample, or thread_sample. The profile
 * thread dequeues messages and sends them to the receive_prof thread, in the
 * server, via the send_samples and send_notices mig interface functions. If
 * there are no messages in the queue profile thread blocks until wakened by
 * profile (called in from mach_clock), or last_sample (called by thread/task_
 * sample).
*/

void
profile_thread(void)
{
    spl_t	    s;
    buffer_t	    buf_entry;
    queue_entry_t   prof_queue_entry;
    prof_data_t	    pbuf;
    kern_return_t   kr;
    int		    j;

    thread_swappable(current_act(), FALSE);

    /* Initialise the queue header for the prof_queue */
    mpqueue_init(&prof_queue);

    while (TRUE) {

	/* Dequeue the first buffer. */
	s = splsched();
	mpdequeue_head(&prof_queue, &prof_queue_entry);
	splx(s);

	if ((buf_entry = (buffer_t) prof_queue_entry) == NULLPBUF) { 
	    assert_wait((event_t) profile_thread, THREAD_UNINT);
	    thread_block((void (*)(void)) 0);
	    if (current_thread()->wait_result != THREAD_AWAKENED)
		break;
	} else 
	{
	    int		    dropped;

	    pbuf = buf_entry->p_prof;
	    kr = send_samples(pbuf->prof_port, (void *)buf_entry->p_zone,
			(mach_msg_type_number_t)buf_entry->p_index);
	    profile_sample_count += buf_entry->p_index;
	    if (kr != KERN_SUCCESS)
	      printf("send_samples(%x, %x, %d) error %x\n",
			pbuf->prof_port, buf_entry->p_zone, buf_entry->p_index, kr); 
	    dropped = buf_entry->p_dropped;
	    if (dropped > 0) {
		printf("kernel: profile dropped %d sample%s\n", dropped,
		       dropped == 1 ? "" : "s");
		buf_entry->p_dropped = 0;
	    }

	    /* Indicate you've finished the dirty job */
	    buf_entry->p_full = FALSE;
	    if (buf_entry->p_wakeme)
	      thread_wakeup((event_t) &buf_entry->p_wakeme);
	}

    }
    /* The profile thread has been signalled to exit.  Any threads waiting
       for the last buffer of samples to be acknowledged should be woken
       up now.  */
    profile_thread_id = THREAD_NULL;
    while (1) {
	s = splsched();
	mpdequeue_head(&prof_queue, &prof_queue_entry);
	splx(s);
	if ((buf_entry = (buffer_t) prof_queue_entry) == NULLPBUF)
	    break;
	if (buf_entry->p_wakeme)
	    thread_wakeup((event_t) &buf_entry->p_wakeme);
    }
#if 0	/* XXXXX */
    thread_halt_self();
#else
	panic("profile_thread(): halt_self");
#endif	/* XXXXX */
}

/*
 *****************************************************************************
 * send_last_sample is the drain mechanism to allow partial profiled buffers
 * to be sent to the receive_prof thread in the server.
 *****************************************************************************
*/

void
send_last_sample_buf(prof_data_t pbuf)
{
    spl_t    s;
    buffer_t buf_entry;

    if (pbuf == NULLPROFDATA)
	return;

    /* Ask for the sending of the last PC buffer.
     * Make a request to the profile_thread by inserting
     * the buffer in the send queue, and wake it up. 
     * The last buffer must be inserted at the head of the
     * send queue, so the profile_thread handles it immediatly. 
     */ 
    buf_entry = pbuf->prof_area + pbuf->prof_index;
    buf_entry->p_prof = pbuf;

    /* 
       Watch out in case profile thread exits while we are about to
       queue data for it.
     */
    s = splsched();
    if (profile_thread_id == THREAD_NULL)
	splx(s);
    else {
	buf_entry->p_wakeme = 1;
	mpenqueue_tail(&prof_queue, &buf_entry->p_list);
	thread_wakeup((event_t) profile_thread);
	assert_wait((event_t) &buf_entry->p_wakeme, THREAD_ABORTSAFE);
	splx(s); 
	thread_block((void (*)(void)) 0);
    }
}


/*
 *****************************************************************************
 * add clock tick parameters to profile/trace buffers. Called from the mach_
 * clock heritz_tick function. DCI version stores thread, sp, and pc values
 * into the profile/trace buffers. MACH_PROF version just stores pc values.
 *****************************************************************************
 */

void
profile(natural_t	pc,
	prof_data_t	pbuf)
{
    natural_t inout_val = pc; 
    buffer_t buf_entry;

    if (pbuf == NULLPROFDATA)
	return;
    
    /* Inserts the PC value in the buffer of the thread */
    set_pbuf_value(pbuf, &inout_val); 
    switch((int)inout_val) {
    case 0: 
	if (profile_thread_id == THREAD_NULL) {
	  reset_pbuf_area(pbuf);
	}
	break;
    case 1: 
	/* Normal case, value successfully inserted */
	break;
    case 2 : 
	/*
	 * The value we have just inserted caused the
	 * buffer to be full, and ready to be sent.
	 * If profile_thread_id is null, the profile
	 * thread has been killed.  Since this generally
	 * happens only when the O/S server task of which
	 * it is a part is killed, it is not a great loss
	 * to throw away the data.
	 */
	if (profile_thread_id == THREAD_NULL) {
	  reset_pbuf_area(pbuf);
	  break;
	}

	buf_entry = (buffer_t) &pbuf->prof_area[pbuf->prof_index];
	buf_entry->p_prof = pbuf;
	mpenqueue_tail(&prof_queue, &buf_entry->p_list);
	
	/* Switch to another buffer */
	reset_pbuf_area(pbuf);
	
	/* Wake up the profile thread */
	if (profile_thread_id != THREAD_NULL)
	  thread_wakeup((event_t) profile_thread);
	break;
      
      default: 
	printf("profile : unexpected case\n"); 
    }
}

/*
 *****************************************************************************
 * pbuf_alloc creates a profile/trace buffer and assoc. zones for storing
 * profiled items.
 *****************************************************************************
 */

prof_data_t
pbuf_alloc(void)
{
	register prof_data_t pbuf;
	register int i;
	register natural_t *zone;

	pbuf = (prof_data_t)kalloc(sizeof(struct prof_data));
	if (!pbuf)
		return(NULLPROFDATA);
	pbuf->prof_port = MACH_PORT_NULL;
	for (i=0; i< NB_PROF_BUFFER; i++) {
		zone = (natural_t *)kalloc(SIZE_PROF_BUFFER*sizeof(natural_t));
		if (!zone) {
			i--;
			while (i--)
				kfree((vm_offset_t)pbuf->prof_area[i].p_zone,
				      SIZE_PROF_BUFFER*sizeof(natural_t));
			kfree((vm_offset_t)pbuf, sizeof(struct prof_data));
			return(NULLPROFDATA);
		}
		pbuf->prof_area[i].p_zone = zone;
		pbuf->prof_area[i].p_full = FALSE;
	}
	pbuf->prof_port = MACH_PORT_NULL;
	return(pbuf);
}

/*
 *****************************************************************************
 * pbuf_free free memory allocated for storing profile/trace items. Called
 * when a task is no longer profiled/traced. Pbuf_free tears down the memory
 * alloced in pbuf_alloc. It does not check to see if the structures are valid
 * since it is only called by functions in this file.
 *****************************************************************************
 */
void
pbuf_free(
	prof_data_t	pbuf)
{
	register int i;

	if (pbuf->prof_port)
		ipc_port_release_send(pbuf->prof_port);
	
	for(i=0; i < NB_PROF_BUFFER ; i++)
		kfree((vm_offset_t)pbuf->prof_area[i].p_zone,
		      SIZE_PROF_BUFFER*sizeof(natural_t));
	kfree((vm_offset_t)pbuf, sizeof(struct prof_data));
}

#endif /* MACH_PROF */

/*
 *****************************************************************************
 * Thread_sample is used by MACH_PROF to profile a single thread, and is only
 * stub in DCI.
 *****************************************************************************
 */

kern_return_t
thread_sample(
	thread_act_t	thr_act,
	ipc_port_t	reply)
{
    /* 
     * This routine is called every time that a new thread has made
     * a request for the sampling service. We must keep track of the 
     * correspondance between its identity (thread) and the port
     * we are going to use as a reply port to send out the samples resulting 
     * from its execution. 
     */
#if !MACH_PROF
    return KERN_FAILURE;
#else 
    prof_data_t	    pbuf;
    vm_offset_t	    vmpbuf;

    if (reply != MACH_PORT_NULL) {
	if (thr_act->act_profiled) 	/* yuck! */
		return KERN_INVALID_ARGUMENT;
	/* Start profiling this activation, do the initialization. */
	pbuf = pbuf_alloc();
	if ((thr_act->profil_buffer = pbuf) == NULLPROFDATA) {
	    printf("thread_sample: cannot allocate pbuf\n");
	    return KERN_RESOURCE_SHORTAGE;
	} 
	else {
	    if (!set_pbuf_nb(pbuf, NB_PROF_BUFFER-1)) {
		printf("mach_sample_thread: cannot set pbuf_nb\n");
		return KERN_FAILURE;
	    }
	    reset_pbuf_area(pbuf);
	}
	pbuf->prof_port = reply;
	thr_act->act_profiled = TRUE;
	thr_act->act_profiled_own = TRUE;
	if (profile_thread_id == THREAD_NULL)
	    profile_thread_id = kernel_thread(kernel_task, profile_thread);
    } else {
	if (!thr_act->act_profiled)
		return(KERN_INVALID_ARGUMENT);

	thr_act->act_profiled = FALSE;
	/* do not stop sampling if thread is not profiled by its own */

	if (!thr_act->act_profiled_own)
	    return KERN_SUCCESS;
	else
	    thr_act->act_profiled_own = FALSE;

	send_last_sample_buf(thr_act->profil_buffer);
	pbuf_free(thr_act->profil_buffer);
	thr_act->profil_buffer = NULLPROFDATA;
    }
    return KERN_SUCCESS;
#endif /* MACH_PROF */
}

/*
 *****************************************************************************
 * Task_sample is used to profile/trace tasks - all thread within a task using
 * a common profile buffer to collect items generated by the hertz_tick. For
 * each task profiled a profile buffer is created that associates a reply port
 * (used to send the data to a server thread), task (used for throttling), and
 * a zone area (used to store profiled/traced items).
 *****************************************************************************
 */

kern_return_t
task_sample(
	task_t		task,
	ipc_port_t	reply)
{
#if !MACH_PROF
    return KERN_FAILURE;
#else
    prof_data_t	    pbuf=task->profil_buffer;
    vm_offset_t	    vmpbuf;
    boolean_t	    turnon = (reply != MACH_PORT_NULL);

    if (task == TASK_NULL)
	    return KERN_INVALID_ARGUMENT;
    if (turnon)			/* Do we want to profile this task? */
    {
	pbuf = pbuf_alloc();	/* allocate a profile buffer */
	task_lock(task);
	if (task->task_profiled) { /* if it is already profiled return so */
		task_unlock(task);
		if (pbuf != NULLPROFDATA)
		    pbuf_free(pbuf);
		return(KERN_INVALID_ARGUMENT);
	}
	if (pbuf == NULLPROFDATA) {
	    task_unlock(task);
	    return KERN_RESOURCE_SHORTAGE; /* can't allocate a buffer, quit */
	}
	task->profil_buffer = pbuf;
	
	if (!set_pbuf_nb(pbuf, NB_PROF_BUFFER-1)) {
	    pbuf_free(pbuf);
	    task_unlock(task);
	    return KERN_FAILURE;
	}
	reset_pbuf_area(pbuf);
	pbuf->prof_port = reply; /* assoc. buffer with reply port */
    } else {			/* We want to stop profiling/tracing */
	task_lock(task);
	if (!task->task_profiled) { /* but this task is not being profiled */
	    task_unlock(task);
	    return(KERN_INVALID_ARGUMENT);
	}
    }

    /*
     * turnon = FALSE && task_profile = TRUE ||
     * turnon = TRUE  && task_profile = FALSE
     */

    if (turnon != task->task_profiled) {
	int actual, i;
	thread_act_t thr_act;

	if (turnon && profile_thread_id == THREAD_NULL)	/* 1st time thru? */
	    profile_thread_id =	/* then start profile thread. */
		kernel_thread(kernel_task, profile_thread);
	task->task_profiled = turnon;  
	actual = task->thr_act_count; 
	for (i = 0, thr_act = (thread_act_t)queue_first(&task->thr_acts);
	     i < actual;
	     i++, thr_act = (thread_act_t)queue_next(&thr_act->thr_acts)) {
		  if (!thr_act->act_profiled_own) {
		    thr_act->act_profiled = turnon;
		    if (turnon) {
			thr_act->profil_buffer = task->profil_buffer;
		  	thr_act->act_profiled = TRUE;
		    } else {
			thr_act->act_profiled = FALSE;
		        thr_act->profil_buffer = NULLPROFDATA;
		    }
		  }
	}
	if (!turnon) {		/* drain buffers and clean-up */
	    send_last_sample_buf(task->profil_buffer);
	    pbuf_free(task->profil_buffer);
	    task->profil_buffer = NULLPROFDATA;
	}
    }

    task_unlock(task);
    return KERN_SUCCESS;
#endif /* MACH_PROF */
}