From: Timo Sirainen Date: Wed, 12 Aug 2009 20:59:47 +0000 (-0400) Subject: Moved all struct timeval comparing/calculation code to lib/time-util. X-Git-Tag: 2.0.alpha1~283 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7ef3553585e556f35d5919589cfdc1de3329e4bb;p=thirdparty%2Fdovecot%2Fcore.git Moved all struct timeval comparing/calculation code to lib/time-util. --HG-- branch : HEAD --- diff --git a/src/imap/imap-search.c b/src/imap/imap-search.c index 8a979a31ca..63ab9ccde9 100644 --- a/src/imap/imap-search.c +++ b/src/imap/imap-search.c @@ -4,6 +4,7 @@ #include "ostream.h" #include "str.h" #include "seq-range-array.h" +#include "time-util.h" #include "imap-resp-code.h" #include "imap-quote.h" #include "imap-seqset.h" @@ -325,6 +326,7 @@ static bool cmd_search_more(struct client_command_context *cmd) unsigned int count; uint32_t id, id_min, id_max; const char *ok_reply; + int time_msecs; bool tryagain, minmax, lost_data; if (cmd->cancel) { @@ -408,12 +410,8 @@ static bool cmd_search_more(struct client_command_context *cmd) if (gettimeofday(&end_time, NULL) < 0) memset(&end_time, 0, sizeof(end_time)); - end_time.tv_sec -= ctx->start_time.tv_sec; - end_time.tv_usec -= ctx->start_time.tv_usec; - if (end_time.tv_usec < 0) { - end_time.tv_sec--; - end_time.tv_usec += 1000000; - } + + time_msecs = timeval_diff_msecs(&end_time, &ctx->start_time); sync_flags = MAILBOX_SYNC_FLAG_FAST; if (!cmd->uid || ctx->have_seqsets) @@ -421,7 +419,7 @@ static bool cmd_search_more(struct client_command_context *cmd) ok_reply = t_strdup_printf("OK %s%s completed (%d.%03d secs).", lost_data ? "["IMAP_RESP_CODE_EXPUNGEISSUED"] " : "", !ctx->sorting ? "Search" : "Sort", - (int)end_time.tv_sec, (int)(end_time.tv_usec/1000)); + time_msecs/1000, time_msecs%1000); return cmd_sync(cmd, sync_flags, 0, ok_reply); } diff --git a/src/lib-storage/index/index-search.c b/src/lib-storage/index/index-search.c index 364c3f4893..5d849bfdbf 100644 --- a/src/lib-storage/index/index-search.c +++ b/src/lib-storage/index/index-search.c @@ -6,6 +6,7 @@ #include "istream.h" #include "utc-offset.h" #include "str.h" +#include "time-util.h" #include "message-address.h" #include "message-date.h" #include "message-search.h" @@ -1120,10 +1121,8 @@ static void index_storage_search_notify(struct mailbox *box, !ctx->mail_ctx.progress_hidden) { percentage = ctx->mail_ctx.progress_cur * 100.0 / ctx->mail_ctx.progress_max; - msecs = (ioloop_timeval.tv_sec - - ctx->search_start_time.tv_sec) * 1000 + - (ioloop_timeval.tv_usec - - ctx->search_start_time.tv_usec) / 1000; + msecs = timeval_diff_msecs(&ioloop_timeval, + &ctx->search_start_time); secs = (msecs / (percentage / 100.0) - msecs) / 1000; T_BEGIN { @@ -1217,7 +1216,7 @@ static bool search_would_block(struct index_search_context *ctx) { struct timeval now; unsigned long long guess_cost; - long usecs; + long long usecs; bool ret; if (ctx->cost < ctx->next_time_check_cost) @@ -1225,9 +1224,9 @@ static bool search_would_block(struct index_search_context *ctx) if (gettimeofday(&now, NULL) < 0) i_fatal("gettimeofday() failed: %m"); - usecs = (now.tv_sec - ctx->last_nonblock_timeval.tv_sec) * 1000000 + - (now.tv_usec - ctx->last_nonblock_timeval.tv_usec); - if (usecs < 0 || now.tv_sec < 0) { + + usecs = timeval_diff_usecs(&now, &ctx->last_nonblock_timeval); + if (usecs < 0) { /* clock moved backwards. */ ctx->last_nonblock_timeval = now; ctx->next_time_check_cost = SEARCH_INITIAL_MAX_COST; diff --git a/src/lib-storage/index/maildir/maildir-filename.c b/src/lib-storage/index/maildir/maildir-filename.c index 9313ad7952..dbe1c1b75c 100644 --- a/src/lib-storage/index/maildir/maildir-filename.c +++ b/src/lib-storage/index/maildir/maildir-filename.c @@ -4,6 +4,7 @@ #include "ioloop.h" #include "array.h" #include "str.h" +#include "time-util.h" #include "hostpid.h" #include "maildir-storage.h" #include "maildir-keywords.h" @@ -17,9 +18,7 @@ const char *maildir_filename_generate(void) struct timeval tv; /* use secs + usecs to guarantee uniqueness within this process. */ - if (ioloop_timeval.tv_sec > last_tv.tv_sec || - (ioloop_timeval.tv_sec == last_tv.tv_sec && - ioloop_timeval.tv_usec > last_tv.tv_usec)) + if (timeval_cmp(&ioloop_timeval, &last_tv) > 0) tv = ioloop_timeval; else { tv = last_tv; diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 19e8c3dd3a..9c70ec2589 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -101,6 +101,7 @@ liblib_la_SOURCES = \ str-sanitize.c \ strescape.c \ strfuncs.c \ + time-util.c \ unix-socket-create.c \ unlink-directory.c \ unlink-old-files.c \ @@ -190,6 +191,7 @@ headers = \ str-sanitize.h \ strescape.h \ strfuncs.h \ + time-util.h \ unix-socket-create.h \ unlink-directory.h \ unlink-old-files.h \ diff --git a/src/lib/ioloop.c b/src/lib/ioloop.c index 2fe3248e17..ca3e5f5642 100644 --- a/src/lib/ioloop.c +++ b/src/lib/ioloop.c @@ -1,6 +1,7 @@ /* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */ #include "lib.h" +#include "time-util.h" #include "ioloop-internal.h" #include @@ -247,12 +248,8 @@ int io_loop_get_wait_time(struct ioloop *ioloop, struct timeval *tv_r) static int timeout_cmp(const void *p1, const void *p2) { const struct timeout *to1 = p1, *to2 = p2; - int diff; - diff = to1->next_run.tv_sec - to2->next_run.tv_sec; - if (diff == 0) - diff = to1->next_run.tv_usec - to2->next_run.tv_usec; - return diff; + return timeval_cmp(&to1->next_run, &to2->next_run); } static void io_loop_default_time_moved(time_t old_time, time_t new_time) diff --git a/src/lib/time-util.c b/src/lib/time-util.c new file mode 100644 index 0000000000..33237a7dc2 --- /dev/null +++ b/src/lib/time-util.c @@ -0,0 +1,48 @@ +/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "time-util.h" + +#include + +int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2) +{ + if (tv1->tv_sec < tv2->tv_sec) + return -1; + if (tv1->tv_sec > tv2->tv_sec) + return 1; + if (tv1->tv_usec < tv2->tv_usec) + return -1; + if (tv1->tv_usec > tv2->tv_usec) + return 1; + return 0; +} + +int timeval_diff_msecs(const struct timeval *tv1, const struct timeval *tv2) +{ + time_t secs; + int usecs; + + secs = tv1->tv_sec - tv2->tv_sec; + usecs = tv1->tv_usec - tv2->tv_usec; + if (usecs < 0) { + secs++; + usecs += 1000000; + } + return (secs * 1000) + (usecs/1000); +} + +long long timeval_diff_usecs(const struct timeval *tv1, + const struct timeval *tv2) +{ + time_t secs; + int usecs; + + secs = tv1->tv_sec - tv2->tv_sec; + usecs = tv1->tv_usec - tv2->tv_usec; + if (usecs < 0) { + secs++; + usecs += 1000000; + } + return ((long long)secs * 1000000ULL) + usecs; +} diff --git a/src/lib/time-util.h b/src/lib/time-util.h new file mode 100644 index 0000000000..dab2ea9121 --- /dev/null +++ b/src/lib/time-util.h @@ -0,0 +1,12 @@ +#ifndef TIME_UTIL_H +#define TIME_UTIL_H + +/* Returns -1 if tv1tv2, 0 if they're equal. */ +int timeval_cmp(const struct timeval *tv1, const struct timeval *tv2); +/* Returns tv1-tv2 in milliseconds. */ +int timeval_diff_msecs(const struct timeval *tv1, const struct timeval *tv2); +/* Returns tv1-tv2 in microseconds. */ +long long timeval_diff_usecs(const struct timeval *tv1, + const struct timeval *tv2); + +#endif diff --git a/src/plugins/fts-squat/squat-test.c b/src/plugins/fts-squat/squat-test.c index 4dec5d3060..5b35e7fcf5 100644 --- a/src/plugins/fts-squat/squat-test.c +++ b/src/plugins/fts-squat/squat-test.c @@ -4,6 +4,7 @@ #include "array.h" #include "file-lock.h" #include "istream.h" +#include "time-util.h" #include "unichar.h" #include "squat-trie.h" #include "squat-uidlist.h" @@ -142,8 +143,7 @@ int main(int argc ATTR_UNUSED, char *argv[]) cputime = (double)(clock_end - clock_start) / CLOCKS_PER_SEC; fprintf(stderr, "\n - Index time: %.2f CPU seconds, " "%.2f real seconds (%.02fMB/CPUs)\n", cputime, - (tv_end.tv_sec - tv_start.tv_sec) + - (tv_end.tv_usec - tv_start.tv_usec)/1000000.0, + timeval_diff_msecs(&tv_end, &tv_start)/1000.0, input->v_offset / cputime / (1024*1024)); if (stat(trie_path, &trie_st) < 0)