From 997337c33dcd1fd0530d2c69ea50367c1dd7eb09 Mon Sep 17 00:00:00 2001 From: "Li, Tingqian" Date: Thu, 26 May 2022 21:08:58 -0400 Subject: [PATCH] Add OV_CPU_DEBUG_LOG controls debug logs to show --- src/plugins/intel_cpu/src/docs/README.md | 21 ++++- .../intel_cpu/src/utils/debug_capabilities.h | 77 +++++++++++++++++-- 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/plugins/intel_cpu/src/docs/README.md b/src/plugins/intel_cpu/src/docs/README.md index f43afabc138..b23ff0329b2 100644 --- a/src/plugins/intel_cpu/src/docs/README.md +++ b/src/plugins/intel_cpu/src/docs/README.md @@ -3,9 +3,24 @@ Use the following cmake option to enable debug capabilities: `-DENABLE_DEBUG_CAPS=ON` -Debug logs starting with `[ DEBUG ]` will be shown after this option is set to ON. - * [Verbose mode](verbose.md) * [Blob dumping](blob_dumping.md) * [Graph serialization](graph_serialization.md) -* Performance summary: set `OV_CPU_SUMMARY_PERF` environment variable to display performance summary at the model destruction time. + +## Debug log + +Debug logs starting with `[ DEBUG ]` will be shown after this option is set to ON, and +each log will be start with `function_name:line_num` indicating the position of the log +in source code. + +Environment variable `OV_CPU_DEBUG_LOG` controls which debug logs to output by combining +patterns of `function_name` or `function_name:line_num`, typical examples of usages are: + - not define it: no debug logs will be output + - `-` : all debug logs will be output + - `foo;bar:line2` : only debug logs at "foo:*" and "bar:line2" are output + - `-foo;bar:line2` : only debug logs at "foo:*" and "bar:line2" are not output + +## Performance summary +set `OV_CPU_SUMMARY_PERF` environment variable to display performance summary at the time when model is being destructed. + +Internal performance counter will be enabled automatically. diff --git a/src/plugins/intel_cpu/src/utils/debug_capabilities.h b/src/plugins/intel_cpu/src/utils/debug_capabilities.h index 09dd1d0169f..63320fbf4a8 100644 --- a/src/plugins/intel_cpu/src/utils/debug_capabilities.h +++ b/src/plugins/intel_cpu/src/utils/debug_capabilities.h @@ -8,12 +8,77 @@ #define CPU_DEBUG_CAP_ENABLE(_x) _x; #define CPU_DEBUG_CAPS_ALWAYS_TRUE(x) true -#define DEBUG_LOG(...) \ - do { \ - ::std::stringstream ss___; \ - ::ov::write_all_to_stream(ss___, "[ DEBUG ] ", __func__, ":", __LINE__, " ", __VA_ARGS__); \ - std::cout << ss___.str() << std::endl; \ - } while (0) +// OV_CPU_DEBUG_LOG controls DEBUG_LOGs to output +// +// positive filter: enables patterns in filter +// [+]foo;bar:line2; enables "foo:*" and "bar:line2" +// - enables all debug log +// +// negative filter: disable patterns in filter +// -f1;f2:l; disables "foo:*" and "bar:line2" +// +class DebugLogEnabled { + bool enabled; + +public: + DebugLogEnabled(const char* func, int line) { + // check ENV + const char* p_filters = std::getenv("OV_CPU_DEBUG_LOG"); + if (!p_filters) { + enabled = false; + return; + } + + // check each filter patten: + bool filter_match_action; + if (p_filters[0] == '-') { + p_filters++; + filter_match_action = false; + } else { + filter_match_action = true; + } + + std::string func_with_line(func); + func_with_line += ":" + std::to_string(line); + + bool match = false; + const char* p0 = p_filters; + const char* p1; + while (*p0 != 0) { + p1 = p0; + while (*p1 != ';' && *p1 != 0) + ++p1; + std::string patten(p0, p1 - p0); + if (patten == func || patten == func_with_line) { + match = true; + break; + } + p0 = p1; + if (*p0 == ';') + ++p0; + } + + if (match) + enabled = filter_match_action; + else + enabled = !filter_match_action; + } + operator bool() const { + return enabled; + } +}; + +#define DEBUG_ENABLE_NAME debug_enable_##__LINE__ + +#define DEBUG_LOG(...) \ + do { \ + static DebugLogEnabled DEBUG_ENABLE_NAME(__func__, __LINE__); \ + if (DEBUG_ENABLE_NAME) { \ + ::std::stringstream ss___; \ + ::ov::write_all_to_stream(ss___, "[ DEBUG ] ", __func__, ":", __LINE__, " ", __VA_ARGS__); \ + std::cout << ss___.str() << std::endl; \ + } \ + } while (0) #else // !CPU_DEBUG_CAPS