Skip to content

Commit

Permalink
Add stats tracking using CVal for starboard storage and file writes f…
Browse files Browse the repository at this point in the history
…rom cobalt.

b/277761317

Change-Id: Iabc8bf97772cfa42d9f0f0425eeef91376e4044d
  • Loading branch information
aee-google committed Jul 25, 2023
1 parent d0684de commit 65ef806
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 6 deletions.
1 change: 1 addition & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,7 @@ component("base") {
public_configs = [ ":base_public_defines" ]
deps += [
"//nb",
"//starboard/common",
"//starboard",
"//starboard/client_porting/eztime",
]
Expand Down
24 changes: 21 additions & 3 deletions base/files/file_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(kSbFileFromBegin) &&
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions cobalt/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
67 changes: 67 additions & 0 deletions cobalt/base/starboard_stats_tracker.h
Original file line number Diff line number Diff line change
@@ -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<int, base::CValPublic> file_write_success_;
base::CVal<int, base::CValPublic> file_write_fail_;
base::CVal<base::cval::SizeInBytes, base::CValPublic>
file_write_bytes_written_;
base::CVal<int, base::CValPublic> storage_write_record_success_;
base::CVal<int, base::CValPublic> storage_write_record_fail_;
base::CVal<base::cval::SizeInBytes, base::CValPublic>
storage_write_record_bytes_written_;
};

#endif // COBALT_BASE_STARBOARD_STATS_TRACKER_H_
1 change: 1 addition & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static_library("browser") {
"//nb",
"//net",
"//starboard",
"//starboard/common",
"//third_party/icu:icui18n",
"//third_party/protobuf:protobuf_lite",
"//url",
Expand Down
5 changes: 5 additions & 0 deletions cobalt/browser/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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<StarboardStatsTracker>());

// Initializes persistent settings.
persistent_settings_ =
std::make_unique<persistent_storage::PersistentSettings>(
Expand Down
11 changes: 8 additions & 3 deletions cobalt/storage/savegame_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/storage/savegame.h"

#include <algorithm>
#include <memory>

Expand All @@ -22,8 +20,9 @@
#include "base/memory/ptr_util.h"
#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 {
Expand Down Expand Up @@ -77,8 +76,14 @@ bool WriteRecord(const std::unique_ptr<starboard::StorageRecord>& record,
int64_t byte_count = static_cast<int64_t>(bytes.size());
bool success =
record->Write(reinterpret_cast<const char*>(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();

Check warning on line 86 in cobalt/storage/savegame_starboard.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/storage/savegame_starboard.cc#L86

Added line #L86 was not covered by tests
}
return success;
}
Expand Down
2 changes: 2 additions & 0 deletions starboard/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions starboard/common/metrics/stats_tracker.cc
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions starboard/common/metrics/stats_tracker.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <utility>

#include "starboard/common/log.h"

namespace starboard {

class StatsTracker {
public:
virtual ~StatsTracker() = default;

Check warning on line 27 in starboard/common/metrics/stats_tracker.h

View check run for this annotation

Codecov / codecov/patch

starboard/common/metrics/stats_tracker.h#L27

Added line #L27 was not covered by tests

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 {}

Check warning on line 45 in starboard/common/metrics/stats_tracker.h

View check run for this annotation

Codecov / codecov/patch

starboard/common/metrics/stats_tracker.h#L45

Added line #L45 was not covered by tests
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());

Check warning on line 60 in starboard/common/metrics/stats_tracker.h

View check run for this annotation

Codecov / codecov/patch

starboard/common/metrics/stats_tracker.h#L60

Added line #L60 was not covered by tests
}

void set_stats_tracker(std::unique_ptr<StatsTracker> stats_tracker) {
stats_tracker_ = std::move(stats_tracker);
}

private:
StatsTrackerUndefined undefined_stats_tracker_;
std::unique_ptr<StatsTracker> stats_tracker_;
bool undefined_logged_ = false;
};

} // namespace starboard

#endif // STARBOARD_COMMON_METRICS_STATS_TRACKER_H_

0 comments on commit 65ef806

Please sign in to comment.