New VFS files have the 'correct' ctime
[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; /* TODO: this is fucked.  Thanks POSIX. */
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 uint64_t epoch_seconds(void);
37
38 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
39  * this if you don't want to muck with overheads or subtraction. */
40 static inline __attribute__((always_inline))
41 uint64_t start_timing(void)
42 {
43     return read_tsc_serialized();
44 }
45
46 /* Takes a time measurement and subtracts the start time and timing overhead,
47  * to return the detected elapsed time.  Use this if you don't want to muck
48  * with overheads or subtraction. */
49 static inline __attribute__((always_inline))
50 uint64_t stop_timing(uint64_t start_time)
51 {
52     uint64_t diff = read_tsc_serialized();
53     diff -= start_time;
54     diff -= system_timing.timing_overhead;
55         if ((int64_t) diff < 0) {
56                 return 1;
57         }
58         return diff;
59 }
60
61 static inline __attribute__((always_inline))
62 uint64_t nsec(void)
63 {
64         return tsc2nsec(read_tsc());
65 }
66
67
68 /* Ancient measurement crap below.  TODO: use or lose it */
69
70 #if 0
71 #include <pool.h>
72 #include <string.h>
73
74 #define TIMER_TAG_SIZE 20
75 #define MAX_TIMERS 20
76 /* timer_t
77  * This struct is used to keep track of counter values as they are spread
78  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
79  * and TAGGED_TIMING_END
80  */
81 typedef struct Timer{
82         uint64_t curr_run;
83         uint64_t aggr_run;
84         char label[TIMER_TAG_SIZE];
85 } timer_t;
86
87 #define TAGGED_TIMING_BEGIN(tag)                    \
88         static timer_t* _timer_##tag = NULL;            \
89         if (_timer_##tag == NULL) {                     \
90                 _timer_##tag = POOL_GET(&timer_pool);       \
91                 strcpy((_timer_##tag->label), #tag);        \
92                 _timer_##tag->aggr_run = 0;                 \
93         }                                               \
94         _timer_##tag->curr_run = start_timing();
95 #define TAGGED_TIMING_END(tag)                                              \
96 ({                                                                          \
97         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
98         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
99 })
100
101 #endif
102 #endif /* ROS_KERN_TIME_H */