Handle pending signals even when resuming current_uthread
[akaros.git] / kern / include / time.h
1 #ifndef ROS_KERN_TIME_H
2 #define ROS_KERN_TIME_H
3
4 #include <ros/common.h>
5 #include <arch/time.h>
6
7 /* (newlib) Time Value Specification Structures, P1003.1b-1993, p. 261 */
8 typedef long time_t;
9
10 struct timespec {
11   time_t  tv_sec;   /* Seconds */
12   long    tv_nsec;  /* Nanoseconds */
13 };
14
15 struct itimerspec {
16   struct timespec  it_interval;  /* Timer period */
17   struct timespec  it_value;     /* Timer expiration */
18 };
19
20 struct timeval {
21         time_t tv_sec;  /* seconds */
22         time_t tv_usec; /* microseconds */
23 };
24
25 void train_timing();
26 void udelay(uint64_t usec);     /* done in arch-specific files */
27 void udelay_sched(uint64_t usec);
28 uint64_t tsc2sec(uint64_t tsc_time);
29 uint64_t tsc2msec(uint64_t tsc_time);
30 uint64_t tsc2usec(uint64_t tsc_time);
31 uint64_t tsc2nsec(uint64_t tsc_time);
32 uint64_t sec2tsc(uint64_t sec);
33 uint64_t msec2tsc(uint64_t msec);
34 uint64_t usec2tsc(uint64_t usec);
35 uint64_t nsec2tsc(uint64_t nsec);
36
37 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
38  * this if you don't want to muck with overheads or subtraction. */
39 static inline __attribute__((always_inline))
40 uint64_t start_timing(void)
41 {
42     return read_tsc_serialized();
43 }
44
45 /* Takes a time measurement and subtracts the start time and timing overhead,
46  * to return the detected elapsed time.  Use this if you don't want to muck
47  * with overheads or subtraction. */
48 static inline __attribute__((always_inline))
49 uint64_t stop_timing(uint64_t start_time)
50 {
51     uint64_t diff = read_tsc_serialized();
52     diff -= start_time;
53     diff -= system_timing.timing_overhead;
54         if ((int64_t) diff < 0) {
55                 return 1;
56         }
57         return diff;
58 }
59
60 static inline __attribute__((always_inline))
61 uint64_t nsec(void)
62 {
63         return tsc2nsec(read_tsc());
64 }
65
66
67 /* Ancient measurement crap below.  TODO: use or lose it */
68
69 #if 0
70 #include <pool.h>
71 #include <string.h>
72
73 #define TIMER_TAG_SIZE 20
74 #define MAX_TIMERS 20
75 /* timer_t
76  * This struct is used to keep track of counter values as they are spread
77  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
78  * and TAGGED_TIMING_END
79  */
80 typedef struct Timer{
81         uint64_t curr_run;
82         uint64_t aggr_run;
83         char label[TIMER_TAG_SIZE];
84 } timer_t;
85
86 #define TAGGED_TIMING_BEGIN(tag)                    \
87         static timer_t* _timer_##tag = NULL;            \
88         if (_timer_##tag == NULL) {                     \
89                 _timer_##tag = POOL_GET(&timer_pool);       \
90                 strcpy((_timer_##tag->label), #tag);        \
91                 _timer_##tag->aggr_run = 0;                 \
92         }                                               \
93         _timer_##tag->curr_run = start_timing();
94 #define TAGGED_TIMING_END(tag)                                              \
95 ({                                                                          \
96         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
97         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
98 })
99
100 #endif
101 #endif /* ROS_KERN_TIME_H */