1 /*
2 * Copyright (c) 2021-2022 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 "handler_checker.h"
17 #include "ipc_skeleton.h"
18 #include "xcollie_utils.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
ScheduleCheck()22 void HandlerChecker::ScheduleCheck()
23 {
24 if (!isCompleted_ || handler_ == nullptr) {
25 return;
26 }
27 if (name_.compare(IPC_FULL) == 0) {
28 auto fb = [] {
29 IPCDfx::BlockUntilThreadAvailable();
30 };
31 if (!handler_->PostTask(fb, "IpcCheck Task", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) {
32 XCOLLIE_LOGE("XCollie IpcCheck Task PostTask failed.");
33 }
34 }
35
36 isCompleted_.store(false);
37 taskSlow = false;
38 auto f = [this] () {
39 this->isCompleted_.store(true);
40 };
41 if (!handler_->PostTask(f, "XCollie Watchdog Task", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) {
42 XCOLLIE_LOGE("XCollie Watchdog Task PostTask False.");
43 }
44 }
45
GetCheckState()46 int HandlerChecker::GetCheckState()
47 {
48 if (isCompleted_) {
49 taskSlow = false;
50 return CheckStatus::COMPLETED;
51 } else {
52 if (!taskSlow) {
53 taskSlow = true;
54 return CheckStatus::WAITED_HALF;
55 } else {
56 return CheckStatus::WAITING;
57 }
58 }
59 }
60
GetDumpInfo()61 std::string HandlerChecker::GetDumpInfo()
62 {
63 std::string ret;
64 if (handler_ != nullptr) {
65 HandlerDumper handlerDumper;
66 handler_->Dump(handlerDumper);
67 ret = handlerDumper.GetDumpInfo();
68 }
69 return ret;
70 }
71
72
GetHandler() const73 std::shared_ptr<AppExecFwk::EventHandler> HandlerChecker::GetHandler() const
74 {
75 return handler_;
76 }
77
Dump(const std::string & message)78 void HandlerDumper::Dump(const std::string &message)
79 {
80 XCOLLIE_LOGD("message is %{public}s", message.c_str());
81 dumpInfo_ += message;
82 }
83
GetTag()84 std::string HandlerDumper::GetTag()
85 {
86 return "";
87 }
88
GetDumpInfo()89 std::string HandlerDumper::GetDumpInfo()
90 {
91 return dumpInfo_;
92 }
93 } // end of namespace HiviewDFX
94 } // end of namespace OHOS
95