Debian Codegraph @

Xpjproject/pjlib/src/pj/os_timestamp_posix.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/os.h>
20 #include <pj/errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25
26 #if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0
27 # include <unistd.h>
28
29 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && \
30 defined(_POSIX_MONOTONIC_CLOCK)
31 # define USE_POSIX_TIMERS 1
32 # endif
33
34 #endif
35
36 #if defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \
37 defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \
38 defined(PJ_M_I386) && PJ_M_I386!=0 && \
39 defined(PJ_LINUX) && PJ_LINUX!=0
40 static int machine_speed_mhz;
41 static pj_timestamp machine_speed;
42
43 static __inline__ unsigned long long int rdtsc()
44 {
45 unsigned long long int x;
46 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
47 return x;
48 }
49
50 /* Determine machine's CPU MHz to get the counter's frequency.
51 */
52 static int get_machine_speed_mhz()
53 {
54 FILE *strm;
55 char buf[512];
56 int len;
57 char *pos, *end;
58
59 PJ_CHECK_STACK();
60
61 /* Open /proc/cpuinfo and read the file */
62 strm = fopen("/proc/cpuinfo", "r");
63 if (!strm)
64 return -1;
65 len = fread(buf, 1, sizeof(buf), strm);
66 fclose(strm);
67 if (len < 1) {
68 return -1;
69 }
70 buf[len] = '\0';
71
72 /* Locate the MHz digit. */
73 pos = strstr(buf, "cpu MHz");
74 if (!pos)
75 return -1;
76 pos = strchr(pos, ':');
77 if (!pos)
78 return -1;
79 end = (pos += 2);
80 while (isdigit(*end)) ++end;
81 *end = '\0';
82
83 /* Return the Mhz part, and give it a +1. */
84 return atoi(pos)+1;
85 }
86
87 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
88 {
89 if (machine_speed_mhz == 0) {
90 machine_speed_mhz = get_machine_speed_mhz();
91 if (machine_speed_mhz > 0) {
92 machine_speed.u64 = machine_speed_mhz * 1000000.0;
93 }
94 }
95
96 if (machine_speed_mhz == -1) {
97 ts->u64 = 0;
98 return -1;
99 }
100 ts->u64 = rdtsc();
101 return 0;
102 }
103
104 PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
105 {
106 if (machine_speed_mhz == 0) {
107 machine_speed_mhz = get_machine_speed_mhz();
108 if (machine_speed_mhz > 0) {
109 machine_speed.u64 = machine_speed_mhz * 1000000.0;
110 }
111 }
112
113 if (machine_speed_mhz == -1) {
114 freq->u64 = 1; /* return 1 to prevent division by zero in apps. */
115 return -1;
116 }
117
118 freq->u64 = machine_speed.u64;
119 return 0;
120 }
121
122 #elif defined(PJ_DARWINOS) && PJ_DARWINOS != 0
123
124 #include <mach/mach.h>
125 #include <mach/mach_time.h>
126
127 static mach_timebase_info_data_t timebase;
128
129 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
130 {
131 if (!timebase.denom) {
132 mach_timebase_info(&timebase);
133 }
134
135 ts->u64 = mach_continuous_time() * timebase.numer / timebase.denom;
136
137 return PJ_SUCCESS;
138 }
139
140 PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
141 {
142 freq->u32.hi = 0;
143 freq->u32.lo = NSEC_PER_SEC;
144
145 return PJ_SUCCESS;
146 }
147
148 #elif defined(__ANDROID__)
149
150 #include <errno.h>
151 #include <time.h>
152
153 #if defined(PJ_HAS_ANDROID_ALARM_H) && PJ_HAS_ANDROID_ALARM_H != 0
154 # include <linux/android_alarm.h>
155 # include <fcntl.h>
156 #endif
157
158 #define NSEC_PER_SEC 1000000000
159
160 #if defined(ANDROID_ALARM_GET_TIME)
161 static int s_alarm_fd = -1;
162
163 void close_alarm_fd()
164 {
165 if (s_alarm_fd != -1)
166 close(s_alarm_fd);
167 s_alarm_fd = -1;
168 }
169 #endif
170
171 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
172 {
173 struct timespec tp;
174 int err = -1;
175
176 #if defined(ANDROID_ALARM_GET_TIME)
177 if (s_alarm_fd == -1) {
178 int fd = open("/dev/alarm", O_RDONLY);
179 if (fd >= 0) {
180 s_alarm_fd = fd;
181 pj_atexit(&close_alarm_fd);
182 }
183 }
184
185 if (s_alarm_fd != -1) {
186 err = ioctl(s_alarm_fd,
187 ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &tp);
188 }
189 #elif defined(CLOCK_BOOTTIME)
190 err = clock_gettime(CLOCK_BOOTTIME, &tp);
191 #endif
192
193 if (err != 0) {
194 /* Fallback to CLOCK_MONOTONIC if /dev/alarm is not found, or
195 * getting ANDROID_ALARM_ELAPSED_REALTIME fails, or
196 * CLOCK_BOOTTIME fails.
197 */
198 err = clock_gettime(CLOCK_MONOTONIC, &tp);
199 }
200
201 if (err != 0) {
202 return PJ_RETURN_OS_ERROR(pj_get_native_os_error());
203 }
204
205 ts->u64 = tp.tv_sec;
206 ts->u64 *= NSEC_PER_SEC;
207 ts->u64 += tp.tv_nsec;
208
209 return PJ_SUCCESS;
210 }
211
212 PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
213 {
214 freq->u32.hi = 0;
215 freq->u32.lo = NSEC_PER_SEC;
216
217 return PJ_SUCCESS;
218 }
219
220 #elif defined(USE_POSIX_TIMERS) && USE_POSIX_TIMERS != 0
221 #include <sys/time.h>
222 #include <time.h>
223 #include <errno.h>
224
225 #define NSEC_PER_SEC 1000000000
226
227 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
228 {
229 struct timespec tp;
230
231 if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) {
232 return PJ_RETURN_OS_ERROR(pj_get_native_os_error());
233 }
234
235 ts->u64 = tp.tv_sec;
236 ts->u64 *= NSEC_PER_SEC;
237 ts->u64 += tp.tv_nsec;
238
239 return PJ_SUCCESS;
240 }
241
242 PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
243 {
244 freq->u32.hi = 0;
245 freq->u32.lo = NSEC_PER_SEC;
246
247 return PJ_SUCCESS;
248 }
249
250 #else
251 #include <sys/time.h>
252 #include <errno.h>
253
254 #define USEC_PER_SEC 1000000
255
256 PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
257 {
258 struct timeval tv;
259
260 if (gettimeofday(&tv, NULL) != 0) {
261 return PJ_RETURN_OS_ERROR(pj_get_native_os_error());
262 }
263
264 ts->u64 = tv.tv_sec;
265 ts->u64 *= USEC_PER_SEC;
266 ts->u64 += tv.tv_usec;
267
268 return PJ_SUCCESS;
269 }
270
271 PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
272 {
273 freq->u32.hi = 0;
274 freq->u32.lo = USEC_PER_SEC;
275
276 return PJ_SUCCESS;
277 }
278
279 #endif