blob: 49699ee813ae7210354d7816417c4b941aef1193 [file] [log] [blame]
Yiwei Zhang2d4c1882019-02-24 22:28:08 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <mutex>
20#include <unordered_map>
21#include <vector>
22
Yiwei Zhangf40fb102019-02-27 21:05:06 -080023#include <graphicsenv/GpuStatsInfo.h>
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080024#include <graphicsenv/GraphicsEnv.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
27
28namespace android {
29
30class GpuStats {
31public:
32 GpuStats() = default;
33 ~GpuStats() = default;
34
35 // Insert new gpu stats into global stats and app stats.
36 void insert(const std::string& driverPackageName, const std::string& driverVersionName,
37 uint64_t driverVersionCode, int64_t driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070038 const std::string& appPackageName, const int32_t vulkanVersion,
39 GraphicsEnv::Driver driver, bool isDriverLoaded, int64_t driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080040 // dumpsys interface
41 void dump(const Vector<String16>& args, std::string* result);
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080042 // Pull gpu global stats
43 void pullGlobalStats(std::vector<GpuStatsGlobalInfo>* outStats);
Yiwei Zhang178e0672019-03-04 14:17:12 -080044 // Pull gpu app stats
45 void pullAppStats(std::vector<GpuStatsAppInfo>* outStats);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080046
Yiwei Zhangf2d84132019-03-06 11:32:50 -080047 // This limits the worst case number of loading times tracked.
48 static const size_t MAX_NUM_LOADING_TIMES = 50;
49
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080050private:
51 // Dump global stats
52 void dumpGlobalLocked(std::string* result);
53 // Dump app stats
54 void dumpAppLocked(std::string* result);
Yiwei Zhang174a2a02019-05-06 19:08:31 -070055 // Append cpuVulkanVersion and glesVersion to system driver stats
56 void interceptSystemDriverStatsLocked();
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080057
Yiwei Zhangf2d84132019-03-06 11:32:50 -080058 // Below limits the memory usage of GpuStats to be less than 10KB. This is
59 // the preferred number for statsd while maintaining nice data quality.
60 static const size_t MAX_NUM_APP_RECORDS = 100;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080061 // GpuStats access should be guarded by mLock.
62 std::mutex mLock;
63 // Key is driver version code.
Yiwei Zhangf40fb102019-02-27 21:05:06 -080064 std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080065 // Key is <app package name>+<driver version code>.
Yiwei Zhangf40fb102019-02-27 21:05:06 -080066 std::unordered_map<std::string, GpuStatsAppInfo> mAppStats;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080067};
68
69} // namespace android