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
24namespace android {
25
26// The String8 class is used by reportPerformance function
27class String8;
28
29namespace ReportPerformance {
30
31constexpr int kMsPerSec = 1000;
32constexpr int kSecPerMin = 60;
33
34constexpr int kJiffyPerMs = 10; // time unit for histogram as a multiple of milliseconds
35
36// stores a histogram: key: observed buffer period (multiple of jiffy). value: count
37using Histogram = std::map<int, int>;
38
39using msInterval = double;
40using jiffyInterval = double;
41
42using timestamp = int64_t;
43
44using log_hash_t = uint64_t;
45
46static inline int deltaMs(int64_t ns1, int64_t ns2) {
47    return (ns2 - ns1) / (1000 * 1000);
48}
49
50static inline int deltaJiffy(int64_t ns1, int64_t ns2) {
51    return (kJiffyPerMs * (ns2 - ns1)) / (1000 * 1000);
52}
53
54static inline uint32_t log2(uint32_t x) {
55    // This works for x > 0
56    return 31 - __builtin_clz(x);
57}
58
59// Writes outlier intervals, timestamps, peaks timestamps, and histograms to a file.
60void writeToFile(const std::deque<std::pair<timestamp, Histogram>> &hists,
61                 const std::deque<std::pair<msInterval, timestamp>> &outlierData,
62                 const std::deque<timestamp> &peakTimestamps,
63                 const char * kDirectory, bool append, int author, log_hash_t hash);
64
65} // namespace ReportPerformance
66
67}   // namespace android
68
69#endif  // ANDROID_MEDIA_REPORTPERFORMANCE_H
70