2009-03-04 11:31:44 +08: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-26 01:39:55 +08:00
|
|
|
#include <errno.h>
|
2009-03-04 11:31:44 +08:00
|
|
|
|
2016-04-26 01:39:55 +08:00
|
|
|
#include <cutils/compiler.h>
|
2009-03-04 11:31:44 +08:00
|
|
|
#include <utils/SystemClock.h>
|
|
|
|
#include <utils/Timers.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "SystemClock"
|
2013-05-07 11:20:50 +08:00
|
|
|
#include <utils/Log.h>
|
2009-03-04 11:31:44 +08: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-20 00:17:24 +08:00
|
|
|
{
|
|
|
|
return nanoseconds_to_milliseconds(elapsedRealtimeNano());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* native public static long elapsedRealtimeNano();
|
|
|
|
*/
|
|
|
|
int64_t elapsedRealtimeNano()
|
2009-03-04 11:31:44 +08:00
|
|
|
{
|
2016-04-26 01:39:55 +08:00
|
|
|
#if defined(__linux__)
|
2016-04-05 07:06:53 +08:00
|
|
|
struct timespec ts;
|
2016-04-26 01:39:55 +08: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 09:04:32 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 01:39:55 +08:00
|
|
|
return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
|
2009-03-04 11:31:44 +08:00
|
|
|
#else
|
2012-07-20 00:17:24 +08:00
|
|
|
return systemTime(SYSTEM_TIME_MONOTONIC);
|
2009-03-04 11:31:44 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|