blob: 813df94bd29dce76eb0c960973d465f61ca17065 [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>
28#include <utility>
29#include <media/nbaio/NBLog.h>
30#include <media/nbaio/PerformanceAnalysis.h>
31#include <media/nbaio/ReportPerformance.h>
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070032#include <utils/Log.h>
33#include <utils/String8.h>
34
35namespace android {
36
37namespace ReportPerformance {
38
39// Writes outlier intervals, timestamps, and histograms spanning long time intervals to a file.
40// TODO: format the data efficiently and write different types of data to different files
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070041void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070042 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070043 const std::deque<timestamp> &peakTimestamps,
44 const char * directory, bool append, int author, log_hash_t hash) {
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070045 if (outlierData.empty() || hists.empty()) {
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070046 ALOGW("No data, returning.");
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070047 return;
48 }
49
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070050 std::stringstream outlierName;
51 std::stringstream histogramName;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070052 std::stringstream peakName;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070053
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070054 histogramName << directory << "histograms_" << author << "_" << hash;
55 outlierName << directory << "outliers_" << author << "_" << hash;
56 peakName << directory << "peaks_" << author << "_" << hash;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070057
58 std::ofstream hfs;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070059 hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070060 if (!hfs.is_open()) {
61 ALOGW("couldn't open file %s", histogramName.str().c_str());
62 return;
63 }
64 hfs << "Histogram data\n";
65 for (const auto &hist : hists) {
66 hfs << "\ttimestamp\n";
67 hfs << hist.first << "\n";
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070068 hfs << "\tbuckets (in ms) and counts\n";
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070069 for (const auto &bucket : hist.second) {
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070070 hfs << bucket.first / static_cast<double>(kJiffyPerMs)
71 << ": " << bucket.second << "\n";
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070072 }
73 hfs << "\n"; // separate histograms with a newline
74 }
75 hfs.close();
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070076
77 std::ofstream ofs;
78 ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
79 if (!ofs.is_open()) {
80 ALOGW("couldn't open file %s", outlierName.str().c_str());
81 return;
82 }
83 ofs << "Outlier data: interval and timestamp\n";
84 for (const auto &outlier : outlierData) {
85 ofs << outlier.first << ": " << outlier.second << "\n";
86 }
87 ofs.close();
88
89 std::ofstream pfs;
90 pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
91 if (!pfs.is_open()) {
92 ALOGW("couldn't open file %s", peakName.str().c_str());
93 return;
94 }
95 pfs << "Peak data: timestamp\n";
96 for (const auto &peak : peakTimestamps) {
97 pfs << peak << "\n";
98 }
99 pfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700100}
101
102} // namespace ReportPerformance
103
104} // namespace android