blob: 87c223bfece75ccd14ee79e23e457788fb764b08 [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 Wagere4865262017-07-14 16:24:15 -070041void writeToFile(const std::deque<std::pair<outlierInterval, timestamp>> &outlierData,
42 const std::deque<std::pair<timestamp, Histogram>> &hists,
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070043 const char * kDirectory,
44 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;
52
53 outlierName << kDirectory << "outliers_" << author << "_" << hash;
54 histogramName << kDirectory << "histograms_" << author << "_" << hash;
55
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070056 std::ofstream ofs;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070057 ofs.open(outlierName.str().c_str(), append ? std::ios::app : std::ios::trunc);
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070058 if (!ofs.is_open()) {
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070059 ALOGW("couldn't open file %s", outlierName.str().c_str());
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070060 return;
61 }
62 ofs << "Outlier data: interval and timestamp\n";
63 for (const auto &outlier : outlierData) {
64 ofs << outlier.first << ": " << outlier.second << "\n";
65 }
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070066 ofs.close();
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070067
68 std::ofstream hfs;
69 hfs.open(histogramName.str().c_str(), append ? std::ios::app : std::ios::trunc);
70 if (!hfs.is_open()) {
71 ALOGW("couldn't open file %s", histogramName.str().c_str());
72 return;
73 }
74 hfs << "Histogram data\n";
75 for (const auto &hist : hists) {
76 hfs << "\ttimestamp\n";
77 hfs << hist.first << "\n";
78 hfs << "\tbuckets and counts\n";
79 for (const auto &bucket : hist.second) {
80 hfs << bucket.first << ": " << bucket.second << "\n";
81 }
82 hfs << "\n"; // separate histograms with a newline
83 }
84 hfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070085}
86
87} // namespace ReportPerformance
88
89} // namespace android