hardlink: use monotonic time like other utils

- use out gettime_monotonic()
- use timeval for calculation rather than double

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-02-04 13:43:05 +01:00
parent ec194114e1
commit 06d8fe8909
2 changed files with 13 additions and 19 deletions

View File

@ -222,8 +222,8 @@ endif
if BUILD_HARDLINK if BUILD_HARDLINK
usrbin_exec_PROGRAMS += hardlink usrbin_exec_PROGRAMS += hardlink
hardlink_SOURCES = misc-utils/hardlink.c hardlink_SOURCES = misc-utils/hardlink.c lib/monotonic.c
hardlink_LDADD = $(LDADD) libcommon.la hardlink_LDADD = $(LDADD) libcommon.la $(REALTIME_LIBS)
hardlink_CFLAGS = $(AM_CFLAGS) hardlink_CFLAGS = $(AM_CFLAGS)
if HAVE_PCRE2_POSIX if HAVE_PCRE2_POSIX
hardlink_LDADD += $(PCRE2_POSIX_LIBS) hardlink_LDADD += $(PCRE2_POSIX_LIBS)

View File

@ -41,6 +41,7 @@
#include "c.h" #include "c.h"
#include "xalloc.h" #include "xalloc.h"
#include "strutils.h" #include "strutils.h"
#include "monotonic.h"
/* Use libpcre2posix if it's available */ /* Use libpcre2posix if it's available */
#ifdef HAVE_PCRE2_POSIX #ifdef HAVE_PCRE2_POSIX
@ -110,7 +111,7 @@ enum log_level {
* @xattr_comparisons: The number of extended attribute comparisons * @xattr_comparisons: The number of extended attribute comparisons
* @comparisons: The number of comparisons * @comparisons: The number of comparisons
* @saved: The (exaggerated) amount of space saved * @saved: The (exaggerated) amount of space saved
* @start_time: The time we started at, in seconds since some unspecified point * @start_time: The time we started at
*/ */
static struct statistics { static struct statistics {
int started; int started;
@ -119,7 +120,7 @@ static struct statistics {
size_t xattr_comparisons; size_t xattr_comparisons;
size_t comparisons; size_t comparisons;
double saved; double saved;
double start_time; struct timeval start_time;
} stats; } stats;
/** /**
@ -240,19 +241,6 @@ static const char *format(double bytes)
return buf; return buf;
} }
/**
* gettime() - Get the current time from the system
*/
static double gettime(void)
{
struct timeval tv = { 0, 0 };
if (gettimeofday(&tv, NULL) != 0)
jlog(JLOG_SYSERR, "Cannot read current time");
return (double) tv.tv_sec + (double) tv.tv_usec / 1000000;
}
/** /**
* regexec_any - Match against multiple regular expressions * regexec_any - Match against multiple regular expressions
* @pregs: A linked list of regular expressions * @pregs: A linked list of regular expressions
@ -323,6 +311,11 @@ static int compare_nodes_ino(const void *_a, const void *_b)
*/ */
static void print_stats(void) static void print_stats(void)
{ {
struct timeval end = { 0, 0 }, delta = { 0, 0 };
gettime_monotonic(&end);
timersub(&end, &stats.start_time, &delta);
jlog(JLOG_SUMMARY, "Mode: %s", opts.dry_run ? "dry-run" : "real"); jlog(JLOG_SUMMARY, "Mode: %s", opts.dry_run ? "dry-run" : "real");
jlog(JLOG_SUMMARY, "Files: %zu", stats.files); jlog(JLOG_SUMMARY, "Files: %zu", stats.files);
jlog(JLOG_SUMMARY, "Linked: %zu files", stats.linked); jlog(JLOG_SUMMARY, "Linked: %zu files", stats.linked);
@ -331,7 +324,8 @@ static void print_stats(void)
#endif #endif
jlog(JLOG_SUMMARY, "Compared: %zu files", stats.comparisons); jlog(JLOG_SUMMARY, "Compared: %zu files", stats.comparisons);
jlog(JLOG_SUMMARY, "Saved: %s", format(stats.saved)); jlog(JLOG_SUMMARY, "Saved: %s", format(stats.saved));
jlog(JLOG_SUMMARY, "Duration: %.2f seconds", gettime() - stats.start_time); jlog(JLOG_SUMMARY, "Duration: %ld.%06ld seconds",
(long)delta.tv_sec, (long)delta.tv_usec);
} }
/** /**
@ -1062,7 +1056,6 @@ int main(int argc, char *argv[])
/* Pretty print numeric output */ /* Pretty print numeric output */
setlocale(LC_NUMERIC, ""); setlocale(LC_NUMERIC, "");
stats.start_time = gettime();
if (atexit(to_be_called_atexit) != 0) if (atexit(to_be_called_atexit) != 0)
err(EXIT_FAILURE, _("cannot register exit handler")); err(EXIT_FAILURE, _("cannot register exit handler"));
@ -1072,6 +1065,7 @@ int main(int argc, char *argv[])
if (optind == argc) if (optind == argc)
errx(EXIT_FAILURE, _("no directory of dile specified")); errx(EXIT_FAILURE, _("no directory of dile specified"));
gettime_monotonic(&stats.start_time);
stats.started = TRUE; stats.started = TRUE;
for (; optind < argc; optind++) for (; optind < argc; optind++)