1 /*
2 * Copyright (C) 2021 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 #include "executor/hidumper_executor.h"
16 #include "datetime_ex.h"
17 namespace OHOS {
18 namespace HiviewDFX {
19 const std::string HidumperExecutor::TIME_OUT_STR = "time out";
20
~HidumperExecutor()21 HidumperExecutor::~HidumperExecutor()
22 {
23 }
24
Reset()25 void HidumperExecutor::Reset()
26 {
27 rawParam_ = nullptr;
28 ptrParent_ = nullptr;
29 hasOnce_ = false;
30 hasOnceTimeout_ = false;
31 hitTickCount_ = 0;
32 timeOutMillSec_ = 0;
33 }
34
DoPreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)35 DumpStatus HidumperExecutor::DoPreExecute(const std::shared_ptr<DumperParameter>& parameter, StringMatrix dumpDatas)
36 {
37 if (parameter != nullptr) {
38 rawParam_ = parameter->getClientCallback();
39 // to mill-second.
40 timeOutMillSec_ = parameter->GetOpts().timeout_ * SEC_TO_MILLISEC;
41 }
42
43 if ((!hasOnce_) && (ptrDumpCfg_ != nullptr) && (!ptrDumpCfg_->IsOutput())) {
44 // run here, record the hit tick count.
45 hitTickCount_ = static_cast<uint64_t>(GetTickCount());
46 if (IsTimeout()) {
47 DUMPER_HILOGI(MODULE_COMMON, "info|timeout");
48 hasOnceTimeout_ = true;
49 std::vector<std::string> line;
50 line.push_back(GetTimeoutStr());
51 dumpDatas->push_back(line);
52 return DumpStatus::DUMP_OK;
53 }
54 }
55
56 hasOnce_ = true;
57 DumpStatus ret = PreExecute(parameter, dumpDatas);
58 return ret;
59 }
60
DoExecute()61 DumpStatus HidumperExecutor::DoExecute()
62 {
63 if (hasOnceTimeout_) {
64 return DumpStatus::DUMP_OK;
65 }
66
67 DumpStatus ret = Execute();
68 return ret;
69 }
70
DoAfterExecute()71 DumpStatus HidumperExecutor::DoAfterExecute()
72 {
73 if (hasOnceTimeout_) {
74 return DumpStatus::DUMP_OK;
75 }
76
77 DumpStatus ret = AfterExecute();
78
79 rawParam_ = nullptr;
80 return ret;
81 }
82
SetDumpConfig(const std::shared_ptr<DumpCfg> & config)83 void HidumperExecutor::SetDumpConfig(const std::shared_ptr<DumpCfg>& config)
84 {
85 if (config == nullptr) {
86 DUMPER_HILOGF(MODULE_COMMON, "fatal|config is null");
87 return;
88 }
89
90 ptrDumpCfg_ = config;
91
92 if ((!ptrDumpCfg_->IsOutput()) && (ptrDumpCfg_->parent_ != nullptr)) {
93 ptrParent_ = ptrDumpCfg_->parent_->executor_;
94 }
95 }
96
GetDumpConfig() const97 const std::shared_ptr<DumpCfg>& HidumperExecutor::GetDumpConfig() const
98 {
99 return ptrDumpCfg_;
100 }
101
GetTimeRemain() const102 uint64_t HidumperExecutor::GetTimeRemain() const
103 {
104 uint64_t timeRemain = 0;
105
106 uint64_t hitTickCount = GetHitTickCount();
107 uint64_t nowTickCount = static_cast<uint64_t>(GetTickCount());
108 if (nowTickCount < hitTickCount) {
109 DUMPER_HILOGE(MODULE_COMMON, "error|time jumped");
110 } else {
111 uint64_t tickSpend = nowTickCount - hitTickCount;
112 timeRemain = (timeOutMillSec_ > tickSpend) ? (timeOutMillSec_ - tickSpend) : 0;
113 }
114 return timeRemain;
115 }
116
IsTimeout() const117 bool HidumperExecutor::IsTimeout() const
118 {
119 if (hasOnceTimeout_) {
120 return true;
121 }
122
123 return IsTimeout(GetTimeRemain());
124 }
125
IsCanceled() const126 bool HidumperExecutor::IsCanceled() const
127 {
128 return ((rawParam_ != nullptr) && rawParam_->IsCanceled());
129 }
130
GetTimeoutStr(std::string head) const131 std::string HidumperExecutor::GetTimeoutStr(std::string head) const
132 {
133 return head + TIME_OUT_STR;
134 }
135
IsTimeout(uint64_t remain)136 bool HidumperExecutor::IsTimeout(uint64_t remain)
137 {
138 return (remain < 1);
139 }
140
GetHitTickCount(int nest) const141 uint64_t HidumperExecutor::GetHitTickCount(int nest) const
142 {
143 // no-parent, return this tick count.
144 if (ptrParent_ == nullptr) {
145 return hitTickCount_;
146 }
147
148 // when meet pid group, return tick count.
149 if ((ptrDumpCfg_ != nullptr) && ptrDumpCfg_->IsGroup()) {
150 if (ptrDumpCfg_->type_ == DumperConstant::GROUPTYPE_PID) {
151 return hitTickCount_;
152 }
153 if (ptrDumpCfg_->type_ == DumperConstant::NONE) {
154 return hitTickCount_;
155 }
156 }
157
158 // get parent tick count, this maybe nesting.
159 return ptrParent_->GetHitTickCount(nest + 1);
160 }
161 } // namespace HiviewDFX
162 } // namespace OHOS
163