Skip to content

Commit

Permalink
Bug 1620145: Part 2 - Add AndroidProcessPriority.cpp to hal; r=gsvelt…
Browse files Browse the repository at this point in the history
…o,geckoview-reviewers,snorp

This patch is pretty straightforward: it translates Gecko priority levels
into GeckoView priority levels and then sends it up to GV's
`GeckoProcessManager` via JNI.

We do assume that the process is content, but if we try to do that on a
non-content process, it's just a no-op. We can expand this coverage to other
process types later as necessary.

Differential Revision: https://phabricator.services.mozilla.com/D68418
  • Loading branch information
dblohm7 committed Mar 27, 2020
1 parent d902162 commit a1eb2d5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions hal/android/AndroidProcessPriority.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "Hal.h"

#include "GeneratedJNIWrappers.h"

/**
* Bucket the Gecko HAL process priority level into one of the three Android
* priority levels.
*/
static mozilla::java::ServiceAllocator::PriorityLevel::LocalRef
ToJavaPriorityLevel(const ProcessPriority aPriority) {
if (aPriority >= PROCESS_PRIORITY_FOREGROUND) {
return mozilla::java::ServiceAllocator::PriorityLevel::FOREGROUND();
} else if (aPriority <= PROCESS_PRIORITY_PREALLOC &&
aPriority >= PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
return mozilla::java::ServiceAllocator::PriorityLevel::BACKGROUND();
}

return mozilla::java::ServiceAllocator::PriorityLevel::IDLE();
}

namespace mozilla {
namespace hal_impl {

bool SetProcessPrioritySupported() { return true; }

void SetProcessPriority(int aPid, ProcessPriority aPriority) {
if (aPriority == PROCESS_PRIORITY_MASTER) {
// This is the parent process itself, which we do not control.
return;
}

const int32_t intPriority = static_cast<int32_t>(aPriority);
if (intPriority < 0 || intPriority >= NUM_PROCESS_PRIORITY) {
return;
}

auto contentProcType = java::GeckoProcessType::CONTENT();
auto selector =
java::GeckoProcessManager::Selector::New(contentProcType, aPid);
auto priorityLevel = ToJavaPriorityLevel(aPriority);

// To Android, a lower-valued integer is a higher relative priority.
// We take the integer value of the enum and subtract it from the value
// of the highest Gecko priority level to obtain a 0-based indicator of
// the relative priority within the Java PriorityLevel.
const int32_t relativeImportance =
(static_cast<int32_t>(NUM_PROCESS_PRIORITY) - 1) - intPriority;

java::GeckoProcessManager::SetProcessPriority(selector, priorityLevel,
relativeImportance);
}

} // namespace hal_impl
} // namespace mozilla
2 changes: 1 addition & 1 deletion hal/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
'/widget/android',
]
UNIFIED_SOURCES += [
'android/AndroidProcessPriority.cpp',
'android/AndroidSensor.cpp',
'fallback/FallbackProcessPriority.cpp',
]
# AndroidHal.cpp cannot be built in unified mode because it relies on HalImpl.h.
SOURCES += [
Expand Down

0 comments on commit a1eb2d5

Please sign in to comment.