* Removed all Id tags.
[cacao.git] / src / vm / cycles-stats.c
1 /* src/vm/cycles-stats.c - functions 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 #include "config.h"
34 #include "vm/types.h"
35 #include "vm/global.h"
36
37 #if defined(ENABLE_CYCLES_STATS)
38
39 #include <stdio.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <assert.h>
43 #include "vm/cycles-stats.h"
44
45 struct cycles_stats_percentile {
46         int         pct;
47         const char *name;
48 };
49
50 static struct cycles_stats_percentile cycles_stats_percentile_defs[] = {
51         { 10, "10%-percentile" },
52         { 50, "median"         },
53         { 90, "90%-percentile" },
54         { 99, "99%-percentile" },
55         {  0, NULL             } /* sentinel */
56 };
57
58 static double cycles_stats_cpu_MHz = 0.0;
59
60 #define CYCLES_STATS_MAXLINE  100
61
62 static double cycles_stats_get_cpu_MHz(void)
63 {
64         FILE *info;
65         char line[CYCLES_STATS_MAXLINE + 1];
66
67         if (cycles_stats_cpu_MHz != 0.0)
68                 return cycles_stats_cpu_MHz;
69
70         info = fopen("/proc/cpuinfo","r");
71         if (!info) {
72                 fprintf(stderr,"error: could not open /proc/cpuinfo: %s\n",strerror(errno));
73                 goto got_no_cpuinfo;
74         }
75
76         while (!feof(info) && !ferror(info)) {
77                 if (fgets(line,CYCLES_STATS_MAXLINE,info)
78                         && sscanf(line,"cpu MHz : %lf",&cycles_stats_cpu_MHz) == 1)
79                 {
80                         fclose(info);
81                         fprintf(stderr,"CPU frequency used for statistics: %f MHz\n",
82                                         cycles_stats_cpu_MHz);
83                         return cycles_stats_cpu_MHz;
84                 }
85         }
86
87         if (ferror(info)) {
88                 fprintf(stderr,"error reading /proc/cpuinfo: %s\n",strerror(errno));
89         }
90
91         fclose(info);
92
93 got_no_cpuinfo:
94         fprintf(stderr,"warning: falling back to default CPU frequency for statistics\n");
95         cycles_stats_cpu_MHz = 1800.0;
96         return cycles_stats_cpu_MHz;
97 }
98
99 u8 cycles_stats_measurement_overhead = 0;
100
101 static void cycles_stats_print_percentile(FILE *file, const char *name,
102                                                                                   double percentile, u8 count,
103                                                                                   u8 cumul, double cumulcycles,
104                                                                                   bool isoverhead, bool printforall)
105 {
106         u8 forall;
107         double cpuMHz = cycles_stats_get_cpu_MHz();
108         u8 cycles_per_ms = cpuMHz * 1000;
109
110         if (isoverhead) {
111                 fprintf(file,"\t\t%14s = %6.1f\n", name, percentile);
112         }
113         else {
114                 percentile -= cycles_stats_measurement_overhead;
115
116                 fprintf(file,"\t\t%14s = %14.1f (+%llu)",
117                                 name, percentile,
118                                 (unsigned long long)cycles_stats_measurement_overhead);
119                 if (printforall) {
120                         forall = cumulcycles - cumul * cycles_stats_measurement_overhead
121                                    + percentile * (count - cumul);
122                         fprintf(file," (%-23s: %15llu cycles = %6lu msec)",
123                                         (count == cumul) ? "total" : "capped here & extrapol.",
124                                         (unsigned long long)forall,
125                                         (unsigned long)(forall / cycles_per_ms));
126                 }
127                 fprintf(file,"\n");
128         }
129 }
130
131 void cycles_stats_print(FILE *file,
132                                                 const char *name, int nbins, int div,
133                                                 u4 *bins, u8 count, u8 total, u8 min, u8 max,
134                                                 int overhead)
135 {
136         s4 i;
137                 struct cycles_stats_percentile *pcd;
138                 u8 floor, ceiling;
139                 u8 p;
140                 u8 cumul;
141                 double cumulcycles;
142                 double percentile;
143                 double cpuMHz = cycles_stats_get_cpu_MHz();
144                 u8 cycles_per_ms = cpuMHz * 1000;
145
146         fprintf(file,"\t%s: %llu calls\n",
147                 (overhead) ? "measurement overhead determined by" : name,
148                                 (unsigned long long)count);
149
150         fprintf(file,"\t%s cycles distribution:\n",
151                                 (overhead) ? "measurement overhead" : name);
152
153                 cycles_stats_print_percentile(file, "min", min, count, 0, 0, overhead, true);
154
155                 pcd = cycles_stats_percentile_defs;
156                 for (; pcd->name; pcd++) {
157                         floor   = (count * pcd->pct) / 100;
158                         ceiling = (count * pcd->pct + 99) / 100;
159                         cumul = 0;
160                         cumulcycles = 0;
161                         p = 0;
162                         percentile = -1.0;
163
164                         assert( ceiling <= floor + 1 );
165
166                         for (i=0; i<nbins; ++i) {
167
168                                 /* { invariant: `cumul` samples are < `p` } */
169
170                                 /* check if percentile lies exactly at the bin boundary */
171
172                                 if (floor == cumul && floor == ceiling) {
173                                         percentile = p;
174                                         break;
175                                 }
176
177                                 /* check if percentile lies within this bin */
178
179                                 if (cumul <= floor && ceiling <= cumul + bins[i]) {
180                                         percentile = p + (double)div/2.0;
181                                         break;
182                                 }
183
184                                 cumul += bins[i];
185                                 p     += div;
186
187                                 cumulcycles += bins[i] * (p - (double)div/2.0);
188
189                                 /* { invariant: `cumul` samples are < `p` } */
190                         }
191
192                         /* check if percentile lies exactly at the bin boundary */
193
194                         if (floor == cumul && floor == ceiling) {
195                                 percentile = p;
196                         }
197
198                         if (percentile >= 0) {
199                                 if (overhead && pcd->pct == 50) {
200                                         cycles_stats_measurement_overhead = percentile;
201                                 }
202                                 cycles_stats_print_percentile(file, pcd->name, percentile,
203                                                                                           count, cumul, cumulcycles,
204                                                                                           overhead, true);
205                         }
206                         else {
207                                 if (!overhead)
208                                         p -= cycles_stats_measurement_overhead;
209                                 fprintf(file,"\t\t%14s = unknown (> %llu)\n", pcd->name, (unsigned long long)p);
210                         }
211                 }
212
213                 cycles_stats_print_percentile(file, "max", max, count, count,
214                                                                           total, overhead, true);
215
216                 if (!overhead) {
217                         fprintf(file,"\t\t(assuming %llu cycles per ms)\n",
218                                         (unsigned long long)cycles_per_ms);
219                         fprintf(file,"\t\t(assuming %llu cycles measurement overhead)\n",
220                                         (unsigned long long)cycles_stats_measurement_overhead);
221                 }
222
223                 fprintf(file,"\n");
224
225                 cumul = 0;
226         for (i=0; i<nbins; ++i) {
227                         cumul += bins[i];
228             fprintf(file,"\t\t<  %8d: %10lu (%3d%%) %10lu\n",
229                     (int)((i+1) * div),
230                                         (unsigned long) cumul,
231                                         (count) ? (int)((cumul * 100) / count) : 0,
232                     (unsigned long) bins[i]);
233         }
234
235         fprintf(file,"\t\t>= %8d: %10s (----) %10lu\n",
236                 (int)(nbins * div),
237                                 "OVER",
238                 (unsigned long) bins[nbins]);
239 }
240
241 #endif /* defined(ENABLE_CYCLES_STATS) */
242
243 /*
244  * These are local overrides for various environment variables in Emacs.
245  * Please do not remove this and leave it at the end of the file, where
246  * Emacs will automagically detect them.
247  * ---------------------------------------------------------------------
248  * Local variables:
249  * mode: c
250  * indent-tabs-mode: t
251  * c-basic-offset: 4
252  * tab-width: 4
253  * End:
254  * vim:noexpandtab:sw=4:ts=4:
255  */