From: Teemu Toivola Date: Sun, 2 Mar 2014 21:42:42 +0000 (+0200) Subject: move simple calculation related functions to common X-Git-Tag: v1.12~78 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0fc494c35200bb8f865e0dbe9b0452adde58b451;p=vnstat move simple calculation related functions to common --- diff --git a/src/Makefile b/src/Makefile index 60d9ee8..5fe15cb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h traffic.o: traffic.c traffic.h common.h ifinfo.h misc.h dbxml.o: dbxml.c dbxml.h common.h dbshow.o: dbshow.c dbshow.h misc.h common.h -dbaccess.o: dbaccess.c dbaccess.h misc.h common.h +dbaccess.o: dbaccess.c dbaccess.h common.h dbmerge.o: dbmerge.c dbmerge.h dbaccess.h common.h dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h common.o: common.c common.h diff --git a/src/common.c b/src/common.c index ffe1872..24e2613 100644 --- a/src/common.c +++ b/src/common.c @@ -172,3 +172,48 @@ uint32_t mosecs(void) return 0; } } + +uint64_t countercalc(uint64_t a, uint64_t b) +{ + /* no flip */ + if (b>=a) { + if (debug) + printf("cc: %"PRIu64" - %"PRIu64" = %"PRIu64"\n", b, a, b-a); + return b-a; + + /* flip exists */ + } else { + /* original counter is 64bit */ + if (a>FP32) { + if (debug) + printf("cc64: uint64 - %"PRIu64" + %"PRIu64" = %"PRIu64"\n", a, b, (uint64_t)FP64-a+b); + return FP64-a+b; + + /* original counter is 32bit */ + } else { + if (debug) + printf("cc32: uint32 - %"PRIu64" + %"PRIu64" = %"PRIu64"\n", a, b, (uint64_t)FP32-a+b); + return FP32-a+b; + } + } +} + +void addtraffic(uint64_t *destmb, int *destkb, uint64_t srcmb, int srckb) +{ + *destmb+=srcmb; + *destkb+=srckb; + + if (*destkb>=1024) { + *destmb+=*destkb/1024; + *destkb-=(*destkb/1024)*1024; + } +} + +uint64_t mbkbtokb(uint64_t mb, uint64_t kb) +{ + if (kb>=1024) { + mb+=kb/1024; + kb-=(kb/1024)*1024; + } + return (mb*1024)+kb; +} diff --git a/src/common.h b/src/common.h index b0c0456..5be8c94 100644 --- a/src/common.h +++ b/src/common.h @@ -262,6 +262,9 @@ int printe(PrintType type); int logprint(PrintType type); int dmonth(int month); uint32_t mosecs(void); +uint64_t countercalc(uint64_t a, uint64_t b); +void addtraffic(uint64_t *destmb, int *destkb, uint64_t srcmb, int srckb); +uint64_t mbkbtokb(uint64_t mb, uint64_t kb); /* global variables */ DATA data; diff --git a/src/dbaccess.c b/src/dbaccess.c index 1819e2f..f033b86 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -1,6 +1,5 @@ #include "common.h" #include "dbaccess.h" -#include "misc.h" int readdb(const char *iface, const char *dirname) { diff --git a/src/ifinfo.c b/src/ifinfo.c index d7a078a..9e06500 100644 --- a/src/ifinfo.c +++ b/src/ifinfo.c @@ -376,31 +376,6 @@ void parseifinfo(int newdb) } } -uint64_t countercalc(uint64_t a, uint64_t b) -{ - /* no flip */ - if (b>=a) { - if (debug) - printf("cc: %"PRIu64" - %"PRIu64" = %"PRIu64"\n", b, a, b-a); - return b-a; - - /* flip exists */ - } else { - /* original counter is 64bit */ - if (a>FP32) { - if (debug) - printf("cc64: uint64 - %"PRIu64" + %"PRIu64" = %"PRIu64"\n", a, b, (uint64_t)FP64-a+b); - return FP64-a+b; - - /* original counter is 32bit */ - } else { - if (debug) - printf("cc32: uint32 - %"PRIu64" + %"PRIu64" = %"PRIu64"\n", a, b, (uint64_t)FP32-a+b); - return FP32-a+b; - } - } -} - #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) int readifaddrs(const char *iface) { diff --git a/src/ifinfo.h b/src/ifinfo.h index ca8f3f6..d2d23a2 100644 --- a/src/ifinfo.h +++ b/src/ifinfo.h @@ -6,7 +6,6 @@ int getiflist(char **ifacelist); int readproc(const char *iface); int readsysclassnet(const char *iface); void parseifinfo(int newdb); -uint64_t countercalc(uint64_t a, uint64_t b); #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) int readifaddrs(const char *iface); #endif diff --git a/src/image.c b/src/image.c index 4fa5424..2cd7cc1 100644 --- a/src/image.c +++ b/src/image.c @@ -140,18 +140,8 @@ void drawbar(int x, int y, int len, uint64_t rx, int rxk, uint64_t tx, int txk, { int l; - if (rxk>=1024) { - rx+=rxk/1024; - rxk-=(rxk/1024)*1024; - } - - if (txk>=1024) { - tx+=txk/1024; - txk-=(txk/1024)*1024; - } - - rx=(rx*1024)+rxk; - tx=(tx*1024)+txk; + rx=mbkbtokb(rx, rxk); + tx=mbkbtokb(tx, txk); if ((rx+tx)!=max) { len=((rx+tx)/(float)max)*len; @@ -835,12 +825,7 @@ void drawoldsummary(int type, int showheader, int showedge, int rate) t=data.day[i].rx+data.day[i].tx; tk=data.day[i].rxk+data.day[i].txk; - if (tk>=1024) { - t+=tk/1024; - tk-=(tk/1024)*1024; - } - - t=(t*1024)+tk; + t=mbkbtokb(t, tk); if (t>max) { max=t; diff --git a/src/image.h b/src/image.h index 6989dbd..7cedb3d 100644 --- a/src/image.h +++ b/src/image.h @@ -15,7 +15,6 @@ void drawhourly(int showheader, int showedge, int rate); void drawdaily(int showheader, int showedge); void drawmonthly(int showheader, int showedge); void drawtop(int showheader, int showedge); -void addtraffic(uint64_t *destmb, int *destkb, uint64_t srcmb, int srckb); void hextorgb(char *input, int *rgb); void modcolor(int *rgb, int offset, int force); char *getimagevalue(uint64_t kb, int len, int rate); diff --git a/src/misc.c b/src/misc.c index ebebbbe..62d3771 100644 --- a/src/misc.c +++ b/src/misc.c @@ -187,17 +187,6 @@ int getbtime(void) return result; } -void addtraffic(uint64_t *destmb, int *destkb, uint64_t srcmb, int srckb) -{ - *destmb=*destmb+srcmb; - *destkb=*destkb+srckb; - - if (*destkb>=1024) { - *destmb+=*destkb/1024; - *destkb-=(*destkb/1024)*1024; - } -} - char *getvalue(uint64_t mb, uint64_t kb, int len, int type) { static char buffer[64]; @@ -210,11 +199,7 @@ char *getvalue(uint64_t mb, uint64_t kb, int len, int type) } if (mb!=0) { - if (kb>=1024) { - mb+=kb/1024; - kb-=(kb/1024)*1024; - } - kB=(mb*1024)+kb; + kB=mbkbtokb(mb, kb); } else { kB=kb; } @@ -259,11 +244,7 @@ char *getrate(uint64_t mb, uint64_t kb, uint32_t interval, int len) } if (mb!=0) { - if (kb>=1024) { - mb+=kb/1024; - kb-=(kb/1024)*1024; - } - kB=(mb*1024)+kb; + kB=mbkbtokb(mb, kb); } else { kB=kb; } diff --git a/src/misc.h b/src/misc.h index 04a095c..32cdc2d 100644 --- a/src/misc.h +++ b/src/misc.h @@ -7,7 +7,6 @@ int kerneltest(void); int spacecheck(char *path); void sighandler(int); int getbtime(void); -void addtraffic(uint64_t *destmb, int *destkb, uint64_t srcmb, int srckb); char *getvalue(uint64_t mb, uint64_t kb, int len, int type); char *getrate(uint64_t mb, uint64_t kb, uint32_t interval, int len); char *gettrafficrate(uint64_t bytes, uint32_t interval, int len); diff --git a/tests/Makefile b/tests/Makefile index 477e5fe..d4fd93a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -28,7 +28,7 @@ misc_tests.o: misc_tests.c misc_tests.h vnstat_tests.h common.h misc.h common.o: common.c common.h ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h -dbaccess.o: dbaccess.c dbaccess.h misc.h common.h +dbaccess.o: dbaccess.c dbaccess.h common.h dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.o: cfg.c cfg.h common.h misc.o: misc.c misc.h common.h diff --git a/tests/common_tests.c b/tests/common_tests.c index 67f996c..bc7b615 100644 --- a/tests/common_tests.c +++ b/tests/common_tests.c @@ -63,6 +63,198 @@ START_TEST(mosecs_return_values) } END_TEST +START_TEST(countercalc_no_change) +{ + ck_assert_int_eq(countercalc(0, 0), 0); + ck_assert_int_eq(countercalc(1, 1), 0); +} +END_TEST + +START_TEST(countercalc_small_change) +{ + ck_assert_int_eq(countercalc(0, 1), 1); + ck_assert_int_eq(countercalc(1, 2), 1); + ck_assert_int_eq(countercalc(1, 3), 2); +} +END_TEST + +START_TEST(countercalc_32bit) +{ + ck_assert(countercalc(1, 0)==(FP32-1)); +} +END_TEST + +START_TEST(countercalc_64bit) +{ + ck_assert(countercalc(FP32+1, 0)==(FP64-FP32-1)); +} +END_TEST + +START_TEST(addtraffic_does_not_add_zero_traffic) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + srcmb=srckb=destmb=destkb=0; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 0); + ck_assert_int_eq(srckb, 0); + ck_assert_int_eq(destmb, 0); + ck_assert_int_eq(destkb, 0); +} +END_TEST + +START_TEST(addtraffic_with_simple_mb_addition) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + destmb=destkb=0; + srcmb=1; + srckb=0; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 1); + ck_assert_int_eq(srckb, 0); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 0); +} +END_TEST + +START_TEST(addtraffic_with_simple_kb_addition) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + destmb=destkb=0; + srcmb=0; + srckb=1; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 0); + ck_assert_int_eq(srckb, 1); + ck_assert_int_eq(destmb, 0); + ck_assert_int_eq(destkb, 1); +} +END_TEST + +START_TEST(addtraffic_with_simple_mixed_addition) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + destmb=destkb=0; + srcmb=1; + srckb=1; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 1); + ck_assert_int_eq(srckb, 1); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 1); +} +END_TEST + +START_TEST(addtraffic_with_multiple_mixed_additions) +{ + uint64_t srcmb, destmb; + int srckb, destkb, i; + + destmb=destkb=0; + srcmb=1; + srckb=1; + + for (i=1; i<=10; i++) { + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 1); + ck_assert_int_eq(srckb, 1); + ck_assert_int_eq(destmb, (uint64_t)i); + ck_assert_int_eq(destkb, i); + } +} +END_TEST + +START_TEST(addtraffic_with_exact_kb_to_mb_conversion) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + destmb=destkb=0; + srcmb=0; + srckb=1024; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 0); + ck_assert_int_eq(srckb, 1024); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 0); +} +END_TEST + +START_TEST(addtraffic_with_inexact_kb_to_mb_conversion) +{ + uint64_t srcmb, destmb; + int srckb, destkb; + + destmb=destkb=0; + srcmb=0; + srckb=1025; + addtraffic(&destmb, &destkb, srcmb, srckb); + + ck_assert_int_eq(srcmb, 0); + ck_assert_int_eq(srckb, 1025); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 1); +} +END_TEST + +START_TEST(addtraffic_with_multiple_kb_to_mb_conversions) +{ + uint64_t destmb; + int destkb; + + destmb=destkb=0; + + addtraffic(&destmb, &destkb, 0, 1023); + ck_assert_int_eq(destmb, 0); + ck_assert_int_eq(destkb, 1023); + + addtraffic(&destmb, &destkb, 0, 1); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 0); + + addtraffic(&destmb, &destkb, 0, 1); + ck_assert_int_eq(destmb, 1); + ck_assert_int_eq(destkb, 1); + + addtraffic(&destmb, &destkb, 0, 1023); + ck_assert_int_eq(destmb, 2); + ck_assert_int_eq(destkb, 0); + + addtraffic(&destmb, &destkb, 0, 1024); + ck_assert_int_eq(destmb, 3); + ck_assert_int_eq(destkb, 0); + + addtraffic(&destmb, &destkb, 0, 1025); + ck_assert_int_eq(destmb, 4); + ck_assert_int_eq(destkb, 1); + + addtraffic(&destmb, &destkb, 0, 512); + ck_assert_int_eq(destmb, 4); + ck_assert_int_eq(destkb, 513); + + addtraffic(&destmb, &destkb, 0, 512); + ck_assert_int_eq(destmb, 5); + ck_assert_int_eq(destkb, 1); + + addtraffic(&destmb, &destkb, 0, 2048); + ck_assert_int_eq(destmb, 7); + ck_assert_int_eq(destkb, 1); +} +END_TEST + void add_common_tests(Suite *s) { /* Common test cases */ @@ -71,5 +263,17 @@ void add_common_tests(Suite *s) tcase_add_test(tc_common, logprint_options); tcase_add_loop_test(tc_common, dmonth_return_within_range, 0, 12); tcase_add_test(tc_common, mosecs_return_values); + tcase_add_test(tc_common, countercalc_no_change); + tcase_add_test(tc_common, countercalc_small_change); + tcase_add_test(tc_common, countercalc_32bit); + tcase_add_test(tc_common, countercalc_64bit); + tcase_add_test(tc_common, addtraffic_does_not_add_zero_traffic); + tcase_add_test(tc_common, addtraffic_with_simple_mb_addition); + tcase_add_test(tc_common, addtraffic_with_simple_kb_addition); + tcase_add_test(tc_common, addtraffic_with_simple_mixed_addition); + tcase_add_test(tc_common, addtraffic_with_multiple_mixed_additions); + tcase_add_test(tc_common, addtraffic_with_exact_kb_to_mb_conversion); + tcase_add_test(tc_common, addtraffic_with_inexact_kb_to_mb_conversion); + tcase_add_test(tc_common, addtraffic_with_multiple_kb_to_mb_conversions); suite_add_tcase(s, tc_common); } diff --git a/tests/ifinfo_tests.c b/tests/ifinfo_tests.c index 4516c94..c45b427 100644 --- a/tests/ifinfo_tests.c +++ b/tests/ifinfo_tests.c @@ -3,41 +3,9 @@ #include "common.h" #include "ifinfo.h" - -START_TEST(countercalc_no_change) -{ - ck_assert_int_eq(countercalc(0, 0), 0); - ck_assert_int_eq(countercalc(1, 1), 0); -} -END_TEST - -START_TEST(countercalc_small_change) -{ - ck_assert_int_eq(countercalc(0, 1), 1); - ck_assert_int_eq(countercalc(1, 2), 1); - ck_assert_int_eq(countercalc(1, 3), 2); -} -END_TEST - -START_TEST(countercalc_32bit) -{ - ck_assert(countercalc(1, 0)==(FP32-1)); -} -END_TEST - -START_TEST(countercalc_64bit) -{ - ck_assert(countercalc(FP32+1, 0)==(FP64-FP32-1)); -} -END_TEST - void add_ifinfo_tests(Suite *s) { /* Ifinfo test cases */ TCase *tc_ifinfo = tcase_create("Ifinfo"); - tcase_add_test(tc_ifinfo, countercalc_no_change); - tcase_add_test(tc_ifinfo, countercalc_small_change); - tcase_add_test(tc_ifinfo, countercalc_32bit); - tcase_add_test(tc_ifinfo, countercalc_64bit); suite_add_tcase(s, tc_ifinfo); } diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 3fd54f2..058a0fc 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -9,171 +9,6 @@ START_TEST(getbtime_does_not_return_zero) } END_TEST -START_TEST(addtraffic_does_not_add_zero_traffic) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - srcmb=srckb=destmb=destkb=0; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 0); - ck_assert_int_eq(srckb, 0); - ck_assert_int_eq(destmb, 0); - ck_assert_int_eq(destkb, 0); -} -END_TEST - -START_TEST(addtraffic_with_simple_mb_addition) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - destmb=destkb=0; - srcmb=1; - srckb=0; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 1); - ck_assert_int_eq(srckb, 0); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 0); -} -END_TEST - -START_TEST(addtraffic_with_simple_kb_addition) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - destmb=destkb=0; - srcmb=0; - srckb=1; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 0); - ck_assert_int_eq(srckb, 1); - ck_assert_int_eq(destmb, 0); - ck_assert_int_eq(destkb, 1); -} -END_TEST - -START_TEST(addtraffic_with_simple_mixed_addition) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - destmb=destkb=0; - srcmb=1; - srckb=1; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 1); - ck_assert_int_eq(srckb, 1); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 1); -} -END_TEST - -START_TEST(addtraffic_with_multiple_mixed_additions) -{ - uint64_t srcmb, destmb; - int srckb, destkb, i; - - destmb=destkb=0; - srcmb=1; - srckb=1; - - for (i=1; i<=10; i++) { - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 1); - ck_assert_int_eq(srckb, 1); - ck_assert_int_eq(destmb, (uint64_t)i); - ck_assert_int_eq(destkb, i); - } -} -END_TEST - -START_TEST(addtraffic_with_exact_kb_to_mb_conversion) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - destmb=destkb=0; - srcmb=0; - srckb=1024; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 0); - ck_assert_int_eq(srckb, 1024); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 0); -} -END_TEST - -START_TEST(addtraffic_with_inexact_kb_to_mb_conversion) -{ - uint64_t srcmb, destmb; - int srckb, destkb; - - destmb=destkb=0; - srcmb=0; - srckb=1025; - addtraffic(&destmb, &destkb, srcmb, srckb); - - ck_assert_int_eq(srcmb, 0); - ck_assert_int_eq(srckb, 1025); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 1); -} -END_TEST - -START_TEST(addtraffic_with_multiple_kb_to_mb_conversions) -{ - uint64_t destmb; - int destkb; - - destmb=destkb=0; - - addtraffic(&destmb, &destkb, 0, 1023); - ck_assert_int_eq(destmb, 0); - ck_assert_int_eq(destkb, 1023); - - addtraffic(&destmb, &destkb, 0, 1); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 0); - - addtraffic(&destmb, &destkb, 0, 1); - ck_assert_int_eq(destmb, 1); - ck_assert_int_eq(destkb, 1); - - addtraffic(&destmb, &destkb, 0, 1023); - ck_assert_int_eq(destmb, 2); - ck_assert_int_eq(destkb, 0); - - addtraffic(&destmb, &destkb, 0, 1024); - ck_assert_int_eq(destmb, 3); - ck_assert_int_eq(destkb, 0); - - addtraffic(&destmb, &destkb, 0, 1025); - ck_assert_int_eq(destmb, 4); - ck_assert_int_eq(destkb, 1); - - addtraffic(&destmb, &destkb, 0, 512); - ck_assert_int_eq(destmb, 4); - ck_assert_int_eq(destkb, 513); - - addtraffic(&destmb, &destkb, 0, 512); - ck_assert_int_eq(destmb, 5); - ck_assert_int_eq(destkb, 1); - - addtraffic(&destmb, &destkb, 0, 2048); - ck_assert_int_eq(destmb, 7); - ck_assert_int_eq(destkb, 1); -} -END_TEST - START_TEST(getunit_returns_something_with_all_cfg_combinations) { char *string; @@ -484,14 +319,6 @@ void add_misc_tests(Suite *s) /* Misc test cases */ TCase *tc_misc = tcase_create("Misc"); tcase_add_test(tc_misc, getbtime_does_not_return_zero); - tcase_add_test(tc_misc, addtraffic_does_not_add_zero_traffic); - tcase_add_test(tc_misc, addtraffic_with_simple_mb_addition); - tcase_add_test(tc_misc, addtraffic_with_simple_kb_addition); - tcase_add_test(tc_misc, addtraffic_with_simple_mixed_addition); - tcase_add_test(tc_misc, addtraffic_with_multiple_mixed_additions); - tcase_add_test(tc_misc, addtraffic_with_exact_kb_to_mb_conversion); - tcase_add_test(tc_misc, addtraffic_with_inexact_kb_to_mb_conversion); - tcase_add_test(tc_misc, addtraffic_with_multiple_kb_to_mb_conversions); tcase_add_loop_test(tc_misc, getunit_returns_something_with_all_cfg_combinations, 0, 2); tcase_add_loop_test(tc_misc, getrateunit_returns_something_with_all_cfg_combinations, 0, 3); tcase_add_loop_test(tc_misc, getunitdivider_returns_something_with_all_cfg_combinations, 0, 3);