Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Eigen Listener for thread pool schedule and execute events #64770

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tensorflow/core/profiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ cc_library(
"//tensorflow/core/profiler/lib:profiler_factory_impl",
"//tensorflow/core/profiler/lib:profiler_session_impl",
"@local_tsl//tsl/profiler/backends/cpu:annotation_stack_impl",
"@local_tsl//tsl/profiler/backends/cpu:threadpool_listener",
"@local_tsl//tsl/profiler/backends/cpu:traceme_recorder_impl",
"@local_tsl//tsl/profiler/utils:time_utils_impl",
],
Expand Down
36 changes: 36 additions & 0 deletions third_party/xla/third_party/tsl/tsl/profiler/backends/cpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,39 @@ cc_library(
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "threadpool_listener",
srcs = ["threadpool_listener.cc"],
hdrs = ["threadpool_listener.h"],
visibility = internal_visibility([
"//tensorflow/python:__pkg__",
"//tsl/platform/cloud:__pkg__",
"//tsl/profiler:__pkg__",
"//tsl/profiler:internal",
"//tsl/profiler:xla_internal",
]),
deps = [
":threadpool_listener_state",
":traceme_recorder",
"//tsl/platform:logging",
"//tsl/platform:tracing",
"//tsl/platform:types",
"//tsl/profiler/lib:context_types_hdrs",
"//tsl/profiler/lib:profiler_interface",
"//tsl/profiler/lib:traceme_encode",
"//tsl/profiler/utils:time_utils",
"//tsl/profiler/utils:xplane_schema",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
],
)

cc_library(
name = "threadpool_listener_state",
srcs = ["threadpool_listener_state.cc"],
hdrs = ["threadpool_listener_state.h"],
visibility = internal_visibility([
"//tsl/platform:__subpackages__",
]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.

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

http://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 "tsl/profiler/backends/cpu/threadpool_listener.h"

#include <cstdint>
#include <memory>

#include "absl/log/log.h"
#include "absl/status/status.h"
#include "tsl/platform/logging.h"
#include "tsl/platform/tracing.h"
#include "tsl/platform/types.h"
#include "tsl/profiler/backends/cpu/threadpool_listener_state.h"
#include "tsl/profiler/backends/cpu/traceme_recorder.h"
#include "tsl/profiler/lib/context_types.h"
#include "tsl/profiler/lib/traceme_encode.h"
#include "tsl/profiler/utils/time_utils.h"
#include "tsl/profiler/utils/xplane_schema.h"

namespace tsl {
namespace profiler {
namespace {

void RegisterThreadpoolEventCollector(ThreadpoolEventCollector* collector) {
tracing::SetEventCollector(tracing::EventCategory::kScheduleClosure,
collector);
tracing::SetEventCollector(tracing::EventCategory::kRunClosure, collector);
}

void UnregisterThreadpoolEventCollector() {
tracing::SetEventCollector(tracing::EventCategory::kScheduleClosure, nullptr);
tracing::SetEventCollector(tracing::EventCategory::kRunClosure, nullptr);
}

} // namespace

void ThreadpoolEventCollector::RecordEvent(uint64 arg) const {
int64_t now = GetCurrentTimeNanos();
TraceMeRecorder::Record(
{TraceMeEncode(kThreadpoolListenerRecord,
{{"_pt", ContextType::kThreadpoolEvent}, {"_p", arg}}),
now, now});
}
void ThreadpoolEventCollector::StartRegion(uint64 arg) const {
int64_t now = GetCurrentTimeNanos();
TraceMeRecorder::Record(
{TraceMeEncode(kThreadpoolListenerStartRegion,
{{"_ct", ContextType::kThreadpoolEvent}, {"_c", arg}}),
now, now});
}
void ThreadpoolEventCollector::StopRegion() const {
int64_t now = GetCurrentTimeNanos();
TraceMeRecorder::Record(
{TraceMeEncode(kThreadpoolListenerStopRegion, {}), now, now});
}

absl::Status ThreadpoolProfilerInterface::Start() {
if (tracing::EventCollector::IsEnabled()) {
LOG(WARNING) << "[ThreadpoolEventCollector] EventCollector is enabled, Not "
"collecting events from ThreadPool.";
status_ = absl::FailedPreconditionError(
"ThreadpoolEventCollector is enabled, Not collecting events from "
"ThreadPool.");
return absl::OkStatus();
}
event_collector_ = std::make_unique<ThreadpoolEventCollector>();
RegisterThreadpoolEventCollector(event_collector_.get());
threadpool_listener::Activate();
return absl::OkStatus();
}

absl::Status ThreadpoolProfilerInterface::Stop() {
threadpool_listener::Deactivate();
UnregisterThreadpoolEventCollector();
return absl::OkStatus();
}

absl::Status ThreadpoolProfilerInterface::CollectData(
tensorflow::profiler::XSpace* space) {
if (!status_.ok()) {
*space->add_errors() = status_.ToString();
}
return absl::OkStatus();
}

} // namespace profiler
} // namespace tsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.

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

http://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 TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_H_
#define TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_H_

#include "absl/status/status.h"
#include "tsl/platform/tracing.h"
#include "tsl/platform/types.h"
#include "tsl/profiler/backends/cpu/threadpool_listener_state.h"
#include "tsl/profiler/lib/profiler_interface.h"
namespace tsl {
namespace profiler {

class ThreadpoolEventCollector : public tsl::tracing::EventCollector {
public:
explicit ThreadpoolEventCollector() = default;

void RecordEvent(uint64 arg) const override;
void StartRegion(uint64 arg) const override;
void StopRegion() const override;

// Annotates the current thread with a name.
void SetCurrentThreadName(const char* name) {}
// Returns whether event collection is enabled.
static bool IsEnabled() { return threadpool_listener::IsEnabled(); }
};

class ThreadpoolProfilerInterface : public ProfilerInterface {
public:
explicit ThreadpoolProfilerInterface() = default;

absl::Status Start() override;
absl::Status Stop() override;

absl::Status CollectData(tensorflow::profiler::XSpace* space) override;

private:
absl::Status status_;
std::unique_ptr<ThreadpoolEventCollector> event_collector_;
};

} // namespace profiler
} // namespace tsl

#endif // TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.

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

http://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 "tsl/profiler/backends/cpu/threadpool_listener_state.h"

#include <atomic>

namespace tsl {
namespace profiler {
namespace threadpool_listener {
namespace {
std::atomic<bool> enabled = false;
}

bool IsEnabled() { return enabled.load(std::memory_order_acquire); }

void Activate() { enabled.store(true, std::memory_order_release); }

void Deactivate() { enabled.store(false, std::memory_order_release); }

} // namespace threadpool_listener
} // namespace profiler
} // namespace tsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.

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

http://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 TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_STATE_H_
#define TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_STATE_H_

namespace tsl {
namespace profiler {
namespace threadpool_listener {

// Check if the threadpool listener is enabled.
bool IsEnabled();

// Set global state of threadpool listener to enabled.
void Activate();

// Set global state of threadpool listener to disabled.
void Deactivate();

} // namespace threadpool_listener
} // namespace profiler
} // namespace tsl

#endif // TENSORFLOW_TSL_PROFILER_BACKENDS_CPU_THREADPOOL_LISTENER_STATE_H_
1 change: 1 addition & 0 deletions third_party/xla/third_party/tsl/tsl/profiler/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ cc_library(
visibility = internal_visibility([
"@local_xla//xla/backends/profiler/plugin:__pkg__",
"//learning/brain/tfrc/executor/stream_executor:__pkg__",
"@local_xla//xla/backends/profiler/cpu:__pkg__",
]),
deps = [
":profiler_interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const char* GetContextTypeString(ContextType context_type) {
return "pathways_exec";
case ContextType::kPjrtLibraryCall:
return "pjrt_library_call";
case ContextType::kThreadpoolEvent:
return "threadpool_event";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum class ContextType : int {
kTpuLaunch,
kPathwaysExecutor,
kPjrtLibraryCall,
kThreadpoolEvent,
kLastContextType = ContextType::kTpuLaunch,
};

Expand Down
1 change: 1 addition & 0 deletions third_party/xla/third_party/tsl/tsl/profiler/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ cc_library(
"//tsl/profiler/protobuf:xplane_proto_cc",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/log",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void MutateXPlane(XPlane* plane,
std::vector<std::unique_ptr<XplaneEventMutatorFactory>>
CreateMutatorFactories() {
std::vector<std::unique_ptr<XplaneEventMutatorFactory>> mutator_factories;
mutator_factories.push_back(ThreadpoolLineMutatorFactory::CreateFactory());
mutator_factories.push_back(XplaneRootEventMutatorFactory::CreateFactory(
HostEventType::kProcessBatch, 2));
mutator_factories.push_back(XplaneRootEventMutatorFactory::CreateFactory(
Expand Down