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 |
| 18 | #include <android-base/stringprintf.h> |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 19 | #include <cutils/compiler.h> |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 20 | #include <utils/Trace.h> |
| 21 | #include <cmath> |
| 22 | #include <string> |
| 23 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 24 | namespace std { |
| 25 | template <class Rep, class Period> |
| 26 | bool signbit(std::chrono::duration<Rep, Period> v) { |
| 27 | return signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count()); |
| 28 | } |
| 29 | } // namespace std |
| 30 | |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 33 | namespace { |
| 34 | template <typename T> |
| 35 | int64_t to_int64(T v) { |
| 36 | return int64_t(v); |
| 37 | } |
| 38 | |
| 39 | template <class Rep, class Period> |
| 40 | int64_t to_int64(std::chrono::duration<Rep, Period> v) { |
| 41 | return int64_t(v.count()); |
| 42 | } |
| 43 | } // namespace |
| 44 | |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 45 | template <typename T> |
| 46 | class TracedOrdinal { |
| 47 | public: |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 48 | static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) || |
| 49 | std::is_same<std::chrono::nanoseconds, T>(), |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 50 | "Type is not supported. Please test it with systrace before adding " |
| 51 | "it to the list."); |
| 52 | |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 53 | TracedOrdinal(std::string name, T initialValue) |
| 54 | : mName(std::move(name)), |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 55 | mHasGoneNegative(std::signbit(initialValue)), |
| 56 | mData(initialValue) { |
| 57 | trace(); |
| 58 | } |
| 59 | |
| 60 | operator T() const { return mData; } |
| 61 | |
| 62 | TracedOrdinal& operator=(T other) { |
| 63 | mData = other; |
| 64 | mHasGoneNegative = mHasGoneNegative || std::signbit(mData); |
| 65 | trace(); |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | void trace() { |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 71 | if (CC_LIKELY(!ATRACE_ENABLED())) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (mNameNegative.empty()) { |
| 76 | mNameNegative = base::StringPrintf("%sNegative", mName.c_str()); |
| 77 | } |
| 78 | |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 79 | if (!std::signbit(mData)) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 80 | ATRACE_INT64(mName.c_str(), to_int64(mData)); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 81 | if (mHasGoneNegative) { |
| 82 | ATRACE_INT64(mNameNegative.c_str(), 0); |
| 83 | } |
| 84 | } else { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame^] | 85 | ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData)); |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 86 | ATRACE_INT64(mName.c_str(), 0); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const std::string mName; |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 91 | std::string mNameNegative; |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 92 | bool mHasGoneNegative; |
| 93 | T mData; |
| 94 | }; |
Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace android |