Skip to content

Commit

Permalink
Adding VLOG overrides - MediaPipe utilizes VLOG heavily, but it's not…
Browse files Browse the repository at this point in the history
… straightforward for how to enable this when running an Android app. VLOG overrides allow to relatively quickly enable VLOGs for various modules within MediaPipe.

PiperOrigin-RevId: 697671583
  • Loading branch information
MediaPipe Team authored and copybara-github committed Nov 18, 2024
1 parent 0cebcc0 commit cccc5d2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mediapipe/framework/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ cc_library(
":thread_pool_executor_cc_proto",
":timestamp",
":validated_graph_config",
":vlog_overrides",
"//mediapipe/framework/port:core_proto",
"//mediapipe/framework/port:integral_types",
"//mediapipe/framework/port:logging",
Expand Down Expand Up @@ -1926,3 +1927,19 @@ cc_library(
":memory_manager",
],
)

cc_library(
name = "vlog_overrides",
srcs = ["vlog_overrides.cc"],
hdrs = ["vlog_overrides.h"],
visibility = ["//visibility:private"],
deps = [
"//mediapipe/framework/deps:no_destructor",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/log:globals",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)
2 changes: 2 additions & 0 deletions mediapipe/framework/calculator_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#include "mediapipe/framework/tool/validate.h"
#include "mediapipe/framework/tool/validate_name.h"
#include "mediapipe/framework/validated_graph_config.h"
#include "mediapipe/framework/vlog_overrides.h"
#include "mediapipe/gpu/gpu_service.h"
#include "mediapipe/gpu/graph_support.h"
#include "mediapipe/util/cpu_util.h"
Expand Down Expand Up @@ -145,6 +146,7 @@ CalculatorGraph::CalculatorGraph(CalculatorContext* cc)
// TODO b/368015341- Use factory method to avoid CHECK in constructor.
ABSL_CHECK_OK(DisallowServiceDefaultInitialization());
}
SetVLogOverrides();
}

CalculatorGraph::CalculatorGraph(CalculatorGraphConfig config)
Expand Down
55 changes: 55 additions & 0 deletions mediapipe/framework/vlog_overrides.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "mediapipe/framework/vlog_overrides.h"

// Template to temporary enable VLOG overrides in code:
// #define MEDIAPIPE_VLOG_VMODULE "calculator_graph*=5,southbound*=5"
// #define MEDIAPIPE_VLOG_V 1

#if defined(MEDIAPIPE_VLOG_V) || defined(MEDIAPIPE_VLOG_VMODULE)

#include <string>
#include <utility>
#include <vector>

#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/log/globals.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/deps/no_destructor.h"

#endif // defined(MEDIAPIPE_VLOG_V) || defined(MEDIAPIPE_VLOG_VMODULE)

namespace mediapipe {

void SetVLogOverrides() {
#if defined(MEDIAPIPE_VLOG_V)
ABSL_LOG(INFO) << absl::StrFormat("Setting global VLOG level: %d",
MEDIAPIPE_VLOG_V);
absl::SetGlobalVLogLevel(MEDIAPIPE_VLOG_V);
#endif // defined(MEDIAPIPE_VLOG_V)

#if defined(MEDIAPIPE_VLOG_VMODULE)
static NoDestructor<std::vector<std::pair<std::string, int>>> kVModuleMapping(
[]() {
constexpr absl::string_view kVModule = MEDIAPIPE_VLOG_VMODULE;
std::vector<std::string> parts =
absl::StrSplit(kVModule, absl::ByAnyChar(",="));
ABSL_CHECK_EQ(parts.size() % 2, 0)
<< "Invalid MEDIAPIPE_VLOG_VMODULE: " << kVModule;
std::vector<std::pair<std::string, int>> result;
for (int i = 0; i < parts.size(); i += 2) {
result.push_back({parts[i], std::stoi(parts[i + 1])});
}
return result;
}());

ABSL_LOG(INFO) << "Setting VLOG levels...";
for (const auto& [key, value] : *kVModuleMapping) {
ABSL_LOG(INFO) << absl::StrFormat("Setting [%s] to level: %d", key, value);
absl::SetVLogLevel(key, value);
}
#endif // defined(MEDIAPIPE_VLOG_VMODULE)
}

} // namespace mediapipe
32 changes: 32 additions & 0 deletions mediapipe/framework/vlog_overrides.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_
#define MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_

namespace mediapipe {

// If possible, rely on --v / --vmodule to set VLOG level and modules.
//
// However, in cases when --v / --vmodule cannot be used (e.g. running an
// Android app and enabling VLOGs), MediaPipe allows to set VLOG --v / --vmodule
// overrides for debugging purposes which are applied when `CalculatorGraph` is
// created.
//
// Overrides:
// - MEDIAPIPE_VLOG_V (define and provide value you provide for --v)
// - MEDIAPIPE_VLOG_VMODULE (define and provide value you provide for --vmodule)
//
// You can set overrides by adding:
// ```
// --copt=-DMEDIAPIPE_VLOG_VMODULE=\"*calculator*=5\"
// ```
// with your desired module patterns and VLOG levels (see more details for
// --vmodule) to your build command.
//
// IMPORTANT: mind that adding the above to your build command will trigger
// rebuild of the whole binary including dependencies. So, considering vlog
// overrides exist for debugging purposes only, it is faster to simply modify
// `vlog_overrides.cc` adding MEDIAPIPE_VLOG_V/VMODULE at the very top.
void SetVLogOverrides();

} // namespace mediapipe

#endif // MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_

0 comments on commit cccc5d2

Please sign in to comment.