blob: fa2b9a0778dbf1ca8533ddab62fa366af1f6d1c7 [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,
42 const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
43 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";
68 hfs << "\tbuckets and counts\n";
69 for (const auto &bucket : hist.second) {
70 hfs << bucket.first << ": " << bucket.second << "\n";
71 }
72 hfs << "\n"; // separate histograms with a newline
73 }
74 hfs.close();
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070075
76 std::ofstream ofs;
77 ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
78 if (!ofs.is_open()) {
79 ALOGW("couldn't open file %s", outlierName.str().c_str());
80 return;
81 }
82 ofs << "Outlier data: interval and timestamp\n";
83 for (const auto &outlier : outlierData) {
84 ofs << outlier.first << ": " << outlier.second << "\n";
85 }
86 ofs.close();
87
88 std::ofstream pfs;
89 pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
90 if (!pfs.is_open()) {
91 ALOGW("couldn't open file %s", peakName.str().c_str());
92 return;
93 }
94 pfs << "Peak data: timestamp\n";
95 for (const auto &peak : peakTimestamps) {
96 pfs << peak << "\n";
97 }
98 pfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070099}
100
101} // namespace ReportPerformance
102
103} // namespace android