summaryrefslogtreecommitdiff
path: root/usr.bin/make/stats.c
blob: 3e72a330db3aef95885e996629c82d900e47ce8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* $OpenPackages$ */
/* $OpenBSD: stats.c,v 1.4 2001/06/05 11:59:54 espie Exp $ */

/*
 * Copyright (c) 1999 Marc Espie.
 *
 * Code written for the OpenBSD project.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/* statistics gathering */

/* collection across make invocations is done with an mmap shared file,
   to allow for concurrent adjustment to variables.
 */

#include "config.h"
#include "defines.h"
#include "stats.h"

#ifdef HAS_STATS
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/resource.h>
#include "memory.h"

static void print_stats(void);
void Init_Stats(void);
static float average_runs(unsigned long val);
unsigned long *statarray;

static bool mmapped = false;

static float
average_runs(val)
    unsigned long val;
{
    return (float)val / STAT_INVOCATIONS;
}

static void
print_stats()
{
    struct rusage ru;

    if (getrusage(RUSAGE_SELF, &ru) != -1) {
	STAT_USER_SECONDS += ru.ru_utime.tv_sec;
	STAT_USER_MS += ru.ru_utime.tv_usec;
	if (STAT_USER_MS > 1000000) {
	    STAT_USER_MS -= 1000000;
	    STAT_USER_SECONDS++;
	}
	STAT_SYS_SECONDS += ru.ru_stime.tv_sec;
	STAT_SYS_MS += ru.ru_stime.tv_usec;
	if (STAT_SYS_MS > 1000000) {
	    STAT_SYS_MS -= 1000000;
	    STAT_SYS_SECONDS++;
	}
    }
    fprintf(stderr, "Make runs: %lu\n", STAT_INVOCATIONS);
    fprintf(stderr, "Time user: %lu.%06lu, sys %lu.%06lu\n",
	STAT_USER_SECONDS, STAT_USER_MS,
	STAT_SYS_SECONDS, STAT_SYS_MS);
#ifdef STATS_VAR_LOOKUP
	/* to get the average total of MAXSIZE, we need this value */
    STAT_VAR_POWER +=
	STAT_VAR_HASH_MAXSIZE * STAT_VAR_HASH_CREATION;
    fprintf(stderr, "Var finds: %f, lookups: %f, average: %f, max: %lu\n",
	average_runs(STAT_VAR_FIND),
	average_runs(STAT_VAR_SEARCHES),
	(float)STAT_VAR_COUNT/STAT_VAR_SEARCHES,
	STAT_VAR_MAXCOUNT);
    fprintf(stderr, "Average hash: %f, creation: %f, from env %f\n",
	average_runs(STAT_VAR_HASH_CREATION),
	average_runs(STAT_VAR_CREATION),
	average_runs(STAT_VAR_FROM_ENV));
    fprintf(stderr, "Local hash max: %lu, global hash max: %lu, average local: %f\n",
	STAT_VAR_HASH_MAXSIZE,
	STAT_VAR_GHASH_MAXSIZE,
	(float)STAT_VAR_POWER/STAT_VAR_HASH_CREATION);
#endif
#ifdef STATS_GN_CREATION
    fprintf(stderr, "Average GN: %f\n", average_runs(STAT_GN_COUNT));
#endif
#ifdef STATS_BUF
    fprintf(stderr, "Buf tot: %f, def: %f, exp %f, weird %f, bad %f\n",
	average_runs(STAT_TOTAL_BUFS),
	average_runs(STAT_DEFAULT_BUFS),
	average_runs(STAT_BUFS_EXPANSION),
	average_runs(STAT_WEIRD_BUFS),
	average_runs(STAT_WEIRD_INEFFICIENT));
#endif
#ifdef STATS_HASH
    fprintf(stderr, "Hashes new: %f, exp: %f, lookup %f, l: %f, +: %f, ent : %f\n",
	average_runs(STAT_HASH_CREATION),
	average_runs(STAT_HASH_EXPAND),
	average_runs(STAT_HASH_LOOKUP),
	(float)STAT_HASH_LENGTH/STAT_HASH_LOOKUP,
	(float)STAT_HASH_POSITIVE/STAT_HASH_LOOKUP,
	(float)STAT_HASH_ENTRIES/STAT_HASH_SIZE);
#endif
    if (mmapped)
	munmap(statarray, STAT_NUMBER * sizeof(unsigned long));
}

void
Init_Stats()
{
    char *name;
    int fd;

	/* try to get ahold of a stats collecting file */
    name = getenv("MAKESTATS");
    if (name) {
	while ((fd = open(name, O_RDWR)) == -1) {
		/* if collecting file does not already exist, fill it with
		 * zeros (so all stats starting values should be 0) */
	    unsigned long n;
	    int i;
	    FILE *f;

	    f = fopen(name, "w");

	    n = 0;
	    for (i = 0; i < STAT_NUMBER; i++)
		fwrite(&n, sizeof(unsigned long), 1, f);
	    fclose(f);
	}

    /* either we've got the file -> share it across makes */
	if (fd) {
	    statarray = mmap(0, STAT_NUMBER * sizeof(unsigned long),
		PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	    if (statarray == MAP_FAILED)
		exit(1);
	    mmapped = true;
	}
    } else
    /* or we don't -> simple stats gathering */
	statarray = ecalloc(sizeof(unsigned long), STAT_NUMBER);
    STAT_INVOCATIONS++;
    atexit(print_stats);
}

#endif