Stats.c 12.9 KB
Newer Older
1
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
2
 * $Id: Stats.c,v 1.12 1999/03/04 13:07:48 sof Exp $
3 4
 *
 * (c) The GHC Team, 1998-1999
5 6 7 8 9 10 11 12 13 14
 *
 * Statistics and timing-related functions.
 *
 * ---------------------------------------------------------------------------*/

#define NON_POSIX_SOURCE

#include "Rts.h"
#include "RtsFlags.h"
#include "RtsUtils.h"
15 16
#include "StoragePriv.h"
#include "MBlock.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

/**
 *  Ian: For the moment we just want to ignore
 * these on Nemesis
 **/
#ifdef _NEMESIS_OS_
#ifdef HAVE_SYS_TIMES_H
#undef HAVE_SYS_TIMES_H /* <sys/times.h> */
#endif
#ifdef HAVE_SYS_RESOURCE_H /* <sys/resource.h> */
#undef HAVE_SYS_RESOURCE_H
#endif
#ifdef HAVE_SYS_TIME_H  /* <sys/time.h> */
#undef HAVE_SYS_TIME_H
#endif
#ifdef HAVE_SYS_TIMEB_H 
#undef HAVE_SYS_TIMEB_H /* <sys/timeb.h> */
#endif
#ifdef HAVE_UNISTD_H
#undef HAVE_UNISTD_H    /* <unistd.h> */
#endif
#ifdef HAVE_TIMES
#undef HAVE_TIMES
#endif 
#ifdef HAVE_FTIME
#undef HAVE_FTIME
#endif
#ifdef HAVE_GETRUSAGE
#undef HAVE_GETRUSAGE
#endif
#ifdef HAVE_SYSCONF
#undef HAVE_SYSCONF
#endif
#endif /* _NEMESIS_OS_ */

#include "Stats.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

sof's avatar
sof committed
58
#ifndef __MINGW32__
sof's avatar
sof committed
59 60 61
# ifdef HAVE_SYS_TIMES_H
#  include <sys/times.h>
# endif
62 63 64 65 66 67
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

sof's avatar
sof committed
68 69 70 71 72 73
#ifdef __CYGWIN32__
# ifdef HAVE_TIME_H
#  include <time.h>
# endif
#endif

sof's avatar
sof committed
74
#if ! irix_TARGET_OS && ! defined(__MINGW32__)
sof's avatar
sof committed
75 76 77
# if defined(HAVE_SYS_RESOURCE_H)
#  include <sys/resource.h>
# endif
78 79 80 81 82 83 84 85 86 87
#endif

#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

sof's avatar
sof committed
88 89 90 91
#if HAVE_WINDOWS_H
#include <windows.h>
#endif

92 93 94
/* huh? */
#define BIG_STRING_LEN              512

95 96
static double ElapsedTimeStart = 0.0;
static double TicksPerSecond   = 0.0;
97

98 99
static double InitUserTime = 0.0;
static double InitElapsedTime = 0.0;
100 101

static ullong GC_tot_alloc = 0;
102
static ullong GC_tot_copied = 0;
103

104 105
static double GC_start_time,  GC_tot_time = 0;  /* User GC Time */
static double GCe_start_time, GCe_tot_time = 0; /* Elapsed GC time */
106 107 108 109 110 111

lnat MaxResidency = 0;     /* in words; for stats only */
lnat ResidencySamples = 0; /* for stats only */

static lnat GC_start_faults = 0, GC_end_faults = 0;

112 113
static double *GC_coll_times;

114 115 116 117
/* ToDo: convert this to use integers? --SDM */

/* elapsedtime() -- The current elapsed time in seconds */

sof's avatar
sof committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#ifdef _WIN32
#define NS_PER_SEC 10000000LL
/* Convert FILETIMEs into secs since the Epoch (Jan1-1970) */
#define FT2longlong(ll,ft)    \
    (ll)=(ft).dwHighDateTime; \
    (ll) <<= 32;              \
    (ll) |= (ft).dwLowDateTime; \
    (ll) /= (unsigned long long) (NS_PER_SEC / CLOCKS_PER_SEC)
#endif

#ifdef _WIN32
/* cygwin32 or mingw32 version */
double
elapsedtime(void)
{
    FILETIME creationTime, exitTime, kernelTime, userTime;
    long long int kT, uT;
 
 
    /* ToDo: pin down elapsed times to just the OS thread(s) that
       are evaluating/managing Haskell code.
    */
    if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
		          &exitTime, &kernelTime, &userTime)) {
	/* Probably on a Win95 box..*/
	return 0;
    }

    FT2longlong(kT,kernelTime);
    FT2longlong(uT,userTime);
    return (((StgDouble)(uT + kT))/TicksPerSecond - ElapsedTimeStart);
}

#else 

153
double
154 155
elapsedtime(void)
{
sof's avatar
sof committed
156
# if ! (defined(HAVE_TIMES) || defined(HAVE_FTIME))
157 158 159 160 161 162
    /* We will #ifdef around the fprintf for machines
       we *know* are unsupported. (WDP 94/05)
    */
    fprintf(stderr, "NOTE: `elapsedtime' does nothing!\n");
    return 0.0;

sof's avatar
sof committed
163
# else /* not stumped */
164 165 166 167 168

/* "ftime" may be nicer, but "times" is more standard;
   but, on a Sun, if you do not get the SysV one, you are *hosed*...
 */

sof's avatar
sof committed
169
#  if defined(HAVE_TIMES) && ! sunos4_TARGET_OS
170 171 172
    struct tms t;
    clock_t r = times(&t);

173
    return (((double)r)/TicksPerSecond - ElapsedTimeStart);
174

sof's avatar
sof committed
175
#  else /* HAVE_FTIME */
176 177 178 179 180
    struct timeb t;

    ftime(&t);
    return (fabs(t.time + 1e-3*t.millitm - ElapsedTimeStart));

sof's avatar
sof committed
181 182
#  endif /* HAVE_FTIME */
# endif /* not stumped */
183
}
sof's avatar
sof committed
184 185
#endif /* !_WIN32 */

186 187 188 189

static nat
pagefaults(void)
{
sof's avatar
sof committed
190 191 192
  /* ToDo (on NT): better, get this via the performance data
     that's stored in the registry. */
# if !defined(HAVE_GETRUSAGE) || irix_TARGET_OS || defined(_WIN32)
193 194 195 196 197 198 199 200 201 202 203 204 205 206
    return 0;
# else
    struct rusage t;

    getrusage(RUSAGE_SELF, &t);
    return(t.ru_majflt);
# endif
}

/* ToDo: use gettimeofday on systems that support it (u-sec accuracy) */

void
start_time(void)
{
sof's avatar
sof committed
207
#ifdef HAVE_SYSCONF
208 209
    long ticks;
    /* Determine TicksPerSecond ... */
sof's avatar
sof committed
210

211 212 213 214 215
    ticks = sysconf(_SC_CLK_TCK);
    if ( ticks == -1 ) {
	fprintf(stderr, "stat_init: bad call to 'sysconf'!\n");
    	stg_exit(EXIT_FAILURE);
    }
216
    TicksPerSecond = (double) ticks;
217

sof's avatar
sof committed
218 219 220
/* no "sysconf"; had better guess */
#elif defined(HZ)
    TicksPerSecond = (StgDouble) (HZ);
221

sof's avatar
sof committed
222 223 224
#elif defined(CLOCKS_PER_SEC)
    TicksPerSecond = (StgDouble) (CLOCKS_PER_SEC);
#else /* had better guess wildly */
225 226 227 228 229 230 231 232 233 234 235 236 237 238
    /* We will #ifdef around the fprintf for machines
       we *know* are unsupported. (WDP 94/05)
    */
    fprintf(stderr, "NOTE: Guessing `TicksPerSecond = 60'!\n");
    TicksPerSecond = 60.0;
#endif

    ElapsedTimeStart = elapsedtime();
}


void
initStats(void)
{
239
  nat i;
240 241 242
  FILE *sf = RtsFlags.GcFlags.statsFile;
  
  if (RtsFlags.GcFlags.giveStats) {
243 244
    fprintf(sf, "    Alloc    Collect    Live    GC    GC     TOT     TOT  Page Flts\n");
    fprintf(sf, "    bytes     bytes     bytes  user  elap    user    elap\n");
245
  }
246 247 248 249 250 251
  GC_coll_times = 
    (double *)stgMallocBytes(sizeof(double) * RtsFlags.GcFlags.generations,
			   "initStats");
  for (i = 0; i < RtsFlags.GcFlags.generations; i++) {
    GC_coll_times[i] = 0.0;
  }
252 253
}    

sof's avatar
sof committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#ifdef _WIN32
double
usertime(void)
{
    FILETIME creationTime, exitTime, kernelTime, userTime;
    long long int uT;

    /* Convert FILETIMEs into long longs */

    if (!GetProcessTimes (GetCurrentProcess(), &creationTime,
		          &exitTime, &kernelTime, &userTime)) {
	/* Probably exec'ing this on a Win95 box..*/
	return 0;
    }

    FT2longlong(uT,userTime);
    return (((StgDouble)uT)/TicksPerSecond);
}
#else
273

274
double
275 276
usertime(void)
{
sof's avatar
sof committed
277
# if ! (defined(HAVE_GETRUSAGE) || defined(HAVE_TIMES))
278 279 280 281 282 283
    /* We will #ifdef around the fprintf for machines
       we *know* are unsupported. (WDP 94/05)
    */
    fprintf(stderr, "NOTE: `usertime' does nothing!\n");
    return 0.0;

sof's avatar
sof committed
284
# else /* not stumped */
285

sof's avatar
sof committed
286
#  if defined(HAVE_TIMES) 
287 288 289
    struct tms t;

    times(&t);
290
    return(((double)(t.tms_utime))/TicksPerSecond);
291

sof's avatar
sof committed
292
#  else /* HAVE_GETRUSAGE */
293 294 295 296 297
    struct rusage t;

    getrusage(RUSAGE_SELF, &t);
    return(t.ru_utime.tv_sec + 1e-6*t.ru_utime.tv_usec);

sof's avatar
sof committed
298 299
#  endif /* HAVE_GETRUSAGE */
# endif /* not stumped */
300
}
sof's avatar
sof committed
301
#endif /* ! _WIN32 */
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

void 
end_init(void)
{
  InitUserTime = usertime();
  InitElapsedTime = elapsedtime();
  if (InitElapsedTime < 0.0) {
    InitElapsedTime = 0.0;
  }
}

/* -----------------------------------------------------------------------------
   Called at the beginning of each GC
   -------------------------------------------------------------------------- */

static nat rub_bell = 0;

void
stat_startGC(void)
{
    FILE *sf = RtsFlags.GcFlags.statsFile;

    nat bell = RtsFlags.GcFlags.ringBell;

    if (bell) {
	if (bell > 1) {
	    fprintf(stderr, " GC ");
	    rub_bell = 1;
	} else {
	    fprintf(stderr, "\007");
	}
    }

    if (sf != NULL) {
	GC_start_time = usertime();
	GCe_start_time = elapsedtime();
	if (RtsFlags.GcFlags.giveStats) {
	  GC_start_faults = pagefaults();
	}
    }
}

/* -----------------------------------------------------------------------------
   Called at the end of each GC
   -------------------------------------------------------------------------- */

void
349
stat_endGC(lnat alloc, lnat collect, lnat live, lnat copied, lnat gen)
350 351 352 353
{
    FILE *sf = RtsFlags.GcFlags.statsFile;

    if (sf != NULL) {
354 355
	double time = usertime();
	double etime = elapsedtime();
356 357 358 359

	if (RtsFlags.GcFlags.giveStats) {
	    nat faults = pagefaults();

360 361 362
	    fprintf(sf, "%9ld %9ld %9ld",
		    alloc*sizeof(W_), collect*sizeof(W_), live*sizeof(W_));
	    fprintf(sf, " %5.2f %5.2f %7.2f %7.2f %4ld %4ld  (Gen: %2ld)\n", 
363 364 365 366 367 368
		    (time-GC_start_time), 
		    (etime-GCe_start_time), 
		    time,
		    etime,
		    faults - GC_start_faults,
		    GC_start_faults - GC_end_faults,
369
		    gen);
370 371 372 373 374

	    GC_end_faults = faults;
	    fflush(sf);
	}

375 376
	GC_coll_times[gen] += time-GC_start_time;

377 378 379 380
	GC_tot_copied += (ullong) copied;
	GC_tot_alloc  += (ullong) alloc;
	GC_tot_time   += time-GC_start_time;
	GCe_tot_time  += etime-GCe_start_time;
381 382 383 384 385 386 387

	if (gen == RtsFlags.GcFlags.generations-1) { /* major GC? */
	  if (live > MaxResidency) {
	    MaxResidency = live;
	  }
	  ResidencySamples++;
	}
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    }

    if (rub_bell) {
	fprintf(stderr, "\b\b\b  \b\b\b");
	rub_bell = 0;
    }
}

/* -----------------------------------------------------------------------------
   Called at the end of execution

   NOTE: number of allocations is not entirely accurate: it doesn't
   take into account the few bytes at the end of the heap that
   were left unused when the heap-check failed.
   -------------------------------------------------------------------------- */

void
stat_exit(int alloc)
{
    FILE *sf = RtsFlags.GcFlags.statsFile;

    if (sf != NULL){
	char temp[BIG_STRING_LEN];
411 412 413
	double time = usertime();
	double etime = elapsedtime();
	double MutTime, MutElapsedTime;
414 415 416 417 418 419

	/* avoid divide by zero if time is measured as 0.00 seconds -- SDM */
	if (time  == 0.0)  time = 0.0001;
	if (etime == 0.0) etime = 0.0001;
	

420 421 422 423 424
	fprintf(sf, "%9ld %9.9s %9.9s",
		(lnat)alloc*sizeof(W_), "", "");
	fprintf(sf, " %5.2f %5.2f\n\n", 0.0, 0.0);

	GC_tot_alloc += alloc;
425 426 427

	ullong_format_string(GC_tot_alloc*sizeof(W_), temp, rtsTrue/*commas*/);
	fprintf(sf, "%11s bytes allocated in the heap\n", temp);
428

429 430 431
	ullong_format_string(GC_tot_copied*sizeof(W_), temp, rtsTrue/*commas*/);
	fprintf(sf, "%11s bytes copied during GC\n", temp);

432 433
	if ( ResidencySamples > 0 ) {
	    ullong_format_string(MaxResidency*sizeof(W_), temp, rtsTrue/*commas*/);
434
	    fprintf(sf, "%11s bytes maximum residency (%ld sample(s))\n",
435 436 437
			      temp,
			      ResidencySamples);
	}
438 439 440 441 442
	fprintf(sf,"\n");

	{ /* Count garbage collections */
	  nat g;
	  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
443 444
	    fprintf(sf, "%11d collections in generation %d (%6.2fs)\n", 
		    generations[g].collections, g, GC_coll_times[g]);
445 446 447 448
	  }
	}
	fprintf(sf,"\n%11ld Mb total memory in use\n\n", 
		mblocks_allocated * MBLOCK_SIZE / (1024 * 1024));
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

	MutTime = time - GC_tot_time - InitUserTime;
	if (MutTime < 0) { MutTime = 0; }
	MutElapsedTime = etime - GCe_tot_time - InitElapsedTime;
	if (MutElapsedTime < 0) { MutElapsedTime = 0; }	/* sometimes -0.00 */

	fprintf(sf, "  INIT  time  %6.2fs  (%6.2fs elapsed)\n",
		InitUserTime, InitElapsedTime);
	fprintf(sf, "  MUT   time  %6.2fs  (%6.2fs elapsed)\n",
		MutTime, MutElapsedTime);
	fprintf(sf, "  GC    time  %6.2fs  (%6.2fs elapsed)\n",
		GC_tot_time, GCe_tot_time);
	fprintf(sf, "  Total time  %6.2fs  (%6.2fs elapsed)\n\n",
		time, etime);

	fprintf(sf, "  %%GC time     %5.1f%%  (%.1f%% elapsed)\n\n",
		GC_tot_time*100./time, GCe_tot_time*100./etime);

	if (time - GC_tot_time == 0.0)
		ullong_format_string(0, temp, rtsTrue/*commas*/);
	else
		ullong_format_string((ullong)(GC_tot_alloc*sizeof(W_)/
					      (time - GC_tot_time)),
				     temp, rtsTrue/*commas*/);

	fprintf(sf, "  Alloc rate    %s bytes per MUT second\n\n", temp);

	fprintf(sf, "  Productivity %5.1f%% of total user, %.1f%% of total elapsed\n\n",
		(time - GC_tot_time - InitUserTime) * 100. / time, 
                (time - GC_tot_time - InitUserTime) * 100. / etime);
	fflush(sf);
	fclose(sf);
    }
}
483 484 485 486 487 488 489 490 491

/* -----------------------------------------------------------------------------
   stat_describe_gens

   Produce some detailed info on the state of the generational GC.
   -------------------------------------------------------------------------- */
void
stat_describe_gens(void)
{
492
  nat g, s, mut, mut_once, lge, live;
493 494 495 496
  StgMutClosure *m;
  bdescr *bd;
  step *step;

497
  fprintf(stderr, "     Gen    Steps      Max   Mutable  Mut-Once  Step   Blocks     Live    Large\n                    Blocks  Closures  Closures                         Objects\n");
498 499 500 501 502

  for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
    for (m = generations[g].mut_list, mut = 0; m != END_MUT_LIST; 
	 m = m->mut_link) 
      mut++;
503 504 505
    for (m = generations[g].mut_once_list, mut_once = 0; m != END_MUT_LIST; 
	 m = m->mut_link) 
      mut_once++;
506
    fprintf(stderr, "%8d %8d %8d %9d %9d", g, generations[g].n_steps,
507
	    generations[g].max_blocks, mut, mut_once);
508 509 510 511 512 513

    for (s = 0; s < generations[g].n_steps; s++) {
      step = &generations[g].steps[s];
      for (bd = step->large_objects, lge = 0; bd; bd = bd->link)
	lge++;
      live = 0;
514 515 516 517 518 519
      if (RtsFlags.GcFlags.generations == 1) {
	bd = step->to_space;
      } else {
	bd = step->blocks;
      }
      for (; bd; bd = bd->link) {
520 521 522
	live += (bd->free - bd->start) * sizeof(W_);
      }
      if (s != 0) {
523
	fprintf(stderr,"%46s","");
524 525 526 527 528 529 530
      }
      fprintf(stderr,"%6d %8d %8d %8d\n", s, step->n_blocks,
	      live, lge);
    }
  }
  fprintf(stderr,"\n");
}