Isolate knowledge of the counters data structure to stats.c

This commit is contained in:
Joel Rosdahl 2010-08-03 17:27:41 +02:00
parent 42b3fd0d46
commit 8c071cf6e0
3 changed files with 40 additions and 32 deletions

View File

@ -132,7 +132,7 @@ unsigned stats_get_pending(enum stats stat);
void stats_zero(void);
void stats_summary(void);
void stats_update_size(enum stats stat, size_t size, unsigned files);
void stats_read(const char *stats_file, unsigned counters[STATS_END]);
void stats_get_limits(const char *dir, unsigned *maxfiles, unsigned *maxsize);
int stats_set_limits(long maxfiles, long maxsize);
size_t value_units(const char *s);
char *format_size(size_t v);

View File

@ -223,20 +223,15 @@ cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize)
/* cleanup in all cache subdirs */
void cleanup_all(const char *dir)
{
unsigned counters[STATS_END];
char *dname, *sfile;
unsigned maxfiles, maxsize;
char *dname;
int i;
for (i = 0; i <= 0xF; i++) {
dname = format("%s/%1x", dir, i);
sfile = format("%s/%1x/stats", dir, i);
memset(counters, 0, sizeof(counters));
stats_read(sfile, counters);
cleanup_dir(dname, counters[STATS_MAXFILES], counters[STATS_MAXSIZE]);
stats_get_limits(dname, &maxfiles, &maxsize);
cleanup_dir(dname, maxfiles, maxsize);
free(dname);
free(sfile);
}
}

57
stats.c
View File

@ -151,6 +151,28 @@ stats_update_size(enum stats stat, size_t size, unsigned files)
counter_updates[STATS_TOTALSIZE] += size;
}
/* Read in the stats from one directory and add to the counters. */
static void
stats_read(const char *path, unsigned counters[STATS_END])
{
int fd;
fd = open(path, O_RDONLY|O_BINARY);
if (fd == -1) {
stats_default(counters);
} else {
char buf[1024];
int len = read(fd, buf, sizeof(buf)-1);
if (len <= 0) {
stats_default(counters);
return;
}
buf[len] = 0;
parse_stats(counters, buf);
close(fd);
}
}
/*
* Write counter updates in pending_counters to disk.
*/
@ -228,28 +250,6 @@ stats_get_pending(enum stats stat)
return counter_updates[stat];
}
/* read in the stats from one dir and add to the counters */
void
stats_read(const char *path, unsigned counters[STATS_END])
{
int fd;
fd = open(path, O_RDONLY|O_BINARY);
if (fd == -1) {
stats_default(counters);
} else {
char buf[1024];
int len = read(fd, buf, sizeof(buf)-1);
if (len <= 0) {
stats_default(counters);
return;
}
buf[len] = 0;
parse_stats(counters, buf);
close(fd);
}
}
/* sum and display the total stats for all cache dirs */
void
stats_summary(void)
@ -331,6 +331,19 @@ stats_zero(void)
}
}
/* Get the per directory limits */
void
stats_get_limits(const char *dir, unsigned *maxfiles, unsigned *maxsize)
{
unsigned counters[STATS_END];
char *sname = format("%s/stats", dir);
memset(counters, 0, sizeof(counters));
stats_read(sname, counters);
free(sname);
*maxfiles = counters[STATS_MAXFILES];
*maxsize = counters[STATS_MAXSIZE];
}
/* set the per directory limits */
int
stats_set_limits(long maxfiles, long maxsize)