Padding in MCS-PDR qnodes (XCC)
[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 void train_timing();
21 void udelay(uint64_t usec);
22 uint64_t tsc2sec(uint64_t tsc_time);
23 uint64_t tsc2msec(uint64_t tsc_time);
24 uint64_t tsc2usec(uint64_t tsc_time);
25 uint64_t tsc2nsec(uint64_t tsc_time);
26 uint64_t sec2tsc(uint64_t sec);
27 uint64_t msec2tsc(uint64_t msec);
28 uint64_t usec2tsc(uint64_t usec);
29 uint64_t nsec2tsc(uint64_t nsec);
30
31 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
32  * this if you don't want to muck with overheads or subtraction. */
33 static inline __attribute__((always_inline))
34 uint64_t start_timing(void)
35 {
36     return read_tsc_serialized();
37 }
38
39 /* Takes a time measurement and subtracts the start time and timing overhead,
40  * to return the detected elapsed time.  Use this if you don't want to muck
41  * with overheads or subtraction. */
42 static inline __attribute__((always_inline))
43 uint64_t stop_timing(uint64_t start_time)
44 {
45     uint64_t diff = read_tsc_serialized();
46     diff -= start_time;
47     diff -= system_timing.timing_overhead;
48         if ((int64_t) diff < 0) {
49                 return 1;
50         }
51         return diff;
52 }
53
54 /* Ancient measurement crap below.  TODO: use or lose it */
55
56 #if 0
57 #include <pool.h>
58 #include <string.h>
59
60 #define TIMER_TAG_SIZE 20
61 #define MAX_TIMERS 20
62 /* timer_t
63  * This struct is used to keep track of counter values as they are spread
64  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
65  * and TAGGED_TIMING_END
66  */
67 typedef struct Timer{
68         uint64_t curr_run;
69         uint64_t aggr_run;
70         char label[TIMER_TAG_SIZE];
71 } timer_t;
72
73 #define TAGGED_TIMING_BEGIN(tag)                    \
74         static timer_t* _timer_##tag = NULL;            \
75         if (_timer_##tag == NULL) {                     \
76                 _timer_##tag = POOL_GET(&timer_pool);       \
77                 strcpy((_timer_##tag->label), #tag);        \
78                 _timer_##tag->aggr_run = 0;                 \
79         }                                               \
80         _timer_##tag->curr_run = start_timing();
81 #define TAGGED_TIMING_END(tag)                                              \
82 ({                                                                          \
83         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
84         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
85 })
86
87 #endif
88 #endif /* ROS_KERN_TIME_H */