summaryrefslogtreecommitdiff
path: root/services/gpuservice/gpustats/include/gpustats/GpuStats.h
blob: 2aba651af9ff2564450d34afa5b50a07927d62cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <graphicsenv/GpuStatsInfo.h>
#include <graphicsenv/GraphicsEnv.h>
#include <stats_pull_atom_callback.h>
#include <utils/String16.h>
#include <utils/Vector.h>

#include <mutex>
#include <unordered_map>
#include <vector>

namespace android {

class GpuStats {
public:
    ~GpuStats();

    // Insert new gpu driver stats into global stats and app stats.
    void insertDriverStats(const std::string& driverPackageName,
                           const std::string& driverVersionName, uint64_t driverVersionCode,
                           int64_t driverBuildTime, const std::string& appPackageName,
                           const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
                           bool isDriverLoaded, int64_t driverLoadingTime);
    // Insert target stats into app stats or potentially global stats as well.
    void insertTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
                           const GpuStatsInfo::Stats stats, const uint64_t value);
    // dumpsys interface
    void dump(const Vector<String16>& args, std::string* result);

    // This limits the worst case number of loading times tracked.
    static const size_t MAX_NUM_LOADING_TIMES = 50;
    // Below limits the memory usage of GpuStats to be less than 10KB. This is
    // the preferred number for statsd while maintaining nice data quality.
    static const size_t MAX_NUM_APP_RECORDS = 100;
    // The number of apps to remove when mAppStats fills up.
    static const size_t APP_RECORD_HEADROOM = 10;

private:
    // Friend class for testing.
    friend class TestableGpuStats;

    // Native atom puller callback registered in statsd.
    static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atomTag,
                                                                 AStatsEventList* data,
                                                                 void* cookie);

    // Remove old packages from mAppStats.
    void purgeOldDriverStats();

    // Pull global into into global atom.
    AStatsManager_PullAtomCallbackReturn pullGlobalInfoAtom(AStatsEventList* data);
    // Pull app into into app atom.
    AStatsManager_PullAtomCallbackReturn pullAppInfoAtom(AStatsEventList* data);
    // Dump global stats
    void dumpGlobalLocked(std::string* result);
    // Dump app stats
    void dumpAppLocked(std::string* result);
    // Append cpuVulkanVersion and glesVersion to system driver stats
    void interceptSystemDriverStatsLocked();
    // Registers statsd callbacks if they have not already been registered
    void registerStatsdCallbacksIfNeeded();

    // GpuStats access should be guarded by mLock.
    std::mutex mLock;
    // True if statsd callbacks have been registered.
    bool mStatsdRegistered = false;
    // Key is driver version code.
    std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
    // Key is <app package name>+<driver version code>.
    std::unordered_map<std::string, GpuStatsAppInfo> mAppStats;
};

} // namespace android