]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Moved all struct timeval comparing/calculation code to lib/time-util.
authorTimo Sirainen <tss@iki.fi>
Wed, 12 Aug 2009 20:59:47 +0000 (16:59 -0400)
committerTimo Sirainen <tss@iki.fi>
Wed, 12 Aug 2009 20:59:47 +0000 (16:59 -0400)
--HG--
branch : HEAD

src/imap/imap-search.c
src/lib-storage/index/index-search.c
src/lib-storage/index/maildir/maildir-filename.c
src/lib/Makefile.am
src/lib/ioloop.c
src/lib/time-util.c [new file with mode: 0644]
src/lib/time-util.h [new file with mode: 0644]
src/plugins/fts-squat/squat-test.c

index 8a979a31ca9e491b2a9707476e6ba3b3a58363c3..63ab9ccde905a6caef7c1bffd63f2947e39ed8e5 100644 (file)
@@ -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);
 }
 
index 364c3f48934a93ff4e6b502dbd6e0f4a3879e784..5d849bfdbfe66ad0e7354ba10c03bd99c94a96ee 100644 (file)
@@ -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;
index 9313ad79525d229fcdd232549baa07220e608b51..dbe1c1b75ce086566881b468bd2fe29d6f207668 100644 (file)
@@ -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;
index 19e8c3dd3a2d86be6cc10fe864b51d785e76cde6..9c70ec25899059eca173f9eecf174be9667bd8f3 100644 (file)
@@ -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 \
index 2fe3248e17dd6ab220a4118abb7496eb430a1391..ca3e5f5642df5567ff8adf4eaaa46f685b0f77a3 100644 (file)
@@ -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 <unistd.h>
@@ -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 (file)
index 0000000..33237a7
--- /dev/null
@@ -0,0 +1,48 @@
+/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "time-util.h"
+
+#include <sys/time.h>
+
+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 (file)
index 0000000..dab2ea9
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef TIME_UTIL_H
+#define TIME_UTIL_H
+
+/* Returns -1 if tv1<tv2, 1 if tv1>tv2, 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
index 4dec5d3060ebe3d32e1a8af1bcc7690c16dac4f4..5b35e7fcf57d491251e22ac1590d1948e0e84975 100644 (file)
@@ -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)