CS350 COS
COS
Loading...
Searching...
No Matches
ktime.h File Reference

Go to the source code of this file.

Data Structures

struct  KTime
 

Typedefs

typedef struct KTime KTime
 
typedef uint64_t UnixEpoch
 
typedef uint64_t UnixEpochNS
 

Functions

uint64_t Time_GetTSC ()
 
void KTime_Fixup (KTime *tm)
 
UnixEpoch KTime_ToEpoch (const KTime *tm)
 
void KTime_FromEpoch (UnixEpoch time, KTime *tm)
 
void KTime_SetTime (UnixEpoch epoch, uint64_t tsc, uint64_t tps)
 
void KTime_Tick (int rate)
 
UnixEpoch KTime_GetEpoch ()
 
UnixEpochNS KTime_GetEpochNS ()
 

Data Structure Documentation

◆ KTime

struct KTime

Definition at line 7 of file ktime.h.

Collaboration diagram for KTime:
[legend]
Data Fields
int hour
int mday
int min
int month
int sec
int wday
int yday
int year

Typedef Documentation

◆ KTime

typedef struct KTime KTime

◆ UnixEpoch

Definition at line 18 of file ktime.h.

◆ UnixEpochNS

Definition at line 19 of file ktime.h.

Function Documentation

◆ KTime_Fixup()

void KTime_Fixup ( KTime tm)

Definition at line 61 of file ktime.c.

62{
63 uint64_t m;
64
65 if (tm->yday == -1) {
66 uint64_t yday = 0;
67 for (m = 0; m < tm->month; m++) {
68 yday += KTimeDaysInMonth(tm->year, m);
69 }
70 yday += tm->mday;
71 tm->yday = yday;
72 }
73}
Definition: time.h:7
static int KTimeDaysInMonth(uint64_t year, uint64_t month)
Definition: ktime.c:47
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:

◆ KTime_FromEpoch()

void KTime_FromEpoch ( UnixEpoch  time,
KTime tm 
)

Definition at line 109 of file ktime.c.

110{
111 uint64_t secs, mins, hours;
112 uint64_t days;
113 uint64_t y, m;
114
115 // Compute seconds
116 secs = epoch % (60 * 60 * 24);
117 days = epoch / (60 * 60 * 24);
118 mins = secs / 60;
119 secs = secs % 60;
120
121 // Compute minutes
122 hours = mins / 60;
123 mins = mins % 60;
124
125 // Compute hours
126 hours = hours % 24;
127
128 tm->sec = secs;
129 tm->min = mins;
130 tm->hour = hours;
131
132 tm->wday = (days + 3) % 7;
133
134 for (y = 1970; ; y++) {
135 uint64_t daysOfYear;
136 if (KTimeIsLeapYear(y)) {
137 daysOfYear = 366;
138 } else {
139 daysOfYear = 365;
140 }
141
142 if (days < daysOfYear) {
143 tm->yday = days;
144 tm->year = y;
145 break;
146 }
147 days -= daysOfYear;
148 }
149
150 for (m = 0; ; m++) {
151 uint64_t daysOfMonth = KTimeDaysInMonth(tm->year, m);
152
153 if (days < daysOfMonth) {
154 tm->mday = days;
155 tm->month = m;
156 break;
157 }
158 days -= daysOfMonth;
159 }
160}
static bool KTimeIsLeapYear(uint64_t year)
Definition: ktime.c:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ KTime_GetEpoch()

UnixEpoch KTime_GetEpoch ( )

Definition at line 180 of file ktime.c.

181{
182 uint64_t tscDiff;
183 uint64_t epoch;
184
186 tscDiff = Time_GetTSC() - ktimeLastTSC;
187 epoch = ktimeLastEpoch + tscDiff / ticksPerSecond;
189
190 return epoch;
191}
static Spinlock ktimeLock
Definition: ktime.c:14
uint64_t ticksPerSecond
Definition: ktime.c:17
static uint64_t ktimeLastTSC
Definition: ktime.c:16
static uint64_t ktimeLastEpoch
Definition: ktime.c:15
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
uint64_t Time_GetTSC()
Definition: time.c:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ KTime_GetEpochNS()

UnixEpochNS KTime_GetEpochNS ( )

Definition at line 194 of file ktime.c.

195{
196 uint64_t tscDiff;
197 uint64_t epoch;
198
200 tscDiff = Time_GetTSC() - ktimeLastTSC;
201 /*
202 * This is ugly but it avoids overflowing tscDiff to time computation.
203 * Note that the bottom bits of ticksPerSecond are not significant so it is
204 * okay to discard them.
205 */
206 epoch = (ktimeLastEpoch * 1000000000ULL) + (tscDiff * 1000000ULL / ticksPerSecond * 1000ULL);
208
209 return epoch;
210}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ KTime_SetTime()

void KTime_SetTime ( UnixEpoch  epoch,
uint64_t  tsc,
uint64_t  tps 
)

Definition at line 163 of file ktime.c.

164{
166 ktimeLastEpoch = epoch;
167 ktimeLastTSC = tsc;
168 ticksPerSecond = tps;
170
171}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ KTime_Tick()

void KTime_Tick ( int  rate)

◆ KTime_ToEpoch()

UnixEpoch KTime_ToEpoch ( const KTime tm)

Definition at line 76 of file ktime.c.

77{
78 uint64_t days = 0;
79 uint64_t secs = 0;
80 uint64_t y, m;
81
82 // Convert to UNIX epoch
83 for (y = 1970; y < tm->year; y++) {
84 if (KTimeIsLeapYear(y))
85 days += 366;
86 else
87 days += 365;
88 }
89
90 if (tm->yday == -1) {
91 uint64_t yday = 0;
92 for (m = 0; m < tm->month; m++) {
93 yday += KTimeDaysInMonth(tm->year, m);
94 }
95 yday += tm->mday;
96 days += yday;
97 } else {
98 days += tm->yday;
99 }
100
101 secs = 24 * days + tm->hour;
102 secs = secs * 60 + tm->min;
103 secs = secs * 60 + tm->sec;
104
105 return secs;
106}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Time_GetTSC()

uint64_t Time_GetTSC ( )

Definition at line 13 of file time.c.

14{
15 return rdtsc();
16}
static INLINE uint64_t rdtsc()
Definition: amd64op.h:39
Here is the call graph for this function:
Here is the caller graph for this function: