[coreboot-gerrit] New patch to review for coreboot: 8411bad timer: add stopwatch construct

Patrick Georgi (pgeorgi@google.com) gerrit at coreboot.org
Fri Mar 20 13:07:39 CET 2015


Patrick Georgi (pgeorgi at google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/8815

-gerrit

commit 8411bad90b8580082e0d5703f26e84ce85d70ccf
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Tue Sep 23 16:28:43 2014 -0500

    timer: add stopwatch construct
    
    There's a lot of places where expiration and running time are
    open coded. Allow for those places to be simplified by adding
    a stopwatch construct. The stopwatch can have an expiration or
    just be used to accumulate time.
    
    BUG=None
    TEST=Built and verified API works as expected by using implementation.
    
    Change-Id: Ibd636542b16d8554f1ff4512319a53dce81c97e5
    Signed-off-by: Patrick Georgi <pgeorgi at chromium.org>
    Original-Commit-Id: bc623a1b36eb08c5877591c4509cd61131c62617
    Original-Change-Id: I53604900fea7d46beeccc17f1dc7900d5f28518b
    Original-Signed-off-by: Aaron Durbin <adurbin at chromium.org>
    Original-Reviewed-on: https://chromium-review.googlesource.com/219492
    Original-Reviewed-by: Vadim Bendebury <vbendeb at chromium.org>
    Original-Reviewed-by: Furquan Shaikh <furquan at chromium.org>
    Original-Reviewed-by: David Hendricks <dhendrix at chromium.org>
---
 src/include/timer.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/src/include/timer.h b/src/include/timer.h
index b37cea4..b034da2 100644
--- a/src/include/timer.h
+++ b/src/include/timer.h
@@ -175,4 +175,64 @@ static inline long mono_time_diff_microseconds(const struct mono_time *t1,
 	return rela_time_in_microseconds(&rt);
 }
 
+struct stopwatch {
+	struct mono_time start;
+	struct mono_time current;
+	struct mono_time expires;
+};
+
+static inline void stopwatch_init(struct stopwatch *sw)
+{
+	timer_monotonic_get(&sw->start);
+	sw->current = sw->expires = sw->start;
+}
+
+static inline void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
+{
+	stopwatch_init(sw);
+	mono_time_add_usecs(&sw->expires, us);
+}
+
+static inline void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
+{
+	stopwatch_init_usecs_expire(sw, USECS_PER_MSEC * ms);
+}
+
+/*
+ * Tick the stopwatch to collect the current time.
+ */
+static inline void stopwatch_tick(struct stopwatch *sw)
+{
+	timer_monotonic_get(&sw->current);
+}
+
+/*
+ * Tick and check the stopwatch for expiration. Returns non-zero on exipration.
+ */
+static inline int stopwatch_expired(struct stopwatch *sw)
+{
+	stopwatch_tick(sw);
+	return !mono_time_before(&sw->current, &sw->expires);
+}
+
+/*
+ * Return number of microseconds since starting the stopwatch.
+ */
+static inline long stopwatch_duration_usecs(struct stopwatch *sw)
+{
+	/*
+	 * If the stopwatch hasn't been ticked (current == start) tick
+	 * the stopwatch to gather the accumulated time.
+	 */
+	if (!mono_time_cmp(&sw->start, &sw->current))
+		stopwatch_tick(sw);
+
+	return mono_time_diff_microseconds(&sw->start, &sw->current);
+}
+
+static inline long stopwatch_duration_msecs(struct stopwatch *sw)
+{
+	return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;
+}
+
 #endif /* TIMER_H */



More information about the coreboot-gerrit mailing list