Annotation of src/usr.bin/make/stats.c, Revision 1.1
1.1 ! espie 1: /* $OpenPackages$ */
! 2: /* $OpenBSD$ */
! 3:
! 4: /*
! 5: * Copyright (c) 1999 Marc Espie.
! 6: *
! 7: * Code written for the OpenBSD project.
! 8: *
! 9: * Redistribution and use in source and binary forms, with or without
! 10: * modification, are permitted provided that the following conditions
! 11: * are met:
! 12: * 1. Redistributions of source code must retain the above copyright
! 13: * notice, this list of conditions and the following disclaimer.
! 14: * 2. Redistributions in binary form must reproduce the above copyright
! 15: * notice, this list of conditions and the following disclaimer in the
! 16: * documentation and/or other materials provided with the distribution.
! 17: *
! 18: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
! 19: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! 20: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
! 21: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
! 22: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! 23: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
! 24: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! 25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
! 26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! 27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
! 28: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 29: */
! 30:
! 31:
! 32: /* statistics gathering */
! 33:
! 34: /* collection across make invocations is done with an mmap shared file,
! 35: to allow for concurrent adjustment to variables.
! 36: */
! 37:
! 38: #include "make.h"
! 39: #include "stats.h"
! 40:
! 41: #ifdef HAS_STATS
! 42: #include <stdio.h>
! 43: #include <fcntl.h>
! 44: #include <sys/mman.h>
! 45: #include <sys/types.h>
! 46: #include <sys/resource.h>
! 47:
! 48: static void print_stats(void);
! 49: void Init_Stats(void);
! 50: static float average_runs(unsigned long val);
! 51: unsigned long *statarray;
! 52:
! 53: static Boolean mmapped = FALSE;
! 54:
! 55: static float
! 56: average_runs(val)
! 57: unsigned long val;
! 58: {
! 59: return (float)val / STAT_INVOCATIONS;
! 60: }
! 61:
! 62: static void
! 63: print_stats()
! 64: {
! 65: struct rusage ru;
! 66:
! 67: if (getrusage(RUSAGE_SELF, &ru) != -1) {
! 68: STAT_USER_SECONDS += ru.ru_utime.tv_sec;
! 69: STAT_USER_MS += ru.ru_utime.tv_usec;
! 70: if (STAT_USER_MS > 1000000) {
! 71: STAT_USER_MS -= 1000000;
! 72: STAT_USER_SECONDS++;
! 73: }
! 74: STAT_SYS_SECONDS += ru.ru_stime.tv_sec;
! 75: STAT_SYS_MS += ru.ru_stime.tv_usec;
! 76: if (STAT_SYS_MS > 1000000) {
! 77: STAT_SYS_MS -= 1000000;
! 78: STAT_SYS_SECONDS++;
! 79: }
! 80: }
! 81: fprintf(stderr, "Make runs: %lu\n", STAT_INVOCATIONS);
! 82: fprintf(stderr, "Time user: %lu.%06lu, sys %lu.%06lu\n",
! 83: STAT_USER_SECONDS, STAT_USER_MS,
! 84: STAT_SYS_SECONDS, STAT_SYS_MS);
! 85: #ifdef STATS_VAR_LOOKUP
! 86: /* to get the average total of MAXSIZE, we need this value */
! 87: STAT_VAR_POWER +=
! 88: STAT_VAR_HASH_MAXSIZE * STAT_VAR_HASH_CREATION;
! 89: fprintf(stderr, "Var finds: %f, lookups: %f, average: %f, max: %lu\n",
! 90: average_runs(STAT_VAR_FIND),
! 91: average_runs(STAT_VAR_SEARCHES),
! 92: (float)STAT_VAR_COUNT/STAT_VAR_SEARCHES,
! 93: STAT_VAR_MAXCOUNT);
! 94: fprintf(stderr, "Average hash: %f, creation: %f, from env %f\n",
! 95: average_runs(STAT_VAR_HASH_CREATION),
! 96: average_runs(STAT_VAR_CREATION),
! 97: average_runs(STAT_VAR_FROM_ENV));
! 98: fprintf(stderr, "Local hash max: %lu, global hash max: %lu, average local: %f\n",
! 99: STAT_VAR_HASH_MAXSIZE,
! 100: STAT_VAR_GHASH_MAXSIZE,
! 101: (float)STAT_VAR_POWER/STAT_VAR_HASH_CREATION);
! 102: #endif
! 103: #ifdef STATS_GN_CREATION
! 104: fprintf(stderr, "Average GN: %f\n", average_runs(STAT_GN_COUNT));
! 105: #endif
! 106: #ifdef STATS_BUF
! 107: fprintf(stderr, "Buf tot: %f, def: %f, exp %f, weird %f, bad %f\n",
! 108: average_runs(STAT_TOTAL_BUFS),
! 109: average_runs(STAT_DEFAULT_BUFS),
! 110: average_runs(STAT_BUFS_EXPANSION),
! 111: average_runs(STAT_WEIRD_BUFS),
! 112: average_runs(STAT_WEIRD_INEFFICIENT));
! 113: #endif
! 114: #ifdef STATS_HASH
! 115: fprintf(stderr, "Hashes new: %f, exp: %f, lookup %f, l: %f, +: %f, ent : %f\n",
! 116: average_runs(STAT_HASH_CREATION),
! 117: average_runs(STAT_HASH_EXPAND),
! 118: average_runs(STAT_HASH_LOOKUP),
! 119: (float)STAT_HASH_LENGTH/STAT_HASH_LOOKUP,
! 120: (float)STAT_HASH_POSITIVE/STAT_HASH_LOOKUP,
! 121: (float)STAT_HASH_ENTRIES/STAT_HASH_SIZE);
! 122: #endif
! 123: if (mmapped)
! 124: munmap(statarray, STAT_NUMBER * sizeof(unsigned long));
! 125: }
! 126:
! 127: void
! 128: Init_Stats()
! 129: {
! 130: char *name;
! 131: int fd;
! 132:
! 133: /* try to get ahold of a stats collecting file */
! 134: name = getenv("MAKESTATS");
! 135: if (name) {
! 136: while ((fd = open(name, O_RDWR)) == -1) {
! 137: /* if collecting file does not already exist, fill it with
! 138: * zeros (so all stats starting values should be 0) */
! 139: unsigned long n;
! 140: int i;
! 141: FILE *f;
! 142:
! 143: f = fopen(name, "w");
! 144:
! 145: n = 0;
! 146: for (i = 0; i < STAT_NUMBER; i++)
! 147: fwrite(&n, sizeof(unsigned long), 1, f);
! 148: fclose(f);
! 149: }
! 150:
! 151: /* either we've got the file -> share it across makes */
! 152: if (fd) {
! 153: statarray = mmap(0, STAT_NUMBER * sizeof(unsigned long),
! 154: PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
! 155: if (statarray == MAP_FAILED)
! 156: exit(1);
! 157: mmapped = TRUE;
! 158: }
! 159: } else
! 160: /* or we don't -> simple stats gathering */
! 161: statarray = emalloc(sizeof(unsigned long) * STAT_NUMBER);
! 162: STAT_INVOCATIONS++;
! 163: atexit(print_stats);
! 164: }
! 165:
! 166: #endif
! 167: