Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 3c46c62

Browse files
iivlevstellaraccident
authored andcommitted
Adding an automatic function macro to easily instrument any function
with a one-liner: WTF_AUTO_FUNCTION();
1 parent ae9b793 commit 3c46c62

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

bindings/cpp/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,18 @@ gtest.o: $(GTEST_ALL_CC)
130130
-iquote $(GTEST_DIR) -o $@ -c $+
131131

132132
buffer_test: buffer_test.o gtest.o libwtf.a
133-
$(CXX) -o $@ $+ $(LDLIBS)
133+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
134134

135135
macros_test: macros_test.o gtest.o libwtf.a
136-
$(CXX) -o $@ $+ $(LDLIBS)
136+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
137137

138138
runtime_test: runtime_test.o gtest.o libwtf.a
139-
$(CXX) -o $@ $+ $(LDLIBS)
139+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
140140

141141
### THREADED TORTURE TEST
142142
ifneq "$(THREADING)" "single"
143143
threaded_torture_test: threaded_torture_test.o libwtf.a
144-
$(CXX) -o $@ $+ $(LDLIBS)
144+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)
145145
endif
146146

147147
### INSTALL.

bindings/cpp/include/wtf/macros.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define TRACING_FRAMEWORK_BINDINGS_CPP_INCLUDE_WTF_MACROS_H_
33

44
#include "wtf/runtime.h"
5+
#include <string>
6+
#include <algorithm>
57

68
#define __INTERNAL_WTF_NAMESPACE ::wtf
79

@@ -117,6 +119,28 @@
117119
__WTF_INTERNAL_UNIQUE(__wtf_scope_eventn_)}; \
118120
__WTF_INTERNAL_UNIQUE(__wtf_scopen_).Enter
119121

122+
// Shortcut to trace a function in case you don't care really much about
123+
// the performance. Might add some insignificant overhead.
124+
// It will also replace colon ":" with hash sign "#" in function names
125+
// (see https://github.com/google/tracing-framework/issues/581)
126+
// Allowed Scopes: Within a function.
127+
// Usually you will place this at the very start of a function.
128+
//
129+
// Example:
130+
// WTF_AUTO_FUNCTION();
131+
#define WTF_AUTO_FUNCTION() \
132+
static std::string \
133+
__WTF_INTERNAL_UNIQUE(__wtf_func_name_) {__PRETTY_FUNCTION__}; \
134+
do { \
135+
__INTERNAL_WTF_NAMESPACE::platform::once_flag __WTF_INTERNAL_UNIQUE(__wtf_replaced_flag_); \
136+
__INTERNAL_WTF_NAMESPACE::platform::call_once(__WTF_INTERNAL_UNIQUE(__wtf_replaced_flag_), []() { \
137+
std::replace(__WTF_INTERNAL_UNIQUE(__wtf_func_name_).begin(), \
138+
__WTF_INTERNAL_UNIQUE(__wtf_func_name_).end(), ':', '#'); \
139+
}); \
140+
WTF_AUTO_THREAD_ENABLE(); \
141+
} while (0); \
142+
WTF_SCOPE0(__WTF_INTERNAL_UNIQUE(__wtf_func_name_).c_str())
143+
120144
// Creates a scoped "Task" zone that will be in effect until scope exit.
121145
// This is ideal for thread pools and such which execute many workers where
122146
// you want a specific zone for each type of task the worker is performing

bindings/cpp/include/wtf/platform/platform_aux_pthreads_threaded_inl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ using std::memory_order_acquire;
2929
using std::memory_order_release;
3030
using std::memory_order_acq_rel;
3131
using std::memory_order_seq_cst;
32+
33+
using once_flag = struct {
34+
pthread_once_t flag {PTHREAD_ONCE_INIT};
35+
};
36+
37+
template <class T>
38+
void call_once(once_flag& once, T func) {
39+
pthread_once(&once.flag, func);
40+
}
41+
3242
} // namespace platform
3343

3444
namespace internal {

bindings/cpp/include/wtf/platform/platform_aux_single_threaded_inl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ struct atomic {
4545
// Implicit cast to T.
4646
operator T() { return value; }
4747
};
48+
49+
using once_flag = struct {
50+
bool flag {false};
51+
};
52+
template <class T>
53+
void call_once(once_flag& once, T func) {
54+
if (!once.flag) {
55+
once.flag = true;
56+
func();
57+
}
58+
}
59+
4860
} // namespace platform
4961

5062
namespace internal {

bindings/cpp/include/wtf/platform/platform_aux_std_threaded_inl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ using lock_guard = std::lock_guard<T>;
2020
template <typename T>
2121
using atomic = std::atomic<T>;
2222

23+
using once_flag = std::once_flag;
24+
template <class Callable>
25+
void call_once(once_flag& flag, Callable&& f) {
26+
std::call_once(flag, std::move(f));
27+
}
28+
2329
// Since memory_order is an old-school enum, it needs to be imported
2430
// individually.
2531
using std::memory_order;

bindings/cpp/macros_test.cc

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ TEST_F(MacrosTest, AssertMasterEnabled) {
5050
namespace disabled {
5151
WTF_NAMESPACE_DISABLE();
5252

53+
class AutoFunctionTestClassDisabled {
54+
public:
55+
static void f() {
56+
WTF_AUTO_FUNCTION();
57+
const int32_t kLimit = 10;
58+
for (int32_t i = 0; i < kLimit; i++) {
59+
usleep(10);
60+
}
61+
}
62+
};
63+
64+
TEST_F(MacrosTest, AutoFunctionTestShouldBeDisabled) {
65+
ClearEventBuffer();
66+
const int32_t kLimit = 10;
67+
68+
for (int32_t i = 0; i < kLimit; i++) {
69+
AutoFunctionTestClassDisabled::f();
70+
usleep(1);
71+
}
72+
EXPECT_FALSE(EventsHaveBeenLogged());
73+
74+
ClearEventBuffer();
75+
}
76+
5377
TEST_F(MacrosTest, ThreadShouldBeDisabled) {
5478
WTF_THREAD_ENABLE("ShouldBeDisabled");
5579
// Enabling a thread scribbles into the buffer.
@@ -80,8 +104,36 @@ TEST_F(MacrosTest, EventsShouldBeDisabled) {
80104
}
81105

82106
namespace enabled {
83-
84107
WTF_NAMESPACE_ENABLE();
108+
109+
class AutoFunctionTestClass {
110+
public:
111+
static void f() {
112+
WTF_AUTO_FUNCTION();
113+
const int32_t kLimit = 10;
114+
for (int32_t i = 0; i < kLimit; i++) {
115+
usleep(10);
116+
}
117+
}
118+
};
119+
120+
TEST_F(MacrosTest, AutoFunctionTest) {
121+
Runtime::GetInstance()->DisableCurrentThread();
122+
ClearEventBuffer();
123+
124+
const int32_t kLimit = 10;
125+
126+
for (int32_t i = 0; i < kLimit; i++) {
127+
AutoFunctionTestClass::f();
128+
usleep(1);
129+
}
130+
EXPECT_TRUE(EventsHaveBeenLogged());
131+
132+
EXPECT_TRUE(
133+
Runtime::GetInstance()->SaveToFile(TMP_PREFIX "tmpautofuncbuf.wtf-trace"));
134+
ClearEventBuffer();
135+
}
136+
85137
TEST_F(MacrosTest, ThreadShouldBeEnabled) {
86138
WTF_THREAD_ENABLE("ShouldBeEnabled");
87139
// Enabling a thread scribbles into the buffer.

0 commit comments

Comments
 (0)