blob: efc1b84d21343e61cc490fea012be660edda9454 [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>
21#include <queue>
22#include <stdarg.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <string.h>
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070026#include <sstream>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070027#include <sys/prctl.h>
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070028#include <sys/time.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070029#include <utility>
30#include <media/nbaio/NBLog.h>
31#include <media/nbaio/PerformanceAnalysis.h>
32#include <media/nbaio/ReportPerformance.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070033#include <utils/Log.h>
34#include <utils/String8.h>
35
36namespace android {
37
38namespace ReportPerformance {
39
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -070040
41// TODO: use a function like this to extract logic from writeToFile
42// https://stackoverflow.com/a/9279620
43
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070044// Writes outlier intervals, timestamps, and histograms spanning long time intervals to file.
45// TODO: write data in binary format
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070046void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070047 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070048 const std::deque<timestamp> &peakTimestamps,
49 const char * directory, bool append, int author, log_hash_t hash) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070050
51 // TODO: remove old files, implement rotating files as in AudioFlinger.cpp
52
53 if (outlierData.empty() && hists.empty() && peakTimestamps.empty()) {
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070054 ALOGW("No data, returning.");
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070055 return;
56 }
57
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070058 std::stringstream outlierName;
59 std::stringstream histogramName;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070060 std::stringstream peakName;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070061
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070062 // get current time
63 char currTime[16]; //YYYYMMDDHHMMSS + '\0' + one unused
64 struct timeval tv;
65 gettimeofday(&tv, NULL);
66 struct tm tm;
67 localtime_r(&tv.tv_sec, &tm);
68 strftime(currTime, sizeof(currTime), "%Y%m%d%H%M%S", &tm);
69
70 // generate file names
71 std::stringstream common;
72 common << author << "_" << hash << "_" << currTime << ".csv";
73
74 histogramName << directory << "histograms_" << common.str();
75 outlierName << directory << "outliers_" << common.str();
76 peakName << directory << "peaks_" << common.str();
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070077
78 std::ofstream hfs;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070079 hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070080 if (!hfs.is_open()) {
81 ALOGW("couldn't open file %s", histogramName.str().c_str());
82 return;
83 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070084 // each histogram is written as a line where the first value is the timestamp and
85 // subsequent values are pairs of buckets and counts. Each value is separated
86 // by a comma, and each histogram is separated by a newline.
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -070087 for (auto hist = hists.begin(); hist != hists.end(); ++hist) {
88 hfs << hist->first << ", ";
89 for (auto bucket = hist->second.begin(); bucket != hist->second.end(); ++bucket) {
90 hfs << bucket->first / static_cast<double>(kJiffyPerMs)
91 << ", " << bucket->second;
92 if (std::next(bucket) != end(hist->second)) {
93 hfs << ", ";
94 }
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070095 }
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -070096 if (std::next(hist) != end(hists)) {
97 hfs << "\n";
98 }
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070099 }
100 hfs.close();
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700101
102 std::ofstream ofs;
103 ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
104 if (!ofs.is_open()) {
105 ALOGW("couldn't open file %s", outlierName.str().c_str());
106 return;
107 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700108 // outliers are written as pairs separated by newlines, where each
109 // pair's values are separated by a comma
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700110 for (const auto &outlier : outlierData) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700111 ofs << outlier.first << ", " << outlier.second << "\n";
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700112 }
113 ofs.close();
114
115 std::ofstream pfs;
116 pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
117 if (!pfs.is_open()) {
118 ALOGW("couldn't open file %s", peakName.str().c_str());
119 return;
120 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700121 // peaks are simply timestamps separated by commas
Sanna Catherine de Treville Wager847b6e62017-08-03 11:35:51 -0700122 for (auto peak = peakTimestamps.begin(); peak != peakTimestamps.end(); ++peak) {
123 pfs << *peak;
124 if (std::next(peak) != end(peakTimestamps)) {
125 pfs << ", ";
126 }
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700127 }
128 pfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700129}
130
131} // namespace ReportPerformance
132
133} // namespace android