* src/vm/cycles-stats.c (cycles_stats_print): Made count, floor, and ceiling
[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
38 #if defined(ENABLE_CYCLES_STATS)
39
40 #include <stdio.h>
41 #include <assert.h>
42 #include "vm/cycles-stats.h"
43
44 struct cycles_stats_percentile {
45         int         pct;
46         const char *name;
47 };
48
49 static struct cycles_stats_percentile cycles_stats_percentile_defs[] = {
50         { 10, "10%-percentile" },
51         { 50, "median"         },
52         { 90, "90%-percentile" },
53         { 99, "99%-percentile" },
54         {  0, NULL             } /* sentinel */
55 };
56
57 void cycles_stats_print(FILE *file,
58                                                 const char *name, int nbins, int div,
59                                                 u4 *bins, u8 count, u8 min, u8 max)
60 {
61         s4 i;
62                 struct cycles_stats_percentile *pcd;
63                 u8 floor, ceiling;
64                 u8 p;
65                 u8 cumul;
66                 double percentile;
67
68         fprintf(file,"\t%s: %u calls\n",
69                 name, count);
70                 
71         fprintf(file,"\t%s cycles distribution:\n", name);
72                 
73         fprintf(file,"\t\t%20s = %llu\n", "min", (unsigned long long)min);
74
75                 pcd = cycles_stats_percentile_defs;
76                 for (; pcd->name; pcd++) {
77                         floor   = (count * pcd->pct) / 100;
78                         ceiling = (count * pcd->pct + 99) / 100;
79                         cumul = 0;
80                         p = 0;
81                         percentile = -1.0;
82
83                         assert( ceiling <= floor + 1 );
84
85                         for (i=0; i<nbins; ++i) {
86
87                                 /* { invariant: `cumul` samples are < `p` } */
88
89                                 /* check if percentile lies exactly at the bin boundary */
90                                 
91                                 if (floor == cumul && floor == ceiling) {
92                                         percentile = p;
93                                         break;
94                                 }
95
96                                 /* check if percentile lies within this bin */
97
98                                 if (cumul <= floor && ceiling <= cumul + bins[i]) {
99                                         percentile = p + (double)div/2.0;
100                                         break;
101                                 }
102                                 
103                                 cumul += bins[i];
104                                 p     += div;
105
106                                 /* { invariant: `cumul` samples are < `p` } */
107                         }
108                         
109                         /* check if percentile lies exactly at the bin boundary */
110
111                         if (floor == cumul && floor == ceiling) {
112                                 percentile = p;
113                         }
114
115                         if (percentile >= 0) {
116                                 fprintf(file,"\t\t%20s = %.1f\n", pcd->name, percentile);
117                         }
118                         else {
119                                 fprintf(file,"\t\t%20s = unknown (> %llu)\n", pcd->name, (unsigned long long)p);
120                         }
121                 }
122                 
123         fprintf(file,"\t\t%20s = %llu\n", "max", (unsigned long long)max);
124                 
125                 cumul = 0;
126         for (i=0; i<nbins; ++i) {
127                         cumul += bins[i];
128             fprintf(file,"\t\t<  %5d: %10lu (%3d%%) %10lu\n",
129                     (int)((i+1) * div),
130                                         (unsigned long) cumul,
131                                         (int)((cumul * 100) / count),
132                     (unsigned long) bins[i]);
133         }
134                 
135         fprintf(file,"\t\t>= %5d: %10s (----) %10lu\n",
136                 (int)(nbins * div),
137                                 "OVER",
138                 (unsigned long) bins[nbins]);
139 }
140
141 #endif /* defined(ENABLE_CYCLES_STATS) */
142
143 /*
144  * These are local overrides for various environment variables in Emacs.
145  * Please do not remove this and leave it at the end of the file, where
146  * Emacs will automagically detect them.
147  * ---------------------------------------------------------------------
148  * Local variables:
149  * mode: c
150  * indent-tabs-mode: t
151  * c-basic-offset: 4
152  * tab-width: 4
153  * End:
154  * vim:noexpandtab:sw=4:ts=4:
155  */