blob: eee4bec344ae7b975f7f3629e7d1e2c8b08cbb0d [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
18#include <android-base/stringprintf.h>
Dan Stoza8ea808c2020-04-01 13:58:56 -070019#include <cutils/compiler.h>
Ady Abraham50204dd2019-07-19 15:47:11 -070020#include <utils/Trace.h>
21#include <cmath>
22#include <string>
23
Ady Abraham9c53ee72020-07-22 21:16:18 -070024namespace std {
25template <class Rep, class Period>
26bool 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 Stoza8ea808c2020-04-01 13:58:56 -070031namespace android {
32
Ady Abraham9c53ee72020-07-22 21:16:18 -070033namespace {
34template <typename T>
35int64_t to_int64(T v) {
36 return int64_t(v);
37}
38
39template <class Rep, class Period>
40int64_t to_int64(std::chrono::duration<Rep, Period> v) {
41 return int64_t(v.count());
42}
43} // namespace
44
Ady Abraham50204dd2019-07-19 15:47:11 -070045template <typename T>
46class TracedOrdinal {
47public:
Ady Abraham9c53ee72020-07-22 21:16:18 -070048 static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) ||
49 std::is_same<std::chrono::nanoseconds, T>(),
Ady Abraham50204dd2019-07-19 15:47:11 -070050 "Type is not supported. Please test it with systrace before adding "
51 "it to the list.");
52
Dan Stoza8ea808c2020-04-01 13:58:56 -070053 TracedOrdinal(std::string name, T initialValue)
54 : mName(std::move(name)),
Ady Abraham50204dd2019-07-19 15:47:11 -070055 mHasGoneNegative(std::signbit(initialValue)),
56 mData(initialValue) {
57 trace();
58 }
59
Ady Abraham326ecde2020-11-06 15:05:53 -080060 T get() const { return mData; }
61
62 operator T() const { return get(); }
Ady Abraham50204dd2019-07-19 15:47:11 -070063
64 TracedOrdinal& operator=(T other) {
65 mData = other;
66 mHasGoneNegative = mHasGoneNegative || std::signbit(mData);
67 trace();
68 return *this;
69 }
70
71private:
72 void trace() {
Dan Stoza8ea808c2020-04-01 13:58:56 -070073 if (CC_LIKELY(!ATRACE_ENABLED())) {
74 return;
75 }
76
77 if (mNameNegative.empty()) {
78 mNameNegative = base::StringPrintf("%sNegative", mName.c_str());
79 }
80
Ady Abraham50204dd2019-07-19 15:47:11 -070081 if (!std::signbit(mData)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070082 ATRACE_INT64(mName.c_str(), to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070083 if (mHasGoneNegative) {
84 ATRACE_INT64(mNameNegative.c_str(), 0);
85 }
86 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070087 ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070088 ATRACE_INT64(mName.c_str(), 0);
89 }
90 }
91
92 const std::string mName;
Dan Stoza8ea808c2020-04-01 13:58:56 -070093 std::string mNameNegative;
Ady Abraham50204dd2019-07-19 15:47:11 -070094 bool mHasGoneNegative;
95 T mData;
96};
Dan Stoza8ea808c2020-04-01 13:58:56 -070097
98} // namespace android