| 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 |  | 
| Ady Abraham | 326ecde | 2020-11-06 15:05:53 -0800 | [diff] [blame] | 60 | T get() const { return mData; } | 
|  | 61 |  | 
|  | 62 | operator T() const { return get(); } | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | TracedOrdinal& operator=(T other) { | 
|  | 65 | mData = other; | 
|  | 66 | mHasGoneNegative = mHasGoneNegative || std::signbit(mData); | 
|  | 67 | trace(); | 
|  | 68 | return *this; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | private: | 
|  | 72 | void trace() { | 
| Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 73 | if (CC_LIKELY(!ATRACE_ENABLED())) { | 
|  | 74 | return; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | if (mNameNegative.empty()) { | 
|  | 78 | mNameNegative = base::StringPrintf("%sNegative", mName.c_str()); | 
|  | 79 | } | 
|  | 80 |  | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 81 | if (!std::signbit(mData)) { | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 82 | ATRACE_INT64(mName.c_str(), to_int64(mData)); | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 83 | if (mHasGoneNegative) { | 
|  | 84 | ATRACE_INT64(mNameNegative.c_str(), 0); | 
|  | 85 | } | 
|  | 86 | } else { | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 87 | ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData)); | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 88 | ATRACE_INT64(mName.c_str(), 0); | 
|  | 89 | } | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | const std::string mName; | 
| Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 93 | std::string mNameNegative; | 
| Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 94 | bool mHasGoneNegative; | 
|  | 95 | T mData; | 
|  | 96 | }; | 
| Dan Stoza | 8ea808c | 2020-04-01 13:58:56 -0700 | [diff] [blame] | 97 |  | 
|  | 98 | } // namespace android |