Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "statsd_audiothread" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <pthread.h> |
| 24 | #include <pwd.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <statslog.h> |
| 33 | |
Ray Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 34 | #include "MediaMetricsService.h" |
shubang | d911b64 | 2021-05-06 09:26:16 -0700 | [diff] [blame] | 35 | #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 36 | #include "iface_statsd.h" |
| 37 | |
| 38 | namespace android { |
| 39 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 40 | bool statsd_audiothread(const std::shared_ptr<const mediametrics::Item>& item, |
| 41 | const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 42 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 43 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 44 | |
| 45 | // these go into the statsd wrapper |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 46 | const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp()); |
| 47 | const std::string package_name = item->getPkgName(); |
| 48 | const int64_t package_version_code = item->getPkgVersionCode(); |
| 49 | const int64_t media_apex_version = 0; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 50 | |
| 51 | // the rest into our own proto |
| 52 | // |
shubang | d911b64 | 2021-05-06 09:26:16 -0700 | [diff] [blame] | 53 | ::android::stats::mediametrics_message::AudioThreadData metrics_proto; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 54 | |
| 55 | #define MM_PREFIX "android.media.audiothread." |
| 56 | |
| 57 | // flesh out the protobuf we'll hand off with our data |
| 58 | // |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 59 | std::string mytype; |
| 60 | if (item->getString(MM_PREFIX "type", &mytype)) { |
| 61 | metrics_proto.set_type(std::move(mytype)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 62 | } |
| 63 | int32_t framecount = -1; |
| 64 | if (item->getInt32(MM_PREFIX "framecount", &framecount)) { |
| 65 | metrics_proto.set_framecount(framecount); |
| 66 | } |
| 67 | int32_t samplerate = -1; |
| 68 | if (item->getInt32(MM_PREFIX "samplerate", &samplerate)) { |
| 69 | metrics_proto.set_samplerate(samplerate); |
| 70 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 71 | std::string work_millis_hist; |
| 72 | if (item->getString(MM_PREFIX "workMs.hist", &work_millis_hist)) { |
| 73 | metrics_proto.set_work_millis_hist(work_millis_hist); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 74 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 75 | std::string latency_millis_hist; |
| 76 | if (item->getString(MM_PREFIX "latencyMs.hist", &latency_millis_hist)) { |
| 77 | metrics_proto.set_latency_millis_hist(latency_millis_hist); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 78 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 79 | std::string warmup_millis_hist; |
| 80 | if (item->getString(MM_PREFIX "warmupMs.hist", &warmup_millis_hist)) { |
| 81 | metrics_proto.set_warmup_millis_hist(warmup_millis_hist); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 82 | } |
| 83 | int64_t underruns = -1; |
| 84 | if (item->getInt64(MM_PREFIX "underruns", &underruns)) { |
| 85 | metrics_proto.set_underruns(underruns); |
| 86 | } |
| 87 | int64_t overruns = -1; |
| 88 | if (item->getInt64(MM_PREFIX "overruns", &overruns)) { |
| 89 | metrics_proto.set_overruns(overruns); |
| 90 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 91 | int64_t active_millis = -1; |
| 92 | if (item->getInt64(MM_PREFIX "activeMs", &active_millis)) { |
| 93 | metrics_proto.set_active_millis(active_millis); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 94 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 95 | int64_t duration_millis = -1; |
| 96 | if (item->getInt64(MM_PREFIX "durationMs", &duration_millis)) { |
| 97 | metrics_proto.set_duration_millis(duration_millis); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 100 | int32_t id = -1; |
| 101 | if (item->getInt32(MM_PREFIX "id", &id)) { |
| 102 | metrics_proto.set_id(id); |
| 103 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 104 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 105 | int32_t port_id = -1; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 106 | if (item->getInt32(MM_PREFIX "portId", &port_id)) { |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 107 | metrics_proto.set_port_id(port_id); |
| 108 | } |
| 109 | // item->setCString(MM_PREFIX "type", threadTypeToString(mType)); |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 110 | std::string type; |
| 111 | if (item->getString(MM_PREFIX "type", &type)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 112 | metrics_proto.set_type(type); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 113 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 114 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 115 | int32_t sample_rate = -1; |
| 116 | if (item->getInt32(MM_PREFIX "sampleRate", &sample_rate)) { |
| 117 | metrics_proto.set_sample_rate(sample_rate); |
| 118 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 119 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 120 | int32_t channel_mask = -1; |
| 121 | if (item->getInt32(MM_PREFIX "channelMask", &channel_mask)) { |
| 122 | metrics_proto.set_channel_mask(channel_mask); |
| 123 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 124 | |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 125 | std::string encoding; |
| 126 | if (item->getString(MM_PREFIX "encoding", &encoding)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 127 | metrics_proto.set_encoding(encoding); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 128 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 129 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 130 | int32_t frame_count = -1; |
| 131 | if (item->getInt32(MM_PREFIX "frameCount", &frame_count)) { |
| 132 | metrics_proto.set_frame_count(frame_count); |
| 133 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 134 | |
| 135 | std::string output_device; |
| 136 | if (item->getString(MM_PREFIX "outDevice", &output_device)) { |
| 137 | metrics_proto.set_output_device(output_device); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 140 | std::string input_device; |
| 141 | if (item->getString(MM_PREFIX "inDevice", &input_device)) { |
| 142 | metrics_proto.set_input_device(input_device); |
| 143 | } |
| 144 | |
| 145 | double io_jitter_mean_millis = -1; |
| 146 | if (item->getDouble(MM_PREFIX "ioJitterMs.mean", &io_jitter_mean_millis)) { |
| 147 | metrics_proto.set_io_jitter_mean_millis(io_jitter_mean_millis); |
| 148 | } |
| 149 | |
| 150 | double io_jitter_stddev_millis = -1; |
| 151 | if (item->getDouble(MM_PREFIX "ioJitterMs.std", &io_jitter_stddev_millis)) { |
| 152 | metrics_proto.set_io_jitter_stddev_millis(io_jitter_stddev_millis); |
| 153 | } |
| 154 | |
| 155 | double process_time_mean_millis = -1; |
| 156 | if (item->getDouble(MM_PREFIX "processTimeMs.mean", &process_time_mean_millis)) { |
| 157 | metrics_proto.set_process_time_mean_millis(process_time_mean_millis); |
| 158 | } |
| 159 | |
| 160 | double process_time_stddev_millis = -1; |
| 161 | if (item->getDouble(MM_PREFIX "processTimeMs.std", &process_time_stddev_millis)) { |
| 162 | metrics_proto.set_process_time_stddev_millis(process_time_stddev_millis); |
| 163 | } |
| 164 | |
| 165 | double timestamp_jitter_mean_millis = -1; |
| 166 | if (item->getDouble(MM_PREFIX "timestampJitterMs.mean", ×tamp_jitter_mean_millis)) { |
| 167 | metrics_proto.set_timestamp_jitter_mean_millis(timestamp_jitter_mean_millis); |
| 168 | } |
| 169 | |
| 170 | double timestamp_jitter_stddev_millis = -1; |
| 171 | if (item->getDouble(MM_PREFIX "timestampJitterMs.std", ×tamp_jitter_stddev_millis)) { |
| 172 | metrics_proto.set_timestamp_jitter_stddev_millis(timestamp_jitter_stddev_millis); |
| 173 | } |
| 174 | |
| 175 | double latency_mean_millis = -1; |
| 176 | if (item->getDouble(MM_PREFIX "latencyMs.mean", &latency_mean_millis)) { |
| 177 | metrics_proto.set_latency_mean_millis(latency_mean_millis); |
| 178 | } |
| 179 | |
| 180 | double latency_stddev_millis = -1; |
| 181 | if (item->getDouble(MM_PREFIX "latencyMs.std", &latency_stddev_millis)) { |
| 182 | metrics_proto.set_latency_stddev_millis(latency_stddev_millis); |
| 183 | } |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 184 | |
| 185 | std::string serialized; |
| 186 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 187 | ALOGE("Failed to serialize audiothread metrics"); |
| 188 | return false; |
| 189 | } |
| 190 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 191 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 192 | int result = android::util::stats_write(android::util::MEDIAMETRICS_AUDIOTHREAD_REPORTED, |
| 193 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 194 | media_apex_version, |
| 195 | bf_serialized); |
| 196 | std::stringstream log; |
| 197 | log << "result:" << result << " {" |
| 198 | << " mediametrics_audiothread_reported:" |
| 199 | << android::util::MEDIAMETRICS_AUDIOTHREAD_REPORTED |
| 200 | << " timestamp_nanos:" << timestamp_nanos |
| 201 | << " package_name:" << package_name |
| 202 | << " package_version_code:" << package_version_code |
| 203 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 204 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 205 | << " type:" << type |
| 206 | << " framecount:" << framecount |
| 207 | << " samplerate:" << samplerate |
| 208 | << " work_millis_hist:" << work_millis_hist |
| 209 | << " latency_millis_hist:" << latency_millis_hist |
| 210 | << " warmup_millis_hist:" << warmup_millis_hist |
| 211 | << " underruns:" << underruns |
| 212 | << " overruns:" << overruns |
| 213 | << " active_millis:" << active_millis |
| 214 | << " duration_millis:" << duration_millis |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 215 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 216 | << " id:" << id |
| 217 | << " port_id:" << port_id |
| 218 | << " sample_rate:" << sample_rate |
| 219 | << " channel_mask:" << channel_mask |
| 220 | << " encoding:" << encoding |
| 221 | << " frame_count:" << frame_count |
| 222 | << " output_device:" << output_device |
| 223 | << " input_device:" << input_device |
| 224 | << " io_jitter_mean_millis:" << io_jitter_mean_millis |
| 225 | << " io_jitter_stddev_millis:" << io_jitter_stddev_millis |
| 226 | |
| 227 | << " process_time_mean_millis:" << process_time_mean_millis |
| 228 | << " process_time_stddev_millis:" << process_time_stddev_millis |
| 229 | << " timestamp_jitter_mean_millis:" << timestamp_jitter_mean_millis |
| 230 | << " timestamp_jitter_stddev_millis:" << timestamp_jitter_stddev_millis |
| 231 | << " latency_mean_millis:" << latency_mean_millis |
| 232 | << " latency_stddev_millis:" << latency_stddev_millis |
| 233 | << " }"; |
| 234 | statsdLog->log(android::util::MEDIAMETRICS_AUDIOTHREAD_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 238 | } // namespace android |