1 /* $OpenPackages$ */ 2 /* $OpenBSD: stats.c,v 1.8 2004/05/05 09:10:47 espie Exp $ */ 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 "config.h" 39 #include "defines.h" 40 #include "stats.h" 41 42 #ifdef HAS_STATS 43 #include <fcntl.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <sys/time.h> 47 #include <sys/mman.h> 48 #include <sys/types.h> 49 #include <sys/resource.h> 50 #include "memory.h" 51 52 static void print_stats(void); 53 void Init_Stats(void); 54 static float average_runs(unsigned long val); 55 unsigned long *statarray; 56 57 static bool mmapped = false; 58 59 static float 60 average_runs(unsigned long val) 61 { 62 return (float)val / STAT_INVOCATIONS; 63 } 64 65 static void 66 print_stats(void) 67 { 68 struct rusage ru; 69 70 if (getrusage(RUSAGE_SELF, &ru) != -1) { 71 STAT_USER_SECONDS += ru.ru_utime.tv_sec; 72 STAT_USER_MS += ru.ru_utime.tv_usec; 73 if (STAT_USER_MS > 1000000) { 74 STAT_USER_MS -= 1000000; 75 STAT_USER_SECONDS++; 76 } 77 STAT_SYS_SECONDS += ru.ru_stime.tv_sec; 78 STAT_SYS_MS += ru.ru_stime.tv_usec; 79 if (STAT_SYS_MS > 1000000) { 80 STAT_SYS_MS -= 1000000; 81 STAT_SYS_SECONDS++; 82 } 83 } 84 fprintf(stderr, "Make runs: %lu\n", STAT_INVOCATIONS); 85 fprintf(stderr, "Time user: %lu.%06lu, sys %lu.%06lu\n", 86 STAT_USER_SECONDS, STAT_USER_MS, 87 STAT_SYS_SECONDS, STAT_SYS_MS); 88 #ifdef STATS_VAR_LOOKUP 89 /* to get the average total of MAXSIZE, we need this value */ 90 STAT_VAR_POWER += 91 STAT_VAR_HASH_MAXSIZE * STAT_VAR_HASH_CREATION; 92 fprintf(stderr, "Var finds: %f, lookups: %f, average: %f, max: %lu\n", 93 average_runs(STAT_VAR_FIND), 94 average_runs(STAT_VAR_SEARCHES), 95 (float)STAT_VAR_COUNT/STAT_VAR_SEARCHES, 96 STAT_VAR_MAXCOUNT); 97 fprintf(stderr, "Average hash: %f, creation: %f, from env %f\n", 98 average_runs(STAT_VAR_HASH_CREATION), 99 average_runs(STAT_VAR_CREATION), 100 average_runs(STAT_VAR_FROM_ENV)); 101 fprintf(stderr, "Local hash max: %lu, global hash max: %lu, average local: %f\n", 102 STAT_VAR_HASH_MAXSIZE, 103 STAT_VAR_GHASH_MAXSIZE, 104 (float)STAT_VAR_POWER/STAT_VAR_HASH_CREATION); 105 #endif 106 #ifdef STATS_GN_CREATION 107 fprintf(stderr, "Average GN: %f\n", average_runs(STAT_GN_COUNT)); 108 #endif 109 #ifdef STATS_SUFF 110 fprintf(stderr, "Average Suffix lookup: %f, transforms: %f\n", 111 average_runs(STAT_SUFF_LOOKUP_NAME), 112 average_runs(STAT_TRANSFORM_LOOKUP_NAME)); 113 #endif 114 #ifdef STATS_BUF 115 fprintf(stderr, "Buf tot: %f, def: %f, exp %f, weird %f, bad %f\n", 116 average_runs(STAT_TOTAL_BUFS), 117 average_runs(STAT_DEFAULT_BUFS), 118 average_runs(STAT_BUFS_EXPANSION), 119 average_runs(STAT_WEIRD_BUFS), 120 average_runs(STAT_WEIRD_INEFFICIENT)); 121 #endif 122 #ifdef STATS_HASH 123 fprintf(stderr, "Hashes new: %f, exp: %f, lookup %f, l: %f, +: %f, ent : %f\n", 124 average_runs(STAT_HASH_CREATION), 125 average_runs(STAT_HASH_EXPAND), 126 average_runs(STAT_HASH_LOOKUP), 127 (float)STAT_HASH_LENGTH/STAT_HASH_LOOKUP, 128 (float)STAT_HASH_POSITIVE/STAT_HASH_LOOKUP, 129 (float)STAT_HASH_ENTRIES/STAT_HASH_SIZE); 130 #endif 131 #ifdef STATS_GROW 132 fprintf(stderr, "Grow: %f\n", average_runs(STAT_GROWARRAY)); 133 #endif 134 if (mmapped) 135 munmap(statarray, STAT_NUMBER * sizeof(unsigned long)); 136 } 137 138 void 139 Init_Stats(void) 140 { 141 char *name; 142 int fd; 143 144 /* try to get ahold of a stats collecting file */ 145 name = getenv("MAKESTATS"); 146 if (name) { 147 while ((fd = open(name, O_RDWR)) == -1) { 148 /* if collecting file does not already exist, fill it with 149 * zeros (so all stats starting values should be 0) */ 150 unsigned long n; 151 int i; 152 FILE *f; 153 154 f = fopen(name, "w"); 155 156 n = 0; 157 for (i = 0; i < STAT_NUMBER; i++) 158 fwrite(&n, sizeof(unsigned long), 1, f); 159 fclose(f); 160 } 161 162 /* either we've got the file -> share it across makes */ 163 if (fd) { 164 statarray = mmap(0, STAT_NUMBER * sizeof(unsigned long), 165 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 166 if (statarray == MAP_FAILED) 167 exit(1); 168 mmapped = true; 169 } 170 } else 171 /* or we don't -> simple stats gathering */ 172 statarray = ecalloc(STAT_NUMBER, sizeof(unsigned long)); 173 STAT_INVOCATIONS++; 174 atexit(print_stats); 175 } 176 177 #endif 178 179