aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-12-18 17:19:04 +0000
committerNick Mathewson <nickm@torproject.org>2008-12-18 17:19:04 +0000
commitbf80e2df3fe48f81c69bb68887376b02ab247e5f (patch)
treed95e835992abf9a2bad5954042ea38e5a6a9df3c /src/common
parentb91335117f9faf84001acc65859886e4ddc267a5 (diff)
downloadtor-bf80e2df3fe48f81c69bb68887376b02ab247e5f.tar.gz
tor-bf80e2df3fe48f81c69bb68887376b02ab247e5f.zip
Replace calls to time(NULL) that occur on the order of once per read, one per write, or once per cell with calls to a function that looks at a cached value of time. This is tricksy to benchmark, since it will only help on systems where time() is a syscall and syscalls are relatively slow.
svn:r17690
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c33
-rw-r--r--src/common/util.h10
2 files changed, 42 insertions, 1 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 2cb95a59a9..b00d26cba4 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1285,8 +1285,39 @@ format_time_interval(char *out, size_t out_len, long interval)
}
/* =====
+ * Cached time
+ * ===== */
+
+#ifndef TIME_IS_FAST
+/** Cached estimate of the currrent time. Updated around once per second;
+ * may be a few seconds off if we are really busy. This is a hack to avoid
+ * calling time(NULL) (which not everybody has optimized) on critical paths.
+ */
+static time_t cached_approx_time = 0;
+
+/** Return a cached estimate of the current time from when
+ * update_approx_time() was last called. This is a hack to avoid calling
+ * time(NULL) on critical paths: please do not even think of calling it
+ * anywhere else. */
+time_t
+approx_time(void)
+{
+ return cached_approx_time;
+}
+
+/** Update the cached estimate of the current time. This function SHOULD be
+ * called once per second, and MUST be called before the first call to
+ * get_approx_time. */
+void
+update_approx_time(time_t now)
+{
+ cached_approx_time = now;
+}
+#endif
+
+/* =====
* Fuzzy time
- * XXXX022 Use this consistently or rip it out.
+ * XXXX022 Use this consistently or rip most of it out.
* ===== */
/* In a perfect world, everybody would run ntp, and ntp would be perfect, so
diff --git a/src/common/util.h b/src/common/util.h
index e8d78c9847..029cad9e3f 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -213,6 +213,16 @@ void format_iso_time(char *buf, time_t t);
int parse_iso_time(const char *buf, time_t *t);
int parse_http_time(const char *buf, struct tm *tm);
int format_time_interval(char *out, size_t out_len, long interval);
+
+/* Cached time */
+#ifdef TIME_IS_FAST
+#define approx_time() time(NULL)
+#define update_approx_time(t) STMT_NIL
+#else
+time_t approx_time(void);
+void update_approx_time(time_t now);
+#endif
+
/* Fuzzy time. */
void ftime_set_maximum_sloppiness(int seconds);
void ftime_set_estimated_skew(int seconds);