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 #ifndef ANDROID_MEDIA_REPORTPERFORMANCE_H
18 #define ANDROID_MEDIA_REPORTPERFORMANCE_H
19 
20 #include <deque>
21 #include <map>
22 #include <vector>
23 
24 namespace android {
25 namespace ReportPerformance {
26 
27 struct PerformanceData;
28 
29 // Dumps performance data in a JSON format.
30 void dumpJson(int fd, const std::map<int, PerformanceData>& threadDataMap);
31 
32 //Dumps performance data as visualized plots.
33 void dumpPlots(int fd, const std::map<int, PerformanceData>& threadDataMap);
34 
35 // Dumps snapshots at important events in the past.
36 void dumpRetro(int fd, const std::map<int, PerformanceData>& threadDataMap);
37 
38 // Send one thread's data to media metrics, if the performance data is nontrivial (i.e. not
39 // all zero values). Return true if data was sent, false if there is nothing to write
40 // or an error occurred while writing.
41 bool sendToMediaMetrics(const PerformanceData& data);
42 
43 //------------------------------------------------------------------------------
44 
45 constexpr int kMsPerSec = 1000;
46 constexpr int kSecPerMin = 60;
47 
48 constexpr int kJiffyPerMs = 10; // time unit for histogram as a multiple of milliseconds
49 
50 // stores a histogram: key: observed buffer period (multiple of jiffy). value: count
51 using Hist = std::map<int, int>;
52 
53 using msInterval = double;
54 using jiffyInterval = double;
55 
56 using timestamp = int64_t;
57 
58 using log_hash_t = uint64_t;
59 
deltaMs(int64_t ns1,int64_t ns2)60 static inline int deltaMs(int64_t ns1, int64_t ns2) {
61     return (ns2 - ns1) / (1000 * 1000);
62 }
63 
deltaJiffy(int64_t ns1,int64_t ns2)64 static inline int deltaJiffy(int64_t ns1, int64_t ns2) {
65     return (kJiffyPerMs * (ns2 - ns1)) / (1000 * 1000);
66 }
67 
log2(uint32_t x)68 static inline uint32_t log2(uint32_t x) {
69     // This works for x > 0
70     return 31 - __builtin_clz(x);
71 }
72 
73 // Writes outlier intervals, timestamps, peaks timestamps, and histograms to a file.
74 void writeToFile(const std::deque<std::pair<timestamp, Hist>> &hists,
75                  const std::deque<std::pair<msInterval, timestamp>> &outlierData,
76                  const std::deque<timestamp> &peakTimestamps,
77                  const char * kDirectory, bool append, int author, log_hash_t hash);
78 
79 }   // namespace ReportPerformance
80 }   // namespace android
81 
82 #endif  // ANDROID_MEDIA_REPORTPERFORMANCE_H
83