Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 17 | #include <android-base/macros.h> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 18 | #include <media/DrmMetrics.h> |
| 19 | |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 20 | using ::android::hardware::drm::V1_0::EventType; |
| 21 | using ::android::hardware::drm::V1_0::KeyStatusType; |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 22 | using ::android::os::PersistableBundle; |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 23 | |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | template<typename T> |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 27 | std::string GetAttributeName(T type); |
| 28 | |
| 29 | template<> |
| 30 | std::string GetAttributeName<KeyStatusType>(KeyStatusType type) { |
| 31 | static const char* type_names[] = { |
| 32 | "USABLE", "EXPIRED", "OUTPUT_NOT_ALLOWED", |
| 33 | "STATUS_PENDING", "INTERNAL_ERROR" }; |
| 34 | if (((size_t) type) > arraysize(type_names)) { |
| 35 | return "UNKNOWN_TYPE"; |
| 36 | } |
| 37 | return type_names[(size_t) type]; |
| 38 | } |
| 39 | |
| 40 | template<> |
| 41 | std::string GetAttributeName<EventType>(EventType type) { |
| 42 | static const char* type_names[] = { |
| 43 | "PROVISION_REQUIRED", "KEY_NEEDED", "KEY_EXPIRED", |
| 44 | "VENDOR_DEFINED", "SESSION_RECLAIMED" }; |
| 45 | if (((size_t) type) > arraysize(type_names)) { |
| 46 | return "UNKNOWN_TYPE"; |
| 47 | } |
| 48 | return type_names[(size_t) type]; |
| 49 | } |
| 50 | |
| 51 | template<typename T> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 52 | void ExportCounterMetric(const android::CounterMetric<T>& counter, |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 53 | PersistableBundle* metrics) { |
| 54 | if (!metrics) { |
| 55 | ALOGE("metrics was unexpectedly null."); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 58 | std::string success_count_name = counter.metric_name() + ".ok.count"; |
| 59 | std::string error_count_name = counter.metric_name() + ".error.count"; |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 60 | counter.ExportValues( |
| 61 | [&] (const android::status_t status, const int64_t value) { |
| 62 | if (status == android::OK) { |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 63 | metrics->putLong(android::String16(success_count_name.c_str()), |
| 64 | value); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 65 | } else { |
| 66 | int64_t total_errors(0); |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 67 | metrics->getLong(android::String16(error_count_name.c_str()), |
| 68 | &total_errors); |
| 69 | metrics->putLong(android::String16(error_count_name.c_str()), |
| 70 | total_errors + value); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 71 | // TODO: Add support for exporting the list of error values. |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 72 | } |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | template<typename T> |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 77 | void ExportCounterMetricWithAttributeNames( |
| 78 | const android::CounterMetric<T>& counter, |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 79 | PersistableBundle* metrics) { |
| 80 | if (!metrics) { |
| 81 | ALOGE("metrics was unexpectedly null."); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | counter.ExportValues( |
| 85 | [&] (const T& attribute, const int64_t value) { |
| 86 | std::string name = counter.metric_name() |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 87 | + "." + GetAttributeName(attribute) + ".count"; |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 88 | metrics->putLong(android::String16(name.c_str()), value); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 89 | }); |
| 90 | } |
| 91 | |
| 92 | template<typename T> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 93 | void ExportEventMetric(const android::EventMetric<T>& event, |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 94 | PersistableBundle* metrics) { |
| 95 | if (!metrics) { |
| 96 | ALOGE("metrics was unexpectedly null."); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 97 | return; |
| 98 | } |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 99 | std::string success_count_name = event.metric_name() + ".ok.count"; |
| 100 | std::string error_count_name = event.metric_name() + ".error.count"; |
| 101 | std::string timing_name = event.metric_name() + ".ok.average_time_micros"; |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 102 | event.ExportValues( |
| 103 | [&] (const android::status_t& status, |
| 104 | const android::EventStatistics& value) { |
| 105 | if (status == android::OK) { |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 106 | metrics->putLong(android::String16(success_count_name.c_str()), |
| 107 | value.count); |
| 108 | metrics->putLong(android::String16(timing_name.c_str()), |
| 109 | value.mean); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 110 | } else { |
| 111 | int64_t total_errors(0); |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 112 | metrics->getLong(android::String16(error_count_name.c_str()), |
| 113 | &total_errors); |
| 114 | metrics->putLong(android::String16(error_count_name.c_str()), |
| 115 | total_errors + value.count); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 116 | // TODO: Add support for exporting the list of error values. |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | } // namespace anonymous |
| 122 | |
| 123 | namespace android { |
| 124 | |
| 125 | MediaDrmMetrics::MediaDrmMetrics() |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 126 | : mOpenSessionCounter("drm.mediadrm.open_session", "status"), |
| 127 | mCloseSessionCounter("drm.mediadrm.close_session", "status"), |
| 128 | mGetKeyRequestTiming("drm.mediadrm.get_key_request", "status"), |
| 129 | mProvideKeyResponseTiming("drm.mediadrm.provide_key_response", "status"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 130 | mGetProvisionRequestCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 131 | "drm.mediadrm.get_provision_request", "status"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 132 | mProvideProvisionResponseCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 133 | "drm.mediadrm.provide_provision_response", "status"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 134 | mKeyStatusChangeCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 135 | "drm.mediadrm.key_status_change", "key_status_type"), |
| 136 | mEventCounter("drm.mediadrm.event", "event_type"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 137 | mGetDeviceUniqueIdCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame] | 138 | "drm.mediadrm.get_device_unique_id", "status") { |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 141 | void MediaDrmMetrics::Export(PersistableBundle* metrics) { |
| 142 | if (!metrics) { |
| 143 | ALOGE("metrics was unexpectedly null."); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 144 | return; |
| 145 | } |
Adam Stone | 637b785 | 2018-01-30 12:09:36 -0800 | [diff] [blame^] | 146 | ExportCounterMetric(mOpenSessionCounter, metrics); |
| 147 | ExportCounterMetric(mCloseSessionCounter, metrics); |
| 148 | ExportEventMetric(mGetKeyRequestTiming, metrics); |
| 149 | ExportEventMetric(mProvideKeyResponseTiming, metrics); |
| 150 | ExportCounterMetric(mGetProvisionRequestCounter, metrics); |
| 151 | ExportCounterMetric(mProvideProvisionResponseCounter, metrics); |
| 152 | ExportCounterMetricWithAttributeNames(mKeyStatusChangeCounter, metrics); |
| 153 | ExportCounterMetricWithAttributeNames(mEventCounter, metrics); |
| 154 | ExportCounterMetric(mGetDeviceUniqueIdCounter, metrics); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace android |