From 6e1bd93a82368047880b109861df5a61c659d51d Mon Sep 17 00:00:00 2001 From: ketmar Date: Tue, 30 Jul 2013 15:37:52 +0000 Subject: [PATCH] using k8clock() instead of clock() FossilOrigin-Name: 1d28f0bb12a3809028dd61dacbfbf2cca9c2ec96bad07c3f3ed4f849b16269e3 --- src/Jamfile | 5 ++- src/fusedrv/Jamfile | 4 +- src/fusedrv/fusedrv.c | 17 ++++---- src/fusedrv/translit.c | 1 - src/fusedrv/translit.h | 1 - src/k8clock.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ src/k8clock.h | 38 ++++++++++++++++++ src/tagscan.c | 9 +++-- src/tagsearch.c | 15 +++---- 9 files changed, 169 insertions(+), 25 deletions(-) delete mode 120000 src/fusedrv/translit.c delete mode 120000 src/fusedrv/translit.h create mode 100644 src/k8clock.c create mode 100644 src/k8clock.h diff --git a/src/Jamfile b/src/Jamfile index b599d25..976a1b3 100644 --- a/src/Jamfile +++ b/src/Jamfile @@ -2,12 +2,13 @@ SubDir TOP src ; Library libtranslit.a : translit.c ; +Library libk8clock.a : k8clock.c ; Main tagscan : tagscan.c ; -LinkLibraries tagscan : libdbg.a libtranslit.a ; +LinkLibraries tagscan : libdbg.a libtranslit.a libk8clock.a ; Main tagsearch : tagsearch.c ; -LinkLibraries tagsearch : libdbg.a ; +LinkLibraries tagsearch : libdbg.a libk8clock.a ; Main tagview : tagview.c ; diff --git a/src/fusedrv/Jamfile b/src/fusedrv/Jamfile index 87970b9..30e202a 100644 --- a/src/fusedrv/Jamfile +++ b/src/fusedrv/Jamfile @@ -1,8 +1,8 @@ SubDir TOP src fusedrv ; -Main muffin : fusedrv.c translit.c ; -LinkLibraries muffin : libdbg.a ; +Main muffin : fusedrv.c ; +LinkLibraries muffin : libdbg.a libtranslit.a libk8clock.a ; SubInclude TOP src libdbg ; diff --git a/src/fusedrv/fusedrv.c b/src/fusedrv/fusedrv.c index f3281a8..88820f8 100644 --- a/src/fusedrv/fusedrv.c +++ b/src/fusedrv/fusedrv.c @@ -44,7 +44,8 @@ #include "../libdbg/dbglog.h" #include "../uthash.h" -#include "translit.h" +#include "../translit.h" +#include "../k8clock.h" //////////////////////////////////////////////////////////////////////////////// @@ -1240,19 +1241,19 @@ static int opt_processor (void *data, const char *arg, int key, struct fuse_args if (key == FUSE_OPT_KEY_NONOPT) { // not an option; first non-option arg is tagdb file name if (!tagfile_loaded) { - clock_t stt, ste; + uint64_t stt, ste; tagfilename = get_real_path(arg, 0); if (tagfilename == NULL) { fprintf(stderr, "FATAL: can't determine tag file name!\n"); return -1; } printf("loading database...\n"); - stt = clock(); + stt = k8clock(); if (tagdb_load(tagfilename) != 0) { fprintf(stderr, "FATAL: can't load tagfile: '%s'!\n", tagfilename); return -1; } - ste = clock(); - printf("database load time: %.15g seconds\n", (double)(ste-stt)/(double)CLOCKS_PER_SEC); + ste = k8clock(); + printf("database load time: %.15g seconds\n", (double)(ste-stt)/1000.0); printf("building hashes...\n"); - stt = clock(); + stt = k8clock(); build_hashes(); - ste = clock(); - printf("hash build time: %.15g seconds\n", (double)(ste-stt)/(double)CLOCKS_PER_SEC); + ste = k8clock(); + printf("hash build time: %.15g seconds\n", (double)(ste-stt)/1000.0); //if (file_info_count == 0) { fprintf(stderr, "FATAL: no valid files in tagfile: '%s'!\n", arg); exit(1); } tagfile_loaded = 1; return 0; diff --git a/src/fusedrv/translit.c b/src/fusedrv/translit.c deleted file mode 120000 index c1978eb..0000000 --- a/src/fusedrv/translit.c +++ /dev/null @@ -1 +0,0 @@ -../translit.c \ No newline at end of file diff --git a/src/fusedrv/translit.h b/src/fusedrv/translit.h deleted file mode 120000 index 33c8df1..0000000 --- a/src/fusedrv/translit.h +++ /dev/null @@ -1 +0,0 @@ -../translit.h \ No newline at end of file diff --git a/src/k8clock.c b/src/k8clock.c new file mode 100644 index 0000000..ddbf9f6 --- /dev/null +++ b/src/k8clock.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +#include "k8clock.h" + + +k8clock_type k8clock_initialized = K8CLOCK_ERROR; +k8clock_t clk; + + +#ifndef CLOCK_MONOTONIC_RAW +# define K8CLOCK_CLOCK_TYPE CLOCK_MONOTONIC_RAW +#else +# define K8CLOCK_CLOCK_TYPE CLOCK_MONOTONIC +#endif + + +static k8clock_type k8clock_check_source (void) { + FILE *fl = fopen("/sys/devices/system/clocksource/clocksource0/current_clocksource", "r"); + char buf[128]; + if (fl == NULL) { + fprintf(stderr, "WARNING: can't determine clock source!\n"); + return K8CLOCK_OTHER; + } + memset(buf, 0, sizeof(buf)); + if (fgets(buf, sizeof(buf)-1, fl) == NULL) { + fclose(fl); + fprintf(stderr, "WARNING: can't determine clock source!\n"); + return K8CLOCK_OTHER; + } + fclose(fl); + while (buf[0] && isspace(buf[strlen(buf)-1])) buf[strlen(buf)-1] = 0; + //fprintf(stderr, "clock source: %s\n", buf); + return (strcasecmp(buf, "hpet") == 0 ? K8CLOCK_HPET : K8CLOCK_OTHER); +} + + +__attribute__((constructor)) static void k8clock_ctor (void) { + if (k8clock_initialized == K8CLOCK_ERROR) { + struct timespec cres; + k8clock_initialized = k8clock_check_source(); + if (clock_getres(K8CLOCK_CLOCK_TYPE, &cres) != 0) { + fprintf(stderr, "ERROR: can't get clock resolution!\n"); + k8clock_initialized = K8CLOCK_ERROR; + return; + } + //fprintf(stderr, "CLOCK_MONOTONIC: %ld:%ld\n", cres.tv_sec, cres.tv_nsec); + if (cres.tv_sec > 0 || cres.tv_nsec > (long)1000000*10 /*10 ms*/) { + fprintf(stderr, "ERROR: real-time clock resolution is too low!\n"); + k8clock_initialized = K8CLOCK_ERROR; + return; + } + k8clock_init(&clk); + } +} + + +void k8clock_init (k8clock_t *clk) { + if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) { + if (clock_gettime(K8CLOCK_CLOCK_TYPE, &clk->stt) != 0) { + fprintf(stderr, "ERROR: can't get real-time clock value!\n"); + } + } +} + + +uint64_t k8clock_ms (k8clock_t *clk) { + if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) { + struct timespec ts; + if (clock_gettime(K8CLOCK_CLOCK_TYPE, &ts) != 0) { + fprintf(stderr, "ERROR: can't get real-time clock value!\n"); + return 0; + } + // ah, ignore nanoseconds in clk->stt here: we need only 'differential' time, and it can start with something weird + return ((int64_t)(ts.tv_sec-clk->stt.tv_sec))*1000+(ts.tv_nsec-clk->stt.tv_nsec)/1000000+1; + } + return 0; +} + + +uint64_t k8clock_micro (k8clock_t *clk) { + if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) { + struct timespec ts; + if (clock_gettime(K8CLOCK_CLOCK_TYPE, &ts) != 0) { + fprintf(stderr, "ERROR: can't get real-time clock value!\n"); + return 0; + } + // ah, ignore nanoseconds in clk->stt here: we need only 'differential' time, and it can start with something weird + return ((int64_t)(ts.tv_sec-clk->stt.tv_sec))*1000000+(ts.tv_nsec-clk->stt.tv_nsec)/1000+1; + } + return 0; +} + + +uint64_t k8clock (void) { + return k8clock_ms(&clk); +} + + +uint64_t k8clockmicro (void) { + return k8clock_micro(&clk); +} diff --git a/src/k8clock.h b/src/k8clock.h new file mode 100644 index 0000000..1a62027 --- /dev/null +++ b/src/k8clock.h @@ -0,0 +1,38 @@ +#ifndef _K8CLOCK_H_ +#define _K8CLOCK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + + +typedef enum { + K8CLOCK_ERROR = -1, + K8CLOCK_HPET = 0, + K8CLOCK_OTHER = 1 +} k8clock_type; + + +typedef struct { + struct timespec stt; +} k8clock_t; + + +extern k8clock_type k8clock_initialized; + + +extern void k8clock_init (k8clock_t *clk); +extern uint64_t k8clock_ms (k8clock_t *clk); // milliseconds; (0: no timer available) +extern uint64_t k8clock_micro (k8clock_t *clk); // microseconds; (0: no timer available) + +extern uint64_t k8clock (void); // milliseconds; (0: no timer available) +extern uint64_t k8clockmicro (void); // microseconds; (0: no timer available) + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/tagscan.c b/src/tagscan.c index 692de7f..3b5f56f 100644 --- a/src/tagscan.c +++ b/src/tagscan.c @@ -39,6 +39,7 @@ #include "libdbg/dbglog.h" #include "translit.h" #include "uthash.h" +#include "k8clock.h" //////////////////////////////////////////////////////////////////////////////// @@ -1376,7 +1377,7 @@ error: //////////////////////////////////////////////////////////////////////////////// static void ts_build_hashes (void) { - clock_t stt; + uint64_t stt; int was_report = 0; fileinfo_t *snhash = NULL, *xf; // 'shortname' hash (sanity check) string_t *snv; @@ -1433,7 +1434,7 @@ do_other: // add fileinfo (and doing stat here) printf("checking %u files...\n", file_count); - stt = clock(); + stt = k8clock(); for (uint32_t f = 0; f < file_count; ++f) { fl_fileinfo_t *tfi = get_fi(f); const char *fname = get_str(tfi->realname); @@ -1483,8 +1484,8 @@ do_other: fi->genre_o = add_tag(fi, tfi->genre_o, TFL_GENRE_O); fi->genre_t = add_tag(fi, tfi->genre_t, TFL_GENRE_T); if (f%1024 == 0) { - clock_t ctime = clock(); - if ((ctime-stt)/CLOCKS_PER_SEC >= 1) { + uint64_t ctime = k8clock(); + if ((ctime-stt)/1000 >= 1) { was_report = 1; fprintf(stdout, "\r[%u/%u] files processed", f+1, file_count); fflush(stdout); diff --git a/src/tagsearch.c b/src/tagsearch.c index 9f19def..35be2ea 100644 --- a/src/tagsearch.c +++ b/src/tagsearch.c @@ -38,6 +38,7 @@ #include "translit.h" #include "uthash.h" +#include "k8clock.h" //////////////////////////////////////////////////////////////////////////////// @@ -66,20 +67,20 @@ //////////////////////////////////////////////////////////////////////////////// int main (int argc, char *argv[]) { - clock_t stt, ste; + uint64_t stt, ste; dbglog_set_screenout(1); dbglog_set_fileout(0); // printf("loading database...\n"); - stt = clock(); + stt = k8clock(); if (tagdb_load("tags.dat") != 0) { fprintf(stderr, "FATAL: can't open tagfile!\n"); return -1; } - ste = clock(); - printf("database load time: %.15g seconds\n", (double)(ste-stt)/(double)CLOCKS_PER_SEC); + ste = k8clock(); + printf("database load time: %.15g seconds\n", (double)(ste-stt)/1000.0); printf("building hashes...\n"); - stt = clock(); + stt = k8clock(); build_hashes(); - ste = clock(); - printf("hash build time: %.15g seconds\n", (double)(ste-stt)/(double)CLOCKS_PER_SEC); + ste = k8clock(); + printf("hash build time: %.15g seconds\n", (double)(ste-stt)/1000.0); if (argc > 1) { for (int f = 1; f < argc; ++f) { dirinfo_t *di = list_dir(argv[f]); -- 2.11.4.GIT