#include "dfeed.h"

static double stats_elapsed[100];
static double stats_cpu[100];
static double start_elapsed[100];
static int start_cpu[100];
static int stats_n[100];
static int nstats;
static double total_start,total;
int stats_start(int x)
{
	if (nstats<=x) nstats = x+1;
	if (total_start==0) total_start = time(NULL);
	start_elapsed[x] = time(NULL);
	start_cpu[x] = clock();
	return TRUE;
}
int stats_end(int x)
{
	stats_elapsed[x] += time(NULL) - start_elapsed[x];
	stats_cpu[x] += clock() - start_cpu[x];
	stats_n[x]++;
	return TRUE;
}
void stats_status(FILE *f)
{
        int cps=CLOCKS_PER_SEC;
	int i;
	total = time(NULL) - total_start;
	if (total<=0) total = 1;
	fprintf(f,"              Total CPU  Elapsed Avg CPU Elapsed      N\n");

	for (i=0; i<nstats; i++) {
		if (stats_n[i]==0) continue; 
                fprintf(f,"%14s %7.2f%% %7.2f%% %7.2f %7.2f %6d\n"
			,stats_names[i]
                        ,stats_cpu[i]/cps/total*100.0,stats_elapsed[i]/total*100.0
                        ,stats_cpu[i]/cps/stats_n[i],stats_elapsed[i]/stats_n[i]
			,stats_n[i]); 

	}
	fprintf(f,"\n");
}
