• 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 
16 #include "effective.h"
17 #include "FPS.h"
18 #include "sp_utils.h"
19 #include "sp_log.h"
20 #include "common.h"
21 
22 namespace {
23 constexpr long long RM_1000000 = 1000000;
24 constexpr long long RM_5000 = 5000;
25 constexpr long long RM_0 = 0;
26 }
27 namespace OHOS {
28 namespace SmartPerf {
ItemData()29 std::map<std::string, std::string> Effective::ItemData()
30 {
31     std::map<std::string, std::string> templateMap;
32 
33     FpsCurrentFpsTime fcf = FPS::GetInstance().GetFpsCurrentFpsTime();
34     long long nowTime = SPUtils::GetCurTime();
35     long long curframeTime = fcf.currentFpsTime / RM_1000000; // Convert to milliseconds
36 
37     if (startCaptuerTime_ > 0) {
38         long long diff =
39             startCaptuerTime_ > nowTime ? (LLONG_MAX - startCaptuerTime_ + nowTime) : (nowTime - startCaptuerTime_);
40 
41         if (diff > RM_5000 && (!CheckCounterId())) {
42             startCaptuerTime_ = RM_0;
43         }
44     }
45 
46     if (fps_ > fcf.fps || frameTime_ < curframeTime) {
47         if (startCaptuerTime_ == 0) {
48             startCaptuerTime_ = nowTime;
49             ThreadGetHiperf(startCaptuerTime_);
50         }
51     }
52     templateMap["fpsWarn"] = std::to_string(fcf.fps);
53     templateMap["FrameTimeWarn"] = std::to_string(fcf.currentFpsTime);
54     templateMap["TraceTime"] = std::to_string(startCaptuerTime_);
55     LOGI("Effective:ItemData map size(%u)", templateMap.size());
56     return templateMap;
57 }
58 
CheckCounterId()59 bool Effective::CheckCounterId()
60 {
61     std::string result;
62     std::string hiprofilerCmd = CMD_COMMAND_MAP.at(CmdCommand::HIPROFILER_CMD);
63     LOGD("Loading hiprofiler command");
64     SPUtils::LoadCmd(hiprofilerCmd, result);
65     if (result.empty()) {
66         LOGW("Failed to load hiprofiler command or received empty result.");
67         return false;
68     }
69 
70     if (result.find("-k") != std::string::npos) {
71         LOGD("Command contains '-k'.");
72         return true;
73     }
74 
75     LOGD("Command does not contain '-k'.");
76     return false;
77 }
ThreadGetHiperf(long long timeStamp)78 std::thread Effective::ThreadGetHiperf(long long timeStamp)
79 {
80     auto thGetTrace = [this, timeStamp]() { this->GetHiperf(std::to_string(timeStamp)); };
81     std::thread spThread(thGetTrace);
82     spThread.detach();
83     return spThread;
84 }
85 
GetHiperf(const std::string & traceName)86 void Effective::GetHiperf(const std::string &traceName)
87 {
88     std::string result;
89     std::string tmp = SetHiperf(traceName);
90     std::cout << tmp << std::endl;
91     SPUtils::LoadCmd(tmp, result);
92     LOGD("hiprofiler exec (%s), hiprofiler exec trace name(%s), hiprofiler exec end (%s)",
93         tmp.c_str(), traceName.c_str(), result.c_str());
94 }
95 
SetHiperf(const std::string & traceName)96 std::string Effective::SetHiperf(const std::string &traceName)
97 {
98     std::string hiPrefix = "hiprofiler_";
99     std::string dataPrefix = "perf_";
100     requestId_++;
101     std::string trtmp = strOne_ + hiPrefix + traceName + strTwo_ + "\n" + strThree_ + std::to_string(requestId_) +
102         "\n" + strFour_ + "\n" + strFive_ + hiPrefix + traceName + strSix_ + "\n" + strNine_ + strEleven_ + "\n" +
103         strSeven_ + dataPrefix + traceName + strEight_ + strTen_ + "\n" + conFig_;
104     return trtmp;
105 }
106 }
107 }