dovecot-1.2: Moved all struct timeval comparing/calculation code...

dovecot at dovecot.org dovecot at dovecot.org
Wed Aug 12 23:59:54 EEST 2009


details:   http://hg.dovecot.org/dovecot-1.2/rev/f834d95f173a
changeset: 9310:f834d95f173a
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Aug 12 16:59:47 2009 -0400
description:
Moved all struct timeval comparing/calculation code to lib/time-util.

diffstat:

8 files changed, 81 insertions(+), 26 deletions(-)
src/imap/imap-search.c                           |   12 ++---
src/lib-storage/index/index-search.c             |   15 +++---
src/lib-storage/index/maildir/maildir-filename.c |    5 --
src/lib/Makefile.am                              |    2 
src/lib/ioloop.c                                 |    9 +---
src/lib/time-util.c                              |   48 ++++++++++++++++++++++
src/lib/time-util.h                              |   12 +++++
src/plugins/fts-squat/squat-test.c               |    4 -

diffs (245 lines):

diff -r bac1371c18e4 -r f834d95f173a src/imap/imap-search.c
--- a/src/imap/imap-search.c	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/imap/imap-search.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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 clien
 	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 clien
 
 	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 clien
 	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 -r bac1371c18e4 -r f834d95f173a src/lib-storage/index/index-search.c
--- a/src/lib-storage/index/index-search.c	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/lib-storage/index/index-search.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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(
 		   !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 in
 {
 	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 in
 
 	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 -r bac1371c18e4 -r f834d95f173a src/lib-storage/index/maildir/maildir-filename.c
--- a/src/lib-storage/index/maildir/maildir-filename.c	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/lib-storage/index/maildir/maildir-filename.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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(vo
 	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 -r bac1371c18e4 -r f834d95f173a src/lib/Makefile.am
--- a/src/lib/Makefile.am	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/lib/Makefile.am	Wed Aug 12 16:59:47 2009 -0400
@@ -100,6 +100,7 @@ liblib_a_SOURCES = \
 	str-sanitize.c \
 	strescape.c \
 	strfuncs.c \
+	time-util.c \
 	unix-socket-create.c \
 	unlink-directory.c \
 	unlink-old-files.c \
@@ -189,6 +190,7 @@ headers = \
 	str-sanitize.h \
 	strescape.h \
 	strfuncs.h \
+	time-util.h \
 	unix-socket-create.h \
 	unlink-directory.h \
 	unlink-old-files.h \
diff -r bac1371c18e4 -r f834d95f173a src/lib/ioloop.c
--- a/src/lib/ioloop.c	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/lib/ioloop.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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>
@@ -246,12 +247,8 @@ static int timeout_cmp(const void *p1, c
 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_handle_timeouts_real(struct ioloop *ioloop)
diff -r bac1371c18e4 -r f834d95f173a src/lib/time-util.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/time-util.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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 -r bac1371c18e4 -r f834d95f173a src/lib/time-util.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/time-util.h	Wed Aug 12 16:59:47 2009 -0400
@@ -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
diff -r bac1371c18e4 -r f834d95f173a src/plugins/fts-squat/squat-test.c
--- a/src/plugins/fts-squat/squat-test.c	Wed Aug 12 15:27:36 2009 -0400
+++ b/src/plugins/fts-squat/squat-test.c	Wed Aug 12 16:59:47 2009 -0400
@@ -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 *arg
 	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)


More information about the dovecot-cvs mailing list