1 /* src/vm/cycles-stats.c - functions for cycle count statistics
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Edwin Steiner
35 #include "vm/global.h"
37 #if defined(ENABLE_CYCLES_STATS)
43 #include "vm/cycles-stats.h"
45 struct cycles_stats_percentile {
50 static struct cycles_stats_percentile cycles_stats_percentile_defs[] = {
51 { 10, "10%-percentile" },
53 { 90, "90%-percentile" },
54 { 99, "99%-percentile" },
55 { 0, NULL } /* sentinel */
58 static double cycles_stats_cpu_MHz = 0.0;
60 #define CYCLES_STATS_MAXLINE 100
62 static double cycles_stats_get_cpu_MHz(void)
65 char line[CYCLES_STATS_MAXLINE + 1];
67 if (cycles_stats_cpu_MHz != 0.0)
68 return cycles_stats_cpu_MHz;
70 info = fopen("/proc/cpuinfo","r");
72 fprintf(stderr,"error: could not open /proc/cpuinfo: %s\n",strerror(errno));
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)
81 fprintf(stderr,"CPU frequency used for statistics: %f MHz\n",
82 cycles_stats_cpu_MHz);
83 return cycles_stats_cpu_MHz;
88 fprintf(stderr,"error reading /proc/cpuinfo: %s\n",strerror(errno));
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;
99 u8 cycles_stats_measurement_overhead = 0;
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)
107 double cpuMHz = cycles_stats_get_cpu_MHz();
108 u8 cycles_per_ms = cpuMHz * 1000;
111 fprintf(file,"\t\t%14s = %6.1f\n", name, percentile);
114 percentile -= cycles_stats_measurement_overhead;
116 fprintf(file,"\t\t%14s = %14.1f (+%llu)",
118 (unsigned long long)cycles_stats_measurement_overhead);
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));
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,
137 struct cycles_stats_percentile *pcd;
143 double cpuMHz = cycles_stats_get_cpu_MHz();
144 u8 cycles_per_ms = cpuMHz * 1000;
146 fprintf(file,"\t%s: %llu calls\n",
147 (overhead) ? "measurement overhead determined by" : name,
148 (unsigned long long)count);
150 fprintf(file,"\t%s cycles distribution:\n",
151 (overhead) ? "measurement overhead" : name);
153 cycles_stats_print_percentile(file, "min", min, count, 0, 0, overhead, true);
155 pcd = cycles_stats_percentile_defs;
156 for (; pcd->name; pcd++) {
157 floor = (count * pcd->pct) / 100;
158 ceiling = (count * pcd->pct + 99) / 100;
164 assert( ceiling <= floor + 1 );
166 for (i=0; i<nbins; ++i) {
168 /* { invariant: `cumul` samples are < `p` } */
170 /* check if percentile lies exactly at the bin boundary */
172 if (floor == cumul && floor == ceiling) {
177 /* check if percentile lies within this bin */
179 if (cumul <= floor && ceiling <= cumul + bins[i]) {
180 percentile = p + (double)div/2.0;
187 cumulcycles += bins[i] * (p - (double)div/2.0);
189 /* { invariant: `cumul` samples are < `p` } */
192 /* check if percentile lies exactly at the bin boundary */
194 if (floor == cumul && floor == ceiling) {
198 if (percentile >= 0) {
199 if (overhead && pcd->pct == 50) {
200 cycles_stats_measurement_overhead = percentile;
202 cycles_stats_print_percentile(file, pcd->name, percentile,
203 count, cumul, cumulcycles,
208 p -= cycles_stats_measurement_overhead;
209 fprintf(file,"\t\t%14s = unknown (> %llu)\n", pcd->name, (unsigned long long)p);
213 cycles_stats_print_percentile(file, "max", max, count, count,
214 total, overhead, true);
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);
226 for (i=0; i<nbins; ++i) {
228 fprintf(file,"\t\t< %8d: %10lu (%3d%%) %10lu\n",
230 (unsigned long) cumul,
231 (count) ? (int)((cumul * 100) / count) : 0,
232 (unsigned long) bins[i]);
235 fprintf(file,"\t\t>= %8d: %10s (----) %10lu\n",
238 (unsigned long) bins[nbins]);
241 #endif /* defined(ENABLE_CYCLES_STATS) */
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 * ---------------------------------------------------------------------
250 * indent-tabs-mode: t
254 * vim:noexpandtab:sw=4:ts=4: