File: profile.c

package info (click to toggle)
nickle 2.47-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,112 kB
  • ctags: 3,255
  • sloc: ansic: 30,401; yacc: 1,843; sh: 865; lex: 838; makefile: 202
file content (90 lines) | stat: -rw-r--r-- 1,926 bytes parent folder | download
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
/* $Header: /local/src/CVS/nickle/profile.c,v 1.11 2004/04/19 03:13:39 keithp Exp $ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"
#include	<sys/time.h>
#include	<assert.h>

#define TICK_MS	10

volatile static unsigned long	currentTick;
volatile Bool			signalProfile;
static unsigned long		previousTick;
Bool				profiling;

static RETSIGTYPE
sigprofile (int sig)
{
    resetSignal (SIGVTALRM, sigprofile);
    currentTick += TICK_MS;
    SetSignalProfile ();
}

void
ProfileInterrupt (Value thread)
{
    unsigned long   now;
    unsigned long   ticks;
    InstPtr	    pc;
    ExprPtr	    stat;
    FramePtr	    frame;
    
    now = currentTick;
    ticks = now - previousTick;
    previousTick = now;
    if (!thread)
	return;
    pc = thread->thread.continuation.pc;
    if (pc)
    {
	stat = ObjStatement (thread->thread.continuation.obj,pc);
	if (stat)
	{
	    stat->base.ticks += ticks;
	}
    }
    for (frame = thread->thread.continuation.frame; frame; frame = frame->previous)
    {
	pc = frame->savePc;
        stat = ObjStatement (frame->saveObj, frame->savePc);
        if (stat)
	    stat->base.sub_ticks += ticks;
    }
}

Value
do_profile (Value on)
{
    struct itimerval    v;
    Bool    previous = profiling;
	
    if (True (on))
    {
	previousTick = currentTick;
	catchSignal (SIGVTALRM, sigprofile);
	v.it_interval.tv_sec = 0;
	v.it_interval.tv_usec = TICK_MS * 1000;
	v.it_value = v.it_interval;
	setitimer (ITIMER_VIRTUAL, &v, 0);
	profiling = True;
    }
    else
    {
	v.it_interval.tv_sec = 0;
	v.it_interval.tv_usec = 0;
	v.it_value = v.it_interval;
	setitimer (ITIMER_VIRTUAL, &v, 0);
	profiling = False;
	/*
	 * expr nodes clear their accumulated time 
	 * on GC when profiling is false 
	 */
	MemCollect ();
    }
    return previous ? TrueVal : FalseVal;
}