Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 18 | |
| 19 | #include <chrono> |
| 20 | #include <cmath> |
| 21 | #include <functional> |
| 22 | #include <string> |
| 23 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 24 | #include <common/trace.h> |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 25 | #include <cutils/compiler.h> |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 26 | |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 29 | namespace { |
Ady Abraham | 6645272 | 2023-03-14 17:41:47 -0700 | [diff] [blame] | 30 | template <class Rep, class Period> |
| 31 | bool signbit(std::chrono::duration<Rep, Period> v) { |
| 32 | return std::signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count()); |
| 33 | } |
| 34 | |
| 35 | template <typename Enum, typename std::enable_if<std::is_enum<Enum>::value>::type* = nullptr> |
| 36 | bool signbit(Enum e) { |
| 37 | return std::signbit(static_cast<typename std::underlying_type<Enum>::type>(e)); |
| 38 | } |
| 39 | |
| 40 | template <typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr> |
| 41 | bool signbit(T t) { |
| 42 | return std::signbit(t); |
| 43 | } |
| 44 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 45 | template <typename T> |
| 46 | int64_t to_int64(T v) { |
| 47 | return int64_t(v); |
| 48 | } |
| 49 | |
| 50 | template <class Rep, class Period> |
| 51 | int64_t to_int64(std::chrono::duration<Rep, Period> v) { |
| 52 | return int64_t(v.count()); |
| 53 | } |
| 54 | } // namespace |
| 55 | |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 56 | template <typename T> |
| 57 | class TracedOrdinal { |
| 58 | public: |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 59 | static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) || |
Ady Abraham | 6645272 | 2023-03-14 17:41:47 -0700 | [diff] [blame] | 60 | std::is_same<std::chrono::nanoseconds, T>() || std::is_enum<T>(), |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 61 | "Type is not supported. Please test it with systrace before adding " |
| 62 | "it to the list."); |
| 63 | |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 64 | TracedOrdinal(std::string name, T initialValue) |
Ady Abraham | 6645272 | 2023-03-14 17:41:47 -0700 | [diff] [blame] | 65 | : mName(std::move(name)), mHasGoneNegative(signbit(initialValue)), mData(initialValue) { |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 66 | trace(); |
| 67 | } |
| 68 | |
Ady Abraham | 326ecde | 2020-11-06 15:05:53 -0800 | [diff] [blame] | 69 | T get() const { return mData; } |
| 70 | |
| 71 | operator T() const { return get(); } |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 72 | |
| 73 | TracedOrdinal& operator=(T other) { |
| 74 | mData = other; |
Ady Abraham | 6645272 | 2023-03-14 17:41:47 -0700 | [diff] [blame] | 75 | mHasGoneNegative = mHasGoneNegative || signbit(mData); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 76 | trace(); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | void trace() { |
Vishnu Nair | 2665ca9 | 2024-07-09 22:08:15 +0000 | [diff] [blame] | 82 | if (CC_LIKELY(!SFTRACE_ENABLED())) { |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (mNameNegative.empty()) { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 87 | mNameNegative = mName + "Negative"; |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ady Abraham | 6645272 | 2023-03-14 17:41:47 -0700 | [diff] [blame] | 90 | if (!signbit(mData)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 91 | SFTRACE_INT64(mName.c_str(), to_int64(mData)); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 92 | if (mHasGoneNegative) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 93 | SFTRACE_INT64(mNameNegative.c_str(), 0); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 94 | } |
| 95 | } else { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 96 | SFTRACE_INT64(mNameNegative.c_str(), -to_int64(mData)); |
| 97 | SFTRACE_INT64(mName.c_str(), 0); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | const std::string mName; |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 102 | std::string mNameNegative; |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 103 | bool mHasGoneNegative; |
| 104 | T mData; |
| 105 | }; |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 106 | |
| 107 | } // namespace android |