Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 26 | #include <sstream> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 28 | #include <sys/time.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 29 | #include <utility> |
| 30 | #include <media/nbaio/NBLog.h> |
| 31 | #include <media/nbaio/PerformanceAnalysis.h> |
| 32 | #include <media/nbaio/ReportPerformance.h> |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> |
| 34 | #include <utils/String8.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | namespace ReportPerformance { |
| 39 | |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 40 | |
| 41 | // TODO: use a function like this to extract logic from writeToFile |
| 42 | // https://stackoverflow.com/a/9279620 |
| 43 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 44 | // Writes outlier intervals, timestamps, and histograms spanning long time intervals to file. |
| 45 | // TODO: write data in binary format |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 46 | void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists, |
Sanna Catherine de Treville Wager | 6ad40ee | 2017-07-28 10:10:55 -0700 | [diff] [blame] | 47 | const std::deque<std::pair<msInterval, timestamp>> &outlierData, |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 48 | const std::deque<timestamp> &peakTimestamps, |
| 49 | const char * directory, bool append, int author, log_hash_t hash) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 50 | |
| 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 Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 54 | ALOGW("No data, returning."); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 58 | std::stringstream outlierName; |
| 59 | std::stringstream histogramName; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 60 | std::stringstream peakName; |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 61 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 62 | // 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 Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 77 | |
| 78 | std::ofstream hfs; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 79 | hfs.open(histogramName.str(), append ? std::ios::app : std::ios::trunc); |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 80 | if (!hfs.is_open()) { |
| 81 | ALOGW("couldn't open file %s", histogramName.str().c_str()); |
| 82 | return; |
| 83 | } |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 84 | // 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 Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 87 | 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 Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 95 | } |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 96 | if (std::next(hist) != end(hists)) { |
| 97 | hfs << "\n"; |
| 98 | } |
Sanna Catherine de Treville Wager | 23f89d3 | 2017-07-24 18:24:48 -0700 | [diff] [blame] | 99 | } |
| 100 | hfs.close(); |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 101 | |
| 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 Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 108 | // outliers are written as pairs separated by newlines, where each |
| 109 | // pair's values are separated by a comma |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 110 | for (const auto &outlier : outlierData) { |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 111 | ofs << outlier.first << ", " << outlier.second << "\n"; |
Sanna Catherine de Treville Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 112 | } |
| 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 Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 121 | // peaks are simply timestamps separated by commas |
Sanna Catherine de Treville Wager | 847b6e6 | 2017-08-03 11:35:51 -0700 | [diff] [blame] | 122 | 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 Wager | f8c3428 | 2017-07-25 11:31:18 -0700 | [diff] [blame] | 127 | } |
| 128 | pfs.close(); |
Sanna Catherine de Treville Wager | 8044808 | 2017-07-11 14:07:59 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace ReportPerformance |
| 132 | |
| 133 | } // namespace android |