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
22using namespace OHOS::HiviewDFX;
23using namespace OHOS::HiviewDFX::UCollectUtil;
24using namespace OHOS::HiviewDFX::UCollect;
25
26namespace OHOS {
27namespace SmartPerf {
28std::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
50int 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
58float 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
66int32_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
74float 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
86void GPU::SetRkFlag()
87{
88    rkFlag = true;
89}
90}
91}
92