1 /* -----------------------------------------------------------------------------
2 * $Id: Stats.c,v 1.17 1999/11/09 15:46:57 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"
25 # ifdef HAVE_SYS_TIMES_H
26 # include <sys/times.h>
30 #ifdef HAVE_SYS_TIME_H
40 #if ! irix_TARGET_OS && ! defined(__MINGW32__)
41 # if defined(HAVE_SYS_RESOURCE_H)
42 # include <sys/resource.h>
46 #ifdef HAVE_SYS_TIMEB_H
47 #include <sys/timeb.h>
59 #define BIG_STRING_LEN 512
61 static double ElapsedTimeStart = 0.0;
62 static double TicksPerSecond = 0.0;
64 static double InitUserTime = 0.0;
65 static double InitElapsedTime = 0.0;
66 static double InitElapsedStamp = 0.0;
68 static double MutUserTime = 0.0;
69 static double MutElapsedTime = 0.0;
70 static double MutElapsedStamp = 0.0;
72 static double ExitUserTime = 0.0;
73 static double ExitElapsedTime = 0.0;
75 static ullong GC_tot_alloc = 0;
76 static ullong GC_tot_copied = 0;
78 static double GC_start_time, GC_tot_time = 0; /* User GC Time */
79 static double GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
81 lnat MaxResidency = 0; /* in words; for stats only */
82 lnat ResidencySamples = 0; /* for stats only */
84 static lnat GC_start_faults = 0, GC_end_faults = 0;
86 static double *GC_coll_times;
88 /* ToDo: convert this to use integers? --SDM */
90 /* elapsedtime() -- The current elapsed time in seconds */
93 #define NS_PER_SEC 10000000LL
94 /* Convert FILETIMEs into secs since the Epoch (Jan1-1970) */
95 #define FT2longlong(ll,ft) \
96 (ll)=(ft).dwHighDateTime; \
98 (ll) |= (ft).dwLowDateTime; \
99 (ll) /= (unsigned long long) (NS_PER_SEC / CLOCKS_PER_SEC)
103 /* cygwin32 or mingw32 version */
107 FILETIME creationTime, exitTime, kernelTime, userTime;
108 long long int kT, uT;
111 /* ToDo: pin down elapsed times to just the OS thread(s) that
112 are evaluating/managing Haskell code.
114 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
115 &exitTime, &kernelTime, &userTime)) {
116 /* Probably on a Win95 box..*/
120 FT2longlong(kT,kernelTime);
121 FT2longlong(uT,userTime);
122 return (((StgDouble)(uT + kT))/TicksPerSecond);
131 # if ! (defined(HAVE_TIMES) || defined(HAVE_FTIME))
132 /* We will #ifdef around the fprintf for machines
133 we *know* are unsupported. (WDP 94/05)
135 fprintf(stderr, "NOTE: `elapsedtime' does nothing!\n");
138 # else /* not stumped */
140 /* "ftime" may be nicer, but "times" is more standard;
141 but, on a Sun, if you do not get the SysV one, you are *hosed*...
144 # if defined(HAVE_TIMES) && ! sunos4_TARGET_OS
146 clock_t r = times(&t);
148 return (((double)r)/TicksPerSecond);
150 # else /* HAVE_FTIME */
154 return (fabs(t.time + 1e-3*t.millitm));
156 # endif /* HAVE_FTIME */
157 # endif /* not stumped */
161 /* mut_user_time_during_GC() and mut_user_time()
163 * This function can be used to get the current mutator time *during*
164 * a GC, i.e. between stat_startGC and stat_endGC. This is used in
165 * the heap profiler for accurately time stamping the heap sample.
168 mut_user_time_during_GC(void)
170 return (GC_start_time - GC_tot_time);
176 return (usertime() - GC_tot_time);
183 /* ToDo (on NT): better, get this via the performance data
184 that's stored in the registry. */
185 # if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(_WIN32)
190 getrusage(RUSAGE_SELF, &t);
195 /* ToDo: use gettimeofday on systems that support it (u-sec accuracy) */
202 /* Determine TicksPerSecond ... */
204 ticks = sysconf(_SC_CLK_TCK);
206 fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
207 stg_exit(EXIT_FAILURE);
209 TicksPerSecond = (double) ticks;
211 /* no "sysconf"; had better guess */
213 TicksPerSecond = (StgDouble) (HZ);
215 #elif defined(CLOCKS_PER_SEC)
216 TicksPerSecond = (StgDouble) (CLOCKS_PER_SEC);
217 #else /* had better guess wildly */
218 /* We will #ifdef around the fprintf for machines
219 we *know* are unsupported. (WDP 94/05)
221 fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
222 TicksPerSecond = 60.0;
225 ElapsedTimeStart = elapsedtime();
233 FILE *sf = RtsFlags.GcFlags.statsFile;
235 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
236 fprintf(sf, " Alloc Collect Live GC GC TOT TOT Page Flts\n");
237 fprintf(sf, " bytes bytes bytes user elap user elap\n");
240 (double *)stgMallocBytes(sizeof(double) * RtsFlags.GcFlags.generations,
242 for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
243 GC_coll_times[i] = 0.0;
251 FILETIME creationTime, exitTime, kernelTime, userTime;
254 /* Convert FILETIMEs into long longs */
256 if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
257 &exitTime, &kernelTime, &userTime)) {
258 /* Probably exec'ing this on a Win95 box..*/
262 FT2longlong(uT,userTime);
263 return (((StgDouble)uT)/TicksPerSecond);
270 # if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
271 /* We will #ifdef around the fprintf for machines
272 we *know* are unsupported. (WDP 94/05)
274 fprintf(stderr, "NOTE: `usertime' does nothing!\n");
277 # else /* not stumped */
279 # if defined(HAVE_TIMES)
283 return(((double)(t.tms_utime))/TicksPerSecond);
285 # else /* HAVE_GETRUSAGE */
288 getrusage(RUSAGE_SELF, &t);
289 return(t.ru_utime.tv_sec + 1e-6*t.ru_utime.tv_usec);
291 # endif /* HAVE_GETRUSAGE */
292 # endif /* not stumped */
294 #endif /* ! _WIN32 */
299 InitUserTime = usertime();
300 InitElapsedStamp = elapsedtime();
301 InitElapsedTime = InitElapsedStamp - ElapsedTimeStart;
302 if (InitElapsedTime < 0.0) {
303 InitElapsedTime = 0.0;
307 /* -----------------------------------------------------------------------------
308 stat_startExit and stat_endExit
310 These two measure the time taken in shutdownHaskell().
311 -------------------------------------------------------------------------- */
316 MutElapsedStamp = elapsedtime();
317 MutElapsedTime = MutElapsedStamp - GCe_tot_time - InitElapsedStamp;
318 if (MutElapsedTime < 0) { MutElapsedTime = 0; } /* sometimes -0.00 */
320 /* for SMP, we don't know the mutator time yet, we have to inspect
321 * all the running threads to find out, and they haven't stopped
322 * yet. So we just timestamp MutUserTime at this point so we can
323 * calculate the EXIT time. The real MutUserTime is calculated
324 * in stat_exit below.
327 MutUserTime = usertime();
329 MutUserTime = usertime() - GC_tot_time - InitUserTime;
337 ExitUserTime = usertime() - MutUserTime;
339 ExitUserTime = usertime() - MutUserTime - GC_tot_time - InitUserTime;
341 ExitElapsedTime = elapsedtime() - MutElapsedStamp;
342 if (ExitUserTime < 0.0) {
345 if (ExitElapsedTime < 0.0) {
346 ExitElapsedTime = 0.0;
350 /* -----------------------------------------------------------------------------
351 Called at the beginning of each GC
352 -------------------------------------------------------------------------- */
354 static nat rub_bell = 0;
359 FILE *sf = RtsFlags.GcFlags.statsFile;
361 nat bell = RtsFlags.GcFlags.ringBell;
365 fprintf(stderr, " GC ");
368 fprintf(stderr, "\007");
373 GC_start_time = usertime();
374 GCe_start_time = elapsedtime();
375 if (RtsFlags.GcFlags.giveStats) {
376 GC_start_faults = pagefaults();
381 /* -----------------------------------------------------------------------------
382 Called at the end of each GC
383 -------------------------------------------------------------------------- */
386 stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
388 FILE *sf = RtsFlags.GcFlags.statsFile;
391 double time = usertime();
392 double etime = elapsedtime();
393 double gc_time = time-GC_start_time;
394 double gc_etime = etime-GCe_start_time;
396 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
397 nat faults = pagefaults();
399 fprintf(sf, "%9ld %9ld %9ld",
400 alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
401 fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld (Gen: %2ld)\n",
406 faults - GC_start_faults,
407 GC_start_faults - GC_end_faults,
410 GC_end_faults = faults;
414 GC_coll_times[gen] += time-GC_start_time;
416 GC_tot_copied += (ullong) copied;
417 GC_tot_alloc += (ullong) alloc;
418 GC_tot_time += gc_time;
419 GCe_tot_time += gc_etime;
424 pthread_t me = pthread_self();
426 for (i = 0; i < RtsFlags.ConcFlags.nNodes; i++) {
427 if (me == task_ids[i].id) {
428 task_ids[i].gc_time += gc_time;
429 task_ids[i].gc_etime += gc_etime;
436 if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
437 if (live > MaxResidency) {
445 fprintf(stderr, "\b\b\b \b\b\b");
450 /* -----------------------------------------------------------------------------
453 Called under SMP when a worker thread finishes. We drop the timing
454 stats for this thread into the task_ids struct for that thread.
455 -------------------------------------------------------------------------- */
459 stat_workerStop(void)
462 pthread_t me = pthread_self();
464 for (i = 0; i < RtsFlags.ConcFlags.nNodes; i++) {
465 if (task_ids[i].id == me) {
466 task_ids[i].mut_time = usertime() - task_ids[i].gc_time;
467 task_ids[i].mut_etime = elapsedtime()
469 - task_ids[i].elapsedtimestart;
470 if (task_ids[i].mut_time < 0.0) { task_ids[i].mut_time = 0.0; }
471 if (task_ids[i].mut_etime < 0.0) { task_ids[i].mut_etime = 0.0; }
477 /* -----------------------------------------------------------------------------
478 Called at the end of execution
480 NOTE: number of allocations is not entirely accurate: it doesn't
481 take into account the few bytes at the end of the heap that
482 were left unused when the heap-check failed.
483 -------------------------------------------------------------------------- */
488 FILE *sf = RtsFlags.GcFlags.statsFile;
491 char temp[BIG_STRING_LEN];
492 double time = usertime();
493 double etime = elapsedtime() - ElapsedTimeStart;
495 /* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
496 if (time == 0.0) time = 0.0001;
497 if (etime == 0.0) etime = 0.0001;
499 if (RtsFlags.GcFlags.giveStats >= VERBOSE_GC_STATS) {
500 fprintf(sf, "%9ld %9.9s %9.9s", (lnat)alloc*sizeof(W_), "", "");
501 fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);
504 GC_tot_alloc += alloc;
506 ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
507 fprintf(sf, "%11s bytes allocated in the heap\n", temp);
509 ullong_format_string(GC_tot_copied*sizeof(W_), temp, rtsTrue/*commas*/);
510 fprintf(sf, "%11s bytes copied during GC\n", temp);
512 if ( ResidencySamples > 0 ) {
513 ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
514 fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
520 { /* Count garbage collections */
522 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
523 fprintf(sf, "%11d collections in generation %d (%6.2fs)\n",
524 generations[g].collections, g, GC_coll_times[g]);
527 fprintf(sf,"\n%11ld Mb total memory in use\n\n",
528 mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
530 /* For SMP, we have to get the user time from each thread
531 * and try to work out the total time.
537 for (i = 0; i < RtsFlags.ConcFlags.nNodes; i++) {
538 MutUserTime += task_ids[i].mut_time;
539 fprintf(sf, " Task %2d: MUT time: %6.2fs (%6.2fs elapsed)\n"
540 " GC time: %6.2fs (%6.2fs elapsed)\n\n",
542 task_ids[i].mut_time, task_ids[i].mut_etime,
543 task_ids[i].gc_time, task_ids[i].gc_etime);
546 time = MutUserTime + GC_tot_time + InitUserTime + ExitUserTime;
547 if (MutUserTime < 0) { MutUserTime = 0; }
550 fprintf(sf, " INIT time %6.2fs (%6.2fs elapsed)\n",
551 InitUserTime, InitElapsedTime);
552 fprintf(sf, " MUT time %6.2fs (%6.2fs elapsed)\n",
553 MutUserTime, MutElapsedTime);
554 fprintf(sf, " GC time %6.2fs (%6.2fs elapsed)\n",
555 GC_tot_time, GCe_tot_time);
556 fprintf(sf, " EXIT time %6.2fs (%6.2fs elapsed)\n",
557 ExitUserTime, ExitElapsedTime);
558 fprintf(sf, " Total time %6.2fs (%6.2fs elapsed)\n\n",
561 fprintf(sf, " %%GC time %5.1f%% (%.1f%% elapsed)\n\n",
562 GC_tot_time*100./time, GCe_tot_time*100./etime);
564 if (time - GC_tot_time == 0.0)
565 ullong_format_string(0, temp, rtsTrue/*commas*/);
567 ullong_format_string((ullong)(GC_tot_alloc*sizeof(W_)/
568 (time - GC_tot_time)),
569 temp, rtsTrue/*commas*/);
571 fprintf(sf, " Alloc rate %s bytes per MUT second\n\n", temp);
573 fprintf(sf, " Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
574 (time - GC_tot_time - InitUserTime) * 100. / time,
575 (time - GC_tot_time - InitUserTime) * 100. / etime);
581 /* -----------------------------------------------------------------------------
584 Produce some detailed info on the state of the generational GC.
585 -------------------------------------------------------------------------- */
587 stat_describe_gens(void)
589 nat g, s, mut, mut_once, lge, live;
594 fprintf(stderr, " Gen Steps Max Mutable Mut-Once Step Blocks Live Large\n Blocks Closures Closures Objects\n");
596 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
597 for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST;
600 for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST;
603 fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
604 generations[g].max_blocks, mut, mut_once);
606 for (s = 0; s < generations[g].n_steps; s++) {
607 step = &generations[g].steps[s];
608 for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
611 if (RtsFlags.GcFlags.generations == 1) {
616 for (; bd; bd = bd->link) {
617 live += (bd->free - bd->start) * sizeof(W_);
620 fprintf(stderr,"%46s","");
622 fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
626 fprintf(stderr,"\n");