-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds and integrates GraphRuntimeInfoLogger into CalculatorGraph.
PiperOrigin-RevId: 703446598
- Loading branch information
1 parent
5fbb13d
commit 531d544
Showing
8 changed files
with
233 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "mediapipe/framework/tool/graph_runtime_info_logger.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "absl/functional/any_invocable.h" | ||
#include "absl/log/absl_check.h" | ||
#include "absl/log/absl_log.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/time/time.h" | ||
#include "mediapipe/framework/calculator.pb.h" | ||
#include "mediapipe/framework/port/ret_check.h" | ||
#include "mediapipe/framework/tool/graph_runtime_info_utils.h" | ||
|
||
namespace mediapipe::tool { | ||
|
||
constexpr absl::Duration kDefaultCaptureInterval = absl::Seconds(10); | ||
|
||
GraphRuntimeInfoLogger::GraphRuntimeInfoLogger() | ||
: thread_pool_("GraphRuntimeInfoLogger", 1) {} | ||
|
||
GraphRuntimeInfoLogger::~GraphRuntimeInfoLogger() { Stop(); }; | ||
|
||
absl::Status GraphRuntimeInfoLogger::StartInBackground( | ||
const mediapipe::GraphRuntimeInfoConfig& config, | ||
absl::AnyInvocable<absl::StatusOr<GraphRuntimeInfo>()> | ||
get_runtime_info_fn) { | ||
get_runtime_info_fn_ = std::move(get_runtime_info_fn); | ||
RET_CHECK(!is_running_.HasBeenNotified()); | ||
ABSL_CHECK_EQ(thread_pool_.num_threads(), 1); | ||
thread_pool_.StartWorkers(); | ||
absl::Duration interval = | ||
config.capture_period_msec() > 0 | ||
? absl::Milliseconds(config.capture_period_msec()) | ||
: kDefaultCaptureInterval; | ||
thread_pool_.Schedule([this, interval]() mutable { | ||
is_running_.Notify(); | ||
while (!shutdown_signal_.HasBeenNotified()) { | ||
const auto runtime_info = get_runtime_info_fn_(); | ||
if (!runtime_info.ok()) { | ||
ABSL_LOG(DFATAL) << "Failed to get graph runtime info: " | ||
<< runtime_info.status(); | ||
return; | ||
} | ||
const auto runtime_info_str = GetGraphRuntimeInfoString(*runtime_info); | ||
if (!runtime_info_str.ok()) { | ||
ABSL_LOG(DFATAL) << "Failed to render graph runtime info: " | ||
<< runtime_info_str.status(); | ||
return; | ||
} | ||
ABSL_LOG(INFO) << *runtime_info_str; | ||
shutdown_signal_.WaitForNotificationWithTimeout(interval); | ||
} | ||
}); | ||
is_running_.WaitForNotification(); | ||
return absl::OkStatus(); | ||
} | ||
|
||
void GraphRuntimeInfoLogger::Stop() { shutdown_signal_.Notify(); } | ||
|
||
} // namespace mediapipe::tool |
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,51 @@ | ||
// Copyright 2024 The MediaPipe 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 | ||
// | ||
// 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 MEDIAPIPE_FRAMEWORK_TOOL_GRAPH_RUNTIME_INFO_LOGGER_H_ | ||
#define MEDIAPIPE_FRAMEWORK_TOOL_GRAPH_RUNTIME_INFO_LOGGER_H_ | ||
|
||
#include "absl/functional/any_invocable.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/synchronization/notification.h" | ||
#include "mediapipe/framework/calculator.pb.h" | ||
#include "mediapipe/framework/graph_runtime_info.pb.h" | ||
#include "mediapipe/framework/port/threadpool.h" | ||
|
||
namespace mediapipe::tool { | ||
|
||
// Periodically collects the graph runtime info and output it to LOG(INFO). | ||
class GraphRuntimeInfoLogger { | ||
public: | ||
GraphRuntimeInfoLogger(); | ||
~GraphRuntimeInfoLogger(); | ||
|
||
// Starts the collector in the background. Can be called only once. | ||
absl::Status StartInBackground( | ||
const mediapipe::GraphRuntimeInfoConfig& config, | ||
absl::AnyInvocable<absl::StatusOr<GraphRuntimeInfo>()> | ||
get_runtime_info_fn); | ||
|
||
private: | ||
void Stop(); | ||
|
||
absl::Notification shutdown_signal_; | ||
absl::Notification is_running_; | ||
ThreadPool thread_pool_; | ||
absl::AnyInvocable<absl::StatusOr<GraphRuntimeInfo>()> get_runtime_info_fn_; | ||
}; | ||
|
||
} // namespace mediapipe::tool | ||
|
||
#endif // MEDIAPIPE_FRAMEWORK_TOOL_GRAPH_RUNTIME_INFO_LOGGER_H_ |
42 changes: 42 additions & 0 deletions
42
mediapipe/framework/tool/graph_runtime_info_logger_test.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,42 @@ | ||
// Copyright 2024 The MediaPipe 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 | ||
// | ||
// 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 "mediapipe/framework/tool/graph_runtime_info_logger.h" | ||
|
||
#include "absl/synchronization/notification.h" | ||
#include "absl/time/time.h" | ||
#include "mediapipe/framework/calculator.pb.h" | ||
#include "mediapipe/framework/port/gmock.h" | ||
#include "mediapipe/framework/port/gtest.h" | ||
#include "mediapipe/framework/port/status_matchers.h" | ||
|
||
namespace mediapipe::tool { | ||
namespace { | ||
|
||
TEST(GraphRuntimeInfoLoggerTest, ShouldCaptureRuntimeInfo) { | ||
mediapipe::GraphRuntimeInfoConfig config; | ||
config.set_enable_graph_runtime_info(true); | ||
|
||
absl::Notification callback_called; | ||
GraphRuntimeInfoLogger logger; | ||
MP_ASSERT_OK(logger.StartInBackground(config, [&]() { | ||
callback_called.Notify(); | ||
return GraphRuntimeInfo(); | ||
})); | ||
EXPECT_TRUE( | ||
callback_called.WaitForNotificationWithTimeout(absl::Seconds(10))); | ||
} | ||
|
||
} // namespace | ||
} // namespace mediapipe::tool |