1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.13 1999/05/20 10:23:43 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * Statistics and timing-related functions.
8 * ---------------------------------------------------------------------------*/
10 #define NON_POSIX_SOURCE
15 #include "StoragePriv.h"
19 * Ian: For the moment we just want to ignore
23 #ifdef HAVE_SYS_TIMES_H
24 #undef HAVE_SYS_TIMES_H /* <sys/times.h> */
26 #ifdef HAVE_SYS_RESOURCE_H /* <sys/resource.h> */
27 #undef HAVE_SYS_RESOURCE_H
29 #ifdef HAVE_SYS_TIME_H /* <sys/time.h> */
30 #undef HAVE_SYS_TIME_H
32 #ifdef HAVE_SYS_TIMEB_H
33 #undef HAVE_SYS_TIMEB_H /* <sys/timeb.h> */
36 #undef HAVE_UNISTD_H /* <unistd.h> */
50 #endif /* _NEMESIS_OS_ */
59 # ifdef HAVE_SYS_TIMES_H
60 # include <sys/times.h>
64 #ifdef HAVE_SYS_TIME_H
74 #if ! irix_TARGET_OS && ! defined(__MINGW32__)
75 # if defined(HAVE_SYS_RESOURCE_H)
76 # include <sys/resource.h>
80 #ifdef HAVE_SYS_TIMEB_H
81 #include <sys/timeb.h>
93 #define BIG_STRING_LEN 512
95 static double ElapsedTimeStart = 0.0;
96 static double TicksPerSecond = 0.0;
98 static double InitUserTime = 0.0;
99 static double InitElapsedTime = 0.0;
101 static ullong GC_tot_alloc = 0;
102 static ullong GC_tot_copied = 0;
104 static double GC_start_time, GC_tot_time = 0; /* User GC Time */
105 static double GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
107 lnat MaxResidency = 0; /* in words; for stats only */
108 lnat ResidencySamples = 0; /* for stats only */
110 static lnat GC_start_faults = 0, GC_end_faults = 0;
112 static double *GC_coll_times;
114 /* ToDo: convert this to use integers? --SDM */
116 /* elapsedtime() -- The current elapsed time in seconds */
119 #define NS_PER_SEC 10000000LL
120 /* Convert FILETIMEs into secs since the Epoch (Jan1-1970) */
121 #define FT2longlong(ll,ft) \
122 (ll)=(ft).dwHighDateTime; \
124 (ll) |= (ft).dwLowDateTime; \
125 (ll) /= (unsigned long long) (NS_PER_SEC / CLOCKS_PER_SEC)
129 /* cygwin32 or mingw32 version */
133 FILETIME creationTime, exitTime, kernelTime, userTime;
134 long long int kT, uT;
137 /* ToDo: pin down elapsed times to just the OS thread(s) that
138 are evaluating/managing Haskell code.
140 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
141 &exitTime, &kernelTime, &userTime)) {
142 /* Probably on a Win95 box..*/
146 FT2longlong(kT,kernelTime);
147 FT2longlong(uT,userTime);
148 return (((StgDouble)(uT + kT))/TicksPerSecond - ElapsedTimeStart);
156 # if ! (defined(HAVE_TIMES) || defined(HAVE_FTIME))
157 /* We will #ifdef around the fprintf for machines
158 we *know* are unsupported. (WDP 94/05)
160 fprintf(stderr, "NOTE: `elapsedtime' does nothing!\n");
163 # else /* not stumped */
165 /* "ftime" may be nicer, but "times" is more standard;
166 but, on a Sun, if you do not get the SysV one, you are *hosed*...
169 # if defined(HAVE_TIMES) && ! sunos4_TARGET_OS
171 clock_t r = times(&t);
173 return (((double)r)/TicksPerSecond - ElapsedTimeStart);
175 # else /* HAVE_FTIME */
179 return (fabs(t.time + 1e-3*t.millitm - ElapsedTimeStart));
181 # endif /* HAVE_FTIME */
182 # endif /* not stumped */
190 /* ToDo (on NT): better, get this via the performance data
191 that's stored in the registry. */
192 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(_WIN32)
197 getrusage(RUSAGE_SELF, &t);
202 /* ToDo: use gettimeofday on systems that support it (u-sec accuracy) */
209 /* Determine TicksPerSecond ... */
211 ticks = sysconf(_SC_CLK_TCK);
213 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
214 stg_exit(EXIT_FAILURE);
216 TicksPerSecond = (double) ticks;
218 /* no "sysconf"; had better guess */
220 TicksPerSecond = (StgDouble) (HZ);
222 #elif defined(CLOCKS_PER_SEC)
223 TicksPerSecond = (StgDouble) (CLOCKS_PER_SEC);
224 #else /* had better guess wildly */
225 /* We will #ifdef around the fprintf for machines
226 we *know* are unsupported. (WDP 94/05)
228 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
229 TicksPerSecond = 60.0;
232 ElapsedTimeStart = elapsedtime();
240 FILE *sf = RtsFlags.GcFlags.statsFile;
242 if (RtsFlags.GcFlags.giveStats) {
243 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
244 fprintf(sf, " bytes bytes bytes user elap user elap\n");
247 (double *)stgMallocBytes(sizeof(double) * RtsFlags.GcFlags.generations,
249 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
250 GC_coll_times[i] = 0.0;
258 FILETIME creationTime, exitTime, kernelTime, userTime;
261 /* Convert FILETIMEs into long longs */
263 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
264 &exitTime, &kernelTime, &userTime)) {
265 /* Probably exec'ing this on a Win95 box..*/
269 FT2longlong(uT,userTime);
270 return (((StgDouble)uT)/TicksPerSecond);
277 # if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
278 /* We will #ifdef around the fprintf for machines
279 we *know* are unsupported. (WDP 94/05)
281 fprintf(stderr, "NOTE: `usertime' does nothing!\n");
284 # else /* not stumped */
286 # if defined(HAVE_TIMES)
290 return(((double)(t.tms_utime))/TicksPerSecond);
292 # else /* HAVE_GETRUSAGE */
295 getrusage(RUSAGE_SELF, &t);
296 return(t.ru_utime.tv_sec + 1e-6*t.ru_utime.tv_usec);
298 # endif /* HAVE_GETRUSAGE */
299 # endif /* not stumped */
301 #endif /* ! _WIN32 */
306 InitUserTime = usertime();
307 InitElapsedTime = elapsedtime();
308 if (InitElapsedTime < 0.0) {
309 InitElapsedTime = 0.0;
313 /* -----------------------------------------------------------------------------
314 Called at the beginning of each GC
315 -------------------------------------------------------------------------- */
317 static nat rub_bell = 0;
322 FILE *sf = RtsFlags.GcFlags.statsFile;
324 nat bell = RtsFlags.GcFlags.ringBell;
328 fprintf(stderr, " GC ");
331 fprintf(stderr, "\007");
336 GC_start_time = usertime();
337 GCe_start_time = elapsedtime();
338 if (RtsFlags.GcFlags.giveStats) {
339 GC_start_faults = pagefaults();
344 /* -----------------------------------------------------------------------------
345 Called at the end of each GC
346 -------------------------------------------------------------------------- */
349 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
351 FILE *sf = RtsFlags.GcFlags.statsFile;
354 double time = usertime();
355 double etime = elapsedtime();
357 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
358 nat faults = pagefaults();
360 fprintf(sf, "%9ld %9ld %9ld",
361 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
362 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
363 (time-GC_start_time),
364 (etime-GCe_start_time),
367 faults - GC_start_faults,
368 GC_start_faults - GC_end_faults,
371 GC_end_faults = faults;
375 GC_coll_times[gen] += time-GC_start_time;
377 GC_tot_copied += (ullong) copied;
378 GC_tot_alloc += (ullong) alloc;
379 GC_tot_time += time-GC_start_time;
380 GCe_tot_time += etime-GCe_start_time;
382 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
383 if (live > MaxResidency) {
391 fprintf(stderr, "\b\b\b \b\b\b");
396 /* -----------------------------------------------------------------------------
397 Called at the end of execution
399 NOTE: number of allocations is not entirely accurate: it doesn't
400 take into account the few bytes at the end of the heap that
401 were left unused when the heap-check failed.
402 -------------------------------------------------------------------------- */
407 FILE *sf = RtsFlags.GcFlags.statsFile;
410 char temp[BIG_STRING_LEN];
411 double time = usertime();
412 double etime = elapsedtime();
413 double MutTime, MutElapsedTime;
415 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
416 if (time == 0.0) time = 0.0001;
417 if (etime == 0.0) etime = 0.0001;
420 fprintf(sf, "%9ld %9.9s %9.9s",
421 (lnat)alloc*sizeof(W_), "", "");
422 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
424 GC_tot_alloc += alloc;
426 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
427 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
429 ullong_format_string(GC_tot_copied*sizeof(W_), temp, rtsTrue/*commas*/);
430 fprintf(sf, "%11s bytes copied during GC\n", temp);
432 if ( ResidencySamples > 0 ) {
433 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
434 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
440 { /* Count garbage collections */
442 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
443 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
444 generations[g].collections, g, GC_coll_times[g]);
447 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
448 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
450 MutTime = time - GC_tot_time - InitUserTime;
451 if (MutTime < 0) { MutTime = 0; }
452 MutElapsedTime = etime - GCe_tot_time - InitElapsedTime;
453 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
455 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
456 InitUserTime, InitElapsedTime);
457 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
458 MutTime, MutElapsedTime);
459 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
460 GC_tot_time, GCe_tot_time);
461 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
464 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
465 GC_tot_time*100./time, GCe_tot_time*100./etime);
467 if (time - GC_tot_time == 0.0)
468 ullong_format_string(0, temp, rtsTrue/*commas*/);
470 ullong_format_string((ullong)(GC_tot_alloc*sizeof(W_)/
471 (time - GC_tot_time)),
472 temp, rtsTrue/*commas*/);
474 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
476 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
477 (time - GC_tot_time - InitUserTime) * 100. / time,
478 (time - GC_tot_time - InitUserTime) * 100. / etime);
484 /* -----------------------------------------------------------------------------
487 Produce some detailed info on the state of the generational GC.
488 -------------------------------------------------------------------------- */
490 stat_describe_gens(void)
492 nat g, s, mut, mut_once, lge, live;
497 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
499 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
500 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
503 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
506 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
507 generations[g].max_blocks, mut, mut_once);
509 for (s = 0; s < generations[g].n_steps; s++) {
510 step = &generations[g].steps[s];
511 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
514 if (RtsFlags.GcFlags.generations == 1) {
519 for (; bd; bd = bd->link) {
520 live += (bd->free - bd->start) * sizeof(W_);
523 fprintf(stderr,"%46s","");
525 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
529 fprintf(stderr,"\n");