diff mbox series

[XTF,5/6] time: Add cycles2{n,u,m}sec functions

Message ID 20200417070528.48329-6-wipawel@amazon.de (mailing list archive)
State New, archived
Headers show
Series Add time management functionality | expand

Commit Message

Wieczorkiewicz, Pawel April 17, 2020, 7:05 a.m. UTC
In order to easily translate CPU cycles to time values add the
following helpers:
- cycles2nsec()
- cycles2usec()
- cycles2msec()

Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
---
 common/time.c      | 17 +++++++++++++++++
 include/xtf/time.h |  5 ++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/common/time.c b/common/time.c
index 3db1f8f..8f73243 100644
--- a/common/time.c
+++ b/common/time.c
@@ -162,6 +162,23 @@  void msleep(uint64_t t)
     mspin_sleep(t);
 }
 
+unsigned long cycles2nsec(uint64_t cycles)
+{
+    return scale_delta(cycles,
+            shared_info.vcpu_info[0].time.tsc_to_system_mul,
+            shared_info.vcpu_info[0].time.tsc_shift);
+}
+
+unsigned long cycles2usec(uint64_t cycles)
+{
+    return cycles2nsec(cycles) / 1000;
+}
+
+unsigned long cycles2msec(uint64_t cycles)
+{
+    return cycles2nsec(cycles) / 1000000;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/include/xtf/time.h b/include/xtf/time.h
index 07fcae5..6aa1efc 100644
--- a/include/xtf/time.h
+++ b/include/xtf/time.h
@@ -43,10 +43,13 @@  void msleep(uint64_t f);
 
 int gettimeofday(struct timeval *tp);
 
-
 /* This returns the current epoch time */
 #define NOW() current_time()
 
+unsigned long cycles2nsec(uint64_t cycles);
+unsigned long cycles2usec(uint64_t cycles);
+unsigned long cycles2msec(uint64_t cycles);
+
 #endif /* XTF_TIME_H */
 
 /*