d70c9d1cbde582cad5bc9ce380875dd45a1c1a64
[cacao.git] / src / vm / cycles-stats.h
1 /* src/vm/cycles-stats.h - macros for cycle count statistics
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Edwin Steiner
28
29    Changes:
30
31 */
32
33 #ifndef _CYCLES_STATS_H
34 #define _CYCLES_STATS_H
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #if defined(ENABLE_CYCLES_STATS)
40
41 #include <stdio.h>
42
43 #define CYCLES_STATS_DECLARE(name,nbins,divisor)                            \
44     static const int CYCLES_STATS_##name##_MAX = (nbins);                   \
45     static const int CYCLES_STATS_##name##_DIV = (divisor);                 \
46     static u4 cycles_stats_##name##_bins[(nbins) + 1] = { 0 };              \
47     static u4 cycles_stats_##name##_count = 0;                              \
48     static u8 cycles_stats_##name##_total = 0;                              \
49     static u8 cycles_stats_##name##_max = 0;                                \
50     static u8 cycles_stats_##name##_min = 1000000000;
51
52 #define CYCLES_STATS_GET(var)                                               \
53         (var) = asm_get_cycle_count()                                           \
54
55 #define CYCLES_STATS_COUNT(name,cyclesexpr)                                 \
56     do {                                                                    \
57         u8 cyc = (cyclesexpr);                                              \
58                 cycles_stats_##name##_total += cyc;                                 \
59         if (cyc > cycles_stats_##name##_max)                                \
60             cycles_stats_##name##_max = cyc;                                \
61         if (cyc < cycles_stats_##name##_min)                                \
62             cycles_stats_##name##_min = cyc;                                \
63         cyc /= CYCLES_STATS_##name##_DIV;                                   \
64         if (cyc < CYCLES_STATS_##name##_MAX)                                \
65             cycles_stats_##name##_bins[cyc]++;                              \
66         else                                                                \
67             cycles_stats_##name##_bins[CYCLES_STATS_##name##_MAX]++;        \
68         cycles_stats_##name##_count++;                                      \
69     } while (0)
70
71 #define CYCLES_STATS_COUNT_OVER(name,ovname,cyclesexpr)                     \
72     do {                                                                    \
73         u8 cyc = (cyclesexpr);                                              \
74         if (cyc / CYCLES_STATS_##name##_DIV >= CYCLES_STATS_##name##_MAX)   \
75             CYCLES_STATS_COUNT(ovname,cyc);                                 \
76     } while (0)
77
78 #define CYCLES_STATS_PRINT(name,file)                                       \
79     do {                                                                    \
80         cycles_stats_print((file), #name,                                   \
81             CYCLES_STATS_##name##_MAX, CYCLES_STATS_##name##_DIV,           \
82             cycles_stats_##name##_bins, cycles_stats_##name##_count,        \
83             cycles_stats_##name##_total,                                    \
84             cycles_stats_##name##_min, cycles_stats_##name##_max, 0);       \
85     } while (0)
86
87 #define CYCLES_STATS_PRINT_OVERHEAD(name,file)                              \
88     do {                                                                    \
89         cycles_stats_print((file), #name,                                   \
90             CYCLES_STATS_##name##_MAX, CYCLES_STATS_##name##_DIV,           \
91             cycles_stats_##name##_bins, cycles_stats_##name##_count,        \
92             cycles_stats_##name##_total,                                    \
93             cycles_stats_##name##_min, cycles_stats_##name##_max, 1);       \
94     } while (0)
95
96 #define CYCLES_STATS_DECLARE_AND_START                                      \
97     u8 cycles_start = asm_get_cycle_count();                                \
98     u8 cycles_end;
99
100 #define CYCLES_STATS_DECLARE_AND_START_WITH_OVERHEAD                        \
101     u8 cycles_start = asm_get_cycle_count();                                \
102     u8 cycles_overhead = asm_get_cycle_count();                             \
103     u8 cycles_end;
104
105 #define CYCLES_STATS_END(name)                                              \
106     cycles_end = asm_get_cycle_count();                                     \
107     CYCLES_STATS_COUNT(name, cycles_end - cycles_start);
108
109 #define CYCLES_STATS_END_WITH_OVERHEAD(name,ovname)                         \
110     cycles_end = asm_get_cycle_count();                                     \
111     CYCLES_STATS_COUNT(ovname, cycles_overhead - cycles_start);             \
112     CYCLES_STATS_COUNT(name, cycles_end - cycles_overhead);
113
114 void cycles_stats_print(FILE *file,
115                                             const char *name, int nbins, int div,
116                                             u4 *bins, u8 count, u8 total, u8 min, u8 max,
117                                                 int overhead);
118
119
120 #else /* !defined(ENABLE_CYCLES_STATS) */
121
122 #define CYCLES_STATS_DECLARE(name,nbins,divisor)
123 #define CYCLES_STATS_GET(var)
124 #define CYCLES_STATS_COUNT(name,cyclesexpr)
125 #define CYCLES_STATS_COUNT_OVER(name,ovname,cyclesexpr)
126 #define CYCLES_STATS_PRINT(name,file)
127 #define CYCLES_STATS_PRINT_OVERHEAD(name,file)
128 #define CYCLES_STATS_DECLARE_AND_START
129 #define CYCLES_STATS_DECLARE_AND_START_WITH_OVERHEAD
130 #define CYCLES_STATS_END(name)
131 #define CYCLES_STATS_END_WITH_OVERHEAD(name,ovname)
132
133 #endif /* defined(ENABLE_CYCLES_STATS) */
134
135 #endif /* _CYCLES_STATS_H */
136
137 /*
138  * These are local overrides for various environment variables in Emacs.
139  * Please do not remove this and leave it at the end of the file, where
140  * Emacs will automagically detect them.
141  * ---------------------------------------------------------------------
142  * Local variables:
143  * mode: c
144  * indent-tabs-mode: t
145  * c-basic-offset: 4
146  * tab-width: 4
147  * End:
148  * vim:noexpandtab:sw=4:ts=4:
149  */