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> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 32 | #include <media/nblog/NBLog.h> |
| 33 | #include <media/nblog/PerformanceAnalysis.h> |
| 34 | #include <media/nblog/ReportPerformance.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> |
| 36 | #include <utils/String8.h> |
| 37 | |
| 38 | namespace android { |
| 39 | |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 40 | std::unique_ptr<Json::Value> dumpToJson(const PerformanceData& data) |
| 41 | { |
| 42 | std::unique_ptr<Json::Value> rootPtr = std::make_unique<Json::Value>(Json::objectValue); |
| 43 | Json::Value& root = *rootPtr; |
| 44 | root["type"] = data.type; |
| 45 | root["frameCount"] = (Json::Value::Int)data.frameCount; |
| 46 | root["sampleRate"] = (Json::Value::Int)data.sampleRate; |
| 47 | root["workMsHist"] = data.workHist.toString(); |
| 48 | root["latencyMsHist"] = data.latencyHist.toString(); |
| 49 | root["warmupMsHist"] = data.warmupHist.toString(); |
| 50 | root["underruns"] = (Json::Value::Int64)data.underruns; |
| 51 | root["overruns"] = (Json::Value::Int64)data.overruns; |
| 52 | root["activeMs"] = (Json::Value::Int64)ns2ms(data.active); |
| 53 | root["durationMs"] = (Json::Value::Int64)ns2ms(systemTime() - data.start); |
| 54 | return rootPtr; |
| 55 | } |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 56 | |
Eric Tan | fefe316 | 2018-09-07 10:09:11 -0700 | [diff] [blame^] | 57 | //------------------------------------------------------------------------------ |
| 58 | |
| 59 | namespace ReportPerformance { |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 60 | |
| 61 | // TODO: use a function like this to extract logic from writeToFile |
| 62 | // https://stackoverflow.com/a/9279620 |
| 63 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 64 | // Writes outlier intervals, timestamps, and histograms spanning long time intervals to file. |
| 65 | // TODO: write data in binary format |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 66 | void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists, |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 67 | const std::deque<std::pair<msInterval, timestamp>> &outlierData, |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 68 | const std::deque<timestamp> &peakTimestamps, |
| 69 | 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] | 70 | |
| 71 | // TODO: remove old files, implement rotating files as in AudioFlinger.cpp |
| 72 | |
| 73 | if (outlierData.empty() && hists.empty() && peakTimestamps.empty()) { |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 74 | ALOGW("No data, returning."); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 78 | std::stringstream outlierName; |
| 79 | std::stringstream histogramName; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 80 | std::stringstream peakName; |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 81 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 82 | // get current time |
| 83 | char currTime[16]; //YYYYMMDDHHMMSS + '\0' + one unused |
| 84 | struct timeval tv; |
| 85 | gettimeofday(&tv, NULL); |
| 86 | struct tm tm; |
| 87 | localtime_r(&tv.tv_sec, &tm); |
| 88 | strftime(currTime, sizeof(currTime), "%Y%m%d%H%M%S", &tm); |
| 89 | |
| 90 | // generate file names |
| 91 | std::stringstream common; |
| 92 | common << author << "_" << hash << "_" << currTime << ".csv"; |
| 93 | |
| 94 | histogramName << directory << "histograms_" << common.str(); |
| 95 | outlierName << directory << "outliers_" << common.str(); |
| 96 | peakName << directory << "peaks_" << common.str(); |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 97 | |
| 98 | std::ofstream hfs; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 99 | 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] | 100 | if (!hfs.is_open()) { |
| 101 | ALOGW("couldn't open file %s", histogramName.str().c_str()); |
| 102 | return; |
| 103 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 104 | // each histogram is written as a line where the first value is the timestamp and |
| 105 | // subsequent values are pairs of buckets and counts. Each value is separated |
| 106 | // 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] | 107 | for (auto hist = hists.begin(); hist != hists.end(); ++hist) { |
| 108 | hfs << hist->first << ", "; |
| 109 | for (auto bucket = hist->second.begin(); bucket != hist->second.end(); ++bucket) { |
| 110 | hfs << bucket->first / static_cast<double>(kJiffyPerMs) |
| 111 | << ", " << bucket->second; |
| 112 | if (std::next(bucket) != end(hist->second)) { |
| 113 | hfs << ", "; |
| 114 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 115 | } |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 116 | if (std::next(hist) != end(hists)) { |
| 117 | hfs << "\n"; |
| 118 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 119 | } |
| 120 | hfs.close(); |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 121 | |
| 122 | std::ofstream ofs; |
| 123 | ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc); |
| 124 | if (!ofs.is_open()) { |
| 125 | ALOGW("couldn't open file %s", outlierName.str().c_str()); |
| 126 | return; |
| 127 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 128 | // outliers are written as pairs separated by newlines, where each |
| 129 | // pair's values are separated by a comma |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 130 | for (const auto &outlier : outlierData) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 131 | ofs << outlier.first << ", " << outlier.second << "\n"; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 132 | } |
| 133 | ofs.close(); |
| 134 | |
| 135 | std::ofstream pfs; |
| 136 | pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc); |
| 137 | if (!pfs.is_open()) { |
| 138 | ALOGW("couldn't open file %s", peakName.str().c_str()); |
| 139 | return; |
| 140 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 141 | // peaks are simply timestamps separated by commas |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 142 | for (auto peak = peakTimestamps.begin(); peak != peakTimestamps.end(); ++peak) { |
| 143 | pfs << *peak; |
| 144 | if (std::next(peak) != end(peakTimestamps)) { |
| 145 | pfs << ", "; |
| 146 | } |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 147 | } |
| 148 | pfs.close(); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace ReportPerformance |
| 152 | |
| 153 | } // namespace android |