mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
Add weak internal tracing API
PiperOrigin-RevId: 654799253 Change-Id: Ide284386e864a21ab2dfae777912409bc74eebf8
This commit is contained in:
committed by
Copybara-Service
parent
3358286120
commit
58df17f78e
@@ -49,6 +49,8 @@ set(ABSL_INTERNAL_DLL_FILES
|
||||
"base/internal/thread_identity.h"
|
||||
"base/internal/throw_delegate.cc"
|
||||
"base/internal/throw_delegate.h"
|
||||
"base/internal/tracing.cc"
|
||||
"base/internal/tracing.h"
|
||||
"base/internal/tsan_mutex_interface.h"
|
||||
"base/internal/unaligned_access.h"
|
||||
"base/internal/unscaledcycleclock.cc"
|
||||
|
||||
@@ -929,3 +929,42 @@ cc_test(
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "tracing_internal",
|
||||
srcs = ["internal/tracing.cc"],
|
||||
hdrs = ["internal/tracing.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = ["//absl:__subpackages__"],
|
||||
deps = [
|
||||
"//absl/base:config",
|
||||
"//absl/base:core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "tracing_internal_weak_test",
|
||||
srcs = ["internal/tracing_weak_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":tracing_internal",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "tracing_internal_strong_test",
|
||||
srcs = ["internal/tracing_strong_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
":tracing_internal",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -769,3 +769,42 @@ absl_cc_test(
|
||||
absl::poison
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
tracing_internal
|
||||
HDRS
|
||||
"internal/tracing.h"
|
||||
SRCS
|
||||
"internal/tracing.cc"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
tracing_internal_weak_test
|
||||
SRCS
|
||||
"internal/tracing_weak_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::tracing_internal
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
tracing_internal_strong_test
|
||||
SRCS
|
||||
"internal/tracing_strong_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::tracing_internal
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
39
absl/base/internal/tracing.cc
Normal file
39
absl/base/internal/tracing.cc
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright 2024 The Abseil Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "absl/base/internal/tracing.h"
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
extern "C" {
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(
|
||||
const void*, ObjectKind) {}
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(
|
||||
const void*, ObjectKind) {}
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(
|
||||
const void*, ObjectKind) {}
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(
|
||||
const void*, ObjectKind) {}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
81
absl/base/internal/tracing.h
Normal file
81
absl/base/internal/tracing.h
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright 2024 The Abseil Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_TRACING_H_
|
||||
#define ABSL_BASE_INTERNAL_TRACING_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// Well known Abseil object types that have causality.
|
||||
enum class ObjectKind { kUnknown };
|
||||
|
||||
// `TraceWait` and `TraceContinue` record the start and end of a potentially
|
||||
// blocking wait operation on `object`. `object` typically represents a higher
|
||||
// level synchronization object such as `absl::Notification`.
|
||||
void TraceWait(const void* object, ObjectKind kind);
|
||||
void TraceContinue(const void* object, ObjectKind kind);
|
||||
|
||||
// `TraceSignal` records a signal on `object`.
|
||||
void TraceSignal(const void* object, ObjectKind kind);
|
||||
|
||||
// `TraceObserved` records the non-blocking observation of a signaled object.
|
||||
void TraceObserved(const void* object, ObjectKind kind);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Weak implementation detail:
|
||||
//
|
||||
// We define the weak API as extern "C": in some build configurations we pass
|
||||
// `--detect-odr-violations` to the gold linker. This causes it to flag weak
|
||||
// symbol overrides as ODR violations. Because ODR only applies to C++ and not
|
||||
// C, `--detect-odr-violations` ignores symbols not mangled with C++ names.
|
||||
// By changing our extension points to be extern "C", we dodge this check.
|
||||
// ---------------------------------------------------------------------------
|
||||
extern "C" {
|
||||
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(const void* object,
|
||||
ObjectKind kind);
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(const void* object,
|
||||
ObjectKind kind);
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(const void* object,
|
||||
ObjectKind kind);
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(const void* object,
|
||||
ObjectKind kind);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
inline void TraceWait(const void* object, ObjectKind kind) {
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(object, kind);
|
||||
}
|
||||
|
||||
inline void TraceContinue(const void* object, ObjectKind kind) {
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(object, kind);
|
||||
}
|
||||
|
||||
inline void TraceSignal(const void* object, ObjectKind kind) {
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(object, kind);
|
||||
}
|
||||
|
||||
inline void TraceObserved(const void* object, ObjectKind kind) {
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(object, kind);
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_TRACING_H_
|
||||
117
absl/base/internal/tracing_strong_test.cc
Normal file
117
absl/base/internal/tracing_strong_test.cc
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2024 The Abseil Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/tracing.h"
|
||||
|
||||
#if ABSL_HAVE_ATTRIBUTE_WEAK
|
||||
|
||||
namespace {
|
||||
|
||||
using ::testing::ElementsAre;
|
||||
|
||||
using ::absl::base_internal::ObjectKind;
|
||||
|
||||
enum Function { kWait, kContinue, kSignal, kObserved };
|
||||
|
||||
using Record = std::tuple<Function, const void*, ObjectKind>;
|
||||
|
||||
thread_local std::vector<Record>* tls_records = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// Strong extern "C" implementation.
|
||||
extern "C" {
|
||||
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(const void* object,
|
||||
ObjectKind kind) {
|
||||
if (tls_records != nullptr) {
|
||||
tls_records->push_back({kWait, object, kind});
|
||||
}
|
||||
}
|
||||
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(const void* object,
|
||||
ObjectKind kind) {
|
||||
if (tls_records != nullptr) {
|
||||
tls_records->push_back({kContinue, object, kind});
|
||||
}
|
||||
}
|
||||
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceSignal)(const void* object,
|
||||
ObjectKind kind) {
|
||||
if (tls_records != nullptr) {
|
||||
tls_records->push_back({kSignal, object, kind});
|
||||
}
|
||||
}
|
||||
|
||||
void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceObserved)(const void* object,
|
||||
ObjectKind kind) {
|
||||
if (tls_records != nullptr) {
|
||||
tls_records->push_back({kObserved, object, kind});
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(TracingInternal, InvokesStrongFunctionWithNullptr) {
|
||||
std::vector<Record> records;
|
||||
tls_records = &records;
|
||||
auto kind = absl::base_internal::ObjectKind::kUnknown;
|
||||
absl::base_internal::TraceWait(nullptr, kind);
|
||||
absl::base_internal::TraceContinue(nullptr, kind);
|
||||
absl::base_internal::TraceSignal(nullptr, kind);
|
||||
absl::base_internal::TraceObserved(nullptr, kind);
|
||||
tls_records = nullptr;
|
||||
|
||||
EXPECT_THAT(records, ElementsAre(Record{kWait, nullptr, kind},
|
||||
Record{kContinue, nullptr, kind},
|
||||
Record{kSignal, nullptr, kind},
|
||||
Record{kObserved, nullptr, kind}));
|
||||
}
|
||||
|
||||
TEST(TracingInternal, InvokesStrongFunctionWithObjectAddress) {
|
||||
int object = 0;
|
||||
std::vector<Record> records;
|
||||
tls_records = &records;
|
||||
auto kind = absl::base_internal::ObjectKind::kUnknown;
|
||||
absl::base_internal::TraceWait(&object, kind);
|
||||
absl::base_internal::TraceContinue(&object, kind);
|
||||
absl::base_internal::TraceSignal(&object, kind);
|
||||
absl::base_internal::TraceObserved(&object, kind);
|
||||
tls_records = nullptr;
|
||||
|
||||
EXPECT_THAT(records, ElementsAre(Record{kWait, &object, kind},
|
||||
Record{kContinue, &object, kind},
|
||||
Record{kSignal, &object, kind},
|
||||
Record{kObserved, &object, kind}));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ABSL_HAVE_ATTRIBUTE_WEAK
|
||||
34
absl/base/internal/tracing_weak_test.cc
Normal file
34
absl/base/internal/tracing_weak_test.cc
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2024 The Abseil Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/internal/tracing.h"
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(TracingInternal, HasDefaultImplementation) {
|
||||
auto kind = absl::base_internal::ObjectKind::kUnknown;
|
||||
absl::base_internal::TraceWait(nullptr, kind);
|
||||
absl::base_internal::TraceContinue(nullptr, kind);
|
||||
absl::base_internal::TraceSignal(nullptr, kind);
|
||||
absl::base_internal::TraceObserved(nullptr, kind);
|
||||
|
||||
int object = 0;
|
||||
absl::base_internal::TraceWait(&object, kind);
|
||||
absl::base_internal::TraceContinue(&object, kind);
|
||||
absl::base_internal::TraceSignal(&object, kind);
|
||||
absl::base_internal::TraceObserved(&object, kind);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user