Bug 980027: Part 1: Provide mechanism to set thread priority via HAL. r=gsvelto

This commit is contained in:
Ben Kelly 2014-03-17 11:52:42 -04:00
parent 5b39187328
commit 07881c0521
7 changed files with 137 additions and 0 deletions

View File

@ -871,6 +871,12 @@ SetProcessPriority(int aPid,
aBackgroundLRU));
}
void
SetCurrentThreadPriority(ThreadPriority aPriority)
{
PROXY_IF_SANDBOXED(SetCurrentThreadPriority(aPriority));
}
// From HalTypes.h.
const char*
ProcessPriorityToString(ProcessPriority aPriority)
@ -898,6 +904,18 @@ ProcessPriorityToString(ProcessPriority aPriority)
}
}
const char *
ThreadPriorityToString(ThreadPriority aPriority)
{
switch (aPriority) {
case THREAD_PRIORITY_COMPOSITOR:
return "COMPOSITOR";
default:
MOZ_ASSERT(false);
return "???";
}
}
// From HalTypes.h.
const char*
ProcessPriorityToString(ProcessPriority aPriority,

View File

@ -496,6 +496,13 @@ void SetProcessPriority(int aPid,
hal::ProcessCPUPriority aCPUPriority,
uint32_t aLRU = 0);
/**
* Set the current thread's priority to appropriate platform-specific value for
* given functionality. Instead of providing arbitrary priority numbers you
* must specify a type of function like THREAD_PRIORITY_COMPOSITOR.
*/
void SetCurrentThreadPriority(hal::ThreadPriority aPriority);
/**
* Register an observer for the FM radio.
*/

View File

@ -99,6 +99,16 @@ enum ProcessCPUPriority {
NUM_PROCESS_CPU_PRIORITY
};
// Values that can be passed to hal::SetThreadPriority(). These should be
// functional in nature, such as COMPOSITOR, instead of levels, like LOW/HIGH.
// This allows us to tune our priority scheme for the system in one place such
// that it makes sense holistically for the overall operating system. On gonk
// or android we may want different priority schemes than on windows, etc.
enum ThreadPriority {
THREAD_PRIORITY_COMPOSITOR,
NUM_THREAD_PRIORITY
};
// Convert a ProcessPriority enum value (with an optional ProcessCPUPriority)
// to a string. The strings returned by this function are statically
// allocated; do not attempt to free one!
@ -112,6 +122,14 @@ const char*
ProcessPriorityToString(ProcessPriority aPriority,
ProcessCPUPriority aCPUPriority);
// Convert a ThreadPriority enum value to a string. The strings returned by
// this function are statically allocated; do not attempt to free one!
//
// If you pass an unknown process priority (or NUM_THREAD_PRIORITY), we
// fatally assert in debug builds and otherwise return "???".
const char *
ThreadPriorityToString(ThreadPriority aPriority);
/**
* Used by ModifyWakeLock
*/

View File

@ -0,0 +1,20 @@
/* 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 http://mozilla.org/MPL/2.0/. */
#include "Hal.h"
using namespace mozilla::hal;
namespace mozilla {
namespace hal_impl {
void
SetCurrentThreadPriority(ThreadPriority aPriority)
{
HAL_LOG(("FallbackThreadPriority - SetCurrentThreadPriority(%d)\n",
ThreadPriorityToString(aPriority)));
}
} // hal_impl
} // namespace mozilla

View File

@ -27,6 +27,7 @@
#include <sys/resource.h>
#include <time.h>
#include <asm/page.h>
#include <sched.h>
#include "mozilla/DebugOnly.h"
@ -38,6 +39,7 @@
#include "hardware_legacy/vibrator.h"
#include "hardware_legacy/power.h"
#include "libdisplay/GonkDisplay.h"
#include "utils/threads.h"
#include "base/message_loop.h"
@ -1348,6 +1350,14 @@ SetNiceForPid(int aPid, int aNice)
int tid = static_cast<int>(tidlong);
// Do not set the priority of threads running with a real-time policy
// as part of the bulk process adjustment. These threads need to run
// at their specified priority in order to meet timing guarantees.
int schedPolicy = sched_getscheduler(tid);
if (schedPolicy == SCHED_FIFO || schedPolicy == SCHED_RR) {
continue;
}
errno = 0;
// Get and set the task's new priority.
int origtaskpriority = getpriority(PRIO_PROCESS, tid);
@ -1360,6 +1370,15 @@ SetNiceForPid(int aPid, int aNice)
int newtaskpriority =
std::max(origtaskpriority - origProcPriority + aNice, aNice);
// Do not reduce priority of threads already running at priorities greater
// than normal. These threads are likely special service threads that need
// elevated priorities to process audio, display composition, etc.
if (newtaskpriority > origtaskpriority &&
origtaskpriority < ANDROID_PRIORITY_NORMAL) {
continue;
}
rv = setpriority(PRIO_PROCESS, tid, newtaskpriority);
if (rv) {
@ -1454,6 +1473,54 @@ SetProcessPriority(int aPid,
}
}
void
SetCurrentThreadPriority(ThreadPriority aPriority)
{
int policy = SCHED_OTHER;
int priorityOrNice = ANDROID_PRIORITY_NORMAL;
switch(aPriority) {
case THREAD_PRIORITY_COMPOSITOR:
priorityOrNice = Preferences::GetInt("hal.gonk.compositor.rt_priority", 0);
if (priorityOrNice >= sched_get_priority_min(SCHED_FIFO) &&
priorityOrNice <= sched_get_priority_max(SCHED_FIFO)) {
policy = SCHED_FIFO;
} else {
priorityOrNice = Preferences::GetInt("hal.gonk.compositor.nice",
ANDROID_PRIORITY_URGENT_DISPLAY);
}
break;
default:
LOG("Unrecognized thread priority %d; Doing nothing", aPriority);
return;
}
int tid = gettid();
int rv = 0;
// If a RT scheduler policy is used, then we must set the priority using
// sched_setscheduler() and the sched_param.sched_priority value.
if (policy == SCHED_FIFO || policy == SCHED_RR) {
LOG("Setting thread %d to priority level %s; RT priority %d",
tid, ThreadPriorityToString(aPriority), priorityOrNice);
sched_param schedParam;
schedParam.sched_priority = priorityOrNice;
rv = sched_setscheduler(tid, policy, &schedParam);
// Otherwise priority is solely defined by the nice level, so use the
// setpriority() function.
} else {
LOG("Setting thread %d to priority level %s; nice level %d",
tid, ThreadPriorityToString(aPriority), priorityOrNice);
rv = setpriority(PRIO_PROCESS, tid, priorityOrNice);
}
if (rv) {
LOG("Failed to set thread %d to priority level %s; error code %d",
tid, ThreadPriorityToString(aPriority), rv);
}
}
void
FactoryReset()
{

View File

@ -151,6 +151,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] != 'gonk':
'fallback/FallbackProcessPriority.cpp',
'fallback/FallbackScreenPower.cpp',
'fallback/FallbackSwitch.cpp',
'fallback/FallbackThreadPriority.cpp',
'fallback/FallbackTime.cpp',
'fallback/FallbackWakeLocks.cpp',
]

View File

@ -362,6 +362,12 @@ SetProcessPriority(int aPid,
NS_RUNTIMEABORT("Only the main process may set processes' priorities.");
}
void
SetCurrentThreadPriority(ThreadPriority aPriority)
{
NS_RUNTIMEABORT("Only the main process may set thread priorities.");
}
void
EnableFMRadio(const hal::FMRadioSettings& aSettings)
{