1 /*
2 * Copyright (C) 2024 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 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <csignal>
17 #include <fcntl.h>
18 #include "securec.h"
19 #include <unistd.h>
20 #ifndef OHOS_ARCH_LITE
21 #ifdef WIFI_FFRT_ENABLE
22 #include "c/ffrt_dump.h"
23 #endif
24 #include "xcollie/xcollie.h"
25 #include "xcollie/xcollie_define.h"
26 #endif
27 #include "wifi_watchdog_utils.h"
28 #include "wifi_logger.h"
29 #undef LOG_TAG
30
31 namespace OHOS {
32 namespace Wifi {
33 DEFINE_WIFILOG_LABEL("WifiWatchDogUtils");
34 constexpr int RESET_NOW = 1; //1s
35 constexpr int TIME_OUT_WATCHDOG = 10; // 10s
36 constexpr uint32_t FFRT_CALLBACK_TIME = 5 * 60 * 1000; // 5min
37 constexpr uint32_t TIME_MS_TO_S = 1000;
GetInstance()38 std::shared_ptr<WifiWatchDogUtils> WifiWatchDogUtils::GetInstance()
39 {
40 static std::shared_ptr<WifiWatchDogUtils> instance = std::make_shared<WifiWatchDogUtils>();
41 return instance;
42 }
43
WifiWatchDogUtils()44 WifiWatchDogUtils::WifiWatchDogUtils()
45 {
46 StartAllWatchDog();
47 }
48
~WifiWatchDogUtils()49 WifiWatchDogUtils::~WifiWatchDogUtils()
50 {}
51
ResetProcess(bool usingHiviewDfx,const std::string & threadName,bool notResetProcess)52 bool WifiWatchDogUtils::ResetProcess(bool usingHiviewDfx, const std::string &threadName, bool notResetProcess)
53 {
54 #ifndef OHOS_ARCH_LITE
55 ReportResetEvent(threadName);
56 if (notResetProcess) {
57 WIFI_LOGI("ResetProcess enter, but should not reset process");
58 HiviewDFX::XCollie::GetInstance().SetTimer("WifiResetTimer", TIME_OUT_WATCHDOG,
59 nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG|HiviewDFX::XCOLLIE_FLAG_RECOVERY);
60 return false;
61 }
62 if (usingHiviewDfx) {
63 WIFI_LOGI("ResetProcess through HiviewDfx");
64 //generate sysfreeze file in faultlogger, report to hiview
65 HiviewDFX::XCollie::GetInstance().SetTimer("WifiResetTimer", RESET_NOW,
66 nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG|HiviewDFX::XCOLLIE_FLAG_RECOVERY);
67 } else {
68 WIFI_LOGI("ResetProcess enter, please check crash.cpp for more information");
69 //generate crash file in faultlogger, report to hiview
70 kill(getpid(), SIGSEGV);
71 }
72 #endif
73 return true;
74 }
75
StartWatchDogForFunc(const std::string & funcName)76 int WifiWatchDogUtils::StartWatchDogForFunc(const std::string &funcName)
77 {
78 #ifndef OHOS_ARCH_LITE
79 WIFI_LOGD("StartWatchDogForFunc enter for funcName:%{public}s", funcName.c_str());
80 // this will generate a watchdog file in faultlogger but will not reset process
81 return HiviewDFX::XCollie::GetInstance().SetTimer(funcName, TIME_OUT_WATCHDOG,
82 nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
83 #endif
84 return -1;
85 }
86
StopWatchDogForFunc(const std::string & funcName,int id)87 bool WifiWatchDogUtils::StopWatchDogForFunc(const std::string &funcName, int id)
88 {
89 #ifndef OHOS_ARCH_LITE
90 WIFI_LOGD("StopWatchDogForFunc enter for funcName:%{public}s", funcName.c_str());
91 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
92 #endif
93 return true;
94 }
95
FfrtCallback(uint64_t taskId,const char * taskInfo,uint32_t delayedTaskCount)96 void WifiWatchDogUtils::FfrtCallback(uint64_t taskId, const char *taskInfo, uint32_t delayedTaskCount)
97 {
98 std::string description = "FfrtCallback: task(";
99 description += taskInfo;
100 description += ") blocked " + std::to_string(FFRT_CALLBACK_TIME / TIME_MS_TO_S) + "s";
101 WIFI_LOGI("%{public}s", description.c_str());
102 #ifndef OHOS_ARCH_LITE
103 HiviewDFX::XCollie::GetInstance().SetTimer("WifiResetTimer", RESET_NOW,
104 nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG|HiviewDFX::XCOLLIE_FLAG_RECOVERY);
105 #endif
106 }
107
StartAllWatchDog()108 void WifiWatchDogUtils::StartAllWatchDog()
109 {
110 #ifndef OHOS_ARCH_LITE
111 WIFI_LOGI("StartAllWatchDog enter");
112 #ifdef WIFI_FFRT_ENABLE
113 ffrt_task_timeout_set_cb(FfrtCallback);
114 ffrt_task_timeout_set_threshold(FFRT_CALLBACK_TIME);
115 #endif
116 #endif
117 }
118
ReportResetEvent(const std::string & threadName)119 bool WifiWatchDogUtils::ReportResetEvent(const std::string &threadName)
120 {
121 WIFI_LOGI("ReportResetEvent enter for threadName:%{public}s", threadName.c_str());
122 return true;
123 }
124 } // namespace Wifi
125 } // namespace OHOSs