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