1/*
2 * Copyright (C) 2024 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#ifndef GPU_COUNTER_H
17#define GPU_COUNTER_H
18
19#include "string"
20#include "vector"
21#include "sp_profiler.h"
22#include "GpuCounterCallback.h"
23#include "thread"
24#include "mutex"
25
26namespace OHOS {
27    namespace SmartPerf {
28        class GpuCounterPlugin {
29        public:
30            uint32_t version;
31            const char *pluginName;
32
33            virtual int32_t StartGetGpuPerfInfo(int64_t duration, std::unique_ptr <GpuCounterCallback> callback) = 0;
34            virtual int32_t StopGetGpuPerfInfo() = 0;
35        };
36
37        class GpuCounter : public SpProfiler {
38        public:
39            enum GcStatus {
40                GC_INIT = 0,
41                GC_RUNNING,
42            };
43
44            enum GcCollectType {
45                GC_START = 0,
46                GC_RESTART,
47            };
48
49        public:
50            std::map<std::string, std::string> ItemData() override;
51
52            static GpuCounter &GetInstance()
53            {
54                static GpuCounter instance;
55                return instance;
56            }
57            void StartCollect(GcCollectType type);
58            void StopCollect();
59            std::vector<std::string> &GetGpuCounterData();
60            std::vector<std::string> &GetGpuCounterSaveReportData();
61            std::mutex &GetRealtimeDataLock();
62            std::string &GetGpuCounterRealtimeData();
63            void AddGpuCounterRealtimeData(std::string dataString);
64            void GetGpuRealtimeData(std::map<std::string, std::string> &dataMap);
65            void SaveData(std::string path);
66        private:
67            GpuCounter() {};
68            GpuCounter(const GpuCounter &);
69            GpuCounter &operator = (const GpuCounter &);
70            void* GetSoHandle();
71            GcStatus gcStatus = GC_INIT;
72            std::vector<std::string> gpuCounterData;
73            std::vector<std::string> gpuCounterSaveReportData;
74            std::mutex realtimeDataLock;
75            std::string gpuCounterRealtimeData;
76            const std::string PLUGIN_SO_PATH = "system/lib64/libgameservice_gpucounter_plugin.z.so";
77            const std::string CREATE_PLUGIN = "onCreatePlugin";
78        };
79    };
80}
81
82
83#endif
84