Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #pragma once |
| 18 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 19 | #include <utils/Timers.h> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 20 | #include <cinttypes> |
| 21 | #include <numeric> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 22 | #include <unordered_map> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 25 | namespace android::scheduler { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 26 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 27 | // Opaque handle to scheduler connection. |
| 28 | struct ConnectionHandle { |
| 29 | using Id = std::uintptr_t; |
| 30 | static constexpr Id INVALID_ID = static_cast<Id>(-1); |
| 31 | |
| 32 | Id id = INVALID_ID; |
| 33 | |
| 34 | explicit operator bool() const { return id != INVALID_ID; } |
| 35 | }; |
| 36 | |
| 37 | inline bool operator==(ConnectionHandle lhs, ConnectionHandle rhs) { |
| 38 | return lhs.id == rhs.id; |
| 39 | } |
| 40 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 41 | // Calculates the statistical mean (average) in the data structure (array, vector). The |
| 42 | // function does not modify the contents of the array. |
| 43 | template <typename T> |
| 44 | auto calculate_mean(const T& v) { |
| 45 | using V = typename T::value_type; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 46 | V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0)); |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 47 | return sum / static_cast<V>(v.size()); |
| 48 | } |
| 49 | |
| 50 | // Calculates the statistical median in the vector. Return 0 if the vector is empty. The |
| 51 | // function modifies the vector contents. |
| 52 | int64_t calculate_median(std::vector<int64_t>* v); |
| 53 | |
| 54 | // Calculates the statistical mode in the vector. Return 0 if the vector is empty. |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 55 | template <typename T> |
| 56 | auto calculate_mode(const T& v) { |
| 57 | if (v.empty()) { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | // Create a map with all the counts for the indivicual values in the vector. |
| 62 | std::unordered_map<int64_t, int> counts; |
| 63 | for (int64_t value : v) { |
| 64 | counts[value]++; |
| 65 | } |
| 66 | |
| 67 | // Sort the map, and return the number with the highest count. If two numbers have |
| 68 | // the same count, first one is returned. |
| 69 | using ValueType = const decltype(counts)::value_type&; |
| 70 | const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; }; |
| 71 | return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first); |
| 72 | } |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 73 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 74 | template <class T, size_t N> |
| 75 | constexpr size_t arrayLen(T (&)[N]) { |
| 76 | return N; |
| 77 | } |
| 78 | |
| 79 | static constexpr size_t max64print = std::numeric_limits<nsecs_t>::digits10 + 1; |
| 80 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 81 | template <typename T> |
| 82 | static inline T round(float f) { |
| 83 | return static_cast<T>(std::round(f)); |
| 84 | } |
| 85 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 86 | } // namespace android::scheduler |
| 87 | |
| 88 | namespace std { |
| 89 | |
| 90 | template <> |
| 91 | struct hash<android::scheduler::ConnectionHandle> { |
| 92 | size_t operator()(android::scheduler::ConnectionHandle handle) const { |
| 93 | return hash<android::scheduler::ConnectionHandle::Id>()(handle.id); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | } // namespace std |