Skip to contentSkip to conversation

Add time management functionality

PATCH New Pawel Wieczorkiewicz 1 participant 7 messages Apr 17, 2020 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

cover Add time management functionality

This is a 2nd attempt to contribute time management functionality to XTF. It is also a 2nd batch of the changes I want to sent upstream.

The patches add common/time.c core with scaling and system time calculating functions.
Other patches add helper functions based on the core functionality.

Finally, there is also a little change adding EVTCHNOP_bind_vcpu defininitions.

Paul Semel (4):
time: introduce time managment in xtf
time: add current_time() function to time management
time: add gettimeofday() function to time managment
time: Add helper functions and macros to time management

Pawel Wieczorkiewicz (2):
time: Add cycles2{n,u,m}sec functions
event_channels: Add EVTCHNOP_bind_vcpu hypercall support

 build/files.mk              |   1 +
 common/time.c               | 190 ++++++++++++++++++++++++++++++++++++++++++++
 include/xen/event_channel.h |   7 ++
 include/xtf/lib.h           |  18 +++++
 include/xtf/time.h          |  63 +++++++++++++++
 5 files changed, 279 insertions(+)
 create mode 100644 common/time.c
 create mode 100644 include/xtf/time.h
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#1 lore raw

PATCH 1/6 time: introduce time managment in xtf

From: Paul Semel <[email protected]>

This commit introduces basic time management functionality. For synchronization purpose, we do really want to be able to "control" time.

Add since_boot_time() that gets the time in nanoseconds from the moment the VM has booted.

---
 build/files.mk     |  1 +
 common/time.c      | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/xtf/lib.h  | 18 ++++++++++++
 include/xtf/time.h | 28 ++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 common/time.c
 create mode 100644 include/xtf/time.h
build/files.mk +1 -0
index dfa27e4..6286317 100644
@@ -16,6 +16,7 @@ obj-perarch += $(ROOT)/common/libc/vsnprintf.o
 obj-perarch += $(ROOT)/common/report.o
 obj-perarch += $(ROOT)/common/setup.o
 obj-perarch += $(ROOT)/common/xenbus.o
+obj-perarch += $(ROOT)/common/time.o
 
 obj-perenv += $(ROOT)/arch/x86/decode.o
 obj-perenv += $(ROOT)/arch/x86/desc.o
common/time.c new file +85 -0

85 lines not on this page · raw message

include/xtf/lib.h +18 -0
index 3348464..c3eb10c 100644
@@ -54,6 +54,24 @@ void __noreturn panic(const char *fmt, ...) __printf(1, 2);
 
 #define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))
 
+#ifdef __GCC_ASM_FLAG_OUTPUTS__
+# define ASM_FLAG_OUT(yes, no) yes
+#else
+# define ASM_FLAG_OUT(yes, no) no
+#endif
+
+static inline uint64_t rdtsc (void) {
+  unsigned int low, high;
+  asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+  return ((uint64_t) high << 32) | low;
+}
+
+static inline uint64_t rdtscp (void) {
+  unsigned int low, high;
+  asm volatile ("rdtscp" : "=a" (low), "=d" (high) :: "ecx");
+  return ((uint64_t) high << 32) | low;
+}
+
 void heapsort(void *base, size_t nmemb, size_t size,
               int (*compar)(const void *, const void *),
               void (*swap)(void *, void *));
include/xtf/time.h new file +28 -0
new file mode 100644
index 0000000..e30d109
@@ -0,0 +1,28 @@
+/**
+ * @file include/xtf/time.h
+ *
+ * Time management
+ */
+#ifndef XTF_TIME_H
+# define XTF_TIME_H
+
+#include <xtf/types.h>
+
+#if defined(__i386__)
+/* Time from boot in nanoseconds */
+uint32_t since_boot_time(void);
+#else
+uint64_t since_boot_time(void);
+#endif
+
+#endif /* XTF_TIME_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#2 lore raw

PATCH 2/6 time: add current_time() function to time management

In reply to Pawel Wieczorkiewicz

From: Paul Semel <[email protected]>

This function returns the "epoch" time.

---
 common/time.c      | 16 ++++++++++++++++
 include/xtf/time.h |  4 ++++
 2 files changed, 20 insertions(+)
common/time.c +16 -0
index b9a6531..7decd07 100644
@@ -74,6 +74,22 @@ uint64_t since_boot_time(void)
     return system_time;
 }
 
+/* This function return the epoch time (number of seconds elapsed
+ * since Juanary 1, 1970) */
+#if defined(__i386__)
+uint32_t current_time(void)
+#else
+uint64_t current_time(void)
+#endif
+{
+#if defined(__i386__)
+    uint32_t seconds = shared_info.wc_sec;
+#else
+    uint64_t seconds = ((uint64_t)shared_info.wc_sec_hi << 32) | shared_info.wc_sec;
+#endif
+    return seconds + (since_boot_time() / 1000000000);
+}
+
 /*
  * Local variables:
  * mode: C
include/xtf/time.h +4 -0
index e30d109..52da27a 100644
@@ -11,8 +11,12 @@
 #if defined(__i386__)
 /* Time from boot in nanoseconds */
 uint32_t since_boot_time(void);
+
+uint32_t current_time(void);
 #else
 uint64_t since_boot_time(void);
+
+uint64_t current_time(void);
 #endif
 
 #endif /* XTF_TIME_H */
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#3 lore raw

PATCH 3/6 time: add gettimeofday() function to time managment

In reply to Pawel Wieczorkiewicz

From: Paul Semel <[email protected]>

---
 common/time.c      | 14 ++++++++++++++
 include/xtf/time.h | 12 ++++++++++++
 2 files changed, 26 insertions(+)
common/time.c +14 -0
index 7decd07..fffae1c 100644
@@ -90,6 +90,20 @@ uint64_t current_time(void)
     return seconds + (since_boot_time() / 1000000000);
 }
 
+/* The POSIX gettimeofday syscall normally takes a second argument, which is
+ * the timezone (struct timezone). However, it sould be NULL because linux
+ * doesn't use it anymore. So we need for us to add it in this function
+ */
+int gettimeofday(struct timeval *tp)
+{
+    if (!tp)
+        return -1;
+
+    tp->sec = current_time();
+    tp->nsec = shared_info.wc_nsec + (since_boot_time() % 1000000000);
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
include/xtf/time.h +12 -0
index 52da27a..e312465 100644
@@ -8,6 +8,16 @@
 
 #include <xtf/types.h>
 
+struct timeval {
+#if !defined(__i386__)
+    uint64_t sec;
+    uint64_t nsec;
+#else
+    uint32_t sec;
+    uint32_t nsec;
+#endif
+};
+
 #if defined(__i386__)
 /* Time from boot in nanoseconds */
 uint32_t since_boot_time(void);
@@ -19,6 +29,8 @@ uint64_t since_boot_time(void);
 uint64_t current_time(void);
 #endif
 
+int gettimeofday(struct timeval *tp);
+
 #endif /* XTF_TIME_H */
 
 /*
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#4 lore raw

PATCH 4/6 time: Add helper functions and macros to time management

In reply to Pawel Wieczorkiewicz

From: Paul Semel <[email protected]>

Add the following helper functions:
- nspin_sleep()
- spin_sleep()
- mspin_sleep()

Add the following helper macros:
- sleep()
- msleep()
- NOW()

---
 common/time.c      | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/xtf/time.h | 16 +++++++++++++++
 2 files changed, 74 insertions(+)
common/time.c +58 -0
index fffae1c..3db1f8f 100644
@@ -104,6 +104,64 @@ int gettimeofday(struct timeval *tp)
     return 0;
 }
 
+#if defined(__i386__)
+static inline void nspin_sleep(uint32_t t)
+#else
+static inline void nspin_sleep(uint64_t t)
+#endif
+{
+    unsigned long end = since_boot_time() + t;
+
+    while ( since_boot_time() < end )
+        asm volatile ( "pause" );
+}
+
+#if defined(__i386__)
+static inline void spin_sleep(uint32_t t)
+#else
+static inline void spin_sleep(uint64_t t)
+#endif
+{
+#if defined(__i386__)
+    uint32_t nsec = t * 1000000000;
+#else
+    uint64_t nsec = t * 1000000000ul;
+#endif
+    nspin_sleep(nsec);
+}
+
+#if defined(__i386__)
+static inline void mspin_sleep(uint32_t t)
+#else
+static inline void mspin_sleep(uint64_t t)
+#endif
+{
+#if defined(__i386__)
+    uint32_t nsec = t * 1000000;
+#else
+    uint64_t nsec = t * 1000000ul;
+#endif
+    nspin_sleep(nsec);
+}
+
+#if defined(__i386__)
+void sleep(uint32_t t)
+#else
+void sleep(uint64_t t)
+#endif
+{
+    spin_sleep(t);
+}
+
+#if defined(__i386__)
+void msleep(uint32_t t)
+#else
+void msleep(uint64_t t)
+#endif
+{
+    mspin_sleep(t);
+}
+
 /*
  * Local variables:
  * mode: C
include/xtf/time.h +16 -0
index e312465..07fcae5 100644
@@ -23,14 +23,30 @@ struct timeval {
 uint32_t since_boot_time(void);
 
 uint32_t current_time(void);
+
+/* This function takes seconds in parameter */
+void sleep(uint32_t f);
+
+/* Be careful, this function takes milliseconds in parameter,
+ * not microseconds !
+ */
+void msleep(uint32_t f);
 #else
 uint64_t since_boot_time(void);
 
 uint64_t current_time(void);
+
+void sleep(uint64_t f);
+
+void msleep(uint64_t f);
 #endif
 
 int gettimeofday(struct timeval *tp);
 
+
+/* This returns the current epoch time */
+#define NOW() current_time()
+
 #endif /* XTF_TIME_H */
 
 /*
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#5 lore raw

PATCH 5/6 time: Add cycles2{n,u,m}sec functions

In reply to Pawel Wieczorkiewicz

In order to easily translate CPU cycles to time values add the following helpers:
- cycles2nsec()
- cycles2usec()
- cycles2msec()

---
 common/time.c      | 17 +++++++++++++++++
 include/xtf/time.h |  5 ++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
common/time.c +17 -0
index 3db1f8f..8f73243 100644
@@ -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
include/xtf/time.h +4 -1
index 07fcae5..6aa1efc 100644
@@ -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 */
 
 /*
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
#6 lore raw

PATCH 6/6 event_channels: Add EVTCHNOP_bind_vcpu hypercall support

In reply to Pawel Wieczorkiewicz
---
 include/xen/event_channel.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/xen/event_channel.h +7 -0
index 62ee95a..6253b89 100644
@@ -2,6 +2,7 @@
 #define XEN_PUBLIC_EVENT_CHANNEL_H
 
 #define EVTCHNOP_send             4
+#define EVTCHNOP_bind_vcpu        8
 #define EVTCHNOP_init_control    11
 #define EVTCHNOP_expand_array    12
 
@@ -22,6 +23,12 @@ struct evtchn_expand_array {
     uint64_t array_gfn;
 };
 
+struct evtchn_bind_vcpu {
+    /* IN parameters. */
+    evtchn_port_t port;
+    uint32_t vcpu;
+};
+
 #endif /* XEN_PUBLIC_EVENT_CHANNEL_H */
 
 /*
Signature · 12 lines
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879