From 6642335c8a3b1567acad678ded4b89aaa79ef3a9 Mon Sep 17 00:00:00 2001 From: Cheer Xiao Date: Tue, 8 Jan 2013 22:47:50 +0800 Subject: [PATCH] counting & output: Convert all counters to double --- src/dhcpd-pools.h | 20 ++++----- src/output.c | 108 +++++++++++++++++++++++----------------------- src/sort.c | 14 +++--- 3 files changed, 69 insertions(+), 73 deletions(-) diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 0aa9210..c0183b8 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -113,10 +113,10 @@ struct configuration_t { */ struct shared_network_t { char *name; - unsigned long int available; - unsigned long int used; - unsigned long int touched; - unsigned long int backups; + double available; + double used; + double touched; + double backups; }; /*! \struct range_t * \brief Counters for an individual range. @@ -125,9 +125,9 @@ struct range_t { struct shared_network_t *shared_net; union ipaddr_t first_ip; union ipaddr_t last_ip; - unsigned long int count; - unsigned long int touched; - unsigned long int backups; + double count; + double touched; + double backups; }; /*! \enum isc_conf_parser * \brief Configuration file parsing state flags. @@ -224,9 +224,9 @@ int rangecomp(const void *__restrict r1, const void *__restrict r2) __attribute__ ((nonnull(1, 2))); /* sort function pointer and functions */ typedef int (*comparer_t) (struct range_t *r1, struct range_t *r2); -unsigned long int ret_percent(struct range_t r); -unsigned long int ret_tc(struct range_t r); -unsigned long int ret_tcperc(struct range_t r); +double ret_percent(struct range_t r); +double ret_tc(struct range_t r); +double ret_tcperc(struct range_t r); void mergesort_ranges(struct range_t *__restrict orig, int size, struct range_t *__restrict temp) __attribute__ ((nonnull(1, 3))); diff --git a/src/output.c b/src/output.c index 43cd5d9..d8e1456 100644 --- a/src/output.c +++ b/src/output.c @@ -61,7 +61,7 @@ int output_txt(void) { unsigned int i; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; @@ -111,7 +111,7 @@ int output_txt(void) max_ipaddr_length, ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, - " - %-*s %5lu %5lu %10.3f %5lu %5lu %9.3f", + " - %-*s %5g %5g %10.3f %5g %5g %9.3f", max_ipaddr_length, ntop_ipaddr(&range_p->last_ip), range_size, @@ -123,7 +123,7 @@ int output_txt(void) (range_p->touched + range_p->count)) / range_size); if (config.backups_found == true) { - fprintf(outfile, "%7lu %8.3f", + fprintf(outfile, "%7g %8.3f", range_p->backups, (float)(100 * range_p->backups) / range_size); @@ -149,7 +149,7 @@ int output_txt(void) for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, - "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f", + "%-20s %5g %5g %10.3f %7g %6g %9.3f", shared_p->name, shared_p->available, shared_p->used, (float)(100 * shared_p->used) / @@ -160,7 +160,7 @@ int output_txt(void) shared_p->used)) / shared_p->available); if (config.backups_found == true) { - fprintf(outfile, "%7lu %8.3f", + fprintf(outfile, "%7g %8.3f", shared_p->backups, (float)(100 * shared_p->backups) / shared_p->available); @@ -183,7 +183,7 @@ int output_txt(void) fprintf(outfile, "\n"); } if (config.output_limit[1] & BIT3) { - fprintf(outfile, "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f", + fprintf(outfile, "%-20s %5g %5g %10.3f %7g %6g %9.3f", shared_networks->name, shared_networks->available, shared_networks->used, @@ -197,7 +197,7 @@ int output_txt(void) shared_networks->available); if (config.backups_found == true) { - fprintf(outfile, "%7lu %8.3f", + fprintf(outfile, "%7g %8.3f", shared_networks->backups, (float)(100 * shared_networks->backups) / shared_networks->available); @@ -226,7 +226,7 @@ int output_xml(void) { unsigned int i; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; @@ -280,11 +280,11 @@ int output_xml(void) fprintf(outfile, "- %s\n", ntop_ipaddr(&range_p->last_ip)); fprintf(outfile, "\t\n"); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", range_size); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", range_p->count); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", range_size - range_p->count); range_p++; range_size = get_range_size(range_p); @@ -298,11 +298,11 @@ int output_xml(void) fprintf(outfile, "\n"); fprintf(outfile, "\t%s\n", shared_p->name); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", shared_p->available); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", shared_p->used); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", shared_p->available - shared_p->used); fprintf(outfile, "\n"); } @@ -312,10 +312,10 @@ int output_xml(void) fprintf(outfile, "\n"); fprintf(outfile, "\t%s\n", shared_networks->name); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", shared_networks->available); - fprintf(outfile, "\t%lu\n", shared_networks->used); - fprintf(outfile, "\t%lu\n", + fprintf(outfile, "\t%g\n", shared_networks->used); + fprintf(outfile, "\t%g\n", shared_networks->available - shared_networks->used); fprintf(outfile, "\n"); } @@ -343,7 +343,7 @@ int output_json(void) { unsigned int i = 0; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; @@ -409,9 +409,9 @@ int output_json(void) ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, " - %s\", ", ntop_ipaddr(&range_p->last_ip)); - fprintf(outfile, "\"defined\":%lu, ", range_size); - fprintf(outfile, "\"used\":%lu, ", range_p->count); - fprintf(outfile, "\"free\":%lu ", + fprintf(outfile, "\"defined\":%g, ", range_size); + fprintf(outfile, "\"used\":%g, ", range_p->count); + fprintf(outfile, "\"free\":%g ", range_size - range_p->count); range_p++; range_size = get_range_size(range_p); @@ -435,10 +435,10 @@ int output_json(void) fprintf(outfile, "{ "); fprintf(outfile, "\"location\":\"%s\", ", shared_p->name); - fprintf(outfile, "\"defined\":%lu, ", + fprintf(outfile, "\"defined\":%g, ", shared_p->available); - fprintf(outfile, "\"used\":%lu, ", shared_p->used); - fprintf(outfile, "\"free\":%lu ", + fprintf(outfile, "\"used\":%g, ", shared_p->used); + fprintf(outfile, "\"free\":%g ", shared_p->available - shared_p->used); if (i + 1 < num_shared_networks) fprintf(outfile, "},\n"); @@ -456,11 +456,11 @@ int output_json(void) fprintf(outfile, " \"summary\": {\n"); fprintf(outfile, " \"location\":\"%s\",\n", shared_networks->name); - fprintf(outfile, " \"defined\":%lu,\n", + fprintf(outfile, " \"defined\":%g,\n", shared_networks->available); - fprintf(outfile, " \"used\":%lu,\n", + fprintf(outfile, " \"used\":%g,\n", shared_networks->used); - fprintf(outfile, " \"free\":%lu\n", + fprintf(outfile, " \"free\":%g\n", shared_networks->available - shared_networks->used); fprintf(outfile, " }"); /* end of summary */ sep++; @@ -617,10 +617,10 @@ static void output_line(FILE *restrict f, char const *restrict type, * \param type HMTL tag name. * \param unlong Actual payload of the printout. */ -static void output_long(FILE *restrict f, char const *restrict type, - unsigned long unlong) +static void output_double(FILE *restrict f, char const *restrict type, + double d) { - fprintf(f, " <%s class=ralign>%lu\n", type, unlong, type); + fprintf(f, " <%s class=ralign>%g\n", type, d, type); } /*! \brief Line with float in html output format. @@ -676,7 +676,7 @@ int output_html(void) { unsigned int i; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; int ret; FILE *outfile; @@ -729,20 +729,20 @@ int output_html(void) ntop_ipaddr(&range_p->first_ip)); output_line(outfile, "td", "calign", ntop_ipaddr(&range_p->last_ip)); - output_long(outfile, "td", range_size); - output_long(outfile, "td", range_p->count); + output_double(outfile, "td", range_size); + output_double(outfile, "td", range_p->count); output_float(outfile, "td", (float)(100 * range_p->count) / range_size); - output_long(outfile, "td", range_p->touched); - output_long(outfile, "td", + output_double(outfile, "td", range_p->touched); + output_double(outfile, "td", range_p->touched + range_p->count); output_float(outfile, "td", (float)(100 * (range_p->touched + range_p->count)) / range_size); if (config.backups_found == true) { - output_long(outfile, "td", range_p->backups); + output_double(outfile, "td", range_p->backups); output_float(outfile, "td", (float)(100 * range_p->backups) / @@ -776,13 +776,13 @@ int output_html(void) shared_p++; newrow(outfile); output_line(outfile, "td", "calign", shared_p->name); - output_long(outfile, "td", shared_p->available); - output_long(outfile, "td", shared_p->used); + output_double(outfile, "td", shared_p->available); + output_double(outfile, "td", shared_p->used); output_float(outfile, "td", (float)(100 * shared_p->used) / shared_p->available); - output_long(outfile, "td", shared_p->touched); - output_long(outfile, "td", + output_double(outfile, "td", shared_p->touched); + output_double(outfile, "td", shared_p->touched + shared_p->used); output_float(outfile, "td", (float)(100 * @@ -790,7 +790,7 @@ int output_html(void) shared_p->used)) / shared_p->available); if (config.backups_found == true) { - output_long(outfile, "td", shared_p->backups); + output_double(outfile, "td", shared_p->backups); output_float(outfile, "td", (float)(100 * shared_p->backups) / @@ -820,13 +820,13 @@ int output_html(void) if (config.output_limit[1] & BIT3) { newrow(outfile); output_line(outfile, "td", "calign", shared_networks->name); - output_long(outfile, "td", shared_networks->available); - output_long(outfile, "td", shared_networks->used); + output_double(outfile, "td", shared_networks->available); + output_double(outfile, "td", shared_networks->used); output_float(outfile, "td", (float)(100 * shared_networks->used) / shared_networks->available); - output_long(outfile, "td", shared_networks->touched); - output_long(outfile, "td", + output_double(outfile, "td", shared_networks->touched); + output_double(outfile, "td", shared_networks->touched + shared_networks->used); output_float(outfile, "td", (float)(100 * @@ -834,7 +834,7 @@ int output_html(void) shared_networks->used)) / shared_networks->available); if (config.backups_found == true) { - output_long(outfile, "td", shared_networks->backups); + output_double(outfile, "td", shared_networks->backups); output_float(outfile, "td", (float)(100 * shared_networks->backups) / @@ -867,7 +867,7 @@ int output_csv(void) { unsigned int i; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; FILE *outfile; int ret; @@ -904,7 +904,7 @@ int output_csv(void) fprintf(outfile, "\"%s\",", ntop_ipaddr(&range_p->first_ip)); fprintf(outfile, - "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", + "\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"", ntop_ipaddr(&range_p->last_ip), range_size, range_p->count, (float)(100 * range_p->count) / range_size, @@ -914,7 +914,7 @@ int output_csv(void) (range_p->touched + range_p->count)) / range_size); if (config.backups_found == true) { - fprintf(outfile, ",\"%lu\",\"%.3f\"", + fprintf(outfile, ",\"%g\",\"%.3f\"", range_p->backups, (float)(100 * range_p->backups) / range_size); @@ -940,7 +940,7 @@ int output_csv(void) for (i = 0; i < num_shared_networks; i++) { shared_p++; fprintf(outfile, - "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", + "\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"", shared_p->name, shared_p->available, shared_p->used, (float)(100 * shared_p->used) / @@ -951,7 +951,7 @@ int output_csv(void) shared_p->used)) / shared_p->available); if (config.backups_found == true) { - fprintf(outfile, ",\"%lu\",\"%.3f\"", + fprintf(outfile, ",\"%g\",\"%.3f\"", shared_p->backups, (float)(100 * shared_p->backups) / shared_p->available); @@ -973,7 +973,7 @@ int output_csv(void) if (config.output_limit[1] & BIT3) { fprintf(outfile, - "\"%s\",\"%lu\",\"%lu\",\"%.3f\",\"%lu\",\"%lu\",\"%.3f\"", + "\"%s\",\"%g\",\"%g\",\"%.3f\",\"%g\",\"%g\",\"%.3f\"", shared_networks->name, shared_networks->available, shared_networks->used, (float)(100 * shared_networks->used) / @@ -985,7 +985,7 @@ int output_csv(void) shared_networks->used)) / shared_networks->available); if (config.backups_found == true) { - fprintf(outfile, "%7lu %8.3f", + fprintf(outfile, "%7g %8.3f", shared_networks->backups, (float)(100 * shared_networks->backups) / shared_networks->available); @@ -1013,7 +1013,7 @@ int output_alarming(void) { FILE *outfile; struct range_t *range_p; - unsigned long range_size; + double range_size; struct shared_network_t *shared_p; unsigned int i; float perc; diff --git a/src/sort.c b/src/sort.c index cdf3d9f..54e4790 100644 --- a/src/sort.c +++ b/src/sort.c @@ -164,18 +164,16 @@ int comp_tcperc(struct range_t *r1, struct range_t *r2) * \param r A range structure. * \return Usage percentage of the given range. */ -unsigned long int ret_percent(struct range_t r) +double ret_percent(struct range_t r) { - float f; - f = (float)r.count / get_range_size(&r); - return ((unsigned long int)(f * 100000)); + return r.count / get_range_size(&r); } /*! \brief Touched and in use in range * \param r A range structure. * \return Number of touched or in use addresses in the given range. */ -unsigned long int ret_tc(struct range_t r) +double ret_tc(struct range_t r) { return (r.count + r.touched); } @@ -184,11 +182,9 @@ unsigned long int ret_tc(struct range_t r) * \param r A range structure. * \return Percentage of touched or in use addresses in the given range. */ -unsigned long int ret_tcperc(struct range_t r) +double ret_tcperc(struct range_t r) { - float f; - f = (float)(r.count + r.touched) / get_range_size(&r); - return ((unsigned long int)(f * 10000)); + return ret_tc(r) / get_range_size(&r); } /*! \brief Sort field selector.