Skip to content

Commit

Permalink
Cherry pick PR #1274: [Watchdog] support query watchdog violation cli…
Browse files Browse the repository at this point in the history
…ent names (#1306)

Refer to the original PR: youtube/cobalt#1274

b/287302949

Co-authored-by: Sherry Zhou <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and sherryzy authored Aug 17, 2023
1 parent d79b3fe commit 1a624df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
36 changes: 25 additions & 11 deletions cobalt/watchdog/watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,17 @@ std::string Watchdog::GetWatchdogFilePath() {
return watchdog_file_path_;
}

std::vector<std::string> Watchdog::GetWatchdogClientNames() {
std::vector<std::string> Watchdog::GetWatchdogViolationClientNames() {
if (pending_write_) WriteWatchdogViolations();

std::string watchdog_json = ReadViolationFile(GetWatchdogFilePath().c_str());
std::vector<std::string> names;
for (auto& it : client_map_) {
names.push_back(it.first);
if (watchdog_json != "") {
std::unique_ptr<base::Value> violations_map =
base::JSONReader::Read(watchdog_json);
for (const auto& it : violations_map->DictItems()) {
names.push_back(it.first);
}
}
return names;
}
Expand Down Expand Up @@ -316,19 +323,26 @@ void Watchdog::UpdateViolationsMap(void* context, Client* client,
static_cast<Watchdog*>(context)->pending_write_ = true;
}

std::string Watchdog::ReadViolationFile(const char* file_path) {
starboard::ScopedFile read_file(file_path, kSbFileOpenOnly | kSbFileRead);
if (read_file.IsValid()) {
int64_t kFileSize = read_file.GetSize();
std::vector<char> buffer(kFileSize + 1, 0);
read_file.ReadAll(buffer.data(), kFileSize);
return std::string(buffer.data());
}
return "";
}

void Watchdog::InitializeViolationsMap(void* context) {
// Loads the previous Watchdog violations file containing violations before
// app start, if it exists, to populate violations_map_.
static_cast<Watchdog*>(context)->violations_count_ = 0;

starboard::ScopedFile read_file(
(static_cast<Watchdog*>(context)->GetWatchdogFilePath()).c_str(),
kSbFileOpenOnly | kSbFileRead);
if (read_file.IsValid()) {
int64_t kFileSize = read_file.GetSize();
std::vector<char> buffer(kFileSize + 1, 0);
read_file.ReadAll(buffer.data(), kFileSize);
std::string watchdog_json = std::string(buffer.data());
std::string watchdog_json =
static_cast<Watchdog*>(context)->ReadViolationFile(
(static_cast<Watchdog*>(context)->GetWatchdogFilePath()).c_str());
if (watchdog_json != "") {
static_cast<Watchdog*>(context)->violations_map_ =
base::JSONReader::Read(watchdog_json);
}
Expand Down
3 changes: 2 additions & 1 deletion cobalt/watchdog/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Watchdog : public Singleton<Watchdog> {
bool InitializeStub();
void Uninitialize();
std::string GetWatchdogFilePath();
std::vector<std::string> GetWatchdogClientNames();
std::vector<std::string> GetWatchdogViolationClientNames();
void UpdateState(base::ApplicationState state);
bool Register(std::string name, std::string description,
base::ApplicationState monitor_state,
Expand All @@ -105,6 +105,7 @@ class Watchdog : public Singleton<Watchdog> {

private:
void WriteWatchdogViolations();
std::string ReadViolationFile(const char* file_path);
static void* Monitor(void* context);
static void UpdateViolationsMap(void* context, Client* client,
SbTimeMonotonic time_delta);
Expand Down
13 changes: 8 additions & 5 deletions cobalt/watchdog/watchdog_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,23 +492,26 @@ TEST_F(WatchdogTest, FrequentConsecutiveViolationsShouldNotWrite) {
ASSERT_NE(write_json, json);
}

TEST_F(WatchdogTest, GetRegisteredClientNames) {
TEST_F(WatchdogTest, GetViolationClientNames) {
ASSERT_TRUE(watchdog_->Register("test-name-1", "test-desc-1",
base::kApplicationStateStarted,
kWatchdogMonitorFrequency));
ASSERT_TRUE(watchdog_->Register("test-name-2", "test-desc-2",
base::kApplicationStateStarted,
kWatchdogMonitorFrequency));
std::vector<std::string> names = watchdog_->GetWatchdogClientNames();
SbThreadSleep(kWatchdogSleepDuration);
ASSERT_TRUE(watchdog_->Unregister("test-name-1"));
ASSERT_TRUE(watchdog_->Unregister("test-name-2"));
std::vector<std::string> names = watchdog_->GetWatchdogViolationClientNames();
ASSERT_EQ(names.size(), 2);
std::set<std::string> expected_names = {"test-name-1", "test-name-2"};
for (std::vector<std::string>::const_iterator it = names.begin();
it != names.end(); ++it) {
const std::string name = *it;
ASSERT_TRUE((expected_names.find(name) != expected_names.end()));
}
ASSERT_TRUE(watchdog_->Unregister("test-name-1"));
ASSERT_TRUE(watchdog_->Unregister("test-name-2"));
names = watchdog_->GetWatchdogClientNames();
watchdog_->GetWatchdogViolations();
names = watchdog_->GetWatchdogViolationClientNames();
ASSERT_EQ(names.size(), 0);
}

Expand Down

0 comments on commit 1a624df

Please sign in to comment.