• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 /*
19  * System clock functions.
20  */
21 
22 #define LOG_TAG "SystemClock"
23 
24 #include <utils/SystemClock.h>
25 
26 #include <string.h>
27 #include <errno.h>
28 #include <time.h>
29 
30 #include <cutils/compiler.h>
31 
32 #include <utils/Timers.h>
33 #include <utils/Log.h>
34 
35 namespace android {
36 
37 /*
38  * native public static long uptimeMillis();
39  */
uptimeMillis()40 int64_t uptimeMillis()
41 {
42     return nanoseconds_to_milliseconds(uptimeNanos());
43 }
44 
45 /*
46  * public static native long uptimeNanos();
47  */
uptimeNanos()48 int64_t uptimeNanos()
49 {
50     return systemTime(SYSTEM_TIME_MONOTONIC);
51 }
52 
53 /*
54  * native public static long elapsedRealtime();
55  */
elapsedRealtime()56 int64_t elapsedRealtime()
57 {
58 	return nanoseconds_to_milliseconds(elapsedRealtimeNano());
59 }
60 
61 /*
62  * native public static long elapsedRealtimeNano();
63  */
elapsedRealtimeNano()64 int64_t elapsedRealtimeNano()
65 {
66 #if defined(__linux__)
67     struct timespec ts;
68     int err = clock_gettime(CLOCK_BOOTTIME, &ts);
69     if (CC_UNLIKELY(err)) {
70         // This should never happen, but just in case ...
71         ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno));
72         return 0;
73     }
74 
75     return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
76 #else
77     return systemTime(SYSTEM_TIME_MONOTONIC);
78 #endif
79 }
80 
81 }; // namespace android
82