blob: 833ffa4d0bd7232289b4adedb2fbe4415ada1024 [file] [log] [blame]
Adam Stonef0e618d2018-01-17 19:20:41 -08001/*
2 * Copyright (C) 2018 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#ifndef DRM_METRICS_H_
18#define DRM_METRICS_H_
19
Adam Stonecea91ce2018-01-22 19:23:28 -080020#include <map>
21
22#include <android/hardware/drm/1.0/types.h>
Adam Stone28f27c32018-02-05 15:07:48 -080023#include <android/hardware/drm/1.1/types.h>
Robert Shiha5033262019-05-06 14:15:12 -070024#include <android/hardware/drm/1.2/types.h>
Adam Stone637b7852018-01-30 12:09:36 -080025#include <binder/PersistableBundle.h>
Adam Stonef0e618d2018-01-17 19:20:41 -080026#include <media/CounterMetric.h>
27#include <media/EventMetric.h>
Robert Shih82ea6be2019-11-07 17:47:23 -080028#include <sys/types.h>
Adam Stonef0e618d2018-01-17 19:20:41 -080029
30namespace android {
31
32/**
33 * This class contains the definition of metrics captured within MediaDrm.
34 * It also contains a method for exporting all of the metrics to a
Adam Stone568b3c42018-01-31 12:57:16 -080035 * PersistableBundle.
Adam Stonef0e618d2018-01-17 19:20:41 -080036 */
37class MediaDrmMetrics {
38 public:
39 explicit MediaDrmMetrics();
Adam Stonefb679e32018-02-07 10:25:48 -080040 virtual ~MediaDrmMetrics() {};
Adam Stonecea91ce2018-01-22 19:23:28 -080041 // Count of openSession calls.
Adam Stonef0e618d2018-01-17 19:20:41 -080042 CounterMetric<status_t> mOpenSessionCounter;
Adam Stonecea91ce2018-01-22 19:23:28 -080043 // Count of closeSession calls.
44 CounterMetric<status_t> mCloseSessionCounter;
45 // Count and timing of getKeyRequest calls.
Adam Stonefb679e32018-02-07 10:25:48 -080046 EventMetric<status_t> mGetKeyRequestTimeUs;
Adam Stonecea91ce2018-01-22 19:23:28 -080047 // Count and timing of provideKeyResponse calls.
Adam Stonefb679e32018-02-07 10:25:48 -080048 EventMetric<status_t> mProvideKeyResponseTimeUs;
Adam Stonecea91ce2018-01-22 19:23:28 -080049 // Count of getProvisionRequest calls.
50 CounterMetric<status_t> mGetProvisionRequestCounter;
51 // Count of provideProvisionResponse calls.
52 CounterMetric<status_t> mProvideProvisionResponseCounter;
Adam Stonef0e618d2018-01-17 19:20:41 -080053
Adam Stonecea91ce2018-01-22 19:23:28 -080054 // Count of key status events broken out by status type.
Robert Shiha5033262019-05-06 14:15:12 -070055 CounterMetric<::android::hardware::drm::V1_2::KeyStatusType>
Adam Stonecea91ce2018-01-22 19:23:28 -080056 mKeyStatusChangeCounter;
57 // Count of events broken out by event type
58 CounterMetric<::android::hardware::drm::V1_0::EventType> mEventCounter;
59
60 // Count getPropertyByteArray calls to retrieve the device unique id.
61 CounterMetric<status_t> mGetDeviceUniqueIdCounter;
62
Adam Stone568b3c42018-01-31 12:57:16 -080063 // Adds a session start time record.
64 void SetSessionStart(const Vector<uint8_t>& sessionId);
Adam Stonef0e618d2018-01-17 19:20:41 -080065
Adam Stone568b3c42018-01-31 12:57:16 -080066 // Adds a session end time record.
67 void SetSessionEnd(const Vector<uint8_t>& sessionId);
68
Adam Stonefb679e32018-02-07 10:25:48 -080069 // The app package name is the application package name that is using the
70 // instance. The app package name is held here for convenience. It is not
71 // serialized or exported with the metrics.
72 void SetAppPackageName(const String8& appPackageName) { mAppPackageName = appPackageName; }
73 const String8& GetAppPackageName() { return mAppPackageName; }
74
Robert Shih82ea6be2019-11-07 17:47:23 -080075 void SetAppUid(uid_t appUid) { mAppUid = appUid; }
76 uid_t GetAppUid() const { return mAppUid; }
77
Adam Stone568b3c42018-01-31 12:57:16 -080078 // Export the metrics to a PersistableBundle.
Adam Stone637b7852018-01-30 12:09:36 -080079 void Export(os::PersistableBundle* metricsBundle);
Adam Stone568b3c42018-01-31 12:57:16 -080080
Adam Stonefb679e32018-02-07 10:25:48 -080081 // Get the serialized metrics. Metrics are formatted as a serialized
82 // DrmFrameworkMetrics proto. If there is a failure serializing the metrics,
83 // this returns an error. The parameter |serlializedMetrics| is owned by the
84 // caller and must not be null.
85 status_t GetSerializedMetrics(std::string* serializedMetrics);
86
Adam Stone28f27c32018-02-05 15:07:48 -080087 // Converts the DRM plugin metrics to a PersistableBundle. All of the metrics
88 // found in |pluginMetrics| are added to the |metricsBundle| parameter.
89 // |pluginBundle| is owned by the caller and must not be null.
90 //
91 // Each item in the pluginMetrics vector is added as a new PersistableBundle. E.g.
92 // DrmMetricGroup {
93 // metrics[0] {
94 // name: "buf_copy"
95 // attributes[0] {
96 // name: "size"
97 // type: INT64_TYPE
98 // int64Value: 1024
99 // }
100 // values[0] {
101 // componentName: "operation_count"
102 // type: INT64_TYPE
103 // int64Value: 75
104 // }
105 // values[1] {
106 // component_name: "average_time_seconds"
107 // type: DOUBLE_TYPE
108 // doubleValue: 0.00000042
109 // }
110 // }
111 // }
112 //
113 // becomes
114 //
115 // metricsBundle {
116 // "0": (PersistableBundle) {
117 // "attributes" : (PersistableBundle) {
118 // "size" : (int64) 1024
119 // }
120 // "operation_count" : (int64) 75
121 // "average_time_seconds" : (double) 0.00000042
122 // }
123 //
124 static status_t HidlMetricsToBundle(
125 const hardware::hidl_vec<hardware::drm::V1_1::DrmMetricGroup>& pluginMetrics,
126 os::PersistableBundle* metricsBundle);
127
Adam Stonefb679e32018-02-07 10:25:48 -0800128 protected:
129 // This is visible for testing only.
130 virtual int64_t GetCurrentTimeMs();
131
Adam Stone568b3c42018-01-31 12:57:16 -0800132 private:
133 // Session lifetimes. A pair of values representing the milliseconds since
134 // epoch, UTC. The first value is the start time, the second is the end time.
Adam Stonefb679e32018-02-07 10:25:48 -0800135 std::map<std::string, std::pair<int64_t, int64_t>> mSessionLifespans;
136
137 String8 mAppPackageName;
Robert Shih82ea6be2019-11-07 17:47:23 -0800138 uid_t mAppUid{~0u};
Adam Stonef0e618d2018-01-17 19:20:41 -0800139};
140
141} // namespace android
142
143#endif // DRM_METRICS_H_