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 <iostream>
16 #include <sstream>
17 #include <thread>
18 #include "unistd.h"
19 #include "include/sp_utils.h"
20 #include "include/ByTrace.h"
21 #include "include/sp_log.h"
22 #include "include/common.h"
23 namespace OHOS {
24 namespace SmartPerf {
SetTraceConfig(int mSum,int mInterval,long long mThreshold,int mLowfps,int mCurNum) const25 void ByTrace::SetTraceConfig(int mSum, int mInterval, long long mThreshold, int mLowfps, int mCurNum) const
26 {
27 sum = mSum;
28 interval = mInterval;
29 threshold = mThreshold;
30 lowfps = mLowfps;
31 curNum = mCurNum;
32 LOGD("ByTrace::SetTraceConfig mSum(%d) mInterval(%d) mThreshold(%lld) mLowfps(%d) mCurNum(%d)",
33 mSum, mInterval, mThreshold, mLowfps, mCurNum);
34 }
ThreadGetTrace() const35 void ByTrace::ThreadGetTrace() const
36 {
37 std::string result;
38 std::string cmdString;
39 if (SPUtils::IsHmKernel()) {
40 cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_1024);
41 } else {
42 cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_2048);
43 }
44 std::string time = std::to_string(SPUtils::GetCurTime());
45 std::string traceFile = "/data/local/tmp/sptrace_" + time + ".ftrace";
46 std::string traceCmdExe = cmdString + traceFile;
47 SPUtils::LoadCmd(traceCmdExe, result);
48 LOGD("TRACE threadGetTrace CMD(%s)", traceCmdExe.c_str());
49 }
CheckFpsJitters(std::vector<long long> jitters,int cfps) const50 TraceStatus ByTrace::CheckFpsJitters(std::vector<long long> jitters, int cfps) const
51 {
52 times++;
53 int two = 2;
54 long long curTime = SPUtils::GetCurTime();
55 LOGD("Bytrace get curTime : %lld", curTime);
56 if (curNum <= sum && currentTrigger < 0 && times > two) {
57 for (size_t i = 0; i < jitters.size(); i++) {
58 long long normalJitter = jitters[i] / 1e6;
59 if (normalJitter > threshold || cfps < lowfps) {
60 TriggerCatch(curTime);
61 }
62 }
63 }
64 if ((curTime - lastTriggerTime) / 1e3 > interval && currentTrigger == 1) {
65 currentTrigger = -1;
66 }
67 return TraceStatus::TRACE_FINISH;
68 }
TriggerCatch(long long curTime) const69 void ByTrace::TriggerCatch(long long curTime) const
70 {
71 if ((curTime - lastTriggerTime) / 1e3 > interval && !CheckHitraceId()) {
72 std::thread tStart([this] { this->ThreadGetTrace(); });
73 currentTrigger = 1;
74 lastTriggerTime = curTime;
75 curNum++;
76 tStart.detach();
77 }
78 }
79
CheckHitraceId() const80 bool ByTrace::CheckHitraceId() const
81 {
82 std::string result;
83 std::string hitrace = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_CMD);
84 SPUtils::LoadCmd(hitrace, result);
85 if (result.empty()) {
86 return false;
87 }
88 if (result.find("-t") != std::string::npos) {
89 return true;
90 }
91 return false;
92 }
93 }
94 }
95