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