Skip to content

Commit

Permalink
[checkpoint] * -> DEFAULT for wildcard.
Browse files Browse the repository at this point in the history
Andrew pointed out *=9 suggests foo/*.cc=9 would work but it is not supported.
  • Loading branch information
mbs-octoml committed Sep 17, 2021
1 parent 247460b commit 3482fc2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions include/tvm/runtime/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ inline bool DebugLoggingEnabled() {
*
* To enable file \p relay/foo.cc up to level 2 and \p ir/bar.cc for level 0 only set:
* \code
* TVM_LOG_DEBUG="relay/foo.cc=2;ir/bar.cc=0;"
* TVM_LOG_DEBUG="relay/foo.cc=2;ir/bar.cc=0"
* \endcode
*
* To enable all files up to level 3 but disable \p ir/bar.cc set:
* \code
* TVM_LOG_DEBUG="*=2;ir/bar.cc=-1;"
* TVM_LOG_DEBUG="DEFAULT=2;ir/bar.cc=-1"
* \endcode
*
* Any of these settings will also enable DLOG statements.
Expand Down Expand Up @@ -574,7 +574,7 @@ TVM_CHECK_FUNC(_NE, !=)

/*!
* \brief If the \p TVM_LOG_DEBUG build flag is enabled, push a context message onto an internal
* stack. All VLOG messages will include this stack as their prefix to help with debugging. E.g.:
* stack. All VLOG messages will include this stack in their prefix to help with debugging. E.g.:
* \code
* VLOG_CONTEXT << "my context";
* VLOG(1) << "my log message";
Expand Down
37 changes: 20 additions & 17 deletions src/runtime/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ namespace tvm {
namespace runtime {
namespace detail {

namespace {
constexpr const char* kSrcPrefix = "/src/";
constexpr const size_t kSrcPrefixLength = 5;
constexpr const char* kDefaultKeyword = "DEFAULT";
} // namespace

// Parse \p opt_spec as a VLOG specification as per comment in
// DebugLoggingEnabled and VerboseLoggingEnabled.
std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char* opt_spec) {
Expand All @@ -184,27 +190,26 @@ std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char* opt_spec)
// Legacy specification for enabling just DLOG.
// A wildcard entry in the map will signal DLOG is on, but all VLOG levels are disabled.
LOG(INFO) << "TVM_LOG_DEBUG enables DLOG statements only";
map.emplace("*", -1);
map.emplace(kDefaultKeyword, -1);
return map;
}
std::istringstream spec_stream(spec);
for (std::string entry; std::getline(spec_stream, entry, ';'); /* no-op */) {
if (entry.empty()) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty entry";
return map;
}
std::istringstream entry_stream(entry);
while (spec_stream) {
std::string name;
if (!std::getline(entry_stream, name, '=')) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, missing '='";
return map;
if (!std::getline(spec_stream, name, '=')) {
// Reached end.
break;
}
if (name.empty()) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty name";
return map;
}

std::string level;
entry_stream >> level;
if (!std::getline(spec_stream, level, ';')) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, expecting level";
return map;
}
if (level.empty()) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty level";
return map;
Expand All @@ -214,6 +219,7 @@ std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char* opt_spec)
int level_val = static_cast<int>(strtol(level.c_str(), &end_of_level, 10));
if (end_of_level != level.c_str() + level.size()) {
LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, invalid level";
return map;
}
if (map.empty()) {
LOG(INFO) << "TVM_LOG_DEBUG enables DLOG statements";
Expand All @@ -231,9 +237,6 @@ const std::unordered_map<std::string, int>& TvmLogDebugSpec() {
return *map;
}

constexpr const char* kSrcPrefix = "/src/";
constexpr const size_t kSrcPrefixLength = 5;

bool VerboseEnabledInMap(const std::string& filename, int level,
const std::unordered_map<std::string, int>& map) {
if (level < 0) {
Expand All @@ -248,13 +251,13 @@ bool VerboseEnabledInMap(const std::string& filename, int level,
// canonicalization.
std::string key =
last_src == std::string::npos ? filename : filename.substr(last_src + kSrcPrefixLength);
// Check for exact.
// Check for exact match.
auto itr = map.find(key);
if (itr != map.end()) {
return level <= itr->second;
}
// Check for '*' wildcard.
itr = map.find("*");
// Check for default.
itr = map.find(kDefaultKeyword);
if (itr != map.end()) {
return level <= itr->second;
}
Expand Down
18 changes: 11 additions & 7 deletions tests/cpp/runtime/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ TEST(ParseTvmLogDebugSpec, Disabled) {
TEST(ParseTvmLogDebugSpec, DlogOnly) {
auto map = ParseTvmLogDebugSpec("1");
EXPECT_EQ(map.size(), 1);
EXPECT_EQ(map["*"], -1);
EXPECT_EQ(map["DEFAULT"], -1);
}

TEST(ParseTvmLogDebugSpec, VLogEnabled) {
auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;*=2;another/file.cc=4");
EXPECT_EQ(map.size(), 4);
TEST(ParseTvmLogDebugSpec, VLogEnabledDefault) {
auto map = ParseTvmLogDebugSpec("DEFAULT=3");
EXPECT_EQ(map.size(), 1);
EXPECT_EQ(map["DEFAULT"], 3);
}

EXPECT_EQ(map["*"], 2);
TEST(ParseTvmLogDebugSpec, VLogEnabledComplex) {
auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;DEFAULT=2;another/file.cc=4");
EXPECT_EQ(map.size(), 4);
EXPECT_EQ(map["DEFAULT"], 2);
EXPECT_EQ(map["foo/bar.cc"], 3);
EXPECT_EQ(map["baz.cc"], -1);
EXPECT_EQ(map["another/file.cc"], 4);
Expand All @@ -52,8 +57,7 @@ TEST(ParseTvmLogDebugSpec, IllFormed) {
}

TEST(VerboseEnabledInMap, Lookup) {
auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;*=2;another/file.cc=4;");

auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;DEFAULT=2;another/file.cc=4;");
EXPECT_TRUE(VerboseEnabledInMap("my/filesystem/src/foo/bar.cc", 3, map));
EXPECT_FALSE(VerboseEnabledInMap("my/filesystem/src/foo/bar.cc", 4, map));
EXPECT_TRUE(VerboseEnabledInMap("my/filesystem/src/foo/other.cc", 2, map));
Expand Down

0 comments on commit 3482fc2

Please sign in to comment.