From dcdc1c0eb1b1c9f59d1241278558d5498ef03663 Mon Sep 17 00:00:00 2001 From: esmael Date: Mon, 26 Jun 2023 08:47:34 -0700 Subject: [PATCH] Add stats tracking using CVal for starboard storage and file writes from cobalt. b/277761317 Change-Id: Iabc8bf97772cfa42d9f0f0425eeef91376e4044d --- base/BUILD.gn | 1 + base/files/file_starboard.cc | 24 +++++++- cobalt/base/BUILD.gn | 1 + cobalt/base/starboard_stats_tracker.h | 67 ++++++++++++++++++++ cobalt/browser/BUILD.gn | 1 + cobalt/browser/application.cc | 5 ++ cobalt/storage/savegame_starboard.cc | 8 ++- starboard/common/BUILD.gn | 2 + starboard/common/metrics/stats_tracker.cc | 24 ++++++++ starboard/common/metrics/stats_tracker.h | 75 +++++++++++++++++++++++ 10 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 cobalt/base/starboard_stats_tracker.h create mode 100644 starboard/common/metrics/stats_tracker.cc create mode 100644 starboard/common/metrics/stats_tracker.h diff --git a/base/BUILD.gn b/base/BUILD.gn index b7cfe95a8232..c507c1b40e10 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -1561,6 +1561,7 @@ component("base") { public_configs = [ ":base_public_defines" ] deps += [ "//nb", + "//starboard/common", "//starboard", "//starboard/client_porting/eztime", ] diff --git a/base/files/file_starboard.cc b/base/files/file_starboard.cc index 6c1e8eee337f..b8a8b704f156 100644 --- a/base/files/file_starboard.cc +++ b/base/files/file_starboard.cc @@ -19,11 +19,26 @@ #include "base/files/file_path.h" #include "base/logging.h" #include "base/threading/thread_restrictions.h" +#include "starboard/common/metrics/stats_tracker.h" #include "starboard/common/log.h" #include "starboard/file.h" namespace base { +namespace { + +void RecordFileWriteStat(int write_file_result) { + auto& stats_tracker = starboard::StatsTrackerContainer::GetInstance()->stats_tracker(); + if (write_file_result <= 0) { + stats_tracker.FileWriteFail(); + } else { + stats_tracker.FileWriteSuccess(); + stats_tracker.FileWriteBytesWritten(/*bytes_written=*/write_file_result); + } +} + +} // namespace + // Make sure our Whence mappings match the system headers. static_assert( File::FROM_BEGIN == static_cast(kSbFileFromBegin) && @@ -181,8 +196,9 @@ int File::WriteAtCurrentPos(const char* data, int size) { return -1; SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size); - - return SbFileWriteAll(file_.get(), data, size); + int write_result = SbFileWriteAll(file_.get(), data, size); + RecordFileWriteStat(write_result); + return write_result; } int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { @@ -192,7 +208,9 @@ int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { return -1; SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size); - return SbFileWrite(file_.get(), data, size); + int write_result = SbFileWrite(file_.get(), data, size); + RecordFileWriteStat(write_result); + return write_result; } int64_t File::GetLength() { diff --git a/cobalt/base/BUILD.gn b/cobalt/base/BUILD.gn index 5a01b69e6d7b..5552a2ebc71a 100644 --- a/cobalt/base/BUILD.gn +++ b/cobalt/base/BUILD.gn @@ -70,6 +70,7 @@ static_library("base") { "ref_counted_lock.h", "source_location.cc", "source_location.h", + "starboard_stats_tracker.h", "startup_timer.cc", "startup_timer.h", "statistics.h", diff --git a/cobalt/base/starboard_stats_tracker.h b/cobalt/base/starboard_stats_tracker.h new file mode 100644 index 000000000000..aea4161f169d --- /dev/null +++ b/cobalt/base/starboard_stats_tracker.h @@ -0,0 +1,67 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef COBALT_BASE_STARBOARD_STATS_TRACKER_H_ +#define COBALT_BASE_STARBOARD_STATS_TRACKER_H_ + +#include "cobalt/base/c_val.h" +#include "starboard/common/metrics/stats_tracker.h" + +class StarboardStatsTracker : public starboard::StatsTracker { + public: + StarboardStatsTracker() + : file_write_success_("Starboard.FileWrite.Success", 0, + "SbFileWrite() success count from cobalt."), + file_write_fail_("Starboard.FileWrite.Fail", 0, + "SbFileWrite() fail count from cobalt."), + file_write_bytes_written_("Starboard.FileWrite.BytesWritten", 0, + "SbFileWrite() bytes written from cobalt."), + storage_write_record_success_( + "Starboard.StorageWriteRecord.Success", 0, + "SbStorageWriteRecord() success count from cobalt."), + storage_write_record_fail_( + "Starboard.StorageWriteRecord.Fail", 0, + "SbStorageWriteRecord() fail count from cobalt."), + storage_write_record_bytes_written_( + "Starboard.StorageWriteRecord.BytesWritten", 0, + "SbStorageWriteRecord() bytes written from cobalt.") {} + + void FileWriteSuccess() override { ++file_write_success_; } + + void FileWriteFail() override { ++file_write_fail_; } + + void FileWriteBytesWritten(int bytes_written) override { + file_write_bytes_written_ += bytes_written; + } + + void StorageWriteRecordSuccess() override { ++storage_write_record_success_; } + + void StorageWriteRecordFail() override { ++storage_write_record_fail_; } + + void StorageWriteRecordBytesWritten(int bytes_written) override { + storage_write_record_bytes_written_ += bytes_written; + } + + private: + base::CVal file_write_success_; + base::CVal file_write_fail_; + base::CVal + file_write_bytes_written_; + base::CVal storage_write_record_success_; + base::CVal storage_write_record_fail_; + base::CVal + storage_write_record_bytes_written_; +}; + +#endif // COBALT_BASE_STARBOARD_STATS_TRACKER_H_ diff --git a/cobalt/browser/BUILD.gn b/cobalt/browser/BUILD.gn index 783226f9f33d..322826071443 100644 --- a/cobalt/browser/BUILD.gn +++ b/cobalt/browser/BUILD.gn @@ -201,6 +201,7 @@ static_library("browser") { "//nb", "//net", "//starboard", + "//starboard/common", "//third_party/icu:icui18n", "//third_party/protobuf:protobuf_lite", "//url", diff --git a/cobalt/browser/application.cc b/cobalt/browser/application.cc index 0be21cba301c..dea2f466ebd2 100644 --- a/cobalt/browser/application.cc +++ b/cobalt/browser/application.cc @@ -49,6 +49,7 @@ #include "cobalt/base/on_screen_keyboard_hidden_event.h" #include "cobalt/base/on_screen_keyboard_shown_event.h" #include "cobalt/base/on_screen_keyboard_suggestions_updated_event.h" +#include "cobalt/base/starboard_stats_tracker.h" #include "cobalt/base/startup_timer.h" #include "cobalt/base/version_compatibility.h" #include "cobalt/base/window_on_offline_event.h" @@ -75,6 +76,7 @@ #include "cobalt/watchdog/watchdog.h" #include "components/metrics/metrics_service.h" #include "starboard/common/device_type.h" +#include "starboard/common/metrics/stats_tracker.h" #include "starboard/common/system_property.h" #include "starboard/configuration.h" #include "starboard/event.h" @@ -672,6 +674,9 @@ Application::Application(const base::Closure& quit_closure, bool should_preload, // URLRequestContext; base::TaskScheduler::CreateAndStartWithDefaultParams("Cobalt TaskScheduler"); + starboard::StatsTrackerContainer::GetInstance()->set_stats_tracker( + std::make_unique()); + // Initializes persistent settings. persistent_settings_ = std::make_unique( diff --git a/cobalt/storage/savegame_starboard.cc b/cobalt/storage/savegame_starboard.cc index 8bd0cdf9650a..77e805209270 100644 --- a/cobalt/storage/savegame_starboard.cc +++ b/cobalt/storage/savegame_starboard.cc @@ -21,8 +21,8 @@ #include "base/optional.h" #include "base/path_service.h" #include "cobalt/storage/savegame.h" +#include "starboard/common/metrics/stats_tracker.h" #include "starboard/common/storage.h" -#include "starboard/user.h" namespace cobalt { namespace storage { @@ -76,8 +76,14 @@ bool WriteRecord(const std::unique_ptr& record, int64_t byte_count = static_cast(bytes.size()); bool success = record->Write(reinterpret_cast(bytes.data()), byte_count); + auto& stats_tracker = + starboard::StatsTrackerContainer::GetInstance()->stats_tracker(); if (success) { DLOG(INFO) << "Successfully wrote storage record."; + stats_tracker.StorageWriteRecordSuccess(); + stats_tracker.StorageWriteRecordBytesWritten(/*bytes_written=*/byte_count); + } else { + stats_tracker.StorageWriteRecordFail(); } return success; } diff --git a/starboard/common/BUILD.gn b/starboard/common/BUILD.gn index 5c1977ea4817..e1db661e7a05 100644 --- a/starboard/common/BUILD.gn +++ b/starboard/common/BUILD.gn @@ -51,6 +51,8 @@ static_library("common") { "log.cc", "media.cc", "media.h", + "metrics/stats_tracker.cc", + "metrics/stats_tracker.h", "move.h", "murmurhash2.cc", "murmurhash2.h", diff --git a/starboard/common/metrics/stats_tracker.cc b/starboard/common/metrics/stats_tracker.cc new file mode 100644 index 000000000000..e9e4483af8a1 --- /dev/null +++ b/starboard/common/metrics/stats_tracker.cc @@ -0,0 +1,24 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "starboard/common/metrics/stats_tracker.h" + +#include "starboard/once.h" + +namespace starboard { + +SB_ONCE_INITIALIZE_FUNCTION(StatsTrackerContainer, + StatsTrackerContainer::GetInstance); + +} // namespace starboard diff --git a/starboard/common/metrics/stats_tracker.h b/starboard/common/metrics/stats_tracker.h new file mode 100644 index 000000000000..940617572c90 --- /dev/null +++ b/starboard/common/metrics/stats_tracker.h @@ -0,0 +1,75 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef STARBOARD_COMMON_METRICS_STATS_TRACKER_H_ +#define STARBOARD_COMMON_METRICS_STATS_TRACKER_H_ + +#include +#include + +#include "starboard/common/log.h" + +namespace starboard { + +class StatsTracker { + public: + virtual ~StatsTracker() = default; + + virtual void FileWriteSuccess() = 0; + virtual void FileWriteFail() = 0; + virtual void FileWriteBytesWritten(int bytes_written) = 0; + + virtual void StorageWriteRecordSuccess() = 0; + virtual void StorageWriteRecordFail() = 0; + virtual void StorageWriteRecordBytesWritten(int bytes_written) = 0; +}; + +class StatsTrackerUndefined : public StatsTracker { + public: + void FileWriteSuccess() override {} + void FileWriteFail() override {} + void FileWriteBytesWritten(int bytes_written) override {} + + void StorageWriteRecordSuccess() override {} + void StorageWriteRecordFail() override {} + void StorageWriteRecordBytesWritten(int bytes_written) override {} +}; + +class StatsTrackerContainer { + public: + static StatsTrackerContainer* GetInstance(); + + StatsTracker& stats_tracker() { + if (!stats_tracker_) { + SB_DLOG_IF(ERROR, !undefined_logged_) + << "[once] StatsTracker is not defined."; + undefined_logged_ = true; + return undefined_stats_tracker_; + } + return *(stats_tracker_.get()); + } + + void set_stats_tracker(std::unique_ptr stats_tracker) { + stats_tracker_ = std::move(stats_tracker); + } + + private: + StatsTrackerUndefined undefined_stats_tracker_; + std::unique_ptr stats_tracker_; + bool undefined_logged_ = false; +}; + +} // namespace starboard + +#endif // STARBOARD_COMMON_METRICS_STATS_TRACKER_H_