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
37 #include "vm/global.h"
39 #if defined(ENABLE_CYCLES_STATS)
45 #include "vm/cycles-stats.h"
47 struct cycles_stats_percentile {
52 static struct cycles_stats_percentile cycles_stats_percentile_defs[] = {
53 { 10, "10%-percentile" },
55 { 90, "90%-percentile" },
56 { 99, "99%-percentile" },
57 { 0, NULL } /* sentinel */
60 static double cycles_stats_cpu_MHz = 0.0;
62 #define CYCLES_STATS_MAXLINE 100
64 static double cycles_stats_get_cpu_MHz(void)
67 char line[CYCLES_STATS_MAXLINE + 1];
69 if (cycles_stats_cpu_MHz != 0.0)
70 return cycles_stats_cpu_MHz;
72 info = fopen("/proc/cpuinfo","r");
74 fprintf(stderr,"error: could not open /proc/cpuinfo: %s\n",strerror(errno));
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)
83 fprintf(stderr,"CPU frequency used for statistics: %f MHz\n",
84 cycles_stats_cpu_MHz);
85 return cycles_stats_cpu_MHz;
90 fprintf(stderr,"error reading /proc/cpuinfo: %s\n",strerror(errno));
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;
101 u8 cycles_stats_measurement_overhead = 0;
103 static void cycles_stats_print_percentile(FILE *file, const char *name,
104 double percentile, u8 count,
105 bool isoverhead, bool printforall)
108 double cpuMHz = cycles_stats_get_cpu_MHz();
109 u8 cycles_per_ms = cpuMHz * 1000;
112 fprintf(file,"\t\t%14s = %6.1f\n", name, percentile);
115 percentile -= cycles_stats_measurement_overhead;
116 forall = percentile * count;
118 fprintf(file,"\t\t%14s = %14.1f (+%llu)",
120 (unsigned long long)cycles_stats_measurement_overhead);
122 fprintf(file," (for all calls: %15llu cycles = %6lu msec)",
123 (unsigned long long)forall,
124 (unsigned long)(forall / cycles_per_ms));
130 void cycles_stats_print(FILE *file,
131 const char *name, int nbins, int div,
132 u4 *bins, u8 count, u8 min, u8 max,
136 struct cycles_stats_percentile *pcd;
141 double cpuMHz = cycles_stats_get_cpu_MHz();
142 u8 cycles_per_ms = cpuMHz * 1000;
144 fprintf(file,"\t%s: %llu calls\n",
145 (overhead) ? "measurement overhead determined by" : name,
146 (unsigned long long)count);
148 fprintf(file,"\t%s cycles distribution:\n",
149 (overhead) ? "measurement overhead" : name);
151 cycles_stats_print_percentile(file, "min", min, count, overhead, true);
153 pcd = cycles_stats_percentile_defs;
154 for (; pcd->name; pcd++) {
155 floor = (count * pcd->pct) / 100;
156 ceiling = (count * pcd->pct + 99) / 100;
161 assert( ceiling <= floor + 1 );
163 for (i=0; i<nbins; ++i) {
165 /* { invariant: `cumul` samples are < `p` } */
167 /* check if percentile lies exactly at the bin boundary */
169 if (floor == cumul && floor == ceiling) {
174 /* check if percentile lies within this bin */
176 if (cumul <= floor && ceiling <= cumul + bins[i]) {
177 percentile = p + (double)div/2.0;
184 /* { invariant: `cumul` samples are < `p` } */
187 /* check if percentile lies exactly at the bin boundary */
189 if (floor == cumul && floor == ceiling) {
193 if (percentile >= 0) {
194 if (overhead && pcd->pct == 50) {
195 cycles_stats_measurement_overhead = percentile;
197 cycles_stats_print_percentile(file, pcd->name, percentile, count, overhead, true);
201 p -= cycles_stats_measurement_overhead;
202 fprintf(file,"\t\t%14s = unknown (> %llu)\n", pcd->name, (unsigned long long)p);
206 cycles_stats_print_percentile(file, "max", max, count, overhead, false);
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);
218 for (i=0; i<nbins; ++i) {
220 fprintf(file,"\t\t< %5d: %10lu (%3d%%) %10lu\n",
222 (unsigned long) cumul,
223 (int)((cumul * 100) / count),
224 (unsigned long) bins[i]);
227 fprintf(file,"\t\t>= %5d: %10s (----) %10lu\n",
230 (unsigned long) bins[nbins]);
233 #endif /* defined(ENABLE_CYCLES_STATS) */
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 * ---------------------------------------------------------------------
242 * indent-tabs-mode: t
246 * vim:noexpandtab:sw=4:ts=4: