mirror of
https://github.com/openharmony/miscservices_time.git
synced 2026-07-19 12:02:04 -04:00
!38 调整定时器5s间隔
Merge pull request !38 from guduhanyan/OpenHarmony-3.0-LTS
This commit is contained in:
@@ -77,7 +77,7 @@ private:
|
||||
void InitTimeZone();
|
||||
void InitTimerHandler();
|
||||
void PaserTimerPara(int32_t type, bool repeat, uint64_t interval, TimerPara ¶s);
|
||||
bool GetTimeByClockid(clockid_t clockID, struct timespec* tv);
|
||||
bool GetTimeByClockid(clockid_t clockID, struct timespec &tv);
|
||||
int set_rtc_time(time_t sec);
|
||||
|
||||
bool check_rtc(std::string rtc_path, uint64_t rtc_id);
|
||||
|
||||
@@ -45,12 +45,12 @@ namespace {
|
||||
static const int MILLI_TO_BASE = 1000LL;
|
||||
static const int MICR_TO_BASE = 1000000LL;
|
||||
static const int NANO_TO_BASE = 1000000000LL;
|
||||
static const int FIVE_THOUSANDS = 5000LL;
|
||||
static const std::int32_t INIT_INTERVAL = 10000L;
|
||||
static const uint32_t TIMER_TYPE_REALTIME_MASK = 1 << 0;
|
||||
static const uint32_t TIMER_TYPE_REALTIME_WAKEUP_MASK = 1 << 1;
|
||||
static const uint32_t TIMER_TYPE_EXACT_MASK = 1 << 2;
|
||||
constexpr int MIN_TRIGGER_TIMES = 5000;
|
||||
constexpr int INVALID_TIMER_ID = 0;
|
||||
constexpr int MILLI_TO_MICR = MICR_TO_BASE / MILLI_TO_BASE;
|
||||
constexpr int NANO_TO_MILLI = NANO_TO_BASE / MILLI_TO_BASE;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ void TimeService::OnStart()
|
||||
return;
|
||||
}
|
||||
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "Start TimeService success.");
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "Start TimeService success.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T
|
||||
paras.timerType = ITimerManager::TimerType::RTC;
|
||||
}
|
||||
if (repeat) {
|
||||
paras.interval = (interval < FIVE_THOUSANDS) ? FIVE_THOUSANDS : interval;
|
||||
paras.interval = interval;
|
||||
} else {
|
||||
paras.interval = 0;
|
||||
}
|
||||
@@ -208,16 +208,17 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T
|
||||
uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval,
|
||||
sptr<IRemoteObject> &obj)
|
||||
{
|
||||
int uid = IPCSkeleton::GetCallingUid();
|
||||
if (obj == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr.");
|
||||
return 0;
|
||||
return INVALID_TIMER_ID;
|
||||
}
|
||||
struct TimerPara paras {};
|
||||
PaserTimerPara(type, repeat, interval, paras);
|
||||
sptr<ITimerCallback> timerCallback = iface_cast<ITimerCallback>(obj);
|
||||
if (timerCallback == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "ITimerCallback nullptr.");
|
||||
return 0;
|
||||
return INVALID_TIMER_ID;
|
||||
}
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "Start create timer.");
|
||||
auto callbackFunc = [timerCallback](uint64_t id) {
|
||||
@@ -229,7 +230,7 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval,
|
||||
timerManagerHandler_ = TimerManager::Create();
|
||||
if (timerManagerHandler_ == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Redo Timer manager Init Failed.");
|
||||
return 0;
|
||||
return INVALID_TIMER_ID;
|
||||
}
|
||||
}
|
||||
return timerManagerHandler_->CreateTimer(paras.timerType,
|
||||
@@ -237,20 +238,20 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval,
|
||||
paras.interval,
|
||||
paras.flag,
|
||||
callbackFunc,
|
||||
0);
|
||||
uid);
|
||||
}
|
||||
|
||||
bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes)
|
||||
{
|
||||
uint64_t triggerTimesIn = (triggerTimes < MIN_TRIGGER_TIMES) ? MIN_TRIGGER_TIMES : triggerTimes;
|
||||
if (timerManagerHandler_ == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Timer manager nullptr.");
|
||||
timerManagerHandler_ = TimerManager::Create();
|
||||
if (timerManagerHandler_ == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Redo Timer manager Init Failed.");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint64_t triggerTimesIn = (triggerTimes < MIN_TRIGGER_TIMES) ? MIN_TRIGGER_TIMES : triggerTimes;
|
||||
auto ret = timerManagerHandler_->StartTimer(timerId, triggerTimesIn);
|
||||
if (!ret) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "TimerId Not found.");
|
||||
@@ -265,7 +266,7 @@ bool TimeService::StopTimer(uint64_t timerId)
|
||||
timerManagerHandler_ = TimerManager::Create();
|
||||
if (timerManagerHandler_ == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Redo Timer manager Init Failed.");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto ret = timerManagerHandler_->StopTimer(timerId);
|
||||
@@ -282,7 +283,7 @@ bool TimeService::DestroyTimer(uint64_t timerId)
|
||||
timerManagerHandler_ = TimerManager::Create();
|
||||
if (timerManagerHandler_ == nullptr) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Redo Timer manager Init Failed.");
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto ret = timerManagerHandler_->DestroyTimer(timerId);
|
||||
@@ -462,7 +463,7 @@ int32_t TimeService::GetWallTimeMs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_REALTIME, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_REALTIME, tv)) {
|
||||
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -473,7 +474,7 @@ int32_t TimeService::GetWallTimeNs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_REALTIME, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_REALTIME, tv)) {
|
||||
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -484,7 +485,7 @@ int32_t TimeService::GetBootTimeMs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_BOOTTIME, tv)) {
|
||||
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -495,7 +496,7 @@ int32_t TimeService::GetBootTimeNs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_BOOTTIME, tv)) {
|
||||
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -506,7 +507,7 @@ int32_t TimeService::GetMonotonicTimeMs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_MONOTONIC, tv)) {
|
||||
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -517,7 +518,7 @@ int32_t TimeService::GetMonotonicTimeNs(int64_t ×)
|
||||
{
|
||||
struct timespec tv {};
|
||||
|
||||
if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) {
|
||||
if (GetTimeByClockid(CLOCK_MONOTONIC, tv)) {
|
||||
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -534,7 +535,7 @@ int32_t TimeService::GetThreadTimeMs(int64_t ×)
|
||||
return E_TIME_PARAMETERS_INVALID;
|
||||
}
|
||||
|
||||
if (GetTimeByClockid(cid, &tv)) {
|
||||
if (GetTimeByClockid(cid, tv)) {
|
||||
times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI;
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -551,16 +552,16 @@ int32_t TimeService::GetThreadTimeNs(int64_t ×)
|
||||
return E_TIME_PARAMETERS_INVALID;
|
||||
}
|
||||
|
||||
if (GetTimeByClockid(cid, &tv)) {
|
||||
if (GetTimeByClockid(cid, tv)) {
|
||||
times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec;
|
||||
return ERR_OK;
|
||||
}
|
||||
return E_TIME_DEAL_FAILED;
|
||||
}
|
||||
|
||||
bool TimeService::GetTimeByClockid(clockid_t clk_id, struct timespec *tv)
|
||||
bool TimeService::GetTimeByClockid(clockid_t clk_id, struct timespec &tv)
|
||||
{
|
||||
if (clock_gettime(clk_id, tv) < 0) {
|
||||
if (clock_gettime(clk_id, &tv) < 0) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "Failed clock_gettime.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
const bool wakeup;
|
||||
const std::function<void (const uint64_t)> callback;
|
||||
const uint32_t flags;
|
||||
const uint64_t uid;
|
||||
const int uid;
|
||||
|
||||
uint64_t count {};
|
||||
std::chrono::milliseconds when;
|
||||
@@ -52,7 +52,7 @@ public:
|
||||
std::chrono::milliseconds interval,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
uint32_t flags,
|
||||
uint64_t uid);
|
||||
int uid);
|
||||
virtual ~TimerInfo() = default;
|
||||
bool operator==(const TimerInfo &other) const;
|
||||
bool Matches(const std::string &packageName) const;
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
uint64_t interval,
|
||||
int flag,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
uint64_t uid) override;
|
||||
int uid) override;
|
||||
bool StartTimer(uint64_t timerNumber, uint64_t triggerTime) override;
|
||||
bool StopTimer(uint64_t timerNumber) override;
|
||||
bool DestroyTimer(uint64_t timerNumber) override;
|
||||
@@ -54,7 +54,7 @@ private:
|
||||
uint64_t interval,
|
||||
int flag,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
uint64_t uid);
|
||||
int uid);
|
||||
void SetHandlerLocked(uint64_t id,
|
||||
int type,
|
||||
std::chrono::milliseconds when,
|
||||
@@ -68,6 +68,7 @@ private:
|
||||
uint64_t callingUid);
|
||||
void RemoveHandler(uint64_t id);
|
||||
void RemoveLocked(uint64_t id);
|
||||
bool IsSystemUid(int uid);
|
||||
void ReBatchAllTimers();
|
||||
void ReBatchAllTimersLocked(bool doValidate);
|
||||
void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
|
||||
|
||||
@@ -28,7 +28,7 @@ struct TimerEntry {
|
||||
uint64_t interval;
|
||||
int flag;
|
||||
std::function<void (const uint64_t)> callback;
|
||||
uint64_t uid;
|
||||
int uid;
|
||||
};
|
||||
|
||||
class ITimerManager {
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
|
||||
virtual uint64_t CreateTimer(int type, uint64_t windowLength, uint64_t interval, int flag,
|
||||
std::function<void(const uint64_t)> callback,
|
||||
uint64_t uid) = 0;
|
||||
int uid) = 0;
|
||||
|
||||
virtual bool StartTimer(uint64_t timerNumber, uint64_t triggerTime) = 0;
|
||||
virtual bool StopTimer(uint64_t timerNumber) = 0;
|
||||
|
||||
@@ -35,7 +35,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type,
|
||||
std::chrono::milliseconds _interval,
|
||||
std::function<void(const uint64_t)> _callback,
|
||||
uint32_t _flags,
|
||||
uint64_t _uid)
|
||||
int _uid)
|
||||
: id {_id},
|
||||
type {_type},
|
||||
origWhen {_when},
|
||||
|
||||
@@ -27,9 +27,12 @@ namespace {
|
||||
static int TIME_CHANGED_BITS = 16;
|
||||
static uint32_t TIME_CHANGED_MASK = 1 << TIME_CHANGED_BITS;
|
||||
const int ONE_THOUSAND = 1000;
|
||||
const int FIRST_APPLICATION_UID = 10000;
|
||||
const float_t BATCH_WINDOW_COE = 0.75;
|
||||
const auto MIN_FUTURITY = seconds(5);
|
||||
const auto MIN_INTERVAL = seconds(5);
|
||||
const auto ZERO_FUTURITY = seconds(0);
|
||||
const auto MIN_INTERVAL_FIVE_SECONDS = seconds(5);
|
||||
const auto MIN_INTERVAL_ONE_SECONDS = seconds(1);
|
||||
const auto MAX_INTERVAL = hours(24 * 365);
|
||||
const auto INTERVAL_HOUR = hours(1);
|
||||
const auto INTERVAL_HALF_DAY = hours(12);
|
||||
@@ -68,7 +71,7 @@ uint64_t TimerManager::CreateTimer(int type,
|
||||
uint64_t interval,
|
||||
int flag,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
uint64_t uid)
|
||||
int uid)
|
||||
{
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE,
|
||||
"Create timer: %{public}d windowLength:%{public}" PRId64 "interval:%{public}" PRId64 "flag:%{public}d",
|
||||
@@ -145,6 +148,14 @@ bool TimerManager::DestroyTimer(uint64_t timerNumber)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TimerManager::IsSystemUid(int uid)
|
||||
{
|
||||
if (uid < FIRST_APPLICATION_UID) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void TimerManager::SetHandler(uint64_t id,
|
||||
int type,
|
||||
uint64_t triggerAtTime,
|
||||
@@ -152,7 +163,7 @@ void TimerManager::SetHandler(uint64_t id,
|
||||
uint64_t interval,
|
||||
int flag,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
uint64_t uid)
|
||||
int uid)
|
||||
{
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id);
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE,
|
||||
@@ -162,10 +173,10 @@ void TimerManager::SetHandler(uint64_t id,
|
||||
if (windowLengthDuration > INTERVAL_HALF_DAY) {
|
||||
windowLengthDuration = INTERVAL_HOUR;
|
||||
}
|
||||
|
||||
auto minInterval = (IsSystemUid(uid)) ? MIN_INTERVAL_ONE_SECONDS : MIN_INTERVAL_FIVE_SECONDS;
|
||||
auto intervalDuration = milliseconds(interval);
|
||||
if (intervalDuration > milliseconds::zero() && intervalDuration < MIN_INTERVAL) {
|
||||
intervalDuration = MIN_INTERVAL;
|
||||
if (intervalDuration > milliseconds::zero() && intervalDuration < minInterval) {
|
||||
intervalDuration = minInterval;
|
||||
} else if (intervalDuration > MAX_INTERVAL) {
|
||||
intervalDuration = MAX_INTERVAL;
|
||||
}
|
||||
@@ -174,7 +185,7 @@ void TimerManager::SetHandler(uint64_t id,
|
||||
}
|
||||
auto nowElapsed = steady_clock::now();
|
||||
auto nominalTrigger = ConvertToElapsed(milliseconds(triggerAtTime), type);
|
||||
auto minTrigger = nowElapsed + MIN_FUTURITY;
|
||||
auto minTrigger = (IsSystemUid(uid)) ? nowElapsed + ZERO_FUTURITY : nowElapsed + MIN_FUTURITY;
|
||||
auto triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger;
|
||||
|
||||
steady_clock::time_point maxElapsed;
|
||||
|
||||
Reference in New Issue
Block a user