diff --git a/tsl/profiler/lib/BUILD b/tsl/profiler/lib/BUILD index bcca03f6f..d8e2a72f5 100644 --- a/tsl/profiler/lib/BUILD +++ b/tsl/profiler/lib/BUILD @@ -213,6 +213,7 @@ cc_library( "//tsl/profiler/protobuf:profiler_options_proto_cc", "//tsl/profiler/protobuf:xplane_proto_cc", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/synchronization", "@xla//xla/tsl/platform:errors", "@xla//xla/tsl/platform:status", @@ -237,15 +238,18 @@ cc_library( "@xla//xla/tsl/profiler:internal", ]), deps = [ + "//tsl/platform", "//tsl/platform:thread_annotations", "//tsl/profiler/protobuf:profiler_options_proto_cc", "//tsl/profiler/protobuf:xplane_proto_cc", "@com_google_absl//absl/memory", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/synchronization", "@xla//xla/tsl/platform:env", "@xla//xla/tsl/platform:errors", "@xla//xla/tsl/platform:logging", + "@xla//xla/tsl/platform:status", "@xla//xla/tsl/platform:types", "@xla//xla/tsl/profiler/utils:xplane_builder", "@xla//xla/tsl/profiler/utils:xplane_schema", @@ -256,9 +260,7 @@ cc_library( ":profiler_factory", ":profiler_interface", ":profiler_lock", - "//tsl/platform", "//tsl/platform:platform_port", - "@xla//xla/tsl/platform:status", "@xla//xla/tsl/profiler/convert:post_process_single_host_xplane", "@xla//xla/tsl/profiler/utils:time_utils", ]), diff --git a/tsl/profiler/lib/profiler_session.cc b/tsl/profiler/lib/profiler_session.cc index 2f5352f47..543235c8f 100644 --- a/tsl/profiler/lib/profiler_session.cc +++ b/tsl/profiler/lib/profiler_session.cc @@ -100,11 +100,31 @@ absl::Status ProfilerSession::CollectData(XSpace* space) { TF_RETURN_IF_ERROR(CollectDataInternal(space)); profiler::SetXSpacePidIfNotSet(*space, tsl::Env::Default()->GetProcessId()); profiler::PostProcessSingleHostXSpace(space, start_time_ns_, stop_time_ns_); -#endif SetProfileOptionsIntoSpace(options_, space); +#endif return absl::OkStatus(); } +#if !defined(IS_MOBILE_PLATFORM) +absl::StatusOr ProfilerSession::Consume() { + absl::MutexLock l(mutex_); + TF_RETURN_IF_ERROR(status_); + if (profilers_ != nullptr) { + return profilers_->Consume(); + } + return absl::UnimplementedError("Consume not implemented"); +} + +absl::Status ProfilerSession::Serialize(std::any data, XSpace* space) { + absl::MutexLock l(mutex_); + TF_RETURN_IF_ERROR(status_); + if (profilers_ != nullptr) { + return profilers_->Serialize(std::move(data), space); + } + return absl::UnimplementedError("Serialize not implemented"); +} +#endif + ProfilerSession::ProfilerSession(const ProfileOptions& options) #if defined(IS_MOBILE_PLATFORM) : status_(errors::Unimplemented( diff --git a/tsl/profiler/lib/profiler_session.h b/tsl/profiler/lib/profiler_session.h index 6644017f5..a5c34ca5e 100644 --- a/tsl/profiler/lib/profiler_session.h +++ b/tsl/profiler/lib/profiler_session.h @@ -15,11 +15,13 @@ limitations under the License. #ifndef TENSORFLOW_TSL_PROFILER_LIB_PROFILER_SESSION_H_ #define TENSORFLOW_TSL_PROFILER_LIB_PROFILER_SESSION_H_ +#include #include #include #include #include "absl/status/status.h" +#include "absl/status/statusor.h" #include "absl/synchronization/mutex.h" #include "xla/tsl/platform/status.h" #include "xla/tsl/platform/types.h" @@ -67,6 +69,15 @@ class ProfilerSession { absl::Status CollectData(tensorflow::profiler::XSpace* space) TF_LOCKS_EXCLUDED(mutex_); +#if !defined(IS_MOBILE_PLATFORM) + // Consumes collected profile data without stopping the profiler. + absl::StatusOr Consume() TF_LOCKS_EXCLUDED(mutex_); + + // Serializes consumed profile data into XSpace. + absl::Status Serialize(std::any data, tensorflow::profiler::XSpace* space) + TF_LOCKS_EXCLUDED(mutex_); +#endif + private: // Constructs an instance of the class and starts profiling explicit ProfilerSession(const tensorflow::ProfileOptions& options);