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 6, 2023
1 parent 94eb6d7 commit b8c6180
Show file tree
Hide file tree
Showing 42 changed files with 641 additions and 449 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_stats",
"//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_stats/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_stats::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
53 changes: 36 additions & 17 deletions base/files/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@
#include "starboard/types.h"
#endif

#if defined(STARBOARD)
#include "starboard/file.h"
#endif

// This macro helps avoid wrapped lines in the test structs.
#define FPL(x) FILE_PATH_LITERAL(x)

Expand Down Expand Up @@ -299,19 +295,47 @@ class FindResultCollector {
std::set<FilePath::StringType> files_;
};

#if defined(STARBOARD)
base::FilePath RemoveRelativeComponents(const base::FilePath& file_path) {
if (!file_path.IsAbsolute()) {
return file_path;
}
bool no_relative_components = file_path.value().find(base::FilePath::kParentDirectory) == FilePath::StringType::npos;
if (no_relative_components) {
return file_path;
}
base::FilePath current_dir_name = file_path;
std::vector<std::string> reverse_components;
while (current_dir_name != current_dir_name.DirName()) {
reverse_components.push_back(current_dir_name.BaseName().value());
current_dir_name = current_dir_name.DirName();
}
base::FilePath root = current_dir_name;
std::vector<std::string> new_components;
for (int i = reverse_components.size() - 1; i >= 0; --i) {
if (reverse_components[i] == base::FilePath::kParentDirectory) {
if (!new_components.empty()) {
new_components.pop_back();
}
continue;
}
new_components.push_back(reverse_components[i]);
}
base::FilePath new_file_path(root);
for (auto& component : new_components) {
new_file_path = new_file_path.Append(component);
}
return new_file_path;
}
#endif

// Simple function to dump some text into a new file.
void CreateTextFile(const FilePath& filename,
const std::wstring& contents) {
#if defined(STARBOARD)
const std::string contents_ascii = UTF16ToASCII(WideToUTF16(contents));
SbFileError file_error = kSbFileOk;
SbFile file =
SbFileOpen(filename.value().c_str(), kSbFileCreateAlways | kSbFileWrite,
nullptr, &file_error);
SB_CHECK((file_error == kSbFileOk));
SB_CHECK(SbFileWriteAll(file, contents_ascii.data(), contents_ascii.size()) ==
SB_CHECK(base::WriteFile(RemoveRelativeComponents(filename), contents_ascii.data(), contents_ascii.size()) ==
contents_ascii.size());
SB_CHECK(SbFileClose(file));
#else // !defined(STARBOARD)
std::wofstream file;
file.open(filename.value().c_str());
Expand All @@ -326,12 +350,7 @@ std::wstring ReadTextFile(const FilePath& filename) {
#if defined(STARBOARD)
const int size_in_bytes = 64 * sizeof(wchar_t);
char contents[size_in_bytes]{0};
SbFileError file_error = kSbFileOk;
SbFile file = SbFileOpen(filename.value().c_str(),
kSbFileOpenOnly | kSbFileRead, nullptr, &file_error);
SB_CHECK(file_error == kSbFileOk);
SB_CHECK(SbFileReadAll(file, contents, size_in_bytes) != -1);
SB_CHECK(SbFileClose(file));
SB_CHECK(base::ReadFile(RemoveRelativeComponents(filename), contents, size_in_bytes) != -1);
return UTF16ToWide(ASCIIToUTF16(contents));
#else // !defined(STARBOARD)
wchar_t contents[64];
Expand Down
24 changes: 12 additions & 12 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

#include "base/logging.h"

#include <memory>

#include <limits.h>

#include "base/macros.h"
#include "build/build_config.h"

#if defined(STARBOARD)
#include "base/files/file.h"
#include "starboard/client_porting/eztime/eztime.h"
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/configuration.h"
#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/system.h"
#include "starboard/time.h"
typedef SbFile FileHandle;
typedef base::File* FileHandle;
typedef SbMutex MutexHandle;
#else
#if defined(OS_WIN)
Expand Down Expand Up @@ -388,12 +390,11 @@ bool InitializeLogFileHandle() {

if ((g_logging_destination & LOG_TO_FILE) != 0) {
#if defined(STARBOARD)
g_log_file = SbFileOpen(g_log_file_name->c_str(),
kSbFileOpenAlways | kSbFileWrite, NULL, NULL);
if (!SbFileIsValid(g_log_file))
g_log_file = new base::File(base::FilePath(g_log_file_name->c_str()), base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE);
if (!g_log_file->IsValid())
return false;

SbFileSeek(g_log_file, kSbFileFromEnd, 0);
g_log_file->Seek(base::File::FROM_END, 0);
#else
#if defined(OS_WIN)
// The FILE_APPEND_DATA access mask ensures that the file is atomically
Expand Down Expand Up @@ -446,16 +447,15 @@ bool InitializeLogFileHandle() {

void CloseFile(FileHandle log) {
#if defined(STARBOARD)
SbFileClose(log);
#else
#if defined(OS_WIN)
// base::File is closed in destructor.
delete log;
#elif defined(OS_WIN)
CloseHandle(log);
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
fclose(log);
#else
#error Unsupported platform
#endif
#endif
}

void CloseLogFileUnlocked() {
Expand Down Expand Up @@ -934,10 +934,10 @@ LogMessage::~LogMessage() {
#endif
if (InitializeLogFileHandle()) {
#if defined(STARBOARD)
SbFileSeek(g_log_file, kSbFileFromEnd, 0);
g_log_file->Seek(base::File::FROM_END, 0);
int written = 0;
while (written < str_newline.length()) {
int result = SbFileWrite(g_log_file, &(str_newline.c_str()[written]),
int result = g_log_file->WriteAtCurrentPos(&(str_newline.c_str()[written]),
str_newline.length() - written);
if (result < 0) {
break;
Expand Down
16 changes: 0 additions & 16 deletions base/metrics/persistent_histogram_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#include "base/time/time.h"
#include "build/build_config.h"

#if defined(STARBOARD)
#include "starboard/common/file.h"
#endif

namespace {

constexpr size_t kAllocSize = 1 << 20; // 1 MiB
Expand Down Expand Up @@ -98,19 +94,7 @@ PersistentHistogramStorage::~PersistentHistogramStorage() {

StringPiece contents(static_cast<const char*>(allocator->data()),
allocator->used());
#if defined(STARBOARD)
// All path should be UTF8 above Starboard.
SbFileError error;
bool out_created;
starboard::ScopedFile sb_file(file_path.AsUTF8Unsafe().c_str(),
kSbFileCreateAlways | kSbFileWrite,
&out_created, &error);

int bytes_written = sb_file.WriteAll(contents.data(), contents.size());
if (bytes_written == contents.size()) {
#else
if (!ImportantFileWriter::WriteFileAtomically(file_path, contents)) {
#endif
LOG(ERROR) << "Persistent histograms fail to write to file: "
<< file_path.value();
}
Expand Down
2 changes: 2 additions & 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 Expand Up @@ -98,6 +99,7 @@ static_library("base") {
deps = [
"//base:i18n",
"//starboard/common",
"//starboard_stats",
"//third_party/icu",
"//third_party/libxml",
"//url",
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_stats/stats_tracker.h"

class StarboardStatsTracker : public starboard_stats::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 @@ -203,6 +203,7 @@ static_library("browser") {
"//nb",
"//net",
"//starboard",
"//starboard_stats",
"//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"
#if defined(COBALT_ENABLE_VERSION_COMPATIBILITY_VALIDATIONS)
#include "cobalt/base/version_compatibility.h"
Expand Down Expand Up @@ -85,6 +86,7 @@
#include "starboard/extension/installation_manager.h"
#include "starboard/system.h"
#include "starboard/time.h"
#include "starboard_stats/stats_tracker.h"
#include "url/gurl.h"

#if SB_IS(EVERGREEN)
Expand Down Expand Up @@ -693,6 +695,9 @@ Application::Application(const base::Closure& quit_closure, bool should_preload,
// URLRequestContext;
base::TaskScheduler::CreateAndStartWithDefaultParams("Cobalt TaskScheduler");

starboard_stats::StatsTrackerContainer::GetInstance()->set_stats_tracker(
std::make_unique<StarboardStatsTracker>());

// Initializes persistent settings.
persistent_settings_ =
std::make_unique<persistent_storage::PersistentSettings>(
Expand Down
Loading

0 comments on commit b8c6180

Please sign in to comment.