Bug 802423 - Updater needs to set oom_adj and nice appropriately. e=ehsan a=b2g-only

This commit is contained in:
Dave Hylands 2013-01-21 12:22:16 -08:00
parent 15cbfdf697
commit 2bae95797b
2 changed files with 72 additions and 2 deletions

View File

@ -84,6 +84,14 @@ void LaunchMacPostProcess(const char* aAppExe);
# include "automounter_gonk.h"
# include <unistd.h>
# include <android/log.h>
# include <linux/ioprio.h>
# include <sys/resource.h>
// The only header file in bionic which has a function prototype for ioprio_set
// is libc/include/sys/linux-unistd.h. However, linux-unistd.h conflicts
// badly with unistd.h, so we declare the prototype for ioprio_set directly.
extern "C" int ioprio_set(int which, int who, int ioprio);
# define MAYBE_USE_HARD_LINKS 1
static bool sUseHardLinks = true;
#else
@ -2401,6 +2409,35 @@ int NS_main(int argc, NS_tchar **argv)
LOG(("Performing a replace request"));
}
#ifdef MOZ_WIDGET_GONK
const char *prioEnv = getenv("MOZ_UPDATER_PRIO");
if (prioEnv) {
int32_t prioVal;
int32_t oomScoreAdj;
int32_t ioprioClass;
int32_t ioprioLevel;
if (sscanf(prioEnv, "%d/%d/%d/%d",
&prioVal, &oomScoreAdj, &ioprioClass, &ioprioLevel) == 4) {
LOG(("MOZ_UPDATER_PRIO=%s", prioEnv));
if (setpriority(PRIO_PROCESS, 0, prioVal)) {
LOG(("setpriority(%d) failed, errno = %d", prioVal, errno));
}
if (ioprio_set(IOPRIO_WHO_PROCESS, 0,
IOPRIO_PRIO_VALUE(ioprioClass, ioprioLevel))) {
LOG(("ioprio_set(%d,%d) failed: errno = %d",
ioprioClass, ioprioLevel, errno));
}
FILE *fs = fopen("/proc/self/oom_score_adj", "w");
if (fs) {
fprintf(fs, "%d", oomScoreAdj);
fclose(fs);
} else {
LOG(("Unable to open /proc/self/oom_score_adj for writing, errno = %d", errno));
}
}
}
#endif
#ifdef XP_WIN
int possibleWriteError; // Variable holding one of the errors 46-48
if (pid > 0) {

View File

@ -24,6 +24,8 @@
#include "nsThreadUtils.h"
#include "nsIXULAppInfo.h"
#include "mozilla/Preferences.h"
#include "nsPrintfCString.h"
#include "mozilla/DebugOnly.h"
#ifdef XP_MACOSX
#include "nsILocalFileMac.h"
@ -46,6 +48,8 @@
# include <unistd.h>
#endif
using namespace mozilla;
//
// We use execv to spawn the updater process on all UNIX systems except Mac OSX
// since it is known to cause problems on the Mac. Windows has execv, but it
@ -96,8 +100,20 @@ static const char kUpdaterPNG[] = "updater.png";
#endif
#if defined(MOZ_WIDGET_GONK)
#include <linux/ioprio.h>
static const int kB2GServiceArgc = 2;
static const char *kB2GServiceArgv[] = { "/system/bin/start", "b2g" };
static const char kAppUpdaterPrio[] = "app.update.updater.prio";
static const char kAppUpdaterOomScoreAdj[] = "app.update.updater.oom_score_adj";
static const char kAppUpdaterIOPrioClass[] = "app.update.updater.ioprio.class";
static const char kAppUpdaterIOPrioLevel[] = "app.update.updater.ioprio.level";
static const int kAppUpdaterPrioDefault = 19; // -20..19 where 19 = lowest priority
static const int kAppUpdaterOomScoreAdjDefault = -1000; // -1000 = Never kill
static const int kAppUpdaterIOPrioClassDefault = IOPRIO_CLASS_IDLE;
static const int kAppUpdaterIOPrioLevelDefault = 0; // Doesn't matter for CLASS IDLE
#endif
static nsresult
@ -579,8 +595,8 @@ GetOSApplyToDir(nsACString& applyToDir)
NS_ASSERTION(ds, "Can't get directory service");
nsCOMPtr<nsIFile> osApplyToDir;
nsresult rv = ds->Get(XRE_OS_UPDATE_APPLY_TO_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(osApplyToDir));
DebugOnly<nsresult> rv = ds->Get(XRE_OS_UPDATE_APPLY_TO_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(osApplyToDir));
NS_ASSERTION(NS_SUCCEEDED(rv), "Can't get the OS applyTo dir");
return osApplyToDir->GetNativePath(applyToDir);
@ -818,6 +834,23 @@ ApplyUpdate(nsIFile *greDir, nsIFile *updateDir, nsIFile *statusFile,
if (isOSUpdate) {
PR_SetEnv("MOZ_OS_UPDATE=1");
}
#if defined(MOZ_WIDGET_GONK)
// We want the updater to be CPU friendly and not subject to being killed by
// the low memory killer, so we pass in some preferences to allow it to
// adjust its priority.
int32_t prioVal = Preferences::GetInt(kAppUpdaterPrio,
kAppUpdaterPrioDefault);
int32_t oomScoreAdj = Preferences::GetInt(kAppUpdaterOomScoreAdj,
kAppUpdaterOomScoreAdjDefault);
int32_t ioprioClass = Preferences::GetInt(kAppUpdaterIOPrioClass,
kAppUpdaterIOPrioClassDefault);
int32_t ioprioLevel = Preferences::GetInt(kAppUpdaterIOPrioLevel,
kAppUpdaterIOPrioLevelDefault);
nsPrintfCString prioEnv("MOZ_UPDATER_PRIO=%d/%d/%d/%d",
prioVal, oomScoreAdj, ioprioClass, ioprioLevel);
PR_SetEnv(prioEnv.get());
#endif
LOG(("spawning updater process [%s]\n", updaterPath.get()));