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 "include/GPU.h"
16 #include <iostream>
17 #include "include/sp_utils.h"
18 #include "gpu_collector.h"
19 #include "collect_result.h"
20 #include "include/sp_log.h"
21 
22 using namespace OHOS::HiviewDFX;
23 using namespace OHOS::HiviewDFX::UCollectUtil;
24 using namespace OHOS::HiviewDFX::UCollect;
25 
26 namespace OHOS {
27 namespace SmartPerf {
ItemData()28 std::map<std::string, std::string> GPU::ItemData()
29 {
30     std::map<std::string, std::string> result;
31     int32_t freq;
32     float load;
33     if (!rkFlag) {
34         freq = GetGpuFreq();
35         load = GetGpuLoad();
36     } else {
37         freq = GetRkGpuFreq();
38         load = GetRkGpuLoad();
39     }
40     result["gpuFrequency"] = std::to_string(freq);
41     result["gpuLoad"] = std::to_string(load);
42     if (result.find("gpuFrequency") != result.end() && result["gpuFrequency"].empty()) {
43         result["gpuFrequency"] = "NA";
44         result["gpuLoad"] = "NA";
45     }
46     LOGI("GPU::ItemData map size(%u)", result.size());
47     return result;
48 }
49 
GetGpuFreq()50 int GPU::GetGpuFreq()
51 {
52     std::shared_ptr<GpuCollector> collector = GpuCollector::Create();
53     CollectResult<GpuFreq> result = collector->CollectGpuFrequency();
54     LOGI("GpuFrequency: %s", std::to_string(result.data.curFeq).c_str());
55     return result.data.curFeq;
56 }
57 
GetGpuLoad()58 float GPU::GetGpuLoad()
59 {
60     std::shared_ptr<GpuCollector> collector = GpuCollector::Create();
61     CollectResult<SysGpuLoad> result = collector->CollectSysGpuLoad();
62     LOGI("SysGpuLoad: %s", std::to_string(result.data.gpuLoad).c_str());
63     return float(result.data.gpuLoad);
64 }
65 
GetRkGpuFreq()66 int32_t GPU::GetRkGpuFreq()
67 {
68     const std::string gpuFreqPath = "/sys/class/devfreq/fde60000.gpu/cur_freq";
69     std::string rkFreq;
70     SPUtils::LoadFile(gpuFreqPath, rkFreq);
71     return std::stoi(rkFreq);
72 }
73 
GetRkGpuLoad()74 float GPU::GetRkGpuLoad()
75 {
76     const std::string gpuLoadPath = "/sys/class/devfreq/fde60000.gpu/load";
77     std::string rkLoad;
78     SPUtils::LoadFile(gpuLoadPath, rkLoad);
79     size_t len = rkLoad.length();
80     if (len > 0) {
81         rkLoad = rkLoad.substr(0, len - 1);
82     }
83     return std::stoi(rkLoad);
84 }
85 
SetRkFlag()86 void GPU::SetRkFlag()
87 {
88     rkFlag = true;
89 }
90 }
91 }
92