• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <graphicsenv/GpuStatsInfo.h>
20 #include <graphicsenv/GraphicsEnv.h>
21 #include <stats_pull_atom_callback.h>
22 #include <utils/String16.h>
23 #include <utils/Vector.h>
24 
25 #include <mutex>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace android {
30 
31 class GpuStats {
32 public:
33     ~GpuStats();
34 
35     // Insert new gpu driver stats into global stats and app stats.
36     void insertDriverStats(const std::string& driverPackageName,
37                            const std::string& driverVersionName, uint64_t driverVersionCode,
38                            int64_t driverBuildTime, const std::string& appPackageName,
39                            const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
40                            bool isDriverLoaded, int64_t driverLoadingTime);
41     // Insert target stats into app stats or potentially global stats as well.
42     void insertTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
43                            const GpuStatsInfo::Stats stats, const uint64_t value);
44     void insertTargetStatsArray(const std::string& appPackageName,
45                            const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
46                            const uint64_t* values, const uint32_t valueCount);
47     // Add the engine name passed in VkApplicationInfo during CreateInstance
48     void addVulkanEngineName(const std::string& appPackageName,
49                              const uint64_t driverVersionCode, const char* engineName);
50     // dumpsys interface
51     void dump(const Vector<String16>& args, std::string* result);
52 
53     // This limits the worst case number of loading times tracked.
54     static const size_t MAX_NUM_LOADING_TIMES = 16;
55     // Below limits the memory usage of GpuStats to be less than 10KB. This is
56     // the preferred number for statsd while maintaining nice data quality.
57     static const size_t MAX_NUM_APP_RECORDS = 100;
58     // The number of apps to remove when mAppStats fills up.
59     static const size_t APP_RECORD_HEADROOM = 10;
60 
61 private:
62     // Friend class for testing.
63     friend class TestableGpuStats;
64 
65     // Native atom puller callback registered in statsd.
66     static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag,
67                                                                  AStatsEventList* data,
68                                                                  void* cookie);
69 
70     // Remove old packages from mAppStats.
71     void purgeOldDriverStats();
72 
73     // Pull global into into global atom.
74     AStatsManager_PullAtomCallbackReturn pullGlobalInfoAtom(AStatsEventList* data);
75     // Pull app into into app atom.
76     AStatsManager_PullAtomCallbackReturn pullAppInfoAtom(AStatsEventList* data);
77     // Dump global stats
78     void dumpGlobalLocked(std::string* result);
79     // Dump app stats
80     void dumpAppLocked(std::string* result);
81     // Append cpuVulkanVersion and glesVersion to system driver stats
82     void interceptSystemDriverStatsLocked();
83     // Registers statsd callbacks if they have not already been registered
84     void registerStatsdCallbacksIfNeeded();
85 
86     // GpuStats access should be guarded by mLock.
87     std::mutex mLock;
88     // True if statsd callbacks have been registered.
89     bool mStatsdRegistered = false;
90     // Key is driver version code.
91     std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
92     // Key is <app package name>+<driver version code>.
93     std::unordered_map<std::string, GpuStatsAppInfo> mAppStats;
94 };
95 
96 } // namespace android
97