• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  * limitations under the License.
7  */
8 
9 #include "battery_log.h"
10 #include "battery_xcollie.h"
11 #include "xcollie/xcollie.h"
12 
13 namespace OHOS {
14 namespace PowerMgr {
BatteryXCollie(const std::string & logTag,bool isRecovery)15 BatteryXCollie::BatteryXCollie(const std::string &logTag, bool isRecovery)
16 {
17     logTag_ = logTag;
18     isCanceled_.store(false, std::memory_order_release);
19     const int DFX_DELAY_S = 60;
20     unsigned int flag = HiviewDFX::XCOLLIE_FLAG_LOG;
21     if (isRecovery) {
22         flag = HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY;
23     }
24     id_ = HiviewDFX::XCollie::GetInstance().SetTimer(logTag_, DFX_DELAY_S, nullptr, nullptr, flag);
25     if (id_ == HiviewDFX::INVALID_ID) {
26         BATTERY_HILOGE(COMP_SVC, "Start BatteryXCollie SetTimer fail, tag:%{public}s, timeout(s):%{public}u",
27             logTag_.c_str(), DFX_DELAY_S);
28         return;
29     }
30     BATTERY_HILOGD(COMP_SVC, "Start BatteryXCollie, id:%{public}d, tag:%{public}s, timeout(s):%{public}u", id_,
31         logTag_.c_str(), DFX_DELAY_S);
32 }
33 
~BatteryXCollie()34 BatteryXCollie::~BatteryXCollie()
35 {
36     CancelBatteryXCollie();
37 }
38 
CancelBatteryXCollie()39 void BatteryXCollie::CancelBatteryXCollie()
40 {
41     if (isCanceled_.load(std::memory_order_acquire)) {
42         return;
43     }
44     if (id_ != HiviewDFX::INVALID_ID) {
45         HiviewDFX::XCollie::GetInstance().CancelTimer(id_);
46         id_ = HiviewDFX::INVALID_ID;
47     }
48     isCanceled_.store(true, std::memory_order_release);
49     BATTERY_HILOGD(COMP_SVC, "Cancel BatteryXCollie, id:%{public}d, tag:%{public}s", id_, logTag_.c_str());
50 }
51 
52 } // namespace PowerMgr
53 } // namespace OHOS