blob: e64a6d379bd97c4a8ce3fc54a4673f437c71bb44 [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 Wager2a6a9452017-07-28 11:02:01 -070040// Writes outlier intervals, timestamps, and histograms spanning long time intervals to file.
41// TODO: write data in binary format
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070042void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070043 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070044 const std::deque<timestamp> &peakTimestamps,
45 const char * directory, bool append, int author, log_hash_t hash) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070046
47 // TODO: remove old files, implement rotating files as in AudioFlinger.cpp
48
49 if (outlierData.empty() && hists.empty() && peakTimestamps.empty()) {
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070050 ALOGW("No data, returning.");
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -070051 return;
52 }
53
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070054 std::stringstream outlierName;
55 std::stringstream histogramName;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070056 std::stringstream peakName;
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070057
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070058 // get current time
59 char currTime[16]; //YYYYMMDDHHMMSS + '\0' + one unused
60 struct timeval tv;
61 gettimeofday(&tv, NULL);
62 struct tm tm;
63 localtime_r(&tv.tv_sec, &tm);
64 strftime(currTime, sizeof(currTime), "%Y%m%d%H%M%S", &tm);
65
66 // generate file names
67 std::stringstream common;
68 common << author << "_" << hash << "_" << currTime << ".csv";
69
70 histogramName << directory << "histograms_" << common.str();
71 outlierName << directory << "outliers_" << common.str();
72 peakName << directory << "peaks_" << common.str();
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070073
74 std::ofstream hfs;
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070075 hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc);
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070076 if (!hfs.is_open()) {
77 ALOGW("couldn't open file %s", histogramName.str().c_str());
78 return;
79 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070080 // each histogram is written as a line where the first value is the timestamp and
81 // subsequent values are pairs of buckets and counts. Each value is separated
82 // by a comma, and each histogram is separated by a newline.
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070083 for (const auto &hist : hists) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070084 hfs << hist.first << ", ";
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070085 for (const auto &bucket : hist.second) {
Sanna Catherine de Treville Wager6ad40ee2017-07-28 10:10:55 -070086 hfs << bucket.first / static_cast<double>(kJiffyPerMs)
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070087 << ", " << bucket.second << ", ";
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070088 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070089 hfs << "\n";
Sanna Catherine de Treville Wager23f89d32017-07-24 18:24:48 -070090 }
91 hfs.close();
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -070092
93 std::ofstream ofs;
94 ofs.open(outlierName.str(), append ? std::ios::app : std::ios::trunc);
95 if (!ofs.is_open()) {
96 ALOGW("couldn't open file %s", outlierName.str().c_str());
97 return;
98 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -070099 // outliers are written as pairs separated by newlines, where each
100 // pair's values are separated by a comma
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700101 for (const auto &outlier : outlierData) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700102 ofs << outlier.first << ", " << outlier.second << "\n";
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700103 }
104 ofs.close();
105
106 std::ofstream pfs;
107 pfs.open(peakName.str(), append ? std::ios::app : std::ios::trunc);
108 if (!pfs.is_open()) {
109 ALOGW("couldn't open file %s", peakName.str().c_str());
110 return;
111 }
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700112 // peaks are simply timestamps separated by commas
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700113 for (const auto &peak : peakTimestamps) {
Sanna Catherine de Treville Wager2a6a9452017-07-28 11:02:01 -0700114 pfs << peak << ", ";
Sanna Catherine de Treville Wagerf8c34282017-07-25 11:31:18 -0700115 }
116 pfs.close();
Sanna Catherine de Treville Wager80448082017-07-11 14:07:59 -0700117}
118
119} // namespace ReportPerformance
120
121} // namespace android