From 83fdb412ad6b4314b9c875b7f8bbf8d548ff199a Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 15 May 2008 19:18:41 +0200 Subject: [PATCH] Add more accurate time measurement. --- cmake/Modules/FindRT.cmake | 74 ++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 3 ++ src/csync.c | 39 ++++++++++---------- src/csync_time.c | 42 ++++++++++++++++++++++ src/csync_time.h | 4 +++ 5 files changed, 144 insertions(+), 18 deletions(-) create mode 100644 cmake/Modules/FindRT.cmake diff --git a/cmake/Modules/FindRT.cmake b/cmake/Modules/FindRT.cmake new file mode 100644 index 000000000..412ef01f7 --- /dev/null +++ b/cmake/Modules/FindRT.cmake @@ -0,0 +1,74 @@ +# - Try to find RT +# Once done this will define +# +# RT_FOUND - system has RT +# RT_INCLUDE_DIRS - the RT include directory +# RT_LIBRARIES - Link these to use RT +# RT_DEFINITIONS - Compiler switches required for using RT +# +# Copyright (c) 2008 Andreas Schneider +# +# Redistribution and use is allowed according to the terms of the New +# BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# + + +if (RT_LIBRARIES AND RT_INCLUDE_DIRS) + # in cache already + set(RT_FOUND TRUE) +else (RT_LIBRARIES AND RT_INCLUDE_DIRS) + find_path(RT_INCLUDE_DIR + NAMES + time.h + PATHS + /usr/include + /usr/local/include + /opt/local/include + /sw/include + ) + + find_library(RT_LIBRARY + NAMES + rt + PATHS + /usr/lib + /usr/local/lib + /opt/local/lib + /sw/lib + ) + + if (RT_LIBRARY) + set(RT_FOUND TRUE) + endif (RT_LIBRARY) + + set(RT_INCLUDE_DIRS + ${RT_INCLUDE_DIR} + ) + + if (RT_FOUND) + set(RT_LIBRARIES + ${RT_LIBRARIES} + ${RT_LIBRARY} + ) + endif (RT_FOUND) + + if (RT_INCLUDE_DIRS AND RT_LIBRARIES) + set(RT_FOUND TRUE) + endif (RT_INCLUDE_DIRS AND RT_LIBRARIES) + + if (RT_FOUND) + if (NOT RT_FIND_QUIETLY) + message(STATUS "Found RT: ${RT_LIBRARIES}") + endif (NOT RT_FIND_QUIETLY) + else (RT_FOUND) + if (RT_FIND_REQUIRED) + message(FATAL_ERROR "Could not find RT") + endif (RT_FIND_REQUIRED) + endif (RT_FOUND) + + # show the RT_INCLUDE_DIRS and RT_LIBRARIES variables only in the advanced view + mark_as_advanced(RT_INCLUDE_DIRS RT_LIBRARIES) + +endif (RT_LIBRARIES AND RT_INCLUDE_DIRS) + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c55d7f3b2..7aa3e55d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ add_subdirectory(std) find_package(Sqlite3 REQUIRED) find_package(Iniparser REQUIRED) find_package(Dlfcn REQUIRED) +find_package(RT REQUIRED) set(CSYNC_PUBLIC_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} @@ -14,6 +15,7 @@ set(CSYNC_PUBLIC_INCLUDE_DIRS set(CSYNC_PRIVATE_INCLUDE_DIRS ${DLFCN_INCLUDE_DIRS} + ${RT_INCLUDE_DIRS} ${INIPARSER_INCLUDE_DIRS} ${LOG4C_INCLUDE_DIRS} ${SQLITE3_INCLUDE_DIRS} @@ -30,6 +32,7 @@ set(CSYNC_LINK_LIBRARIES ${CSYNC_LIBRARY} ${CSTDLIB_LIBRARY} ${DLFCN_LIBRARIES} + ${RT_LIBRARIES} ${INIPARSER_LIBRARIES} ${LOG4C_LIBRARIES} ${SQLITE3_LIBRARIES} diff --git a/src/csync.c b/src/csync.c index 3e91282b6..d23e6e21a 100644 --- a/src/csync.c +++ b/src/csync.c @@ -80,6 +80,8 @@ int csync_create(CSYNC **csync, const char *local, const char *remote) { CSYNC *ctx; size_t len = 0; + printf("%f\n", 1E9); + ctx = c_malloc(sizeof(CSYNC)); if (ctx == NULL) { return -1; @@ -282,7 +284,7 @@ out: int csync_update(CSYNC *ctx) { int rc = -1; - time_t start, finish; + struct timespec start, finish; if (ctx == NULL) { errno = EBADF; @@ -292,16 +294,17 @@ int csync_update(CSYNC *ctx) { csync_memstat_check(); /* update detection for local replica */ - time(&start); + clock_gettime(CLOCK_REALTIME, &start); ctx->current = LOCAL_REPLICA; ctx->replica = ctx->local.type; rc = csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH); - time(&finish); + clock_gettime(CLOCK_REALTIME, &finish); + CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Update detection for local replica took %.2f seconds walking %lu files.", - difftime(finish, start), c_rbtree_size(ctx->local.tree)); + csync_secdiff(finish, start), c_rbtree_size(ctx->local.tree)); csync_memstat_check(); if (rc < 0) { @@ -309,17 +312,17 @@ int csync_update(CSYNC *ctx) { } /* update detection for remote replica */ - time(&start); + clock_gettime(CLOCK_REALTIME, &start); ctx->current = REMOTE_REPLCIA; ctx->replica = ctx->remote.type; rc = csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH); - time(&finish); + clock_gettime(CLOCK_REALTIME, &finish); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Update detection for remote replica took %.2f seconds walking %lu files.", - difftime(finish, start), c_rbtree_size(ctx->remote.tree)); + csync_secdiff(finish, start), c_rbtree_size(ctx->remote.tree)); csync_memstat_check(); if (rc < 0) { @@ -333,7 +336,7 @@ int csync_update(CSYNC *ctx) { int csync_reconcile(CSYNC *ctx) { int rc = -1; - time_t start, finish; + struct timespec start, finish; if (ctx == NULL) { errno = EBADF; @@ -341,32 +344,32 @@ int csync_reconcile(CSYNC *ctx) { } /* Reconciliation for local replica */ - time(&start); + clock_gettime(CLOCK_REALTIME, &start); ctx->current = LOCAL_REPLICA; ctx->replica = ctx->local.type; rc = csync_reconcile_updates(ctx); - time(&finish); + clock_gettime(CLOCK_REALTIME, &finish); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Reconciliation for local replica took %.2f seconds visiting %llu files.", - difftime(finish, start), c_rbtree_size(ctx->local.tree)); + csync_secdiff(finish, start), c_rbtree_size(ctx->local.tree)); if (rc < 0) { return -1; } /* Reconciliation for local replica */ - time(&start); + clock_gettime(CLOCK_REALTIME, &start); ctx->current = REMOTE_REPLCIA; ctx->replica = ctx->remote.type; rc = csync_reconcile_updates(ctx); - time(&finish); + clock_gettime(CLOCK_REALTIME, &finish); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Reconciliation for local replica took %.2f seconds visiting %llu files.", - difftime(finish, start), c_rbtree_size(ctx->remote.tree)); + csync_secdiff(finish, start), c_rbtree_size(ctx->remote.tree)); if (rc < 0) { return -1; @@ -385,7 +388,7 @@ static void tree_destructor(void *data) { } int csync_destroy(CSYNC *ctx) { - time_t start, finish; + struct timespec start, finish; char *lock = NULL; char *journal = NULL; int jwritten = 0; @@ -399,13 +402,13 @@ int csync_destroy(CSYNC *ctx) { if (ctx->journal.db != NULL) { if (ctx->status >= CSYNC_DONE) { - time(&start); + clock_gettime(CLOCK_REALTIME, &start); if (csync_journal_write(ctx) == 0) { jwritten = 1; - time(&finish); + clock_gettime(CLOCK_REALTIME, &finish); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Writing the journal to disk took %.2f seconds", - difftime(finish, start)); + csync_secdiff(finish, start)); } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to write journal: %s", strerror(errno)); diff --git a/src/csync_time.c b/src/csync_time.c index 909a65e88..34e662241 100644 --- a/src/csync_time.c +++ b/src/csync_time.c @@ -116,3 +116,45 @@ out: return timediff; } +struct timespec csync_tsub(struct timespec timespec1, struct timespec timespec2) { + struct timespec ret; + int xsec = 0; + int sign = 1; + + if (timespec2.tv_nsec > timespec1.tv_nsec) { + xsec = (int) ((timespec2.tv_nsec - timespec1.tv_nsec) / (1E9 + 1)); + timespec2.tv_nsec -= (long int) (1E9 * xsec); + timespec2.tv_sec += xsec; + } + + if ((timespec1.tv_nsec - timespec2.tv_nsec) > 1E9) { + xsec = (int) ((timespec1.tv_nsec - timespec2.tv_nsec) / 1E9); + timespec2.tv_nsec += (long int) (1E9 * xsec); + timespec2.tv_sec -= xsec; + } + + ret.tv_sec = timespec1.tv_sec - timespec2.tv_sec; + ret.tv_nsec = timespec1.tv_nsec - timespec2.tv_nsec; + + if (timespec1.tv_sec < timespec2.tv_sec) { + sign = -1; + } + + ret.tv_sec = ret.tv_sec * sign; + + return ret; +} + +double csync_secdiff(struct timespec clock1, struct timespec clock2) { + double ret; + struct timespec diff; + + diff = csync_tsub(clock1, clock2); + + ret = diff.tv_sec; + ret += (double) diff.tv_nsec / (double) 1E9; + + return ret; +} + + diff --git a/src/csync_time.h b/src/csync_time.h index 3a87b9008..ab0c74dd8 100644 --- a/src/csync_time.h +++ b/src/csync_time.h @@ -29,4 +29,8 @@ time_t csync_timediff(CSYNC *ctx); +struct timespec csync_tsub(struct timespec timespec_1,struct timespec timespec_2); + +double csync_secdiff(struct timespec clock_1, struct timespec clock_2); + #endif /* _CSYNC_TIME_H */