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 "include/sp_utils.h"
19 #include "include/ByTrace.h"
20 namespace OHOS {
21 namespace SmartPerf {
SetTraceConfig(int mSum,int mInterval,long long mThreshold,int mLowfps,int mCurNum) const22 void ByTrace::SetTraceConfig(int mSum, int mInterval, long long mThreshold, int mLowfps, int mCurNum) const
23 {
24 sum = mSum;
25 interval = mInterval;
26 threshold = mThreshold;
27 lowfps = mLowfps;
28 curNum = mCurNum;
29 }
SetTraceCmd(std::string mTraceCmd) const30 void ByTrace::SetTraceCmd(std::string mTraceCmd) const
31 {
32 traceCmd = mTraceCmd;
33 }
ThreadGetTrace() const34 void ByTrace::ThreadGetTrace() const
35 {
36 std::string result;
37 std::stringstream sstream;
38 std::string time = std::to_string(SPUtils::GetCurTime());
39 sstream.str("");
40 sstream << traceCmd;
41 sstream << " > /data/app/el2/100/base/com.ohos.smartperf/haps/entry/files/sptrace_";
42 sstream << time << ".ftrace";
43 std::string traceCmdExe = sstream.str();
44 SPUtils::LoadCmd(traceCmdExe, result);
45 std::cout << "TRACE threadGetTrace >> CMD >>" << traceCmdExe << std::endl;
46 }
CheckFpsJitters(std::vector<long long> jitters,int cfps)47 TraceStatus ByTrace::CheckFpsJitters(std::vector<long long> jitters, int cfps)
48 {
49 times++;
50 int two = 2;
51 long long curTime = SPUtils::GetCurTime();
52 if (curNum <= sum && currentTrigger < 0 && times > two) {
53 for (size_t i = 0; i < jitters.size(); i++) {
54 long long normalJitter = jitters[i] / 1e6;
55 if (normalJitter > threshold || cfps < lowfps) {
56 TriggerCatch(curTime);
57 }
58 }
59 }
60 std::cout << "TRACE CHECK RUNING >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
61 std::cout << "TRACE CHECK lastTriggerTime:" << lastTriggerTime << " curTime:" << curTime << " currentTrigger:" <<
62 currentTrigger << std::endl;
63 if ((curTime - lastTriggerTime) / 1e3 > interval && currentTrigger == 1) {
64 currentTrigger = -1;
65 }
66 return TraceStatus::TRACE_FINISH;
67 }
TriggerCatch(long long curTime) const68 void ByTrace::TriggerCatch(long long curTime) const
69 {
70 if ((curTime - lastTriggerTime) / 1e3 > interval) {
71 std::thread tStart(&ByTrace::ThreadGetTrace, this);
72 currentTrigger = 1;
73 lastTriggerTime = curTime;
74 curNum++;
75 std::cout << "TRACE START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
76 tStart.detach();
77 }
78 }
79 }
80 }
81