Skip to content

Commit

Permalink
Add setting getter for CPU interval definition. (#3967)
Browse files Browse the repository at this point in the history
b/341774149
  • Loading branch information
aee-google authored Aug 16, 2024
1 parent 2eda1f1 commit 4d4699e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cobalt/black_box_tests/testdata/cpu_usage_tracker_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
</head>
<body>
<script>
const assertObjectsEqual = (expected, actual, msg='') => {
const expectedType = typeof(expected);
const actualType = typeof(actual);
assertEqual(expectedType, actualType, `types match, (${msg})`);

if (expectedType === 'object') {
const expectedKeys = new Set(Object.keys(expected));
const actualKeys = new Set(Object.keys(actual));
assertEqual(expectedKeys.size, actualKeys.size, `key count matches, (${msg})`);
expectedKeys.forEach(key => {
assertTrue(actualKeys.has(key), `key ${key} exists, (${msg})`);
assertObjectsEqual(expected[key], actual[key], `value for ${key} matches, (${msg})`);
});
} else {
assertEqual(expected, actual, `simple match, (${msg})`);
}
};

const delay = durationMs =>
new Promise(resolve => {
setTimeout(resolve, durationMs);
Expand All @@ -31,6 +49,16 @@
// Set invalid config.
window.h5vcc.settings.set('cpu_usage_tracker_intervals', '');
delay(2500).then(() => {
const defaultSetting = JSON.parse(
window.h5vcc.settings.getPersistentSettingAsString(
'cpu_usage_tracker_intervals'));
assertObjectsEqual([
{type: 'per_thread', seconds: 2},
{type: 'per_thread', seconds: 30},
{type: 'total', seconds: 2},
{type: 'total', seconds: 30},
], defaultSetting);

const cvalKeys = window.h5vcc.cVal.keys().filter(k => k.startsWith('CPU.'));
// Check for default CVals.
assertIncludes('CPU.Total.Usage.IntervalSeconds.2', cvalKeys);
Expand Down Expand Up @@ -74,6 +102,20 @@
window.h5vcc.settings.set('cpu_usage_tracker_intervals', JSON.stringify(newConfig));
return delay(1500);
}).then(() => {
const newSetting = JSON.parse(
window.h5vcc.settings.getPersistentSettingAsString(
'cpu_usage_tracker_intervals'));
assertObjectsEqual([
{type: 'total', seconds: 1},
{type: 'total', seconds: 3},
{type: 'total', seconds: 4},
{type: 'total', seconds: 120},
{type: 'per_thread', seconds: 1},
{type: 'per_thread', seconds: 3},
{type: 'per_thread', seconds: 5},
{type: 'per_thread', seconds: 60},
], newSetting);

const cvalKeys = window.h5vcc.cVal.keys().filter(k => k.startsWith('CPU.'));
assertIncludes('CPU.Total.Usage.IntervalSeconds.1', cvalKeys);
assertIncludes('CPU.Total.Usage.IntervalSeconds.3', cvalKeys);
Expand Down
6 changes: 6 additions & 0 deletions cobalt/browser/cpu_usage_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ void CpuUsageTracker::UpdateIntervalsEnabled(bool enabled) {
}
}

base::Value CpuUsageTracker::GetIntervalsDefinition() {
base::Value intervals;
storage_->Get(kIntervals, &intervals);
return std::move(intervals);
}

void CpuUsageTracker::UpdateIntervalsDefinition(
const base::Value& intervals_value) {
if (!task_runner_->RunsTasksInCurrentSequence()) {
Expand Down
1 change: 1 addition & 0 deletions cobalt/browser/cpu_usage_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CpuUsageTracker : base::CurrentThread::DestructionObserver {
static CpuUsageTracker* GetInstance();

void Initialize(persistent_storage::PersistentSettings*);
base::Value GetIntervalsDefinition();
void UpdateIntervalsDefinition(const base::Value&);
void UpdateIntervalsEnabled(bool);
void StartOneTimeTracking();
Expand Down
11 changes: 11 additions & 0 deletions cobalt/h5vcc/h5vcc_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,16 @@ int H5vccSettings::GetPersistentSettingAsInt(const std::string& key,
return default_setting;
}

std::string H5vccSettings::GetPersistentSettingAsString(
const std::string& key) const {
if (key.compare("cpu_usage_tracker_intervals") == 0) {
base::Value value =
browser::CpuUsageTracker::GetInstance()->GetIntervalsDefinition();
absl::optional<std::string> json = base::WriteJson(value);
return json.value_or(std::string());
}
return std::string();
}

} // namespace h5vcc
} // namespace cobalt
2 changes: 2 additions & 0 deletions cobalt/h5vcc/h5vcc_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class H5vccSettings : public script::Wrappable {
int GetPersistentSettingAsInt(const std::string& key,
int default_setting) const;

std::string GetPersistentSettingAsString(const std::string& key) const;

DEFINE_WRAPPABLE_TYPE(H5vccSettings);

private:
Expand Down
1 change: 1 addition & 0 deletions cobalt/h5vcc/h5vcc_settings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ interface H5vccSettings {
boolean set(DOMString name, (long or DOMString) value);
void setPersistentSettingAsInt(DOMString name, long value);
long getPersistentSettingAsInt(DOMString name, long default_setting);
DOMString getPersistentSettingAsString(DOMString name);
};

0 comments on commit 4d4699e

Please sign in to comment.