/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- ScmProfSample
- ScmProfCount
1 /*
2 * prof.h - Profiler
3 *
4 * Copyright (c) 2005 Shiro Kawai, All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id: prof.h,v 1.3 2005/05/16 09:57:40 shirok Exp $
34 */
35
36 #ifndef GAUCHE_PROF_H
37 #define GAUCHE_PROF_H
38
39 /*=============================================================
40 * Profiler
41 */
42
43 /* We have two types of profilers, a statistic sampler and call-counter.
44 *
45 * The statistic sampler uses ITIMER_PROF and records the current code
46 * base and PC for every SIGPROF.
47 * (NB: in order for this to work, VM's PC must always be saved
48 * in VM structure; in another word, vm.c must be compiled with
49 * SMALL_REGS == 0).
50 *
51 * The call counter records every event of CALL and TAIL-CALL instruction
52 * execution on the thread. Each entry just records the address of
53 * the called object.
54 *
55 * TODO: It is not known if sampling profiler works when more than one
56 * thread requests profiling. Should be considrered later.
57 *
58 * When the on-memory buffer of the call counter gets full, it is collected
59 * to a hash table. When the statistic sampling buffer gets full, it
60 * is flushed to a temporary file (we can't use a hashtable, since the
61 * flushing may be done within a signal handler and we can't call allocator
62 * in it).
63 *
64 * Profiler status:
65 *
66 * Scm_ProfilerStart Scm_ProfilerStop
67 * --------> --------->
68 * INACTIVE RUNNING PAUSING -----\
69 * <-------- |
70 * ^ Scm_ProfilerStart |
71 * | |
72 * \--------------------------------------------/
73 * Scm_ProfilerReset
74 *
75 * Profile result can only be examined when the profile is in "PAUSING"
76 * state.
77 */
78
79 /* Profiler status */
80 enum {
81 SCM_PROFILER_INACTIVE,
82 SCM_PROFILER_RUNNING,
83 SCM_PROFILER_PAUSING
84 };
85
86 /* A sample of statistic sampler */
87 typedef struct ScmProfSampleRec {
88 ScmObj func; /* ScmCompiledCode or ScmSubr */
89 ScmWord *pc;
90 } ScmProfSample;
91
92 /* # of on-memory samples for the statistic sampler. */
93 #define SCM_PROF_SAMPLES_IN_BUFFER 6000
94
95 /* A record of call counter */
96 typedef struct ScmProfCountRec {
97 ScmObj func; /* Called Function */
98 } ScmProfCount;
99
100 /* # of on-memory samples for the call counter. */
101 #define SCM_PROF_COUNTER_IN_BUFFER 12000
102
103 /* Profiling buffer.
104 * It is allocated when profiler-start is called on this thread
105 * for the first time.
106 */
107 struct ScmVMProfilerRec {
108 int state; /* profiler state */
109 int samplerFd; /* temporary file for the sampler */
110 int currentSample; /* index to the current sample */
111 int totalSamples; /* total # of samples */
112 int errorOccurred; /* TRUE if error has occurred during I/O */
113 int currentCount; /* index to the current counter */
114 ScmHashTable* statHash; /* hashtable for collected data.
115 value is a pair of integers,
116 (<call-count> . <sample-hits>) */
117
118 ScmProfSample samples[SCM_PROF_SAMPLES_IN_BUFFER];
119 ScmProfCount counts[SCM_PROF_COUNTER_IN_BUFFER];
120 };
121
122 SCM_EXTERN ScmObj Scm_ProfilerRawResult(void);
123
124 /* Call Counter API */
125
126 SCM_EXTERN void Scm_ProfilerCountBufferFlush(ScmVM *vm);
127
128 #ifdef GAUCHE_PROFILE
129 #define SCM_PROF_COUNT_CALL(vm, obj) \
130 do { \
131 if (vm->profilerRunning) { \
132 if (vm->prof->currentCount == SCM_PROF_COUNTER_IN_BUFFER) { \
133 Scm_ProfilerCountBufferFlush(vm); \
134 } \
135 vm->prof->counts[vm->prof->currentCount++].func = obj; \
136 } \
137 } while (0)
138 #else /*!GAUCHE_PROFILE*/
139 #define SCM_PROF_COUNT_CALL(vm, obj) /*empty*/
140 #endif /*!GAUCHE_PROFILE*/
141
142
143 #endif /*GAUCHE_PROF_H*/