blob: 51f33a6988ba844047d59f4085f9f242f6dd88b9 [file] [log] [blame]
Ady Abraham50204dd2019-07-19 15:47:11 -07001/*
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 Laskowski4e0d20d2021-12-06 11:31:02 -080018
19#include <chrono>
20#include <cmath>
21#include <functional>
22#include <string>
23
Vishnu Nairbe0ad902024-06-27 23:38:43 +000024#include <common/trace.h>
Dan Stoza8ea808c2020-04-01 13:58:56 -070025#include <cutils/compiler.h>
Ady Abraham50204dd2019-07-19 15:47:11 -070026
Dan Stoza8ea808c2020-04-01 13:58:56 -070027namespace android {
28
Ady Abraham9c53ee72020-07-22 21:16:18 -070029namespace {
Ady Abraham66452722023-03-14 17:41:47 -070030template <class Rep, class Period>
31bool signbit(std::chrono::duration<Rep, Period> v) {
32 return std::signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count());
33}
34
35template <typename Enum, typename std::enable_if<std::is_enum<Enum>::value>::type* = nullptr>
36bool signbit(Enum e) {
37 return std::signbit(static_cast<typename std::underlying_type<Enum>::type>(e));
38}
39
40template <typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
41bool signbit(T t) {
42 return std::signbit(t);
43}
44
Ady Abraham9c53ee72020-07-22 21:16:18 -070045template <typename T>
46int64_t to_int64(T v) {
47 return int64_t(v);
48}
49
50template <class Rep, class Period>
51int64_t to_int64(std::chrono::duration<Rep, Period> v) {
52 return int64_t(v.count());
53}
54} // namespace
55
Ady Abraham50204dd2019-07-19 15:47:11 -070056template <typename T>
57class TracedOrdinal {
58public:
Ady Abraham9c53ee72020-07-22 21:16:18 -070059 static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) ||
Ady Abraham66452722023-03-14 17:41:47 -070060 std::is_same<std::chrono::nanoseconds, T>() || std::is_enum<T>(),
Ady Abraham50204dd2019-07-19 15:47:11 -070061 "Type is not supported. Please test it with systrace before adding "
62 "it to the list.");
63
Dan Stoza8ea808c2020-04-01 13:58:56 -070064 TracedOrdinal(std::string name, T initialValue)
Ady Abraham66452722023-03-14 17:41:47 -070065 : mName(std::move(name)), mHasGoneNegative(signbit(initialValue)), mData(initialValue) {
Ady Abraham50204dd2019-07-19 15:47:11 -070066 trace();
67 }
68
Ady Abraham326ecde2020-11-06 15:05:53 -080069 T get() const { return mData; }
70
71 operator T() const { return get(); }
Ady Abraham50204dd2019-07-19 15:47:11 -070072
73 TracedOrdinal& operator=(T other) {
74 mData = other;
Ady Abraham66452722023-03-14 17:41:47 -070075 mHasGoneNegative = mHasGoneNegative || signbit(mData);
Ady Abraham50204dd2019-07-19 15:47:11 -070076 trace();
77 return *this;
78 }
79
80private:
81 void trace() {
Vishnu Nair2665ca92024-07-09 22:08:15 +000082 if (CC_LIKELY(!SFTRACE_ENABLED())) {
Dan Stoza8ea808c2020-04-01 13:58:56 -070083 return;
84 }
85
86 if (mNameNegative.empty()) {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080087 mNameNegative = mName + "Negative";
Dan Stoza8ea808c2020-04-01 13:58:56 -070088 }
89
Ady Abraham66452722023-03-14 17:41:47 -070090 if (!signbit(mData)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000091 SFTRACE_INT64(mName.c_str(), to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070092 if (mHasGoneNegative) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000093 SFTRACE_INT64(mNameNegative.c_str(), 0);
Ady Abraham50204dd2019-07-19 15:47:11 -070094 }
95 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000096 SFTRACE_INT64(mNameNegative.c_str(), -to_int64(mData));
97 SFTRACE_INT64(mName.c_str(), 0);
Ady Abraham50204dd2019-07-19 15:47:11 -070098 }
99 }
100
101 const std::string mName;
Dan Stoza8ea808c2020-04-01 13:58:56 -0700102 std::string mNameNegative;
Ady Abraham50204dd2019-07-19 15:47:11 -0700103 bool mHasGoneNegative;
104 T mData;
105};
Dan Stoza8ea808c2020-04-01 13:58:56 -0700106
107} // namespace android