x86: Use a KERNBASE address for the bootstack
[akaros.git] / kern / include / time.h
1 #pragma once
2
3 #include <ros/common.h>
4 #include <ros/time.h>
5 #include <arch/time.h>
6 #include <ros/procinfo.h>
7
8 /* Conversion factors */
9 #define NSEC_PER_SEC    1000000000L
10 #define NSEC_PER_MSEC   1000000L
11 #define NSEC_PER_USEC   1000L
12
13 void time_init(void);
14 void udelay(uint64_t usec);     /* done in arch-specific files */
15
16 uint64_t read_persistent_clock(void);   /* arch-specific */
17
18 uint64_t tsc2nsec(uint64_t tsc_time);
19 static inline uint64_t tsc2usec(uint64_t tsc_time)
20 {
21         return tsc2nsec(tsc_time) / NSEC_PER_USEC;
22 }
23 static inline uint64_t tsc2msec(uint64_t tsc_time)
24 {
25         return tsc2nsec(tsc_time) / NSEC_PER_MSEC;
26 }
27 static inline uint64_t tsc2sec(uint64_t tsc_time)
28 {
29         return tsc2nsec(tsc_time) / NSEC_PER_SEC;
30 }
31
32 uint64_t nsec2tsc(uint64_t nsec);
33 static inline uint64_t usec2tsc(uint64_t usec)
34 {
35         return nsec2tsc(usec * NSEC_PER_USEC);
36 }
37 static inline uint64_t msec2tsc(uint64_t msec)
38 {
39         return nsec2tsc(msec * NSEC_PER_MSEC);
40 }
41 static inline uint64_t sec2tsc(uint64_t sec)
42 {
43         return nsec2tsc(sec * NSEC_PER_SEC);
44 }
45
46 uint64_t epoch_nsec(void);
47
48 static inline struct timespec nsec2timespec(uint64_t ns)
49 {
50         return (struct timespec) {
51                 .tv_sec = ns / NSEC_PER_SEC,
52                 .tv_nsec = ns % NSEC_PER_SEC
53         };
54 }
55
56 static inline struct timeval nsec2timeval(uint64_t ns)
57 {
58         return (struct timeval) {
59                 .tv_sec = ns / NSEC_PER_SEC,
60                 .tv_usec = (ns % NSEC_PER_SEC) / NSEC_PER_USEC
61         };
62 }
63
64 static inline struct timespec tsc2timespec(uint64_t tsc_time)
65 {
66         return nsec2timespec(tsc2nsec(tsc_time));
67 }
68
69 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
70  * this if you don't want to muck with overheads or subtraction. */
71 static inline __attribute__((always_inline))
72 uint64_t start_timing(void)
73 {
74     return read_tsc_serialized();
75 }
76
77 /* Takes a time measurement and subtracts the start time and timing overhead,
78  * to return the detected elapsed time.  Use this if you don't want to muck
79  * with overheads or subtraction. */
80 static inline __attribute__((always_inline))
81 uint64_t stop_timing(uint64_t start_time)
82 {
83     uint64_t diff = read_tsc_serialized();
84     diff -= start_time;
85     diff -= __proc_global_info.tsc_overhead;
86         if ((int64_t) diff < 0) {
87                 return 1;
88         }
89         return diff;
90 }
91
92 static inline __attribute__((always_inline))
93 uint64_t nsec(void)
94 {
95         return tsc2nsec(read_tsc());
96 }
97
98
99 /* Ancient measurement crap below.  TODO: use or lose it */
100
101 #if 0
102 #include <pool.h>
103 #include <string.h>
104
105 #define TIMER_TAG_SIZE 20
106 #define MAX_TIMERS 20
107 /* timer_t
108  * This struct is used to keep track of counter values as they are spread
109  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
110  * and TAGGED_TIMING_END
111  */
112 typedef struct Timer{
113         uint64_t curr_run;
114         uint64_t aggr_run;
115         char label[TIMER_TAG_SIZE];
116 } timer_t;
117
118 #define TAGGED_TIMING_BEGIN(tag)                    \
119         static timer_t* _timer_##tag = NULL;            \
120         if (_timer_##tag == NULL) {                     \
121                 _timer_##tag = POOL_GET(&timer_pool);       \
122                 strcpy((_timer_##tag->label), #tag);        \
123                 _timer_##tag->aggr_run = 0;                 \
124         }                                               \
125         _timer_##tag->curr_run = start_timing();
126 #define TAGGED_TIMING_END(tag)                                              \
127 ({                                                                          \
128         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
129         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
130 })
131
132 #endif