• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) const22 void ByTrace::SetTraceConfig(int mSum, int mInterval, long long mThreshold) const
23 {
24     sum = mSum;
25     interval = mInterval;
26     threshold = mThreshold;
27 }
28 
ThreadGetTrace() const29 void ByTrace::ThreadGetTrace() const
30 {
31     std::string result;
32     SPUtils::LoadCmd(std::string("bytrace --trace_begin --overwrite"), result);
33     std::cout << "TRACE threadGetTrace >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
34 }
ThreadEndTrace() const35 void ByTrace::ThreadEndTrace() const
36 {
37     std::string result;
38     SPUtils::LoadCmd(std::string("bytrace --trace_finish --overwrite"), result);
39     std::cout << "TRACE threadGetTrace >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
40 }
ThreadFinishTrace(const std::string & pathName) const41 void ByTrace::ThreadFinishTrace(const std::string &pathName) const
42 {
43     std::string result;
44     std::stringstream sstream;
45     sstream.str("");
46     sstream << "bytrace --overwrite sched ace app disk ohos graphic sync workq ability";
47     sstream << " > /data/app/el2/100/base/com.ohos.gameperceptio/haps/entry/files/sptrace_";
48     sstream << pathName << ".ftrace";
49     const std::string &cmdTraceOverwrite = sstream.str();
50     SPUtils::LoadCmd(cmdTraceOverwrite, result);
51     std::cout << "TRACE threadFinishTrace >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
52 }
53 
CheckFpsJitters(std::vector<long long> jitters)54 TraceStatus ByTrace::CheckFpsJitters(std::vector<long long> jitters)
55 {
56     long long curTime = SPUtils::GetCurTime();
57     if (curNum <= sum && curTriggerFlag < 0) {
58         for (size_t i = 0; i < jitters.size(); i++) {
59             long long normalJitter = jitters[i] / 1e6;
60             if (normalJitter > threshold) {
61                 TriggerCatch(curTime);
62             }
63         }
64     }
65     std::cout << "TRACE CHECK RUNING >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
66     std::cout << "TRACE CHECK lastTriggerTime:" << lastTriggerTime << " curTime:" << curTime << " curTriggerFlag:" <<
67         curTriggerFlag << std::endl;
68     if ((curTime - lastTriggerTime) > cost && curTriggerFlag > 0) {
69         std::string result;
70         SPUtils::LoadCmd(std::string("bytrace --trace_finish"), result);
71         std::string pathName = std::to_string(curTime);
72         std::thread tFinish(&ByTrace::ThreadFinishTrace, this, std::ref(pathName));
73         curTriggerFlag = -1;
74         std::cout << "TRACE FINISH >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
75         tFinish.detach();
76     }
77     return TraceStatus::TRACE_FINISH;
78 }
TriggerCatch(long long curTime) const79 void ByTrace::TriggerCatch(long long curTime) const
80 {
81     if ((curTime - lastTriggerTime) > interval) {
82         std::thread tStart(&ByTrace::ThreadGetTrace, this);
83         curTriggerFlag = 1;
84         lastTriggerTime = curTime;
85         curNum++;
86         std::cout << "TRACE START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
87         tStart.detach();
88     }
89 }
90 }
91 }
92