• 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 "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 
47     LOGI("GPU:ItemData map size(%u)", result.size());
48     return result;
49 }
50 
GetGpuFreq()51 int GPU::GetGpuFreq()
52 {
53     std::shared_ptr<GpuCollector> collector = GpuCollector::Create();
54     CollectResult<GpuFreq> result = collector->CollectGpuFrequency();
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     return float(result.data.gpuLoad);
63 }
64 
GetRkGpuFreq()65 int32_t GPU::GetRkGpuFreq()
66 {
67     const std::string gpuFreqPath = "/sys/class/devfreq/fde60000.gpu/cur_freq";
68     std::string rkFreq;
69     SPUtils::LoadFile(gpuFreqPath, rkFreq);
70     return SPUtilesTye::StringToSometype<int32_t>(rkFreq);
71 }
72 
GetRkGpuLoad()73 float GPU::GetRkGpuLoad()
74 {
75     const std::string gpuLoadPath = "/sys/class/devfreq/fde60000.gpu/load";
76     std::string rkLoad;
77     SPUtils::LoadFile(gpuLoadPath, rkLoad);
78     size_t len = rkLoad.length();
79     if (len > 0) {
80         rkLoad = rkLoad.substr(0, len - 1);
81     }
82     return SPUtilesTye::StringToSometype<float>(rkLoad);
83 }
84 
SetRkFlag()85 void GPU::SetRkFlag()
86 {
87     rkFlag = true;
88 }
89 }
90 }
91