2009-03-04 04:31:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* System clock functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2016-04-25 19:39:55 +02:00
|
|
|
#include <errno.h>
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2016-04-25 19:39:55 +02:00
|
|
|
#include <cutils/compiler.h>
|
2009-03-04 04:31:44 +01:00
|
|
|
#include <utils/SystemClock.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "SystemClock"
|
2013-05-07 05:20:50 +02:00
|
|
|
#include <utils/Log.h>
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* native public static long uptimeMillis();
|
|
|
|
*/
|
|
|
|
int64_t uptimeMillis()
|
|
|
|
{
|
|
|
|
int64_t when = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
return (int64_t) nanoseconds_to_milliseconds(when);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* native public static long elapsedRealtime();
|
|
|
|
*/
|
|
|
|
int64_t elapsedRealtime()
|
2012-07-19 18:17:24 +02:00
|
|
|
{
|
|
|
|
return nanoseconds_to_milliseconds(elapsedRealtimeNano());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* native public static long elapsedRealtimeNano();
|
|
|
|
*/
|
|
|
|
int64_t elapsedRealtimeNano()
|
2009-03-04 04:31:44 +01:00
|
|
|
{
|
2016-04-25 19:39:55 +02:00
|
|
|
#if defined(__linux__)
|
2016-04-05 01:06:53 +02:00
|
|
|
struct timespec ts;
|
2016-04-25 19:39:55 +02:00
|
|
|
int err = clock_gettime(CLOCK_BOOTTIME, &ts);
|
|
|
|
if (CC_UNLIKELY(err)) {
|
|
|
|
// This should never happen, but just in case ...
|
|
|
|
ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno));
|
|
|
|
return 0;
|
2013-12-17 02:04:32 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:55 +02:00
|
|
|
return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
|
2009-03-04 04:31:44 +01:00
|
|
|
#else
|
2012-07-19 18:17:24 +02:00
|
|
|
return systemTime(SYSTEM_TIME_MONOTONIC);
|
2009-03-04 04:31:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|