blob: 98e11cd49b634cb4e2a0dcd6655995950d5e1579 [file] [log] [blame]
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -07001/*
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 Tanfefe3162018-09-07 10:09:11 -070021#include <memory>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070022#include <queue>
23#include <stdarg.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <string.h>
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070027#include <sstream>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070028#include <sys/prctl.h>
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070029#include <sys/time.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070030#include <utility>
Eric Tanfefe3162018-09-07 10:09:11 -070031#include <json/json.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070032#include <media/nblog/NBLog.h>
33#include <media/nblog/PerformanceAnalysis.h>
34#include <media/nblog/ReportPerformance.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070035#include <utils/Log.h>
36#include <utils/String8.h>
37
38namespace android {
39
Eric Tanfefe3162018-09-07 10:09:11 -070040std::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 Wager80448082017-07-11 14:07:59 -070056
Eric Tanfefe3162018-09-07 10:09:11 -070057//------------------------------------------------------------------------------
58
59namespace ReportPerformance {
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -070060
61// TODO: use a function like this to extract logic from writeToFile
62// https://stackoverflow.com/a/9279620
63
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070064// Writes outlier intervals, timestamps, and histograms spanning long time intervals to file.
65// TODO: write data in binary format
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070066void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070067 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070068 const std::deque<timestamp> &peakTimestamps,
69 const char * directory, bool append, int author, log_hash_t hash) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070070
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 Wager23f89d32017-07-24 18:24:48 -070074 ALOGW("No data, returning.");
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070075 return;
76 }
77
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070078 std::stringstream outlierName;
79 std::stringstream histogramName;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070080 std::stringstream peakName;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070081
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070082 // 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 Wager23f89d32017-07-24 18:24:48 -070097
98 std::ofstream hfs;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070099 hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700100 if (!hfs.is_open()) {
101 ALOGW("couldn't open file %s", histogramName.str().c_str());
102 return;
103 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700104 // 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 Wager847b6e62017-08-03 11:35:51 -0700107 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 Wager23f89d32017-07-24 18:24:48 -0700115 }
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700116 if (std::next(hist) != end(hists)) {
117 hfs << "\n";
118 }
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -0700119 }
120 hfs.close();
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700121
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 Wager2a6a9452017-07-28 11:02:01 -0700128 // outliers are written as pairs separated by newlines, where each
129 // pair's values are separated by a comma
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700130 for (const auto &outlier : outlierData) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700131 ofs << outlier.first << ", " << outlier.second << "\n";
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700132 }
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 Wager2a6a9452017-07-28 11:02:01 -0700141 // peaks are simply timestamps separated by commas
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700142 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 Wagerf8c34282017-07-25 11:31:18 -0700147 }
148 pfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700149}
150
151} // namespace ReportPerformance
152
153} // namespace android