CS350 COS
COS
Loading...
Searching...
No Matches
ktime.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/ktime.h>
#include <sys/spinlock.h>
Include dependency graph for ktime.c:

Go to the source code of this file.

Functions

void KTime_Init ()
 
static bool KTimeIsLeapYear (uint64_t year)
 
static int KTimeDaysInMonth (uint64_t year, uint64_t month)
 
void KTime_Fixup (KTime *tm)
 
UnixEpoch KTime_ToEpoch (const KTime *tm)
 
void KTime_FromEpoch (UnixEpoch epoch, KTime *tm)
 
void KTime_SetTime (UnixEpoch epoch, uint64_t tsc, uint64_t tps)
 
void KTime_GetTime (KTime *tm)
 
UnixEpoch KTime_GetEpoch ()
 
UnixEpochNS KTime_GetEpochNS ()
 
static void Debug_Date ()
 
 REGISTER_DBGCMD (date, "Print date", Debug_Date)
 
static void Debug_Ticks ()
 
 REGISTER_DBGCMD (ticks, "Print ticks per second", Debug_Ticks)
 

Variables

static Spinlock ktimeLock
 
static uint64_t ktimeLastEpoch
 
static uint64_t ktimeLastTSC
 
uint64_t ticksPerSecond
 
static const char * dayOfWeek [7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }
 
static const char * months [12]
 

Function Documentation

◆ Debug_Date()

static void Debug_Date ( )
static

Definition at line 213 of file ktime.c.

214{
215 UnixEpoch epoch = KTime_GetEpoch();
216 KTime tm;
217
218 KTime_FromEpoch(epoch, &tm);
219
220 kprintf("%s %s %d %02d:%02d:%02d %04d\n",
221 dayOfWeek[tm.wday], months[tm.month],
222 tm.mday, tm.hour, tm.min, tm.sec, tm.year);
223
224 kprintf("Epoch: %lu\n", epoch);
225}
Definition: time.h:7
int kprintf(const char *fmt,...)
Definition: printf.c:210
void KTime_FromEpoch(UnixEpoch epoch, KTime *tm)
Definition: ktime.c:109
static const char * months[12]
Definition: ktime.c:20
static const char * dayOfWeek[7]
Definition: ktime.c:19
UnixEpoch KTime_GetEpoch()
Definition: ktime.c:180
uint64_t UnixEpoch
Definition: ktime.h:18
Definition: ktime.h:7
Here is the call graph for this function:

◆ Debug_Ticks()

static void Debug_Ticks ( )
static

Definition at line 230 of file ktime.c.

231{
232 kprintf("Ticks Per Second: %lu\n", ticksPerSecond);
233}
uint64_t ticksPerSecond
Definition: ktime.c:17
Here is the call graph for this function:

◆ 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}
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  epoch,
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
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_GetTime()

void KTime_GetTime ( KTime tm)

Definition at line 174 of file ktime.c.

175{
177}
Here is the call graph for this function:

◆ KTime_Init()

void KTime_Init ( )

Definition at line 26 of file ktime.c.

27{
30 ktimeLastTSC = 0;
32}
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
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_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:

◆ KTimeDaysInMonth()

static int KTimeDaysInMonth ( uint64_t  year,
uint64_t  month 
)
static

Definition at line 47 of file ktime.c.

48{
49 static const uint64_t days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
50
51 if ((month == 2) && KTimeIsLeapYear(year))
52 return 29;
53 else
54 return days[month];
55}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ KTimeIsLeapYear()

static bool KTimeIsLeapYear ( uint64_t  year)
static

Definition at line 35 of file ktime.c.

36{
37 if ((year % 4) != 0)
38 return false;
39 if ((year % 100) != 0)
40 return true;
41 if ((year % 400) != 0)
42 return false;
43 return true;
44}
Here is the caller graph for this function:

◆ REGISTER_DBGCMD() [1/2]

REGISTER_DBGCMD ( date  ,
"Print date"  ,
Debug_Date   
)

◆ REGISTER_DBGCMD() [2/2]

REGISTER_DBGCMD ( ticks  ,
"Print ticks per second"  ,
Debug_Ticks   
)

Variable Documentation

◆ dayOfWeek

const char* dayOfWeek[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }
static

Definition at line 19 of file ktime.c.

◆ ktimeLastEpoch

uint64_t ktimeLastEpoch
static

Definition at line 15 of file ktime.c.

◆ ktimeLastTSC

uint64_t ktimeLastTSC
static

Definition at line 16 of file ktime.c.

◆ ktimeLock

Spinlock ktimeLock
static

Definition at line 14 of file ktime.c.

◆ months

const char* months[12]
static
Initial value:
= {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
}

Definition at line 20 of file ktime.c.

◆ ticksPerSecond

uint64_t ticksPerSecond

Definition at line 17 of file ktime.c.