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
34 #include "MediaMetricsService.h"
35 #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
36 #include "iface_statsd.h"
37
38 namespace android {
39
statsd_audiothread(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)40 bool statsd_audiothread(const std::shared_ptr<const mediametrics::Item>& item,
41 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
42 {
43 if (item == nullptr) return false;
44
45 // these go into the statsd wrapper
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;
50
51 // the rest into our own proto
52 //
53 ::android::stats::mediametrics_message::AudioThreadData metrics_proto;
54
55 #define MM_PREFIX "android.media.audiothread."
56
57 // flesh out the protobuf we'll hand off with our data
58 //
59 std::string mytype;
60 if (item->getString(MM_PREFIX "type", &mytype)) {
61 metrics_proto.set_type(std::move(mytype));
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 }
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);
74 }
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);
78 }
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);
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 }
91 int64_t active_millis = -1;
92 if (item->getInt64(MM_PREFIX "activeMs", &active_millis)) {
93 metrics_proto.set_active_millis(active_millis);
94 }
95 int64_t duration_millis = -1;
96 if (item->getInt64(MM_PREFIX "durationMs", &duration_millis)) {
97 metrics_proto.set_duration_millis(duration_millis);
98 }
99
100 int32_t id = -1;
101 if (item->getInt32(MM_PREFIX "id", &id)) {
102 metrics_proto.set_id(id);
103 }
104
105 int32_t port_id = -1;
106 if (item->getInt32(MM_PREFIX "portId", &port_id)) {
107 metrics_proto.set_port_id(port_id);
108 }
109 // item->setCString(MM_PREFIX "type", threadTypeToString(mType));
110 std::string type;
111 if (item->getString(MM_PREFIX "type", &type)) {
112 metrics_proto.set_type(type);
113 }
114
115 int32_t sample_rate = -1;
116 if (item->getInt32(MM_PREFIX "sampleRate", &sample_rate)) {
117 metrics_proto.set_sample_rate(sample_rate);
118 }
119
120 int32_t channel_mask = -1;
121 if (item->getInt32(MM_PREFIX "channelMask", &channel_mask)) {
122 metrics_proto.set_channel_mask(channel_mask);
123 }
124
125 std::string encoding;
126 if (item->getString(MM_PREFIX "encoding", &encoding)) {
127 metrics_proto.set_encoding(encoding);
128 }
129
130 int32_t frame_count = -1;
131 if (item->getInt32(MM_PREFIX "frameCount", &frame_count)) {
132 metrics_proto.set_frame_count(frame_count);
133 }
134
135 std::string output_device;
136 if (item->getString(MM_PREFIX "outDevice", &output_device)) {
137 metrics_proto.set_output_device(output_device);
138 }
139
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 }
184
185 std::string serialized;
186 if (!metrics_proto.SerializeToString(&serialized)) {
187 ALOGE("Failed to serialize audiothread metrics");
188 return false;
189 }
190
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
204
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
215
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());
235 return true;
236 }
237
238 } // namespace android
239