1 /*
2  * Copyright (c) 2023 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 "gpu_collector_impl.h"
17 
18 #include <sstream>
19 
20 #include "common_util.h"
21 #include "file_util.h"
22 #include "gpu_decorator.h"
23 #include "hiview_logger.h"
24 
25 using namespace OHOS::HiviewDFX::UCollect;
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace UCollectUtil {
30 DEFINE_LOG_TAG("UCollectUtil");
Create()31 std::shared_ptr<GpuCollector> GpuCollector::Create()
32 {
33     return std::make_shared<GpuDecorator>(std::make_shared<GpuCollectorImpl>());
34 }
35 
GetValue(const std::string & fileName)36 inline int32_t GetValue(const std::string& fileName)
37 {
38     std::string content;
39     FileUtil::LoadStringFromFile(fileName, content);
40     int32_t parsedVal = 0;
41     // this string content might be empty or consist of some special charactors
42     // so "std::stoi" and "StringUtil::StrToInt" aren't applicable here.
43     std::stringstream ss(content);
44     ss >> parsedVal;
45     return parsedVal;
46 }
47 
CollectGpuFrequency()48 CollectResult<GpuFreq> GpuCollectorImpl::CollectGpuFrequency()
49 {
50     CollectResult<GpuFreq> result;
51     GpuFreq& gpuFreq = result.data;
52     gpuFreq.curFeq = GetValue(GPU_CUR_FREQ);
53     gpuFreq.maxFeq = GetValue(GPU_MAX_FREQ);
54     gpuFreq.minFeq = GetValue(GPU_MIN_FREQ);
55     HIVIEW_LOGD("curFeq=%{public}d,maxFeq=%{public}d,minFeq=%{public}d",
56         gpuFreq.curFeq, gpuFreq.maxFeq, gpuFreq.minFeq);
57     result.retCode = UcError::SUCCESS;
58     return result;
59 }
60 
CollectSysGpuLoad()61 CollectResult<SysGpuLoad> GpuCollectorImpl::CollectSysGpuLoad()
62 {
63     CollectResult<SysGpuLoad> result;
64     SysGpuLoad& sysGpuLoad = result.data;
65     sysGpuLoad.gpuLoad = GetValue(GPU_LOAD);
66     HIVIEW_LOGD("gpuLoad=%{public}f", sysGpuLoad.gpuLoad);
67     result.retCode = UcError::SUCCESS;
68     return result;
69 }
70 } // UCollectUtil
71 } // HiViewDFX
72 } // OHOS
73