annotate lib/swig/swigwin-2.0.11/CCache/stats.c @ 2562:b8a56afc6ba1

new var no_actors, lights_flag, debug_lights, StationaryLights and MobileLights
author Ritor1
date Sun, 17 May 2015 22:42:13 +0600
parents b3009adc0e2f
children
rev   line source
1899
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
1 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
2 Copyright (C) Andrew Tridgell 2002
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
3
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
6 the Free Software Foundation; either version 2 of the License, or
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
7 (at your option) any later version.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
8
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
12 GNU General Public License for more details.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
13
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
15 along with this program; if not, write to the Free Software
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
17 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
18 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
19 routines to handle the stats files
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
20
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
21 the stats file is stored one per cache subdirectory to make this more
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
22 scalable
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
23 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
24
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
25 #include "ccache.h"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
26
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
27 extern char *stats_file;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
28 extern char *cache_dir;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
29
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
30 #define STATS_VERSION 1
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
31
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
32 #define FLAG_NOZERO 1 /* don't zero with the -z option */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
33 #define FLAG_ALWAYS 2 /* always show, even if zero */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
34
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
35 static struct {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
36 enum stats stat;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
37 char *message;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
38 void (*fn)(unsigned );
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
39 unsigned flags;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
40 } stats_info[] = {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
41 { STATS_CACHED, "cache hit ", NULL, FLAG_ALWAYS },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
42 { STATS_TOCACHE, "cache miss ", NULL, FLAG_ALWAYS },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
43 { STATS_LINK, "called for link ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
44 { STATS_MULTIPLE, "multiple source files ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
45 { STATS_STDOUT, "compiler produced stdout ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
46 { STATS_STATUS, "compile failed ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
47 { STATS_ERROR, "ccache internal error ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
48 { STATS_PREPROCESSOR, "preprocessor error ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
49 { STATS_COMPILER, "couldn't find the compiler ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
50 { STATS_MISSING, "cache file missing ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
51 { STATS_ARGS, "bad compiler arguments ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
52 { STATS_NOTC, "not a C/C++ file ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
53 { STATS_CONFTEST, "autoconf compile/link ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
54 { STATS_UNSUPPORTED, "unsupported compiler option ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
55 { STATS_OUTSTDOUT, "output to stdout ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
56 { STATS_DEVICE, "output to a non-regular file ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
57 { STATS_NOINPUT, "no input file ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
58 { STATS_ENVIRONMMENT, "error due to bad env variable ", NULL, 0 },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
59 { STATS_NUMFILES, "files in cache ", NULL, FLAG_NOZERO|FLAG_ALWAYS },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
60 { STATS_TOTALSIZE, "cache size ", display_size , FLAG_NOZERO|FLAG_ALWAYS },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
61 { STATS_MAXFILES, "max files ", NULL, FLAG_NOZERO },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
62 { STATS_MAXSIZE, "max cache size ", display_size, FLAG_NOZERO },
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
63 { STATS_NONE, NULL, NULL, 0 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
64 };
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
65
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
66 /* parse a stats file from a buffer - adding to the counters */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
67 static void parse_stats(unsigned counters[STATS_END], char *buf)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
68 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
69 int i;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
70 char *p, *p2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
71
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
72 p = buf;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
73 for (i=0;i<STATS_END;i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
74 counters[i] += strtol(p, &p2, 10);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
75 if (!p2 || p2 == p) break;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
76 p = p2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
77 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
78 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
79
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
80 /* write out a stats file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
81 static void write_stats(int fd, unsigned counters[STATS_END])
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
82 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
83 int i;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
84 int len = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
85 char buf[1024];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
86
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
87 for (i=0;i<STATS_END;i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
88 len += snprintf(buf+len, sizeof(buf)-(len+1), "%u ", counters[i]);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
89 if (len >= (int)sizeof(buf)-1) fatal("stats too long?!");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
90 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
91 len += snprintf(buf+len, sizeof(buf)-(len+1), "\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
92 if (len >= (int)sizeof(buf)-1) fatal("stats too long?!");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
93
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
94 lseek(fd, 0, SEEK_SET);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
95 if (write(fd, buf, len) == -1) fatal("could not write stats");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
96 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
97
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
98
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
99 /* fill in some default stats values */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
100 static void stats_default(unsigned counters[STATS_END])
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
101 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
102 counters[STATS_MAXSIZE] += DEFAULT_MAXSIZE / 16;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
103 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
104
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
105 /* read in the stats from one dir and add to the counters */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
106 static void stats_read_fd(int fd, unsigned counters[STATS_END])
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
107 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
108 char buf[1024];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
109 int len;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
110 len = read(fd, buf, sizeof(buf)-1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
111 if (len <= 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
112 stats_default(counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
113 return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
114 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
115 buf[len] = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
116 parse_stats(counters, buf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
117 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
118
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
119 /* update the stats counter for this compile */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
120 static void stats_update_size(enum stats stat, size_t size, size_t numfiles)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
121 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
122 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
123 unsigned counters[STATS_END];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
124 int need_cleanup = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
125
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
126 if (getenv("CCACHE_NOSTATS")) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
127
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
128 if (!stats_file) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
129 if (!cache_dir) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
130 x_asprintf(&stats_file, "%s/stats", cache_dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
131 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
132
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
133 /* open safely to try to prevent symlink races */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
134 fd = safe_open(stats_file);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
135
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
136 /* still can't get it? don't bother ... */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
137 if (fd == -1) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
138
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
139 memset(counters, 0, sizeof(counters));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
140
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
141 if (lock_fd(fd) != 0) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
142
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
143 /* read in the old stats */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
144 stats_read_fd(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
145
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
146 /* update them */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
147 counters[stat]++;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
148
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
149 /* on a cache miss we up the file count and size */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
150 if (stat == STATS_TOCACHE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
151 counters[STATS_NUMFILES] += numfiles;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
152 counters[STATS_TOTALSIZE] += size;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
153 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
154
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
155 /* and write them out */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
156 write_stats(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
157 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
158
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
159 /* we might need to cleanup if the cache has now got too big */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
160 if (counters[STATS_MAXFILES] != 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
161 counters[STATS_NUMFILES] > counters[STATS_MAXFILES]) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
162 need_cleanup = 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
163 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
164 if (counters[STATS_MAXSIZE] != 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
165 counters[STATS_TOTALSIZE] > counters[STATS_MAXSIZE]) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
166 need_cleanup = 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
167 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
168
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
169 if (need_cleanup) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
170 char *p = dirname(stats_file);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
171 cleanup_dir(p, counters[STATS_MAXFILES], counters[STATS_MAXSIZE],
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
172 numfiles);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
173 free(p);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
174 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
175 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
176
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
177 /* record a cache miss */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
178 void stats_tocache(size_t size, size_t numfiles)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
179 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
180 /* convert size to kilobytes */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
181 size = size / 1024;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
182
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
183 stats_update_size(STATS_TOCACHE, size, numfiles);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
184 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
185
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
186 /* update a normal stat */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
187 void stats_update(enum stats stat)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
188 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
189 stats_update_size(stat, 0, 0);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
190 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
191
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
192 /* read in the stats from one dir and add to the counters */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
193 void stats_read(const char *stats_file, unsigned counters[STATS_END])
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
194 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
195 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
196
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
197 fd = open(stats_file, O_RDONLY|O_BINARY);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
198 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
199 stats_default(counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
200 return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
201 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
202 lock_fd(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
203 stats_read_fd(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
204 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
205 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
206
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
207 /* sum and display the total stats for all cache dirs */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
208 void stats_summary(void)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
209 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
210 int dir, i;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
211 unsigned counters[STATS_END];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
212
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
213 memset(counters, 0, sizeof(counters));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
214
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
215 /* add up the stats in each directory */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
216 for (dir=-1;dir<=0xF;dir++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
217 char *fname;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
218
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
219 if (dir == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
220 x_asprintf(&fname, "%s/stats", cache_dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
221 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
222 x_asprintf(&fname, "%s/%1x/stats", cache_dir, dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
223 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
224
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
225 stats_read(fname, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
226 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
227
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
228 /* oh what a nasty hack ... */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
229 if (dir == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
230 counters[STATS_MAXSIZE] = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
231 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
232
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
233 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
234
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
235 printf("cache directory %s\n", cache_dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
236
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
237 /* and display them */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
238 for (i=0;stats_info[i].message;i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
239 enum stats stat = stats_info[i].stat;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
240
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
241 if (counters[stat] == 0 &&
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
242 !(stats_info[i].flags & FLAG_ALWAYS)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
243 continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
244 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
245
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
246 printf("%s ", stats_info[i].message);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
247 if (stats_info[i].fn) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
248 stats_info[i].fn(counters[stat]);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
249 printf("\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
250 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
251 printf("%8u\n", counters[stat]);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
252 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
253 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
254 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
255
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
256 /* zero all the stats structures */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
257 void stats_zero(void)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
258 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
259 int dir, fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
260 unsigned i;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
261 char *fname;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
262 unsigned counters[STATS_END];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
263
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
264 x_asprintf(&fname, "%s/stats", cache_dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
265 unlink(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
266 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
267
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
268 for (dir=0;dir<=0xF;dir++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
269 x_asprintf(&fname, "%s/%1x/stats", cache_dir, dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
270 fd = safe_open(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
271 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
272 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
273 continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
274 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
275 memset(counters, 0, sizeof(counters));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
276 lock_fd(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
277 stats_read_fd(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
278 for (i=0;stats_info[i].message;i++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
279 if (!(stats_info[i].flags & FLAG_NOZERO)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
280 counters[stats_info[i].stat] = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
281 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
282 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
283 write_stats(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
284 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
285 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
286 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
287 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
288
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
289
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
290 /* set the per directory limits */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
291 int stats_set_limits(long maxfiles, long maxsize)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
292 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
293 int dir;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
294 unsigned counters[STATS_END];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
295
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
296 if (maxfiles != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
297 maxfiles /= 16;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
298 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
299 if (maxsize != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
300 maxsize /= 16;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
301 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
302
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
303 if (create_dir(cache_dir) != 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
304 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
305 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
306
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
307 /* set the limits in each directory */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
308 for (dir=0;dir<=0xF;dir++) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
309 char *fname, *cdir;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
310 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
311
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
312 x_asprintf(&cdir, "%s/%1x", cache_dir, dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
313 if (create_dir(cdir) != 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
314 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
315 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
316 x_asprintf(&fname, "%s/stats", cdir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
317 free(cdir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
318
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
319 memset(counters, 0, sizeof(counters));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
320 fd = safe_open(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
321 if (fd != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
322 lock_fd(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
323 stats_read_fd(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
324 if (maxfiles != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
325 counters[STATS_MAXFILES] = maxfiles;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
326 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
327 if (maxsize != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
328 counters[STATS_MAXSIZE] = maxsize;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
329 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
330 write_stats(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
331 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
332 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
333 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
334 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
335
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
336 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
337 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
338
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
339 /* set the per directory sizes */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
340 void stats_set_sizes(const char *dir, size_t num_files, size_t total_size)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
341 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
342 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
343 unsigned counters[STATS_END];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
344 char *stats_file;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
345
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
346 create_dir(dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
347 x_asprintf(&stats_file, "%s/stats", dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
348
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
349 memset(counters, 0, sizeof(counters));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
350
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
351 fd = safe_open(stats_file);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
352 if (fd != -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
353 lock_fd(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
354 stats_read_fd(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
355 counters[STATS_NUMFILES] = num_files;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
356 counters[STATS_TOTALSIZE] = total_size;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
357 write_stats(fd, counters);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
358 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
359 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
360
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
361 free(stats_file);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
362 }