• 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 <iostream>
17 #include "include/GPU.h"
18 
19 namespace OHOS {
20     namespace SmartPerf {
21         pthread_mutex_t GPU::mutex;
22         GPU *GPU::instance = nullptr;
23 
getInstance()24         GPU *GPU::getInstance()
25         {
26             if (instance == nullptr) {
27                 pthread_mutex_lock(&mutex);
28                 if (instance == nullptr) {
29                     instance = new GPU();
30                 }
31                 pthread_mutex_unlock(&mutex);
32             }
33             return instance;
34         }
35 
GPU()36         GPU::GPU()
37         {
38             pthread_mutex_init(&mutex, nullptr);
39 
40             for (int i = 0; i < sizeof(GPU_CUR_FREQ_PATH) / sizeof(const char *); ++i) {
41                 if (GPUtils::canOpen(GPU_CUR_FREQ_PATH[i])) {
42                     gpu_cur_freq_path = std::string(GPU_CUR_FREQ_PATH[i]);
43                 }
44             }
45 
46             for (int i = 0; i < sizeof(GPU_CUR_WORKLOAD_PATH) / sizeof(const char *); ++i) {
47                 if (GPUtils::canOpen(GPU_CUR_WORKLOAD_PATH[i])) {
48                     gpu_cur_load_path = std::string(GPU_CUR_WORKLOAD_PATH[i]);
49                 }
50             }
51         }
52 
get_gpu_freq()53         int GPU::get_gpu_freq()
54         {
55             const int unit = 1000;
56             static char buffer[128];
57             FILE *fp = fopen(gpu_cur_freq_path.c_str(), "r");
58             if (fp == nullptr) {
59                 return -1;
60             }
61             buffer[0] = '\0';
62             while (fgets(buffer, sizeof(buffer), fp) == nullptr) {
63                 std::cout << "fgets fail";
64             }
65             if (fclose(fp) == EOF) {
66                 return EOF;
67             }
68             int curGpuFreq = -1;
69             int tmp;
70             if (sscanf(buffer, "%d %d", &tmp, &curGpuFreq) < 0) {
71                 std::cout << "sscanf fail";
72             }
73             if (curGpuFreq != -1) {
74                 return curGpuFreq * unit;
75             } else {
76                 return atoi(buffer);
77             }
78         }
79 
calc_workload(const char * buffer) const80         float GPU::calc_workload(const char *buffer) const
81         {
82             std::vector<std::string> sps;
83             std::string buffer_line = buffer;
84             GPUtils::mSplit(buffer, "@", sps);
85             if (sps.size() > 0) {
86                 // rk3568
87                 float loadRk = std::stof(sps[0]);
88                 return loadRk;
89             } else {
90                 // wgr
91                 float loadWgr = std::stof(buffer);
92                 return loadWgr;
93             }
94             return -1.0;
95         }
96 
get_gpu_load()97         float GPU::get_gpu_load()
98         {
99             static char buffer[128];
100             FILE *fp = fopen(gpu_cur_load_path.c_str(), "r");
101             if (fp == nullptr) {
102                 return EOF;
103             }
104             buffer[0] = '\0';
105             while (fgets(buffer, sizeof(buffer), fp) == nullptr) {
106                 std::cout << "fgets fail";
107             }
108             if (fclose(fp) == EOF) {
109                 return -1.0;
110             }
111             return calc_workload(buffer);
112         }
113     }
114 }
115