From bd510370eaa92a3f635116e72f9935bf5a2d9491 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Fri, 3 Dec 2021 00:49:41 +0800 Subject: [PATCH] 20211203master00 Signed-off-by: guduhanyan --- services/BUILD.gn | 2 +- services/time_manager/include/time_service.h | 2 +- services/time_manager/src/time_service.cpp | 42 ++++++++++--------- services/time_manager/test/BUILD.gn | 2 +- services/timer/include/timer_info.h | 4 +- services/timer/include/timer_manager.h | 5 ++- .../timer/include/timer_manager_interface.h | 4 +- services/timer/src/timer_info.cpp | 2 +- services/timer/src/timer_manager.cpp | 26 ++++++++---- 9 files changed, 51 insertions(+), 38 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 8143b75..ad6e83e 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -71,7 +71,7 @@ ohos_shared_library("time_service") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", - "samgr_L2:samgr_proxy", + "samgr_standard:samgr_proxy", ] part_name = "time_native" subsystem_name = "miscservices" diff --git a/services/time_manager/include/time_service.h b/services/time_manager/include/time_service.h index 8f5abe1..2f31957 100644 --- a/services/time_manager/include/time_service.h +++ b/services/time_manager/include/time_service.h @@ -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); diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index a45d584..b435db5 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -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; } @@ -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 &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 timerCallback = iface_cast(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,21 @@ 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; + 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 +267,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 +284,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 +464,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 +475,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 +486,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 +497,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 +508,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 +519,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 +536,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 +553,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; } diff --git a/services/time_manager/test/BUILD.gn b/services/time_manager/test/BUILD.gn index 33927f5..6d97235 100644 --- a/services/time_manager/test/BUILD.gn +++ b/services/time_manager/test/BUILD.gn @@ -55,7 +55,7 @@ ohos_unittest("TimeServiceTest") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", - "samgr_L2:samgr_proxy", + "samgr_standard:samgr_proxy", ] } diff --git a/services/timer/include/timer_info.h b/services/timer/include/timer_info.h index 2376a3a..6a65251 100644 --- a/services/timer/include/timer_info.h +++ b/services/timer/include/timer_info.h @@ -33,7 +33,7 @@ public: const bool wakeup; const std::function 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 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; diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 0388aa4..4ce05f3 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -37,7 +37,7 @@ public: uint64_t interval, int flag, std::function 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 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 timer, diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index a0e71c7..05cccd4 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -28,7 +28,7 @@ struct TimerEntry { uint64_t interval; int flag; std::function 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 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; diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index ab4f42b..b3365ec 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -35,7 +35,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type, std::chrono::milliseconds _interval, std::function _callback, uint32_t _flags, - uint64_t _uid) + int _uid) : id {_id}, type {_type}, origWhen {_when}, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 48c26f9..153b755 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -29,7 +29,9 @@ static uint32_t TIME_CHANGED_MASK = 1 << TIME_CHANGED_BITS; const int ONE_THOUSAND = 1000; 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); @@ -67,8 +69,8 @@ uint64_t TimerManager::CreateTimer(int type, uint64_t windowLength, uint64_t interval, int flag, - std::function callback, - uint64_t uid) + std::function callback, + int uid) { TIME_HILOGI(TIME_MODULE_SERVICE, "Create timer: %{public}d windowLength:%{public}" PRId64 "interval:%{public}" PRId64 "flag:%{public}d", @@ -145,6 +147,14 @@ bool TimerManager::DestroyTimer(uint64_t timerNumber) return true; } +bool TimerManager::IsSystemUid(int uid) +{ + if (uid < 10000){ + return true; + } + return false; +} + void TimerManager::SetHandler(uint64_t id, int type, uint64_t triggerAtTime, @@ -152,7 +162,7 @@ void TimerManager::SetHandler(uint64_t id, uint64_t interval, int flag, std::function callback, - uint64_t uid) + int uid) { TIME_HILOGI(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id); TIME_HILOGI(TIME_MODULE_SERVICE, @@ -162,10 +172,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 +184,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;