Skip to content

Commit

Permalink
Cherry pick PR #3340: Add slot selection status to loader app metrics (
Browse files Browse the repository at this point in the history
…#3392)

Refer to the original PR: #3340

b/331208101

Change-Id: Ic17c4e47aaa5c1c23869e0ce8d5a7a3e69d763ae

Co-authored-by: yuying-y <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and yuying-y authored Jun 1, 2024
1 parent e39b52c commit 5429e94
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 24 deletions.
7 changes: 7 additions & 0 deletions cobalt/browser/loader_app_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ void RecordLoaderAppMetrics(
RecordLoaderAppTimeMetrics(metrics_extension);
RecordLoaderAppSpaceMetrics(metrics_extension);
}
if (metrics_extension->version >= 3) {
base::UmaHistogramEnumeration(
"Cobalt.LoaderApp.SlotSelectionStatus",
metrics_extension->GetSlotSelectionStatus());
LOG(INFO) << "Recorded sample for "
<< "Cobalt.LoaderApp.SlotSelectionStatus";
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ TEST(ExtensionTest, LoaderAppMetrics) {

EXPECT_STREQ(extension_api->name, kExtensionName);
EXPECT_GE(extension_api->version, 1u);
EXPECT_LE(extension_api->version, 2u);
EXPECT_LE(extension_api->version, 3u);
EXPECT_NE(extension_api->SetCrashpadInstallationStatus, nullptr);
EXPECT_NE(extension_api->GetCrashpadInstallationStatus, nullptr);

Expand All @@ -535,6 +535,11 @@ TEST(ExtensionTest, LoaderAppMetrics) {
EXPECT_NE(extension_api->GetMaxSampledUsedCpuBytesDuringElfLoad, nullptr);
}

if (extension_api->version >= 3) {
EXPECT_NE(extension_api->SetSlotSelectionStatus, nullptr);
EXPECT_NE(extension_api->GetSlotSelectionStatus, nullptr);
}

const ExtensionApi* second_extension_api =
static_cast<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
EXPECT_EQ(second_extension_api, extension_api)
Expand Down
24 changes: 23 additions & 1 deletion starboard/extension/loader_app_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
// numeric values should never be reused. Must be kept in sync with the
// corresponding definition in
// tools/metrics/histograms/metadata/cobalt/enums.xml.
typedef enum CrashpadInstallationStatus {
typedef enum class CrashpadInstallationStatus {
// The enumerators below this point were added in version 1 or later.
kUnknown = 0,
kSucceeded = 1,
Expand All @@ -38,6 +38,23 @@ typedef enum CrashpadInstallationStatus {
kMaxValue = kFailedSignalHandlerInstallationFailed
} CrashpadInstallationStatus;

typedef enum class SlotSelectionStatus {
// The enumerators below this point were added in version 3 or later.
kUnknown = 0,
kCurrentSlot = 1,
kRollForward = 2,
kRollBackOutOfRetries = 3,
kRollBackNoLibFile = 4,
kRollBackBadAppKeyFile = 5,
kRollBackSlotDraining = 6,
kRollBackFailedToAdopt = 7,
kRollBackFailedToLoadCobalt = 8,
kRollBackFailedToCheckSabi = 9,
kRollBackFailedToLookUpSymbols = 10,
kEGLite = 11,
kMaxValue = kEGLite,
} SlotSelectionStatus;

typedef struct StarboardExtensionLoaderAppMetricsApi {
// Name should be the string |kStarboardExtensionLoaderAppMetricsName|.
// This helps to validate that the extension API is correct.
Expand Down Expand Up @@ -87,6 +104,11 @@ typedef struct StarboardExtensionLoaderAppMetricsApi {
// using RecordUsedCpuBytesDuringElfLoad(), or -1 if no value was recorded.
int64_t (*GetMaxSampledUsedCpuBytesDuringElfLoad)();

// The fields below this point were added in version 3 or later.

void (*SetSlotSelectionStatus)(SlotSelectionStatus status);
SlotSelectionStatus (*GetSlotSelectionStatus)();

} StarboardExtensionLoaderAppMetricsApi;

#ifdef __cplusplus
Expand Down
10 changes: 10 additions & 0 deletions starboard/loader_app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (sb_is_evergreen_compatible && current_toolchain == starboard_toolchain) {
":app_key",
":installation_manager",
":memory_tracker_thread",
":record_loader_app_status",
":reset_evergreen_update",
":slot_management",
"//starboard:starboard_group",
Expand Down Expand Up @@ -77,6 +78,14 @@ if (sb_is_evergreen_compatible && sb_evergreen_compatible_package &&
}
}

static_library("record_loader_app_status") {
sources = [
"record_loader_app_status.cc",
"record_loader_app_status.h",
]
deps = [ "//starboard:starboard_group" ]
}

if (sb_is_evergreen_compatible && current_toolchain == starboard_toolchain) {
target(starboard_level_final_executable_type, "loader_app") {
build_loader = false
Expand Down Expand Up @@ -246,6 +255,7 @@ static_library("installation_manager") {
deps = [
":installation_store_proto",
":pending_restart",
":record_loader_app_status",
"//starboard:starboard_group",
]
}
Expand Down
19 changes: 14 additions & 5 deletions starboard/loader_app/installation_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#include "starboard/common/string.h"
#include "starboard/configuration_constants.h"
#include "starboard/directory.h"
#include "starboard/extension/loader_app_metrics.h"
#include "starboard/file.h"
#include "starboard/loader_app/installation_store.pb.h"
#if !SB_IS(EVERGREEN_COMPATIBLE_LITE)
#include "starboard/loader_app/pending_restart.h" // nogncheck
#endif // !SB_IS(EVERGREEN_COMPATIBLE_LITE)
#include "starboard/common/once.h"
#include "starboard/loader_app/record_loader_app_status.h"
#include "starboard/string.h"

namespace starboard {
Expand All @@ -54,7 +56,7 @@ class InstallationManager {
int RollForward(int installation_index);
int DecrementInstallationNumTries(int installation_index);

int RevertToSuccessfulInstallation();
int RevertToSuccessfulInstallation(SlotSelectionStatus status);
int GetInstallationPath(int installation_index, char* path, int path_length);
int GetCurrentInstallationIndex();
int MarkInstallationSuccessful(int installation_index);
Expand Down Expand Up @@ -299,7 +301,8 @@ int InstallationManager::DecrementInstallationNumTries(int installation_index) {
// [x] => [ ]
// low [ ] [-]
//
int InstallationManager::RevertToSuccessfulInstallation() {
int InstallationManager::RevertToSuccessfulInstallation(
SlotSelectionStatus status) {
if (!initialized_) {
SB_LOG(ERROR) << "RevertToSuccessfulInstallation: not initialized";
return IM_ERROR;
Expand Down Expand Up @@ -347,6 +350,7 @@ int InstallationManager::RevertToSuccessfulInstallation() {
<< DumpInstallationSlots();

if (SaveInstallationStore()) {
RecordSlotSelectionStatus(status);
return fallback_installation;
}
return IM_ERROR;
Expand Down Expand Up @@ -418,7 +422,12 @@ int InstallationManager::RollForwardInternal(int installation_index) {
current_installation_ = installation_index;

SB_DLOG(INFO) << "RollForwardInternal: " << DumpInstallationSlots();
return SaveInstallationStore() ? IM_SUCCESS : IM_ERROR;

if (SaveInstallationStore()) {
RecordSlotSelectionStatus(SlotSelectionStatus::kRollForward);
return IM_SUCCESS;
}
return IM_ERROR;
}

// Shift the priority in the inclusive range either up or down based
Expand Down Expand Up @@ -831,9 +840,9 @@ int ImRollForward(int installation_index) {
return g_installation_manager_->RollForward(installation_index);
}

int ImRevertToSuccessfulInstallation() {
int ImRevertToSuccessfulInstallation(SlotSelectionStatus status) {
starboard::ScopedLock lock(*GetImMutex());
return g_installation_manager_->RevertToSuccessfulInstallation();
return g_installation_manager_->RevertToSuccessfulInstallation(status);
}

int ImRequestRollForwardToInstallation(int installation_index) {
Expand Down
4 changes: 3 additions & 1 deletion starboard/loader_app/installation_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_LOADER_APP_INSTALLATION_MANAGER_H_
#define STARBOARD_LOADER_APP_INSTALLATION_MANAGER_H_

#include "starboard/extension/loader_app_metrics.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -122,7 +124,7 @@ int ImRollForward(int installation_index);
// Revert to a previous successful installation.
// Returns the installation to which it was reverted.
// Returns |IM_ERROR| on error.
int ImRevertToSuccessfulInstallation();
int ImRevertToSuccessfulInstallation(SlotSelectionStatus status);

// Request the installation at |installation_index| to be rolled
// forward next time the Loader App tries to load the app.
Expand Down
9 changes: 6 additions & 3 deletions starboard/loader_app/installation_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "starboard/common/file.h"
#include "starboard/configuration_constants.h"
#include "starboard/extension/loader_app_metrics.h"
#include "starboard/loader_app/installation_store.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -146,7 +147,8 @@ class InstallationManagerTest : public ::testing::TestWithParam<int> {
SaveStorageState(installation_store);

ASSERT_EQ(IM_SUCCESS, ImInitialize(max_num_installations, kAppKey));
int result = ImRevertToSuccessfulInstallation();
int result =
ImRevertToSuccessfulInstallation(SlotSelectionStatus::kUnknown);
if (!expected_succeed) {
ASSERT_EQ(IM_ERROR, result);
}
Expand Down Expand Up @@ -398,7 +400,7 @@ TEST_P(InstallationManagerTest, RevertToSuccessfulInstallation) {
ASSERT_EQ(1, ImGetCurrentInstallationIndex());
ASSERT_EQ(IM_SUCCESS, ImMarkInstallationSuccessful(1));
ASSERT_EQ(1, ImGetCurrentInstallationIndex());
ASSERT_EQ(0, ImRevertToSuccessfulInstallation());
ASSERT_EQ(0, ImRevertToSuccessfulInstallation(SlotSelectionStatus::kUnknown));
ASSERT_EQ(0, ImGetCurrentInstallationIndex());
}

Expand All @@ -410,7 +412,8 @@ TEST_F(InstallationManagerTest, InvalidInput) {
ASSERT_EQ(IM_INSTALLATION_STATUS_ERROR, ImGetInstallationStatus(10));
ASSERT_EQ(IM_SUCCESS, ImMarkInstallationSuccessful(0));
ASSERT_EQ(IM_INSTALLATION_STATUS_ERROR, ImGetInstallationStatus(-2));
ASSERT_EQ(IM_ERROR, ImRevertToSuccessfulInstallation());
ASSERT_EQ(IM_ERROR,
ImRevertToSuccessfulInstallation(SlotSelectionStatus::kUnknown));
ASSERT_EQ(IM_ERROR, ImMarkInstallationSuccessful(10));
ASSERT_EQ(IM_ERROR, ImMarkInstallationSuccessful(-2));
ASSERT_EQ(IM_ERROR, ImDecrementInstallationNumTries(10));
Expand Down
4 changes: 4 additions & 0 deletions starboard/loader_app/loader_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#include "starboard/elf_loader/evergreen_info.h"
#include "starboard/elf_loader/sabi_string.h"
#include "starboard/event.h"
#include "starboard/extension/loader_app_metrics.h"
#include "starboard/file.h"
#include "starboard/loader_app/app_key.h"
#include "starboard/loader_app/loader_app_switches.h"
#include "starboard/loader_app/memory_tracker_thread.h"
#include "starboard/loader_app/record_loader_app_status.h"
#include "starboard/loader_app/reset_evergreen_update.h"
#include "starboard/loader_app/slot_management.h"
#include "starboard/loader_app/system_get_extension_shim.h"
Expand Down Expand Up @@ -259,6 +261,8 @@ void SbEventHandle(const SbEvent* event) {
}

if (is_evergreen_lite) {
starboard::loader_app::RecordSlotSelectionStatus(
SlotSelectionStatus::kEGLite);
LoadLibraryAndInitialize(alternative_content, use_memory_mapped_file);
} else {
std::string url =
Expand Down
37 changes: 37 additions & 0 deletions starboard/loader_app/record_loader_app_status.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 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/loader_app/record_loader_app_status.h"

#include <cstring>

#include "starboard/system.h"

namespace starboard {
namespace loader_app {

void RecordSlotSelectionStatus(SlotSelectionStatus status) {
auto metrics_extension =
static_cast<const StarboardExtensionLoaderAppMetricsApi*>(
SbSystemGetExtension(kStarboardExtensionLoaderAppMetricsName));
if (metrics_extension &&
strcmp(metrics_extension->name,
kStarboardExtensionLoaderAppMetricsName) == 0 &&
metrics_extension->version >= 3) {
metrics_extension->SetSlotSelectionStatus(status);
}
}

} // namespace loader_app
} // namespace starboard
31 changes: 31 additions & 0 deletions starboard/loader_app/record_loader_app_status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 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_LOADER_APP_RECORD_LOADER_APP_STATUS_H_
#define STARBOARD_LOADER_APP_RECORD_LOADER_APP_STATUS_H_

#include <string>

#include "starboard/extension/loader_app_metrics.h"

namespace starboard {
namespace loader_app {

// Persist the slot selection status so that it can be read in the Cobalt layer
void RecordSlotSelectionStatus(SlotSelectionStatus status);

} // namespace loader_app
} // namespace starboard

#endif // STARBOARD_LOADER_APP_RECORD_LOADER_APP_STATUS_H_
Loading

0 comments on commit 5429e94

Please sign in to comment.