Merge pull request #1155 from steffen-kiess/json-string
[mono.git] / mono / utils / mono-time.c
1 /*
2  * Time utility functions.
3  * Author: Paolo Molaro (<lupus@ximian.com>)
4  * Copyright (C) 2008 Novell, Inc.
5  */
6
7 #include <utils/mono-time.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 #define MTICKS_PER_SEC 10000000
12
13 #ifdef HOST_WIN32
14 #include <windows.h>
15
16 guint32
17 mono_msec_ticks (void)
18 {
19         /* GetTickCount () is reportedly monotonic */
20         return GetTickCount ();
21 }
22
23 /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */
24 gint64
25 mono_100ns_ticks (void)
26 {
27         static LARGE_INTEGER freq;
28         static UINT64 start_time;
29         UINT64 cur_time;
30         LARGE_INTEGER value;
31
32         if (!freq.QuadPart) {
33                 if (!QueryPerformanceFrequency (&freq))
34                         return mono_100ns_datetime ();
35                 QueryPerformanceCounter (&value);
36                 start_time = value.QuadPart;
37         }
38         QueryPerformanceCounter (&value);
39         cur_time = value.QuadPart;
40         /* we use unsigned numbers and return the difference to avoid overflows */
41         return (cur_time - start_time) * (double)MTICKS_PER_SEC / freq.QuadPart;
42 }
43
44 /* Returns the number of 100ns ticks since Jan 1, 1601, UTC timezone */
45 gint64
46 mono_100ns_datetime (void)
47 {
48         ULARGE_INTEGER ft;
49
50         if (sizeof(ft) != sizeof(FILETIME))
51                 g_assert_not_reached ();
52
53         GetSystemTimeAsFileTime ((FILETIME*) &ft);
54         return ft.QuadPart;
55 }
56
57 #else
58
59 #ifdef HAVE_SYS_TIME_H
60 #include <sys/time.h>
61 #endif
62
63 #if defined (HAVE_SYS_PARAM_H)
64 #include <sys/param.h>
65 #endif
66 #if defined(HAVE_SYS_SYSCTL_H)
67 #include <sys/sysctl.h>
68 #endif
69
70 #if defined(PLATFORM_MACOSX)
71 #include <mach/mach.h>
72 #include <mach/mach_time.h>
73 #endif
74
75 #include <time.h>
76
77 static gint64
78 get_boot_time (void)
79 {
80 #if defined (HAVE_SYS_PARAM_H) && defined (KERN_BOOTTIME)
81         int mib [2];
82         size_t size;
83         time_t now;
84         struct timeval boottime;
85
86         (void)time(&now);
87
88         mib [0] = CTL_KERN;
89         mib [1] = KERN_BOOTTIME;
90
91         size = sizeof(boottime);
92
93         if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
94                 return (gint64)((now - boottime.tv_sec) * MTICKS_PER_SEC);
95 #else
96         FILE *uptime = fopen ("/proc/uptime", "r");
97         if (uptime) {
98                 double upt;
99                 if (fscanf (uptime, "%lf", &upt) == 1) {
100                         gint64 now = mono_100ns_ticks ();
101                         fclose (uptime);
102                         return now - (gint64)(upt * MTICKS_PER_SEC);
103                 }
104                 fclose (uptime);
105         }
106 #endif
107         /* a made up uptime of 300 seconds */
108         return (gint64)300 * MTICKS_PER_SEC;
109 }
110
111 /* Returns the number of milliseconds from boot time: this should be monotonic */
112 guint32
113 mono_msec_ticks (void)
114 {
115         static gint64 boot_time = 0;
116         gint64 now;
117         if (!boot_time)
118                 boot_time = get_boot_time ();
119         now = mono_100ns_ticks ();
120         /*printf ("now: %llu (boot: %llu) ticks: %llu\n", (gint64)now, (gint64)boot_time, (gint64)(now - boot_time));*/
121         return (now - boot_time)/10000;
122 }
123
124 /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */
125 gint64
126 mono_100ns_ticks (void)
127 {
128         struct timeval tv;
129 #ifdef CLOCK_MONOTONIC
130         struct timespec tspec;
131         static struct timespec tspec_freq = {0};
132         static int can_use_clock = 0;
133         if (!tspec_freq.tv_nsec) {
134                 can_use_clock = clock_getres (CLOCK_MONOTONIC, &tspec_freq) == 0;
135                 /*printf ("resolution: %lu.%lu\n", tspec_freq.tv_sec, tspec_freq.tv_nsec);*/
136         }
137         if (can_use_clock) {
138                 if (clock_gettime (CLOCK_MONOTONIC, &tspec) == 0) {
139                         /*printf ("time: %lu.%lu\n", tspec.tv_sec, tspec.tv_nsec); */
140                         return ((gint64)tspec.tv_sec * MTICKS_PER_SEC + tspec.tv_nsec / 100);
141                 }
142         }
143         
144 #elif defined(PLATFORM_MACOSX)
145         /* http://developer.apple.com/library/mac/#qa/qa1398/_index.html */
146         static mach_timebase_info_data_t timebase;
147         guint64 now = mach_absolute_time ();
148         if (timebase.denom == 0) {
149                 mach_timebase_info (&timebase);
150                 timebase.denom *= 100; /* we return 100ns ticks */
151         }
152         return now * timebase.numer / timebase.denom;
153 #endif
154         if (gettimeofday (&tv, NULL) == 0)
155                 return ((gint64)tv.tv_sec * 1000000 + tv.tv_usec) * 10;
156         return 0;
157 }
158
159 /*
160  * Magic number to convert unix epoch start to windows epoch start
161  * Jan 1, 1970 into a value which is relative to Jan 1, 1601.
162  */
163 #define EPOCH_ADJUST    ((guint64)11644473600LL)
164
165 /* Returns the number of 100ns ticks since 1/1/1601, UTC timezone */
166 gint64
167 mono_100ns_datetime (void)
168 {
169         struct timeval tv;
170         if (gettimeofday (&tv, NULL) == 0)
171                 return (((gint64)tv.tv_sec + EPOCH_ADJUST) * 1000000 + tv.tv_usec) * 10;
172         return 0;
173 }
174
175 #endif
176