Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_TAG "ReportPerformance" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <iostream> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 21 | #include <memory> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 22 | #include <queue> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 27 | #include <sstream> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 28 | #include <sys/prctl.h> |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 29 | #include <sys/time.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 30 | #include <utility> |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 31 | #include <json/json.h> |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame^] | 32 | #include <media/MediaAnalyticsItem.h> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 33 | #include <media/nblog/NBLog.h> |
| 34 | #include <media/nblog/PerformanceAnalysis.h> |
| 35 | #include <media/nblog/ReportPerformance.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 36 | #include <utils/Log.h> |
| 37 | #include <utils/String8.h> |
| 38 | |
| 39 | namespace android { |
| 40 | |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame^] | 41 | namespace ReportPerformance { |
| 42 | |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 43 | std::unique_ptr<Json::Value> dumpToJson(const PerformanceData& data) |
| 44 | { |
| 45 | std::unique_ptr<Json::Value> rootPtr = std::make_unique<Json::Value>(Json::objectValue); |
| 46 | Json::Value& root = *rootPtr; |
| 47 | root["type"] = data.type; |
| 48 | root["frameCount"] = (Json::Value::Int)data.frameCount; |
| 49 | root["sampleRate"] = (Json::Value::Int)data.sampleRate; |
| 50 | root["workMsHist"] = data.workHist.toString(); |
| 51 | root["latencyMsHist"] = data.latencyHist.toString(); |
| 52 | root["warmupMsHist"] = data.warmupHist.toString(); |
| 53 | root["underruns"] = (Json::Value::Int64)data.underruns; |
| 54 | root["overruns"] = (Json::Value::Int64)data.overruns; |
| 55 | root["activeMs"] = (Json::Value::Int64)ns2ms(data.active); |
| 56 | root["durationMs"] = (Json::Value::Int64)ns2ms(systemTime() - data.start); |
| 57 | return rootPtr; |
| 58 | } |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 59 | |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame^] | 60 | bool sendToMediaMetrics(const PerformanceData& data) |
| 61 | { |
| 62 | // See documentation for these metrics here: |
| 63 | // docs.google.com/document/d/11--6dyOXVOpacYQLZiaOY5QVtQjUyqNx2zT9cCzLKYE/edit?usp=sharing |
| 64 | static constexpr char kThreadType[] = "android.media.audiothread.type"; |
| 65 | static constexpr char kThreadFrameCount[] = "android.media.audiothread.framecount"; |
| 66 | static constexpr char kThreadSampleRate[] = "android.media.audiothread.samplerate"; |
| 67 | static constexpr char kThreadWorkHist[] = "android.media.audiothread.workMs.hist"; |
| 68 | static constexpr char kThreadLatencyHist[] = "android.media.audiothread.latencyMs.hist"; |
| 69 | static constexpr char kThreadWarmupHist[] = "android.media.audiothread.warmupMs.hist"; |
| 70 | static constexpr char kThreadUnderruns[] = "android.media.audiothread.underruns"; |
| 71 | static constexpr char kThreadOverruns[] = "android.media.audiothread.overruns"; |
| 72 | static constexpr char kThreadActive[] = "android.media.audiothread.activeMs"; |
| 73 | static constexpr char kThreadDuration[] = "android.media.audiothread.durationMs"; |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame] | 74 | |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame^] | 75 | std::unique_ptr<MediaAnalyticsItem> item(new MediaAnalyticsItem("audiothread")); |
| 76 | |
| 77 | const Histogram &workHist = data.workHist; |
| 78 | if (workHist.totalCount() > 0) { |
| 79 | item->setCString(kThreadWorkHist, workHist.toString().c_str()); |
| 80 | } |
| 81 | |
| 82 | const Histogram &latencyHist = data.latencyHist; |
| 83 | if (latencyHist.totalCount() > 0) { |
| 84 | item->setCString(kThreadLatencyHist, latencyHist.toString().c_str()); |
| 85 | } |
| 86 | |
| 87 | const Histogram &warmupHist = data.warmupHist; |
| 88 | if (warmupHist.totalCount() > 0) { |
| 89 | item->setCString(kThreadWarmupHist, warmupHist.toString().c_str()); |
| 90 | } |
| 91 | |
| 92 | if (data.underruns > 0) { |
| 93 | item->setInt64(kThreadUnderruns, data.underruns); |
| 94 | } |
| 95 | |
| 96 | if (data.overruns > 0) { |
| 97 | item->setInt64(kThreadOverruns, data.overruns); |
| 98 | } |
| 99 | |
| 100 | // Send to Media Metrics if the record is not empty. |
| 101 | // The thread and time info are added inside the if statement because |
| 102 | // we want to send them only if there are performance metrics to send. |
| 103 | if (item->count() > 0) { |
| 104 | // Add thread info fields. |
| 105 | const int type = data.type; |
| 106 | // TODO have a int-to-string mapping defined somewhere else for other thread types. |
| 107 | if (type == 2) { |
| 108 | item->setCString(kThreadType, "FASTMIXER"); |
| 109 | } else { |
| 110 | item->setCString(kThreadType, "UNKNOWN"); |
| 111 | } |
| 112 | item->setInt32(kThreadFrameCount, data.frameCount); |
| 113 | item->setInt32(kThreadSampleRate, data.sampleRate); |
| 114 | // Add time info fields. |
| 115 | item->setInt64(kThreadActive, data.active / 1000000); |
| 116 | item->setInt64(kThreadDuration, (systemTime() - data.start) / 1000000); |
| 117 | return item->selfrecord(); |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | //------------------------------------------------------------------------------ |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 123 | |
| 124 | // TODO: use a function like this to extract logic from writeToFile |
| 125 | // https://stackoverflow.com/a/9279620 |
| 126 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 127 | // Writes outlier intervals, timestamps, and histograms spanning long time intervals to file. |
| 128 | // TODO: write data in binary format |
Eric Tan | d6eee71 | 2018-09-07 10:58:19 -0700 | [diff] [blame^] | 129 | void writeToFile(const std::deque<std::pair<timestamp, Hist>> &hists, |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 130 | const std::deque<std::pair<msInterval, timestamp>> &outlierData, |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 131 | const std::deque<timestamp> &peakTimestamps, |
| 132 | const char * directory, bool append, int author, log_hash_t hash) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 133 | |
| 134 | // TODO: remove old files, implement rotating files as in AudioFlinger.cpp |
| 135 | |
| 136 | if (outlierData.empty() && hists.empty() && peakTimestamps.empty()) { |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 137 | ALOGW("No data, returning."); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 141 | std::stringstream outlierName; |
| 142 | std::stringstream histogramName; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 143 | std::stringstream peakName; |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 144 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 145 | // get current time |
| 146 | char currTime[16]; //YYYYMMDDHHMMSS + '\0' + one unused |
| 147 | struct timeval tv; |
| 148 | gettimeofday(&tv, NULL); |
| 149 | struct tm tm; |
| 150 | localtime_r(&tv.tv_sec, &tm); |
| 151 | strftime(currTime, sizeof(currTime), "%Y%m%d%H%M%S", &tm); |
| 152 | |
| 153 | // generate file names |
| 154 | std::stringstream common; |
| 155 | common << author << "_" << hash << "_" << currTime << ".csv"; |
| 156 | |
| 157 | histogramName << directory << "histograms_" << common.str(); |
| 158 | outlierName << directory << "outliers_" << common.str(); |
| 159 | peakName << directory << "peaks_" << common.str(); |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 160 | |
| 161 | std::ofstream hfs; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 162 | hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc); |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 163 | if (!hfs.is_open()) { |
| 164 | ALOGW("couldn't open file %s", histogramName.str().c_str()); |
| 165 | return; |
| 166 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 167 | // each histogram is written as a line where the first value is the timestamp and |
| 168 | // subsequent values are pairs of buckets and counts. Each value is separated |
| 169 | // by a comma, and each histogram is separated by a newline. |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 170 | for (auto hist = hists.begin(); hist != hists.end(); ++hist) { |
| 171 | hfs << hist->first << ", "; |
| 172 | for (auto bucket = hist->second.begin(); bucket != hist->second.end(); ++bucket) { |
| 173 | hfs << bucket->first / static_cast<double>(kJiffyPerMs) |
| 174 | << ", " << bucket->second; |
| 175 | if (std::next(bucket) != end(hist->second)) { |
| 176 | hfs << ", "; |
| 177 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 178 | } |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 179 | if (std::next(hist) != end(hists)) { |
| 180 | hfs << "\n"; |
| 181 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 182 | } |
| 183 | hfs.close(); |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 184 | |
| 185 | std::ofstream ofs; |
| 186 | ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc); |
| 187 | if (!ofs.is_open()) { |
| 188 | ALOGW("couldn't open file %s", outlierName.str().c_str()); |
| 189 | return; |
| 190 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 191 | // outliers are written as pairs separated by newlines, where each |
| 192 | // pair's values are separated by a comma |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 193 | for (const auto &outlier : outlierData) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 194 | ofs << outlier.first << ", " << outlier.second << "\n"; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 195 | } |
| 196 | ofs.close(); |
| 197 | |
| 198 | std::ofstream pfs; |
| 199 | pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc); |
| 200 | if (!pfs.is_open()) { |
| 201 | ALOGW("couldn't open file %s", peakName.str().c_str()); |
| 202 | return; |
| 203 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 204 | // peaks are simply timestamps separated by commas |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 205 | for (auto peak = peakTimestamps.begin(); peak != peakTimestamps.end(); ++peak) { |
| 206 | pfs << *peak; |
| 207 | if (std::next(peak) != end(peakTimestamps)) { |
| 208 | pfs << ", "; |
| 209 | } |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 210 | } |
| 211 | pfs.close(); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace ReportPerformance |
| 215 | |
| 216 | } // namespace android |