-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable profiler tracing support for threadpools
PiperOrigin-RevId: 621967537
- Loading branch information
1 parent
774befd
commit 6e26348
Showing
19 changed files
with
510 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
third_party/tsl/tsl/profiler/backends/cpu/threadpool_listener.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
58 changes: 58 additions & 0 deletions
58
third_party/tsl/tsl/profiler/backends/cpu/threadpool_listener.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
35 changes: 35 additions & 0 deletions
35
third_party/tsl/tsl/profiler/backends/cpu/threadpool_listener_state.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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 |
36 changes: 36 additions & 0 deletions
36
third_party/tsl/tsl/profiler/backends/cpu/threadpool_listener_state.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.