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