* src/vm/cycles-stats.c (cycles_stats_print_percentile): Better extrapolation.
[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                                                                                   u8 cumul, double cumulcycles,
106                                                                                   bool isoverhead, bool printforall)
107 {
108         u8 forall;
109         double cpuMHz = cycles_stats_get_cpu_MHz();
110         u8 cycles_per_ms = cpuMHz * 1000;
111         
112         if (isoverhead) {
113                 fprintf(file,"\t\t%14s = %6.1f\n", name, percentile);
114         }
115         else {
116                 percentile -= cycles_stats_measurement_overhead;
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                         forall = cumulcycles + percentile * (count - cumul);
123                         fprintf(file," (%-23s: %15llu cycles = %6lu msec)",
124                                         (count == cumul) ? "total" : "capped here & extrapol.",
125                                         (unsigned long long)forall,
126                                         (unsigned long)(forall / cycles_per_ms));
127                 }
128                 fprintf(file,"\n");
129         }
130 }
131
132 void cycles_stats_print(FILE *file,
133                                                 const char *name, int nbins, int div,
134                                                 u4 *bins, u8 count, u8 total, u8 min, u8 max,
135                                                 int overhead)
136 {
137         s4 i;
138                 struct cycles_stats_percentile *pcd;
139                 u8 floor, ceiling;
140                 u8 p;
141                 u8 cumul;
142                 double cumulcycles;
143                 double percentile;
144                 double cpuMHz = cycles_stats_get_cpu_MHz();
145                 u8 cycles_per_ms = cpuMHz * 1000;
146
147         fprintf(file,"\t%s: %llu calls\n",
148                 (overhead) ? "measurement overhead determined by" : name, 
149                                 (unsigned long long)count);
150                 
151         fprintf(file,"\t%s cycles distribution:\n", 
152                                 (overhead) ? "measurement overhead" : name);
153
154                 cycles_stats_print_percentile(file, "min", min, count, 0, 0, overhead, true);
155
156                 pcd = cycles_stats_percentile_defs;
157                 for (; pcd->name; pcd++) {
158                         floor   = (count * pcd->pct) / 100;
159                         ceiling = (count * pcd->pct + 99) / 100;
160                         cumul = 0;
161                         cumulcycles = 0;
162                         p = 0;
163                         percentile = -1.0;
164
165                         assert( ceiling <= floor + 1 );
166
167                         for (i=0; i<nbins; ++i) {
168
169                                 /* { invariant: `cumul` samples are < `p` } */
170
171                                 /* check if percentile lies exactly at the bin boundary */
172                                 
173                                 if (floor == cumul && floor == ceiling) {
174                                         percentile = p;
175                                         break;
176                                 }
177
178                                 /* check if percentile lies within this bin */
179
180                                 if (cumul <= floor && ceiling <= cumul + bins[i]) {
181                                         percentile = p + (double)div/2.0;
182                                         break;
183                                 }
184                                 
185                                 cumul += bins[i];
186                                 p     += div;
187
188                                 cumulcycles += bins[i] * (p - (double)div/2.0);
189
190                                 /* { invariant: `cumul` samples are < `p` } */
191                         }
192                         
193                         /* check if percentile lies exactly at the bin boundary */
194
195                         if (floor == cumul && floor == ceiling) {
196                                 percentile = p;
197                         }
198
199                         if (percentile >= 0) {
200                                 if (overhead && pcd->pct == 50) {
201                                         cycles_stats_measurement_overhead = percentile;
202                                 }
203                                 cycles_stats_print_percentile(file, pcd->name, percentile,
204                                                                                           count, cumul, cumulcycles,
205                                                                                           overhead, true);
206                         }
207                         else {
208                                 if (!overhead)
209                                         p -= cycles_stats_measurement_overhead;
210                                 fprintf(file,"\t\t%14s = unknown (> %llu)\n", pcd->name, (unsigned long long)p);
211                         }
212                 }
213
214                 cycles_stats_print_percentile(file, "max", max, count, count,
215                                                                           total, overhead, true);
216
217                 if (!overhead) {
218                         fprintf(file,"\t\t(assuming %llu cycles per ms)\n",
219                                         (unsigned long long)cycles_per_ms);
220                         fprintf(file,"\t\t(assuming %llu cycles measurement overhead)\n", 
221                                         (unsigned long long)cycles_stats_measurement_overhead);
222                 }
223
224                 fprintf(file,"\n");
225                 
226                 cumul = 0;
227         for (i=0; i<nbins; ++i) {
228                         cumul += bins[i];
229             fprintf(file,"\t\t<  %5d: %10lu (%3d%%) %10lu\n",
230                     (int)((i+1) * div),
231                                         (unsigned long) cumul,
232                                         (int)((cumul * 100) / count),
233                     (unsigned long) bins[i]);
234         }
235                 
236         fprintf(file,"\t\t>= %5d: %10s (----) %10lu\n",
237                 (int)(nbins * div),
238                                 "OVER",
239                 (unsigned long) bins[nbins]);
240 }
241
242 #endif /* defined(ENABLE_CYCLES_STATS) */
243
244 /*
245  * These are local overrides for various environment variables in Emacs.
246  * Please do not remove this and leave it at the end of the file, where
247  * Emacs will automagically detect them.
248  * ---------------------------------------------------------------------
249  * Local variables:
250  * mode: c
251  * indent-tabs-mode: t
252  * c-basic-offset: 4
253  * tab-width: 4
254  * End:
255  * vim:noexpandtab:sw=4:ts=4:
256  */