return versionbuffer;
}
-void timeused(const char *func, const int reset)
+double timeused(const char *func, const int reset)
{
static struct timeval starttime;
struct timeval endtime;
-
- if (!debug) {
- return;
- }
+ double used_secs;
if (reset) {
gettimeofday(&starttime, NULL);
- return;
+ return 0.0;
}
if (gettimeofday(&endtime, NULL) != 0) {
+ return 0.0;
+ }
+
+ used_secs = (double)(endtime.tv_usec - starttime.tv_usec) / 1000000 + (double)(endtime.tv_sec - starttime.tv_sec);
+
+ if (debug) {
+ printf("%s() in %f s\n", func, used_secs);
+ }
+
+ return used_secs;
+}
+
+void timeused_debug(const char *func, const int reset)
+{
+ if (!debug) {
return;
}
- printf("%s() in %f s\n", func, (double)(endtime.tv_usec - starttime.tv_usec) / 1000000 + (double)(endtime.tv_sec - starttime.tv_sec));
+
+ timeused(func, reset);
}
int isnumeric(const char *s);
void panicexit(const char *sourcefile, const int sourceline) __attribute__((noreturn));
char *getversion(void);
-void timeused(const char *func, const int reset);
+double timeused(const char *func, const int reset);
+void timeused_debug(const char *func, const int reset);
/* global variables */
extern CFG cfg;
unsigned int index = 0, count = 0;
uint32_t bwlimit = 0;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
/* get list of currently visible interfaces */
if (getiflist(&ifacelist, 0) == 0) {
}
free(ifacelist);
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
return count;
}
{
dbiflist *dbifl = NULL, *dbifl_iterator = NULL;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (db_getiflist(&dbifl) < 0) {
errorexitdaemon(s, 1);
} else {
s->updateinterval = 120;
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void adjustsaveinterval(DSTATE *s)
{
datacache *iterator = s->dcache;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
while (iterator != NULL) {
if (s->bootdetected) {
s->bootdetected = 0;
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
if (s->dodbsave) {
flushcachetodisk(s);
xferlog *logiterator;
interfaceinfo info;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (!db_begintransaction()) {
handledatabaseerror(s);
} else {
db_rollbacktransaction();
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void handledatabaseerror(DSTATE *s)
datacache *iterator = s->dcache;
dbiflist *dbifl = NULL, *dbifl_iterator = NULL;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
while (iterator != NULL) {
if (!db_getinterfacecountbyname(iterator->interface)) {
datacache_status(&s->dcache);
dbiflistfree(&dbifl);
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void handleintsignals(DSTATE *s)
uint32_t bwlimit = 0;
datacache *iterator = *dc;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
snprintf(buffer, 1024, "Monitoring (%d): ", datacache_count(dc));
b = (unsigned int)strlen(buffer) + 1;
snprintf(errorstring, 1024, "Nothing to monitor");
}
printe(PT_Info);
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void interfacechangecheck(DSTATE *s)
uint32_t newhash;
int offset, found;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
/* get list of currently visible interfaces */
if (getiflist(&ifacelist, 0) == 0) {
free(ifacelist);
s->iflisthash = newhash;
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
uint32_t simplehash(const char *data, int len)
{
interfaceinfo info;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (!db_getinterfacecountbyname(interface)) {
return;
printf("}}");
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void jsondump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
dbdatalist *datalist = NULL, *datalist_i = NULL;
dbdatalistinfo datainfo;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
current = time(NULL);
yesterday = current - 86400;
}
dbdatalistfree(&datalist);
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void showlist(const interfaceinfo *interface, const char *listname, const char *databegin, const char *dataend)
dbdatalist *datalist = NULL, *datalist_i = NULL;
dbdatalistinfo datainfo;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (strcmp(listname, "day") == 0) {
listtype = LT_Day;
printf("\n");
}
dbdatalistfree(&datalist);
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void showoneline(const interfaceinfo *interface)
dbdatalist *datalist = NULL;
dbdatalistinfo datainfo;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
/* version string */
printf("%d;", ONELINEVERSION);
printf("%s;", getvalue(interface->txtotal, 1, RT_Normal));
printf("%s\n", getvalue(interface->rxtotal + interface->txtotal, 1, RT_Normal));
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void showhours(const interfaceinfo *interface)
dbdatalistinfo datainfo;
HOURDATA hourdata[24];
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
for (i = 0; i < 24; i++) {
hourdata[i].rx = hourdata[i].tx = 0;
}
printf("\n");
}
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int len)
#if HAVE_DECL_SQLITE_CHECKPOINT_RESTART
void db_walcheckpoint(void)
{
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (debug)
printf("wal checkpoint");
#if HAVE_DECL_SQLITE_CHECKPOINT_TRUNCATE
#else
sqlite3_wal_checkpoint_v2(db, NULL, SQLITE_CHECKPOINT_RESTART, NULL, NULL);
#endif
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
#endif
{
interfaceinfo info;
- timeused(__func__, 1);
+ timeused_debug(__func__, 1);
if (!db_getinterfacecountbyname(interface)) {
return;
printf(" </traffic>\n");
printf(" </interface>\n");
- timeused(__func__, 0);
+ timeused_debug(__func__, 0);
}
void xmldump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
}
}
- timeused("daemon_startup", 1);
+ timeused_debug("daemon_startup", 1);
/* load config if available */
if (!loadcfg(s.cfgfile)) {
}
}
- timeused("daemon_startup", 0);
+ timeused_debug("daemon_startup", 0);
s.running = 1;
#if defined(__linux__)
}
END_TEST
-START_TEST(timeused_outputs_something_expected)
+START_TEST(timeused_debug_outputs_something_expected_when_debug_is_enabled)
{
int pipe, len;
char buffer[512];
/* the assumption here is that the next two steps
can always execute in less than one second resulting
in a duration that starts with a zero */
- timeused("nothing", 1);
- timeused("something", 0);
+ timeused_debug("that_func", 1);
+ timeused_debug("that_func", 0);
fflush(stdout);
len = (int)read(pipe, buffer, 512);
- ck_assert_int_gt(len, 0);
- ck_assert_ptr_ne(strstr(buffer, "something() in 0"), NULL);
+ ck_assert_int_gt(len, 1);
+ ck_assert_ptr_ne(strstr(buffer, "that_func() in 0"), NULL);
+}
+END_TEST
+
+START_TEST(timeused_debug_does_not_output_anything_when_debug_is_disabled)
+{
+ int pipe, len;
+ char buffer[512];
+ memset(&buffer, '\0', sizeof(buffer));
+
+ defaultcfg();
+ debug = 0;
+ pipe = pipe_output();
+ /* the assumption here is that the next two steps
+ can always execute in less than one second resulting
+ in a duration that starts with a zero */
+ timeused_debug("other_func", 1);
+ timeused_debug("other_func", 0);
+ printf("-"); // stdout needs to contain something so that read doesn't block
+ fflush(stdout);
+
+ len = (int)read(pipe, buffer, 512);
+ ck_assert_int_eq(len, 1);
+}
+END_TEST
+
+START_TEST(timeused_tracks_used_time)
+{
+ int i, j = 0;
+ double used;
+
+ defaultcfg();
+
+ used = timeused("quick_func", 1);
+ ck_assert(used == 0.0);
+
+ /* assume that this spends some non-zero time */
+ for (i = 0; i < 10000; i++) {
+ j = j + 2;
+ }
+
+ used = timeused("quick_func", 0);
+ ck_assert(used > 0.0);
}
END_TEST
tcase_add_test(tc_common, isnumeric_it_is);
tcase_add_test(tc_common, isnumeric_it_is_not);
tcase_add_test(tc_common, getversion_returns_a_version);
- tcase_add_test(tc_common, timeused_outputs_something_expected);
+ tcase_add_test(tc_common, timeused_debug_outputs_something_expected_when_debug_is_enabled);
+ tcase_add_test(tc_common, timeused_debug_does_not_output_anything_when_debug_is_disabled);
+ tcase_add_test(tc_common, timeused_tracks_used_time);
tcase_add_exit_test(tc_common, can_panic, 1);
suite_add_tcase(s, tc_common);
}