2011-12-20 23:31:53 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et ft=cpp : */
|
2012-07-20 13:10:44 +04:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2014-01-11 10:22:55 +04:00
|
|
|
#include <ctype.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-07-06 14:42:10 +04:00
|
|
|
#include <linux/android_alarm.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
#include <math.h>
|
2014-01-11 10:22:55 +04:00
|
|
|
#include <regex.h>
|
2014-05-05 19:37:00 +04:00
|
|
|
#include <sched.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
#include <stdio.h>
|
2014-01-11 10:22:55 +04:00
|
|
|
#include <sys/klog.h>
|
2015-01-08 17:39:43 +03:00
|
|
|
#include <sys/stat.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
#include <sys/syscall.h>
|
2012-08-05 09:09:39 +04:00
|
|
|
#include <sys/resource.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
#include <time.h>
|
2014-12-17 19:20:09 +03:00
|
|
|
#include <unistd.h>
|
2012-05-09 01:36:07 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2012-05-09 01:36:07 +04:00
|
|
|
#include "android/log.h"
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#include "hardware/hardware.h"
|
|
|
|
#include "hardware/lights.h"
|
2011-12-20 23:31:53 +04:00
|
|
|
#include "hardware_legacy/uevent.h"
|
2012-05-09 01:36:07 +04:00
|
|
|
#include "hardware_legacy/vibrator.h"
|
2012-05-10 01:30:22 +04:00
|
|
|
#include "hardware_legacy/power.h"
|
2013-05-08 07:58:22 +04:00
|
|
|
#include "libdisplay/GonkDisplay.h"
|
2014-05-05 19:37:00 +04:00
|
|
|
#include "utils/threads.h"
|
2012-05-09 01:36:07 +04:00
|
|
|
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
#include "Hal.h"
|
2011-12-24 09:03:55 +04:00
|
|
|
#include "HalImpl.h"
|
2014-09-24 17:23:18 +04:00
|
|
|
#include "HalLog.h"
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2015-01-08 17:39:43 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2011-12-20 23:31:53 +04:00
|
|
|
#include "mozilla/dom/battery/Constants.h"
|
2014-12-18 02:05:28 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2011-12-20 23:31:53 +04:00
|
|
|
#include "mozilla/FileUtils.h"
|
2011-12-22 05:58:29 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2011-12-22 05:58:29 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-08-29 19:32:56 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-05-09 01:36:07 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2012-08-05 09:09:39 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2011-12-22 05:58:29 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2012-10-04 13:28:34 +04:00
|
|
|
#include "nsIRecoveryService.h"
|
2012-05-09 01:36:07 +04:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsScreenManagerGonk.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIThread.h"
|
2012-04-11 11:59:00 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-04-20 15:43:25 +04:00
|
|
|
#include "OrientationObserver.h"
|
2012-04-11 11:59:00 +04:00
|
|
|
#include "UeventPoller.h"
|
2014-05-29 17:12:00 +04:00
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2012-02-17 14:44:00 +04:00
|
|
|
|
2012-09-28 10:38:50 +04:00
|
|
|
#define NsecPerMsec 1000000LL
|
2012-02-17 14:44:00 +04:00
|
|
|
#define NsecPerSec 1000000000
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
// The header linux/oom.h is not available in bionic libc. We
|
|
|
|
// redefine some of its constants here.
|
|
|
|
|
|
|
|
#ifndef OOM_DISABLE
|
|
|
|
#define OOM_DISABLE (-17)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OOM_ADJUST_MIN
|
|
|
|
#define OOM_ADJUST_MIN (-16)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OOM_ADJUST_MAX
|
|
|
|
#define OOM_ADJUST_MAX 15
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OOM_SCORE_ADJ_MIN
|
|
|
|
#define OOM_SCORE_ADJ_MIN (-1000)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OOM_SCORE_ADJ_MAX
|
|
|
|
#define OOM_SCORE_ADJ_MAX 1000
|
|
|
|
#endif
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-10-22 07:10:05 +04:00
|
|
|
#ifndef BATTERY_CHARGING_ARGB
|
|
|
|
#define BATTERY_CHARGING_ARGB 0x00FF0000
|
|
|
|
#endif
|
|
|
|
#ifndef BATTERY_FULL_ARGB
|
|
|
|
#define BATTERY_FULL_ARGB 0x0000FF00
|
|
|
|
#endif
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::hal;
|
2014-07-18 05:30:47 +04:00
|
|
|
using namespace mozilla::dom;
|
2011-12-20 23:31:53 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
/**
|
|
|
|
* These are defined by libhardware, specifically, hardware/libhardware/include/hardware/lights.h
|
|
|
|
* in the gonk subsystem.
|
|
|
|
* If these change and are exposed to JS, make sure nsIHal.idl is updated as well.
|
|
|
|
*/
|
|
|
|
enum LightType {
|
|
|
|
eHalLightID_Backlight = 0,
|
|
|
|
eHalLightID_Keyboard = 1,
|
|
|
|
eHalLightID_Buttons = 2,
|
|
|
|
eHalLightID_Battery = 3,
|
|
|
|
eHalLightID_Notifications = 4,
|
|
|
|
eHalLightID_Attention = 5,
|
|
|
|
eHalLightID_Bluetooth = 6,
|
|
|
|
eHalLightID_Wifi = 7,
|
|
|
|
eHalLightID_Count // This should stay at the end
|
|
|
|
};
|
|
|
|
enum LightMode {
|
|
|
|
eHalLightMode_User = 0, // brightness is managed by user setting
|
|
|
|
eHalLightMode_Sensor = 1, // brightness is managed by a light sensor
|
|
|
|
eHalLightMode_Count
|
|
|
|
};
|
|
|
|
enum FlashMode {
|
|
|
|
eHalLightFlash_None = 0,
|
|
|
|
eHalLightFlash_Timed = 1, // timed flashing. Use flashOnMS and flashOffMS for timing
|
|
|
|
eHalLightFlash_Hardware = 2, // hardware assisted flashing
|
|
|
|
eHalLightFlash_Count
|
|
|
|
};
|
|
|
|
|
2014-06-11 14:17:23 +04:00
|
|
|
struct LightConfiguration {
|
2015-01-22 06:36:00 +03:00
|
|
|
LightType light;
|
|
|
|
LightMode mode;
|
|
|
|
FlashMode flash;
|
2014-06-11 14:17:23 +04:00
|
|
|
uint32_t flashOnMS;
|
|
|
|
uint32_t flashOffMS;
|
|
|
|
uint32_t color;
|
|
|
|
};
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
static light_device_t* sLights[eHalLightID_Count]; // will be initialized to nullptr
|
2014-06-11 14:17:23 +04:00
|
|
|
|
|
|
|
static light_device_t*
|
|
|
|
GetDevice(hw_module_t* module, char const* name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
hw_device_t* device;
|
|
|
|
err = module->methods->open(module, name, &device);
|
|
|
|
if (err == 0) {
|
|
|
|
return (light_device_t*)device;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitLights()
|
|
|
|
{
|
|
|
|
// assume that if backlight is nullptr, nothing has been set yet
|
|
|
|
// if this is not true, the initialization will occur everytime a light is read or set!
|
2015-01-22 06:36:00 +03:00
|
|
|
if (!sLights[eHalLightID_Backlight]) {
|
2014-06-11 14:17:23 +04:00
|
|
|
int err;
|
|
|
|
hw_module_t* module;
|
|
|
|
|
|
|
|
err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
|
|
|
|
if (err == 0) {
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Backlight]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_BACKLIGHT);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Keyboard]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_KEYBOARD);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Buttons]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_BUTTONS);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Battery]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_BATTERY);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Notifications]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_NOTIFICATIONS);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Attention]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_ATTENTION);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Bluetooth]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_BLUETOOTH);
|
2015-01-22 06:36:00 +03:00
|
|
|
sLights[eHalLightID_Wifi]
|
2014-06-11 14:17:23 +04:00
|
|
|
= GetDevice(module, LIGHT_ID_WIFI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state last set for the lights until liblights supports
|
|
|
|
* getting the light state.
|
|
|
|
*/
|
2015-01-22 06:36:00 +03:00
|
|
|
static light_state_t sStoredLightState[eHalLightID_Count];
|
2014-06-11 14:17:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a light to a particular color, with a specific flash pattern.
|
|
|
|
* light specifices which light. See Hal.idl for the list of constants
|
|
|
|
* mode specifies user set or based on ambient light sensor
|
|
|
|
* flash specifies whether or how to flash the light
|
|
|
|
* flashOnMS and flashOffMS specify the pattern for XXX flash mode
|
|
|
|
* color specifies the color. If the light doesn't support color, the given color is
|
|
|
|
* transformed into a brightness, or just an on/off if that is all the light is capable of.
|
|
|
|
* returns true if successful and false if failed.
|
|
|
|
*/
|
|
|
|
static bool
|
2015-01-22 06:36:00 +03:00
|
|
|
SetLight(LightType light, const LightConfiguration& aConfig)
|
2014-06-11 14:17:23 +04:00
|
|
|
{
|
|
|
|
light_state_t state;
|
|
|
|
|
|
|
|
InitLights();
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
if (light < 0 || light >= eHalLightID_Count ||
|
2014-06-11 14:17:23 +04:00
|
|
|
sLights[light] == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&state, 0, sizeof(light_state_t));
|
|
|
|
state.color = aConfig.color;
|
|
|
|
state.flashMode = aConfig.flash;
|
|
|
|
state.flashOnMS = aConfig.flashOnMS;
|
|
|
|
state.flashOffMS = aConfig.flashOffMS;
|
|
|
|
state.brightnessMode = aConfig.mode;
|
|
|
|
|
|
|
|
sLights[light]->set_light(sLights[light], &state);
|
|
|
|
sStoredLightState[light] = state;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET the value of a light returning a particular color, with a specific flash pattern.
|
|
|
|
* returns true if successful and false if failed.
|
|
|
|
*/
|
|
|
|
static bool
|
2015-01-22 06:36:00 +03:00
|
|
|
GetLight(LightType light, LightConfiguration* aConfig)
|
2014-06-11 14:17:23 +04:00
|
|
|
{
|
|
|
|
light_state_t state;
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
if (light < 0 || light >= eHalLightID_Count ||
|
2014-06-11 14:17:23 +04:00
|
|
|
sLights[light] == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&state, 0, sizeof(light_state_t));
|
|
|
|
state = sStoredLightState[light];
|
|
|
|
|
|
|
|
aConfig->light = light;
|
|
|
|
aConfig->color = state.color;
|
2015-01-22 06:36:00 +03:00
|
|
|
aConfig->flash = FlashMode(state.flashMode);
|
2014-06-11 14:17:23 +04:00
|
|
|
aConfig->flashOnMS = state.flashOnMS;
|
|
|
|
aConfig->flashOffMS = state.flashOffMS;
|
2015-01-22 06:36:00 +03:00
|
|
|
aConfig->mode = LightMode(state.brightnessMode);
|
2014-06-11 14:17:23 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-22 05:58:29 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This runnable runs for the lifetime of the program, once started. It's
|
|
|
|
* responsible for "playing" vibration patterns.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class VibratorRunnable final
|
2011-12-22 05:58:29 +04:00
|
|
|
: public nsIRunnable
|
|
|
|
, public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VibratorRunnable()
|
|
|
|
: mMonitor("VibratorRunnable")
|
|
|
|
, mIndex(0)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (!os) {
|
|
|
|
NS_WARNING("Could not get observer service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-20 10:34:07 +04:00
|
|
|
os->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
2012-09-14 22:52:07 +04:00
|
|
|
}
|
2012-09-20 10:34:07 +04:00
|
|
|
|
2013-07-19 06:23:31 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-12-22 05:58:29 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
// Run on the main thread, not the vibrator thread.
|
2012-05-25 11:18:30 +04:00
|
|
|
void Vibrate(const nsTArray<uint32_t> &pattern);
|
2011-12-22 05:58:29 +04:00
|
|
|
void CancelVibrate();
|
|
|
|
|
2012-09-20 10:34:07 +04:00
|
|
|
static bool ShuttingDown() { return sShuttingDown; }
|
|
|
|
|
2015-03-10 15:44:02 +03:00
|
|
|
protected:
|
|
|
|
~VibratorRunnable() {}
|
|
|
|
|
2011-12-22 05:58:29 +04:00
|
|
|
private:
|
|
|
|
Monitor mMonitor;
|
|
|
|
|
|
|
|
// The currently-playing pattern.
|
2012-05-25 11:18:30 +04:00
|
|
|
nsTArray<uint32_t> mPattern;
|
2011-12-22 05:58:29 +04:00
|
|
|
|
|
|
|
// The index we're at in the currently-playing pattern. If mIndex >=
|
|
|
|
// mPattern.Length(), then we're not currently playing anything.
|
2012-05-25 11:18:30 +04:00
|
|
|
uint32_t mIndex;
|
2011-12-22 05:58:29 +04:00
|
|
|
|
|
|
|
// Set to true in our shutdown observer. When this is true, we kill the
|
|
|
|
// vibrator thread.
|
2012-09-20 10:34:07 +04:00
|
|
|
static bool sShuttingDown;
|
2011-12-22 05:58:29 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(VibratorRunnable, nsIRunnable, nsIObserver);
|
2011-12-22 05:58:29 +04:00
|
|
|
|
2012-09-20 10:34:07 +04:00
|
|
|
bool VibratorRunnable::sShuttingDown = false;
|
|
|
|
|
2013-04-23 19:30:25 +04:00
|
|
|
static StaticRefPtr<VibratorRunnable> sVibratorRunnable;
|
2012-09-20 10:34:07 +04:00
|
|
|
|
2011-12-22 05:58:29 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VibratorRunnable::Run()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
// We currently assume that mMonitor.Wait(X) waits for X milliseconds. But in
|
|
|
|
// reality, the kernel might not switch to this thread for some time after the
|
|
|
|
// wait expires. So there's potential for some inaccuracy here.
|
|
|
|
//
|
|
|
|
// This doesn't worry me too much. Note that we don't even start vibrating
|
|
|
|
// immediately when VibratorRunnable::Vibrate is called -- we go through a
|
|
|
|
// condvar onto another thread. Better just to be chill about small errors in
|
|
|
|
// the timing here.
|
|
|
|
|
2012-09-20 10:34:07 +04:00
|
|
|
while (!sShuttingDown) {
|
2011-12-22 05:58:29 +04:00
|
|
|
if (mIndex < mPattern.Length()) {
|
2012-05-25 11:18:30 +04:00
|
|
|
uint32_t duration = mPattern[mIndex];
|
2011-12-22 05:58:29 +04:00
|
|
|
if (mIndex % 2 == 0) {
|
|
|
|
vibrator_on(duration);
|
|
|
|
}
|
|
|
|
mIndex++;
|
|
|
|
mMonitor.Wait(PR_MillisecondsToInterval(duration));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mMonitor.Wait();
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 23:51:15 +04:00
|
|
|
sVibratorRunnable = nullptr;
|
2011-12-22 05:58:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VibratorRunnable::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *data)
|
2011-12-22 05:58:29 +04:00
|
|
|
{
|
2011-12-22 05:58:29 +04:00
|
|
|
MOZ_ASSERT(strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
|
2011-12-22 05:58:29 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-09-20 10:34:07 +04:00
|
|
|
sShuttingDown = true;
|
2011-12-22 05:58:29 +04:00
|
|
|
mMonitor.Notify();
|
2012-09-20 10:34:07 +04:00
|
|
|
|
2011-12-22 05:58:29 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-25 11:18:30 +04:00
|
|
|
VibratorRunnable::Vibrate(const nsTArray<uint32_t> &pattern)
|
2011-12-22 05:58:29 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mPattern = pattern;
|
|
|
|
mIndex = 0;
|
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VibratorRunnable::CancelVibrate()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mPattern.Clear();
|
|
|
|
mPattern.AppendElement(0);
|
|
|
|
mIndex = 0;
|
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
void
|
2011-12-22 05:58:29 +04:00
|
|
|
EnsureVibratorThreadInitialized()
|
|
|
|
{
|
|
|
|
if (sVibratorRunnable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-20 10:34:07 +04:00
|
|
|
sVibratorRunnable = new VibratorRunnable();
|
2011-12-22 05:58:29 +04:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
NS_NewThread(getter_AddRefs(thread), sVibratorRunnable);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2011-12-20 23:31:53 +04:00
|
|
|
|
|
|
|
void
|
2012-05-25 11:18:30 +04:00
|
|
|
Vibrate(const nsTArray<uint32_t> &pattern, const hal::WindowIdentifier &)
|
2011-12-22 05:58:29 +04:00
|
|
|
{
|
2012-09-20 10:34:07 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (VibratorRunnable::ShuttingDown()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-22 05:58:29 +04:00
|
|
|
EnsureVibratorThreadInitialized();
|
|
|
|
sVibratorRunnable->Vibrate(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelVibrate(const hal::WindowIdentifier &)
|
|
|
|
{
|
2012-09-20 10:34:07 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (VibratorRunnable::ShuttingDown()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-22 05:58:29 +04:00
|
|
|
EnsureVibratorThreadInitialized();
|
|
|
|
sVibratorRunnable->CancelVibrate();
|
|
|
|
}
|
2011-12-20 23:31:53 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class BatteryUpdater : public nsRunnable {
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
hal::BatteryInformation info;
|
|
|
|
hal_impl::GetCurrentBatteryInformation(&info);
|
2012-10-22 07:10:05 +04:00
|
|
|
|
|
|
|
// Control the battery indicator (led light) here using BatteryInformation
|
|
|
|
// we just retrieved.
|
|
|
|
uint32_t color = 0; // Format: 0x00rrggbb.
|
|
|
|
if (info.charging() && (info.level() == 1)) {
|
|
|
|
// Charging and battery full.
|
|
|
|
color = BATTERY_FULL_ARGB;
|
|
|
|
} else if (info.charging() && (info.level() < 1)) {
|
|
|
|
// Charging but not full.
|
|
|
|
color = BATTERY_CHARGING_ARGB;
|
|
|
|
} // else turn off battery indicator.
|
|
|
|
|
2014-06-11 14:17:23 +04:00
|
|
|
LightConfiguration aConfig;
|
2015-01-22 06:36:00 +03:00
|
|
|
aConfig.light = eHalLightID_Battery;
|
|
|
|
aConfig.mode = eHalLightMode_User;
|
|
|
|
aConfig.flash = eHalLightFlash_None;
|
2014-06-11 14:17:23 +04:00
|
|
|
aConfig.flashOnMS = aConfig.flashOffMS = 0;
|
|
|
|
aConfig.color = color;
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
SetLight(eHalLightID_Battery, aConfig);
|
2012-10-22 07:10:05 +04:00
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
hal::NotifyBatteryChange(info);
|
2014-05-29 17:12:00 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
// bug 975667
|
|
|
|
// Gecko gonk hal is required to emit battery charging/level notification via nsIObserverService.
|
|
|
|
// This is useful for XPCOM components that are not statically linked to Gecko and cannot call
|
|
|
|
// hal::EnableBatteryNotifications
|
|
|
|
nsCOMPtr<nsIObserverService> obsService = mozilla::services::GetObserverService();
|
|
|
|
nsCOMPtr<nsIWritablePropertyBag2> propbag =
|
|
|
|
do_CreateInstance("@mozilla.org/hash-property-bag;1");
|
|
|
|
if (obsService && propbag) {
|
|
|
|
propbag->SetPropertyAsBool(NS_LITERAL_STRING("charging"),
|
|
|
|
info.charging());
|
|
|
|
propbag->SetPropertyAsDouble(NS_LITERAL_STRING("level"),
|
|
|
|
info.level());
|
|
|
|
|
|
|
|
obsService->NotifyObservers(propbag, "gonkhal-battery-notifier", nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-04-11 11:59:00 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class BatteryObserver final : public IUeventObserver
|
2012-04-11 11:59:00 +04:00
|
|
|
{
|
2011-12-20 23:31:53 +04:00
|
|
|
public:
|
2014-04-14 23:04:24 +04:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(BatteryObserver)
|
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
BatteryObserver()
|
|
|
|
:mUpdater(new BatteryUpdater())
|
2011-12-20 23:31:53 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
virtual void Notify(const NetlinkEvent &aEvent)
|
2011-12-20 23:31:53 +04:00
|
|
|
{
|
2012-04-11 11:59:00 +04:00
|
|
|
// this will run on IO thread
|
|
|
|
NetlinkEvent *event = const_cast<NetlinkEvent*>(&aEvent);
|
|
|
|
const char *subsystem = event->getSubsystem();
|
|
|
|
// e.g. DEVPATH=/devices/platform/sec-battery/power_supply/battery
|
|
|
|
const char *devpath = event->findParam("DEVPATH");
|
|
|
|
if (strcmp(subsystem, "power_supply") == 0 &&
|
|
|
|
strstr(devpath, "battery")) {
|
|
|
|
// aEvent will be valid only in this method.
|
|
|
|
NS_DispatchToMainThread(mUpdater);
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 15:44:02 +03:00
|
|
|
protected:
|
|
|
|
~BatteryObserver() {}
|
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BatteryUpdater> mUpdater;
|
2011-12-20 23:31:53 +04:00
|
|
|
};
|
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
// sBatteryObserver is owned by the IO thread. Only the IO thread may
|
|
|
|
// create or destroy it.
|
2012-08-29 19:32:56 +04:00
|
|
|
static StaticRefPtr<BatteryObserver> sBatteryObserver;
|
2012-04-11 11:59:00 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
RegisterBatteryObserverIOThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
MOZ_ASSERT(!sBatteryObserver);
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
sBatteryObserver = new BatteryObserver();
|
|
|
|
RegisterUeventListener(sBatteryObserver);
|
|
|
|
}
|
2011-12-20 23:31:53 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
EnableBatteryNotifications()
|
|
|
|
{
|
2012-04-11 11:59:00 +04:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(RegisterBatteryObserverIOThread));
|
|
|
|
}
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
static void
|
|
|
|
UnregisterBatteryObserverIOThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
|
|
|
|
MOZ_ASSERT(sBatteryObserver);
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-04-11 11:59:00 +04:00
|
|
|
UnregisterUeventListener(sBatteryObserver);
|
2013-08-23 23:51:15 +04:00
|
|
|
sBatteryObserver = nullptr;
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableBatteryNotifications()
|
|
|
|
{
|
2012-04-11 11:59:00 +04:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableFunction(UnregisterBatteryObserverIOThread));
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
static bool
|
|
|
|
GetCurrentBatteryCharge(int* aCharge)
|
|
|
|
{
|
|
|
|
bool success = ReadSysFile("/sys/class/power_supply/battery/capacity",
|
|
|
|
aCharge);
|
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if ((*aCharge < 0) || (*aCharge > 100)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("charge level contains unknown value: %d", *aCharge);
|
2012-11-29 23:18:46 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (*aCharge >= 0) && (*aCharge <= 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetCurrentBatteryCharging(int* aCharging)
|
2011-12-20 23:31:53 +04:00
|
|
|
{
|
2014-12-18 02:05:28 +03:00
|
|
|
static const DebugOnly<int> BATTERY_NOT_CHARGING = 0;
|
2012-02-15 22:10:15 +04:00
|
|
|
static const int BATTERY_CHARGING_USB = 1;
|
|
|
|
static const int BATTERY_CHARGING_AC = 2;
|
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
// Generic device support
|
|
|
|
|
|
|
|
int chargingSrc;
|
|
|
|
bool success =
|
|
|
|
ReadSysFile("/sys/class/power_supply/battery/charging_source", &chargingSrc);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (chargingSrc != BATTERY_NOT_CHARGING &&
|
|
|
|
chargingSrc != BATTERY_CHARGING_USB &&
|
|
|
|
chargingSrc != BATTERY_CHARGING_AC) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("charging_source contained unknown value: %d", chargingSrc);
|
2012-11-29 23:18:46 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*aCharging = (chargingSrc == BATTERY_CHARGING_USB ||
|
|
|
|
chargingSrc == BATTERY_CHARGING_AC);
|
|
|
|
return true;
|
2011-12-22 05:58:29 +04:00
|
|
|
}
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
// Otoro device support
|
|
|
|
|
|
|
|
char chargingSrcString[16];
|
|
|
|
|
|
|
|
success = ReadSysFile("/sys/class/power_supply/battery/status",
|
2013-03-27 19:19:09 +04:00
|
|
|
chargingSrcString, sizeof(chargingSrcString));
|
2012-11-29 23:18:46 +04:00
|
|
|
if (success) {
|
2013-03-27 19:19:09 +04:00
|
|
|
*aCharging = strcmp(chargingSrcString, "Charging") == 0 ||
|
|
|
|
strcmp(chargingSrcString, "Full") == 0;
|
2012-11-29 23:18:46 +04:00
|
|
|
return true;
|
2012-02-17 02:40:43 +04:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
|
|
|
|
{
|
|
|
|
int charge;
|
2015-03-11 06:41:00 +03:00
|
|
|
static bool previousCharging = false;
|
|
|
|
static double previousLevel = 0.0, remainingTime = 0.0;
|
|
|
|
static struct timespec lastLevelChange;
|
|
|
|
struct timespec now;
|
|
|
|
double dtime, dlevel;
|
2012-11-29 23:18:46 +04:00
|
|
|
|
|
|
|
if (GetCurrentBatteryCharge(&charge)) {
|
|
|
|
aBatteryInfo->level() = (double)charge / 100.0;
|
|
|
|
} else {
|
|
|
|
aBatteryInfo->level() = dom::battery::kDefaultLevel;
|
2011-12-22 05:58:29 +04:00
|
|
|
}
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
int charging;
|
|
|
|
|
|
|
|
if (GetCurrentBatteryCharging(&charging)) {
|
|
|
|
aBatteryInfo->charging() = charging;
|
|
|
|
} else {
|
|
|
|
aBatteryInfo->charging() = true;
|
2012-02-15 22:10:15 +04:00
|
|
|
}
|
|
|
|
|
2015-03-11 06:41:00 +03:00
|
|
|
if (aBatteryInfo->charging() != previousCharging){
|
2012-11-29 23:18:46 +04:00
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
2015-03-11 06:41:00 +03:00
|
|
|
memset(&lastLevelChange, 0, sizeof(struct timespec));
|
2015-05-13 19:35:57 +03:00
|
|
|
remainingTime = 0.0;
|
2015-03-11 06:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aBatteryInfo->charging()) {
|
|
|
|
if (aBatteryInfo->level() == 1.0) {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kDefaultRemainingTime;
|
|
|
|
} else if (aBatteryInfo->level() != previousLevel){
|
|
|
|
if (lastLevelChange.tv_sec != 0) {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
dtime = now.tv_sec - lastLevelChange.tv_sec;
|
|
|
|
dlevel = aBatteryInfo->level() - previousLevel;
|
|
|
|
|
|
|
|
if (dlevel <= 0.0) {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
} else {
|
|
|
|
remainingTime = (double) round(dtime / dlevel * (1.0 - aBatteryInfo->level()));
|
|
|
|
aBatteryInfo->remainingTime() = remainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastLevelChange = now;
|
|
|
|
} else { // lastLevelChange.tv_sec == 0
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &lastLevelChange);
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
dtime = now.tv_sec - lastLevelChange.tv_sec;
|
|
|
|
if (dtime < remainingTime) {
|
|
|
|
aBatteryInfo->remainingTime() = round(remainingTime - dtime);
|
|
|
|
} else {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-29 23:18:46 +04:00
|
|
|
} else {
|
2015-05-13 19:35:57 +03:00
|
|
|
if (aBatteryInfo->level() == 0.0) {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kDefaultRemainingTime;
|
|
|
|
} else if (aBatteryInfo->level() != previousLevel){
|
|
|
|
if (lastLevelChange.tv_sec != 0) {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
dtime = now.tv_sec - lastLevelChange.tv_sec;
|
|
|
|
dlevel = previousLevel - aBatteryInfo->level();
|
|
|
|
|
|
|
|
if (dlevel <= 0.0) {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
} else {
|
|
|
|
remainingTime = (double) round(dtime / dlevel * aBatteryInfo->level());
|
|
|
|
aBatteryInfo->remainingTime() = remainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastLevelChange = now;
|
|
|
|
} else { // lastLevelChange.tv_sec == 0
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &lastLevelChange);
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
dtime = now.tv_sec - lastLevelChange.tv_sec;
|
|
|
|
if (dtime < remainingTime) {
|
|
|
|
aBatteryInfo->remainingTime() = round(remainingTime - dtime);
|
|
|
|
} else {
|
|
|
|
aBatteryInfo->remainingTime() = dom::battery::kUnknownRemainingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-11-29 23:18:46 +04:00
|
|
|
}
|
2015-03-11 06:41:00 +03:00
|
|
|
|
|
|
|
previousCharging = aBatteryInfo->charging();
|
|
|
|
previousLevel = aBatteryInfo->level();
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// We can write to screenEnabledFilename to enable/disable the screen, but when
|
|
|
|
// we read, we always get "mem"! So we have to keep track ourselves whether
|
|
|
|
// the screen is on or not.
|
|
|
|
bool sScreenEnabled = true;
|
|
|
|
|
2012-04-17 02:35:33 +04:00
|
|
|
// We can read wakeLockFilename to find out whether the cpu wake lock
|
|
|
|
// is already acquired, but reading and parsing it is a lot more work
|
|
|
|
// than tracking it ourselves, and it won't be accurate anyway (kernel
|
|
|
|
// internal wake locks aren't counted here.)
|
|
|
|
bool sCpuSleepAllowed = true;
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
// Some CPU wake locks may be acquired internally in HAL. We use a counter to
|
|
|
|
// keep track of these needs. Note we have to hold |sInternalLockCpuMonitor|
|
|
|
|
// when reading or writing this variable to ensure thread-safe.
|
|
|
|
int32_t sInternalLockCpuCount = 0;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2011-12-20 23:31:53 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
GetScreenEnabled()
|
|
|
|
{
|
|
|
|
return sScreenEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-30 07:11:23 +04:00
|
|
|
SetScreenEnabled(bool aEnabled)
|
2011-12-20 23:31:53 +04:00
|
|
|
{
|
2014-05-30 07:11:23 +04:00
|
|
|
GetGonkDisplay()->SetEnabled(aEnabled);
|
|
|
|
sScreenEnabled = aEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetKeyLightEnabled()
|
|
|
|
{
|
2014-06-11 14:17:23 +04:00
|
|
|
LightConfiguration config;
|
2015-05-29 14:21:36 +03:00
|
|
|
bool ok = GetLight(eHalLightID_Buttons, &config);
|
|
|
|
if (ok) {
|
|
|
|
return (config.color != 0x00000000);
|
|
|
|
}
|
|
|
|
return false;
|
2014-05-30 07:11:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetKeyLightEnabled(bool aEnabled)
|
|
|
|
{
|
2014-06-11 14:17:23 +04:00
|
|
|
LightConfiguration config;
|
2015-01-22 06:36:00 +03:00
|
|
|
config.mode = eHalLightMode_User;
|
|
|
|
config.flash = eHalLightFlash_None;
|
2014-06-11 14:17:23 +04:00
|
|
|
config.flashOnMS = config.flashOffMS = 0;
|
|
|
|
config.color = 0x00000000;
|
2014-05-30 07:11:23 +04:00
|
|
|
|
|
|
|
if (aEnabled) {
|
|
|
|
// Convert the value in [0, 1] to an int between 0 and 255 and then convert
|
|
|
|
// it to a color. Note that the high byte is FF, corresponding to the alpha
|
|
|
|
// channel.
|
|
|
|
double brightness = GetScreenBrightness();
|
|
|
|
uint32_t val = static_cast<int>(round(brightness * 255.0));
|
|
|
|
uint32_t color = (0xff<<24) + (val<<16) + (val<<8) + val;
|
|
|
|
|
2014-06-11 14:17:23 +04:00
|
|
|
config.color = color;
|
2014-05-30 07:11:23 +04:00
|
|
|
}
|
|
|
|
|
2015-01-22 06:36:00 +03:00
|
|
|
SetLight(eHalLightID_Buttons, config);
|
|
|
|
SetLight(eHalLightID_Keyboard, config);
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
GetScreenBrightness()
|
|
|
|
{
|
2014-06-11 14:17:23 +04:00
|
|
|
LightConfiguration config;
|
2015-01-22 06:36:00 +03:00
|
|
|
LightType light = eHalLightID_Backlight;
|
2011-12-20 23:31:53 +04:00
|
|
|
|
2015-05-29 14:21:36 +03:00
|
|
|
bool ok = GetLight(light, &config);
|
|
|
|
if (ok) {
|
|
|
|
// backlight is brightness only, so using one of the RGB elements as value.
|
|
|
|
int brightness = config.color & 0xFF;
|
|
|
|
return brightness / 255.0;
|
|
|
|
}
|
|
|
|
// If GetLight fails, it's because the light doesn't exist. So return
|
|
|
|
// a value corresponding to "off".
|
|
|
|
return 0;
|
2011-12-20 23:31:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetScreenBrightness(double brightness)
|
|
|
|
{
|
|
|
|
// Don't use De Morgan's law to push the ! into this expression; we want to
|
|
|
|
// catch NaN too.
|
|
|
|
if (!(0 <= brightness && brightness <= 1)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("SetScreenBrightness: Dropping illegal brightness %f.", brightness);
|
2011-12-20 23:31:53 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-02 10:09:00 +04:00
|
|
|
// Convert the value in [0, 1] to an int between 0 and 255 and convert to a color
|
|
|
|
// note that the high byte is FF, corresponding to the alpha channel.
|
2014-05-30 07:11:23 +04:00
|
|
|
uint32_t val = static_cast<int>(round(brightness * 255.0));
|
2012-02-02 10:09:00 +04:00
|
|
|
uint32_t color = (0xff<<24) + (val<<16) + (val<<8) + val;
|
|
|
|
|
2014-06-11 14:17:23 +04:00
|
|
|
LightConfiguration config;
|
2015-01-22 06:36:00 +03:00
|
|
|
config.mode = eHalLightMode_User;
|
|
|
|
config.flash = eHalLightFlash_None;
|
2014-06-11 14:17:23 +04:00
|
|
|
config.flashOnMS = config.flashOffMS = 0;
|
|
|
|
config.color = color;
|
2015-01-22 06:36:00 +03:00
|
|
|
SetLight(eHalLightID_Backlight, config);
|
2014-05-30 07:11:23 +04:00
|
|
|
if (GetKeyLightEnabled()) {
|
2015-01-22 06:36:00 +03:00
|
|
|
SetLight(eHalLightID_Buttons, config);
|
|
|
|
SetLight(eHalLightID_Keyboard, config);
|
2014-05-30 07:11:23 +04:00
|
|
|
}
|
2012-02-02 10:09:00 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
static Monitor* sInternalLockCpuMonitor = nullptr;
|
|
|
|
|
|
|
|
static void
|
|
|
|
UpdateCpuSleepState()
|
|
|
|
{
|
2015-01-08 17:39:38 +03:00
|
|
|
const char *wakeLockFilename = "/sys/power/wake_lock";
|
|
|
|
const char *wakeUnlockFilename = "/sys/power/wake_unlock";
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
sInternalLockCpuMonitor->AssertCurrentThreadOwns();
|
|
|
|
bool allowed = sCpuSleepAllowed && !sInternalLockCpuCount;
|
2015-12-29 13:49:50 +03:00
|
|
|
WriteSysFile(allowed ? wakeUnlockFilename : wakeLockFilename, "gecko");
|
2013-05-02 14:33:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InternalLockCpu() {
|
|
|
|
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
|
|
|
|
++sInternalLockCpuCount;
|
|
|
|
UpdateCpuSleepState();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InternalUnlockCpu() {
|
|
|
|
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
|
|
|
|
--sInternalLockCpuCount;
|
|
|
|
UpdateCpuSleepState();
|
|
|
|
}
|
|
|
|
|
2012-04-17 02:35:33 +04:00
|
|
|
bool
|
|
|
|
GetCpuSleepAllowed()
|
|
|
|
{
|
|
|
|
return sCpuSleepAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetCpuSleepAllowed(bool aAllowed)
|
|
|
|
{
|
2013-05-02 14:33:58 +04:00
|
|
|
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
|
2012-04-17 02:35:33 +04:00
|
|
|
sCpuSleepAllowed = aAllowed;
|
2013-05-02 14:33:58 +04:00
|
|
|
UpdateCpuSleepState();
|
2012-04-17 02:35:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
void
|
2012-09-26 07:51:29 +04:00
|
|
|
AdjustSystemClock(int64_t aDeltaMilliseconds)
|
2012-02-17 14:44:00 +04:00
|
|
|
{
|
2012-09-28 10:38:50 +04:00
|
|
|
int fd;
|
|
|
|
struct timespec now;
|
|
|
|
|
2012-09-11 10:40:00 +04:00
|
|
|
if (aDeltaMilliseconds == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-14 22:52:07 +04:00
|
|
|
|
|
|
|
// Preventing context switch before setting system clock
|
2012-02-17 14:44:00 +04:00
|
|
|
sched_yield();
|
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
2012-09-28 10:38:50 +04:00
|
|
|
now.tv_sec += (time_t)(aDeltaMilliseconds / 1000LL);
|
|
|
|
now.tv_nsec += (long)((aDeltaMilliseconds % 1000LL) * NsecPerMsec);
|
|
|
|
if (now.tv_nsec >= NsecPerSec) {
|
2012-02-17 14:44:00 +04:00
|
|
|
now.tv_sec += 1;
|
|
|
|
now.tv_nsec -= NsecPerSec;
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:38:50 +04:00
|
|
|
if (now.tv_nsec < 0) {
|
2012-02-17 14:44:00 +04:00
|
|
|
now.tv_nsec += NsecPerSec;
|
2012-09-14 22:52:07 +04:00
|
|
|
now.tv_sec -= 1;
|
2012-02-17 14:44:00 +04:00
|
|
|
}
|
2012-09-28 10:38:50 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
fd = open("/dev/alarm", O_RDWR);
|
|
|
|
} while (fd == -1 && errno == EINTR);
|
|
|
|
ScopedClose autoClose(fd);
|
|
|
|
if (fd < 0) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to open /dev/alarm: %s", strerror(errno));
|
2012-09-28 10:38:50 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, ANDROID_ALARM_SET_RTC, &now) < 0) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("ANDROID_ALARM_SET_RTC failed: %s", strerror(errno));
|
2012-09-11 10:40:00 +04:00
|
|
|
}
|
2012-09-14 22:52:07 +04:00
|
|
|
|
2012-10-23 11:15:43 +04:00
|
|
|
hal::NotifySystemClockChange(aDeltaMilliseconds);
|
|
|
|
}
|
|
|
|
|
2013-10-15 12:43:53 +04:00
|
|
|
int32_t
|
2012-10-23 11:15:43 +04:00
|
|
|
GetTimezoneOffset()
|
|
|
|
{
|
|
|
|
PRExplodedTime prTime;
|
|
|
|
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &prTime);
|
|
|
|
|
|
|
|
// Daylight saving time (DST) will be taken into account.
|
|
|
|
int32_t offset = prTime.tm_params.tp_gmt_offset;
|
|
|
|
offset += prTime.tm_params.tp_dst_offset;
|
|
|
|
|
|
|
|
// Returns the timezone offset relative to UTC in minutes.
|
|
|
|
return -(offset / 60);
|
2012-02-17 14:44:00 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:33:15 +04:00
|
|
|
static int32_t sKernelTimezoneOffset = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
UpdateKernelTimezone(int32_t timezoneOffset)
|
|
|
|
{
|
|
|
|
if (sKernelTimezoneOffset == timezoneOffset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the kernel about the new time zone as well, so that FAT filesystems
|
|
|
|
// will get local timestamps rather than UTC timestamps.
|
|
|
|
//
|
|
|
|
// We assume that /init.rc has a sysclktz entry so that settimeofday has
|
|
|
|
// already been called once before we call it (there is a side-effect in
|
|
|
|
// the kernel the very first time settimeofday is called where it does some
|
|
|
|
// special processing if you only set the timezone).
|
|
|
|
struct timezone tz;
|
|
|
|
memset(&tz, 0, sizeof(tz));
|
|
|
|
tz.tz_minuteswest = timezoneOffset;
|
|
|
|
settimeofday(nullptr, &tz);
|
|
|
|
sKernelTimezoneOffset = timezoneOffset;
|
|
|
|
}
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
void
|
2012-02-17 14:44:00 +04:00
|
|
|
SetTimezone(const nsCString& aTimezoneSpec)
|
2012-09-11 10:40:00 +04:00
|
|
|
{
|
|
|
|
if (aTimezoneSpec.Equals(GetTimezone())) {
|
2014-01-29 01:33:15 +04:00
|
|
|
// Even though the timezone hasn't changed, we still need to tell the
|
|
|
|
// kernel what the current timezone is. The timezone is persisted in
|
|
|
|
// a property and doesn't change across reboots, but the kernel still
|
|
|
|
// needs to be updated on every boot.
|
|
|
|
UpdateKernelTimezone(GetTimezoneOffset());
|
2012-09-11 10:40:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:15:43 +04:00
|
|
|
int32_t oldTimezoneOffsetMinutes = GetTimezoneOffset();
|
2012-02-17 14:44:00 +04:00
|
|
|
property_set("persist.sys.timezone", aTimezoneSpec.get());
|
2013-05-02 14:33:58 +04:00
|
|
|
// This function is automatically called by the other time conversion
|
2012-09-14 22:52:07 +04:00
|
|
|
// functions that depend on the timezone. To be safe, we call it manually.
|
2012-02-17 14:44:00 +04:00
|
|
|
tzset();
|
2012-10-23 11:15:43 +04:00
|
|
|
int32_t newTimezoneOffsetMinutes = GetTimezoneOffset();
|
2014-01-29 01:33:15 +04:00
|
|
|
UpdateKernelTimezone(newTimezoneOffsetMinutes);
|
2012-10-23 11:15:43 +04:00
|
|
|
hal::NotifySystemTimezoneChange(
|
|
|
|
hal::SystemTimezoneChangeInformation(
|
|
|
|
oldTimezoneOffsetMinutes, newTimezoneOffsetMinutes));
|
2012-02-17 14:44:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
nsCString
|
2012-08-23 13:00:00 +04:00
|
|
|
GetTimezone()
|
|
|
|
{
|
|
|
|
char timezone[32];
|
|
|
|
property_get("persist.sys.timezone", timezone, "");
|
|
|
|
return nsCString(timezone);
|
|
|
|
}
|
|
|
|
|
2012-09-23 21:00:32 +04:00
|
|
|
void
|
2012-10-23 11:15:43 +04:00
|
|
|
EnableSystemClockChangeNotifications()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableSystemClockChangeNotifications()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EnableSystemTimezoneChangeNotifications()
|
2012-09-23 21:00:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-23 11:15:43 +04:00
|
|
|
DisableSystemTimezoneChangeNotifications()
|
2012-09-23 21:00:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-09 01:36:07 +04:00
|
|
|
// Nothing to do here. Gonk widgetry always listens for screen
|
|
|
|
// orientation changes.
|
|
|
|
void
|
|
|
|
EnableScreenConfigurationNotifications()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableScreenConfigurationNotifications()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetCurrentScreenConfiguration(hal::ScreenConfiguration* aScreenConfiguration)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScreenGonk> screen = nsScreenManagerGonk::GetPrimaryScreen();
|
2015-04-30 06:17:57 +03:00
|
|
|
*aScreenConfiguration = screen->GetConfiguration();
|
2012-05-09 01:36:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-08-19 00:55:09 +03:00
|
|
|
LockScreenOrientation(const dom::ScreenOrientationInternal& aOrientation)
|
2012-05-09 01:36:07 +04:00
|
|
|
{
|
2012-04-20 15:43:25 +04:00
|
|
|
return OrientationObserver::GetInstance()->LockScreenOrientation(aOrientation);
|
2012-05-09 01:36:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnlockScreenOrientation()
|
|
|
|
{
|
2012-04-20 15:43:25 +04:00
|
|
|
OrientationObserver::GetInstance()->UnlockScreenOrientation();
|
2012-05-09 01:36:07 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
// This thread will wait for the alarm firing by a blocking IO.
|
2012-07-06 14:42:10 +04:00
|
|
|
static pthread_t sAlarmFireWatcherThread;
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
// If |sAlarmData| is non-null, it's owned by the alarm-watcher thread.
|
|
|
|
struct AlarmData {
|
2012-07-06 14:42:10 +04:00
|
|
|
public:
|
2013-05-02 14:33:58 +04:00
|
|
|
AlarmData(int aFd) : mFd(aFd),
|
|
|
|
mGeneration(sNextGeneration++),
|
|
|
|
mShuttingDown(false) {}
|
2012-07-06 14:42:10 +04:00
|
|
|
ScopedClose mFd;
|
|
|
|
int mGeneration;
|
|
|
|
bool mShuttingDown;
|
|
|
|
|
|
|
|
static int sNextGeneration;
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
};
|
2012-07-06 14:42:10 +04:00
|
|
|
|
|
|
|
int AlarmData::sNextGeneration = 0;
|
|
|
|
|
2013-08-23 23:51:15 +04:00
|
|
|
AlarmData* sAlarmData = nullptr;
|
2012-07-06 14:42:10 +04:00
|
|
|
|
|
|
|
class AlarmFiredEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
AlarmFiredEvent(int aGeneration) : mGeneration(aGeneration) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
// Guard against spurious notifications caused by an alarm firing
|
|
|
|
// concurrently with it being disabled.
|
2013-05-02 14:33:58 +04:00
|
|
|
if (sAlarmData && !sAlarmData->mShuttingDown &&
|
|
|
|
mGeneration == sAlarmData->mGeneration) {
|
2012-07-06 14:42:10 +04:00
|
|
|
hal::NotifyAlarmFired();
|
|
|
|
}
|
2013-05-02 14:33:58 +04:00
|
|
|
// The fired alarm event has been delivered to the observer (if needed);
|
|
|
|
// we can now release a CPU wake lock.
|
|
|
|
InternalUnlockCpu();
|
2012-07-06 14:42:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int mGeneration;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Runs on alarm-watcher thread.
|
2012-09-14 22:52:07 +04:00
|
|
|
static void
|
2012-07-06 14:42:10 +04:00
|
|
|
DestroyAlarmData(void* aData)
|
|
|
|
{
|
|
|
|
AlarmData* alarmData = static_cast<AlarmData*>(aData);
|
|
|
|
delete alarmData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs on alarm-watcher thread.
|
|
|
|
void ShutDownAlarm(int aSigno)
|
|
|
|
{
|
2013-09-18 21:53:07 +04:00
|
|
|
if (aSigno == SIGUSR1 && sAlarmData) {
|
2012-07-06 14:42:10 +04:00
|
|
|
sAlarmData->mShuttingDown = true;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
static void*
|
2012-07-06 14:42:10 +04:00
|
|
|
WaitForAlarm(void* aData)
|
|
|
|
{
|
|
|
|
pthread_cleanup_push(DestroyAlarmData, aData);
|
|
|
|
|
|
|
|
AlarmData* alarmData = static_cast<AlarmData*>(aData);
|
|
|
|
|
|
|
|
while (!alarmData->mShuttingDown) {
|
|
|
|
int alarmTypeFlags = 0;
|
|
|
|
|
|
|
|
// ALARM_WAIT apparently will block even if an alarm hasn't been
|
|
|
|
// programmed, although this behavior doesn't seem to be
|
|
|
|
// documented. We rely on that here to avoid spinning the CPU
|
|
|
|
// while awaiting an alarm to be programmed.
|
|
|
|
do {
|
|
|
|
alarmTypeFlags = ioctl(alarmData->mFd, ANDROID_ALARM_WAIT);
|
2013-05-02 14:33:58 +04:00
|
|
|
} while (alarmTypeFlags < 0 && errno == EINTR &&
|
|
|
|
!alarmData->mShuttingDown);
|
|
|
|
|
|
|
|
if (!alarmData->mShuttingDown && alarmTypeFlags >= 0 &&
|
|
|
|
(alarmTypeFlags & ANDROID_ALARM_RTC_WAKEUP_MASK)) {
|
|
|
|
// To make sure the observer can get the alarm firing notification
|
|
|
|
// *on time* (the system won't sleep during the process in any way),
|
|
|
|
// we need to acquire a CPU wake lock before firing the alarm event.
|
|
|
|
InternalLockCpu();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AlarmFiredEvent> event =
|
2013-05-02 14:33:58 +04:00
|
|
|
new AlarmFiredEvent(alarmData->mGeneration);
|
|
|
|
NS_DispatchToMainThread(event);
|
2012-07-06 14:42:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_cleanup_pop(1);
|
2013-08-23 23:51:15 +04:00
|
|
|
return nullptr;
|
2012-07-06 14:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EnableAlarm()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!sAlarmData);
|
|
|
|
|
|
|
|
int alarmFd = open("/dev/alarm", O_RDWR);
|
|
|
|
if (alarmFd < 0) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to open alarm device: %s.", strerror(errno));
|
2012-07-06 14:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<AlarmData> alarmData(new AlarmData(alarmFd));
|
|
|
|
|
|
|
|
struct sigaction actions;
|
|
|
|
memset(&actions, 0, sizeof(actions));
|
|
|
|
sigemptyset(&actions.sa_mask);
|
|
|
|
actions.sa_flags = 0;
|
|
|
|
actions.sa_handler = ShutDownAlarm;
|
2013-08-23 23:51:15 +04:00
|
|
|
if (sigaction(SIGUSR1, &actions, nullptr)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to set SIGUSR1 signal for alarm-watcher thread.");
|
2012-07-06 14:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
// Initialize the monitor for internally locking CPU to ensure thread-safe
|
|
|
|
// before running the alarm-watcher thread.
|
|
|
|
sInternalLockCpuMonitor = new Monitor("sInternalLockCpuMonitor");
|
|
|
|
int status = pthread_create(&sAlarmFireWatcherThread, &attr, WaitForAlarm,
|
|
|
|
alarmData.get());
|
2012-07-06 14:42:10 +04:00
|
|
|
if (status) {
|
2013-08-23 23:51:15 +04:00
|
|
|
alarmData = nullptr;
|
2013-05-02 14:33:58 +04:00
|
|
|
delete sInternalLockCpuMonitor;
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to create alarm-watcher thread. Status: %d.", status);
|
2012-07-06 14:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
|
|
|
// The thread owns this now. We only hold a pointer.
|
|
|
|
sAlarmData = alarmData.forget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableAlarm()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sAlarmData);
|
|
|
|
|
|
|
|
// NB: this must happen-before the thread cancellation.
|
2013-08-23 23:51:15 +04:00
|
|
|
sAlarmData = nullptr;
|
2012-07-06 14:42:10 +04:00
|
|
|
|
|
|
|
// The cancel will interrupt the thread and destroy it, freeing the
|
|
|
|
// data pointed at by sAlarmData.
|
2012-07-26 20:15:26 +04:00
|
|
|
DebugOnly<int> err = pthread_kill(sAlarmFireWatcherThread, SIGUSR1);
|
2012-07-06 14:42:10 +04:00
|
|
|
MOZ_ASSERT(!err);
|
2013-05-02 14:33:58 +04:00
|
|
|
|
|
|
|
delete sInternalLockCpuMonitor;
|
2012-07-06 14:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
SetAlarm(int32_t aSeconds, int32_t aNanoseconds)
|
2012-07-06 14:42:10 +04:00
|
|
|
{
|
|
|
|
if (!sAlarmData) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("We should have enabled the alarm.");
|
2012-07-06 14:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = aSeconds;
|
|
|
|
ts.tv_nsec = aNanoseconds;
|
|
|
|
|
2013-05-02 14:33:58 +04:00
|
|
|
// Currently we only support RTC wakeup alarm type.
|
|
|
|
const int result = ioctl(sAlarmData->mFd,
|
|
|
|
ANDROID_ALARM_SET(ANDROID_ALARM_RTC_WAKEUP), &ts);
|
2012-07-06 14:42:10 +04:00
|
|
|
|
|
|
|
if (result < 0) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Unable to set alarm: %s.", strerror(errno));
|
2012-07-06 14:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-14 22:52:07 +04:00
|
|
|
static int
|
2012-10-31 21:29:14 +04:00
|
|
|
OomAdjOfOomScoreAdj(int aOomScoreAdj)
|
2012-09-14 22:52:07 +04:00
|
|
|
{
|
|
|
|
// Convert OOM adjustment from the domain of /proc/<pid>/oom_score_adj
|
2012-10-31 21:29:14 +04:00
|
|
|
// to the domain of /proc/<pid>/oom_adj.
|
2012-09-14 22:52:07 +04:00
|
|
|
|
|
|
|
int adj;
|
|
|
|
|
|
|
|
if (aOomScoreAdj < 0) {
|
|
|
|
adj = (OOM_DISABLE * aOomScoreAdj) / OOM_SCORE_ADJ_MIN;
|
|
|
|
} else {
|
|
|
|
adj = (OOM_ADJUST_MAX * aOomScoreAdj) / OOM_SCORE_ADJ_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
return adj;
|
|
|
|
}
|
|
|
|
|
2013-10-01 07:54:59 +04:00
|
|
|
static void
|
2015-02-25 11:37:38 +03:00
|
|
|
RoundOomScoreAdjUpWithLRU(int& aOomScoreAdj, uint32_t aLRU)
|
2013-10-01 07:54:59 +04:00
|
|
|
{
|
|
|
|
// We want to add minimum value to round OomScoreAdj up according to
|
2015-02-25 11:37:38 +03:00
|
|
|
// the steps by aLRU.
|
2013-10-01 07:54:59 +04:00
|
|
|
aOomScoreAdj +=
|
2015-02-25 11:37:38 +03:00
|
|
|
ceil(((float)OOM_SCORE_ADJ_MAX / OOM_ADJUST_MAX) * aLRU);
|
2013-10-01 07:54:59 +04:00
|
|
|
}
|
|
|
|
|
2014-01-11 10:22:55 +04:00
|
|
|
#define OOM_LOG(level, args...) __android_log_print(level, "OomLogger", ##args)
|
2015-03-21 19:28:04 +03:00
|
|
|
class OomVictimLogger final
|
2014-01-11 10:22:55 +04:00
|
|
|
: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OomVictimLogger()
|
|
|
|
: mLastLineChecked(-1.0),
|
|
|
|
mRegexes(nullptr)
|
|
|
|
{
|
|
|
|
// Enable timestamps in kernel's printk
|
2015-12-29 13:49:50 +03:00
|
|
|
WriteSysFile("/sys/module/printk/parameters/time", "Y");
|
2014-01-11 10:22:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2015-03-10 15:44:02 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~OomVictimLogger() {}
|
|
|
|
|
2014-01-11 10:22:55 +04:00
|
|
|
private:
|
|
|
|
double mLastLineChecked;
|
|
|
|
ScopedFreePtr<regex_t> mRegexes;
|
|
|
|
};
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(OomVictimLogger, nsIObserver);
|
2014-01-11 10:22:55 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OomVictimLogger::Observe(
|
|
|
|
nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
nsDependentCString event_type(aTopic);
|
|
|
|
if (!event_type.EqualsLiteral("ipc:content-shutdown")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OOM message finding regexes
|
|
|
|
const char* const regexes_raw[] = {
|
|
|
|
".*select.*to kill.*",
|
|
|
|
".*send sigkill to.*",
|
2014-05-17 04:16:20 +04:00
|
|
|
".*lowmem_shrink.*",
|
|
|
|
".*[Oo]ut of [Mm]emory.*",
|
|
|
|
".*[Kk]ill [Pp]rocess.*",
|
|
|
|
".*[Kk]illed [Pp]rocess.*",
|
|
|
|
".*oom-killer.*",
|
|
|
|
// The regexes below are for the output of dump_task from oom_kill.c
|
|
|
|
// 1st - title 2nd - body lines (8 ints and a string)
|
|
|
|
// oom_adj and oom_score_adj can be negative
|
|
|
|
"\\[ pid \\] uid tgid total_vm rss cpu oom_adj oom_score_adj name",
|
|
|
|
"\\[.*[0-9][0-9]*\\][ ]*[0-9][0-9]*[ ]*[0-9][0-9]*[ ]*[0-9][0-9]*[ ]*[0-9][0-9]*[ ]*[0-9][0-9]*[ ]*.[0-9][0-9]*[ ]*.[0-9][0-9]*.*"
|
|
|
|
};
|
2014-01-30 22:26:54 +04:00
|
|
|
const size_t regex_count = ArrayLength(regexes_raw);
|
2014-01-11 10:22:55 +04:00
|
|
|
|
|
|
|
// Compile our regex just in time
|
|
|
|
if (!mRegexes) {
|
|
|
|
mRegexes = static_cast<regex_t*>(malloc(sizeof(regex_t) * regex_count));
|
|
|
|
for (size_t i = 0; i < regex_count; i++) {
|
|
|
|
int compilation_err = regcomp(&(mRegexes[i]), regexes_raw[i], REG_NOSUB);
|
|
|
|
if (compilation_err) {
|
|
|
|
OOM_LOG(ANDROID_LOG_ERROR, "Cannot compile regex \"%s\"\n", regexes_raw[i]);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef KLOG_SIZE_BUFFER
|
|
|
|
// Upstream bionic in commit
|
|
|
|
// e249b059637b49a285ed9f58a2a18bfd054e5d95
|
|
|
|
// deprecated the old klog defs.
|
|
|
|
// Our current bionic does not hit this
|
|
|
|
// change yet so handle the future change.
|
2014-11-25 04:01:41 +03:00
|
|
|
// (ICS doesn't have KLOG_SIZE_BUFFER but
|
|
|
|
// JB and onwards does.)
|
2014-01-11 10:22:55 +04:00
|
|
|
#define KLOG_SIZE_BUFFER KLOG_WRITE
|
|
|
|
#endif
|
|
|
|
// Retreive kernel log
|
|
|
|
int msg_buf_size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
|
|
|
|
ScopedFreePtr<char> msg_buf(static_cast<char *>(malloc(msg_buf_size + 1)));
|
|
|
|
int read_size = klogctl(KLOG_READ_ALL, msg_buf.rwget(), msg_buf_size);
|
|
|
|
|
|
|
|
// Turn buffer into cstring
|
|
|
|
read_size = read_size > msg_buf_size ? msg_buf_size : read_size;
|
|
|
|
msg_buf.rwget()[read_size] = '\0';
|
|
|
|
|
|
|
|
// Foreach line
|
|
|
|
char* line_end;
|
|
|
|
char* line_begin = msg_buf.rwget();
|
|
|
|
for (; (line_end = strchr(line_begin, '\n')); line_begin = line_end + 1) {
|
|
|
|
// make line into cstring
|
|
|
|
*line_end = '\0';
|
|
|
|
|
|
|
|
// Note: Kernel messages look like:
|
|
|
|
// <5>[63648.286409] sd 35:0:0:0: Attached scsi generic sg1 type 0
|
|
|
|
// 5 is the loging level
|
|
|
|
// [*] is the time timestamp, seconds since boot
|
|
|
|
// last comes the logged message
|
|
|
|
|
|
|
|
// Since the logging level can be a string we must
|
|
|
|
// skip it since scanf lacks wildcard matching
|
|
|
|
char* timestamp_begin = strchr(line_begin, '[');
|
|
|
|
char after_float;
|
|
|
|
double lineTimestamp = -1;
|
|
|
|
bool lineTimestampFound = false;
|
|
|
|
if (timestamp_begin &&
|
|
|
|
// Note: scanf treats a ' ' as [ ]*
|
|
|
|
// Note: scanf treats [ %lf] as [ %lf thus we must check
|
|
|
|
// for the closing bracket outselves.
|
|
|
|
2 == sscanf(timestamp_begin, "[ %lf%c", &lineTimestamp, &after_float) &&
|
|
|
|
after_float == ']') {
|
|
|
|
if (lineTimestamp <= mLastLineChecked) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
lineTimestampFound = true;
|
|
|
|
mLastLineChecked = lineTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log interesting lines
|
|
|
|
for (size_t i = 0; i < regex_count; i++) {
|
|
|
|
int matching = !regexec(&(mRegexes[i]), line_begin, 0, NULL, 0);
|
|
|
|
if (matching) {
|
2014-03-19 01:18:14 +04:00
|
|
|
// Log content of kernel message. We try to skip the ], but if for
|
|
|
|
// some reason (most likely due to buffer overflow/wraparound), we
|
|
|
|
// can't find the ] then we just log the entire line.
|
|
|
|
char* endOfTimestamp = strchr(line_begin, ']');
|
|
|
|
if (endOfTimestamp && endOfTimestamp[1] == ' ') {
|
|
|
|
// skip the ] and the space that follows it
|
|
|
|
line_begin = endOfTimestamp + 2;
|
|
|
|
}
|
2014-01-11 10:22:55 +04:00
|
|
|
if (!lineTimestampFound) {
|
|
|
|
OOM_LOG(ANDROID_LOG_WARN, "following kill message may be a duplicate");
|
|
|
|
}
|
|
|
|
OOM_LOG(ANDROID_LOG_ERROR, "[Kill]: %s\n", line_begin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
/**
|
|
|
|
* Wraps a particular ProcessPriority, giving us easy access to the prefs that
|
|
|
|
* are relevant to it.
|
|
|
|
*
|
|
|
|
* Creating a PriorityClass also ensures that the control group is created.
|
|
|
|
*/
|
|
|
|
class PriorityClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a PriorityClass for the given ProcessPriority. This implicitly
|
|
|
|
* reads the relevant prefs and opens the cgroup.procs file of the relevant
|
|
|
|
* control group caching its file descriptor for later use.
|
|
|
|
*/
|
|
|
|
PriorityClass(ProcessPriority aPriority);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the file descriptor for the cgroup.procs file of the associated
|
|
|
|
* control group.
|
|
|
|
*/
|
|
|
|
~PriorityClass();
|
|
|
|
|
|
|
|
PriorityClass(const PriorityClass& aOther);
|
|
|
|
PriorityClass& operator=(const PriorityClass& aOther);
|
|
|
|
|
|
|
|
ProcessPriority Priority()
|
|
|
|
{
|
|
|
|
return mPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t OomScoreAdj()
|
|
|
|
{
|
|
|
|
return clamped<int32_t>(mOomScoreAdj, OOM_SCORE_ADJ_MIN, OOM_SCORE_ADJ_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t KillUnderKB()
|
|
|
|
{
|
|
|
|
return mKillUnderKB;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString CGroup()
|
|
|
|
{
|
|
|
|
return mGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a process to this priority class, this moves the process' PID into
|
|
|
|
* the associated control group.
|
|
|
|
*
|
|
|
|
* @param aPid The PID of the process to be added.
|
|
|
|
*/
|
|
|
|
void AddProcess(int aPid);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProcessPriority mPriority;
|
|
|
|
int32_t mOomScoreAdj;
|
|
|
|
int32_t mKillUnderKB;
|
2015-01-12 22:19:00 +03:00
|
|
|
int mCpuCGroupProcsFd;
|
|
|
|
int mMemCGroupProcsFd;
|
2015-01-08 17:39:43 +03:00
|
|
|
nsCString mGroup;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a string that identifies where we can find the value of aPref
|
|
|
|
* that's specific to mPriority. For example, we might return
|
|
|
|
* "hal.processPriorityManager.gonk.FOREGROUND_HIGH.oomScoreAdjust".
|
|
|
|
*/
|
|
|
|
nsCString PriorityPrefName(const char* aPref)
|
|
|
|
{
|
|
|
|
return nsPrintfCString("hal.processPriorityManager.gonk.%s.%s",
|
|
|
|
ProcessPriorityToString(mPriority), aPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full path of the cgroup.procs file associated with the group.
|
|
|
|
*/
|
2015-01-12 22:19:00 +03:00
|
|
|
nsCString CpuCGroupProcsFilename()
|
2015-01-08 17:39:43 +03:00
|
|
|
{
|
|
|
|
nsCString cgroupName = mGroup;
|
|
|
|
|
|
|
|
/* If mGroup is empty, our cgroup.procs file is the root procs file,
|
|
|
|
* located at /dev/cpuctl/cgroup.procs. Otherwise our procs file is
|
|
|
|
* /dev/cpuctl/NAME/cgroup.procs. */
|
|
|
|
|
|
|
|
if (!mGroup.IsEmpty()) {
|
|
|
|
cgroupName.AppendLiteral("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_LITERAL_CSTRING("/dev/cpuctl/") + cgroupName +
|
|
|
|
NS_LITERAL_CSTRING("cgroup.procs");
|
|
|
|
}
|
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
nsCString MemCGroupProcsFilename()
|
2015-01-08 17:39:43 +03:00
|
|
|
{
|
2015-01-12 22:19:00 +03:00
|
|
|
nsCString cgroupName = mGroup;
|
|
|
|
|
|
|
|
/* If mGroup is empty, our cgroup.procs file is the root procs file,
|
|
|
|
* located at /sys/fs/cgroup/memory/cgroup.procs. Otherwise our procs
|
|
|
|
* file is /sys/fs/cgroup/memory/NAME/cgroup.procs. */
|
|
|
|
|
|
|
|
if (!mGroup.IsEmpty()) {
|
|
|
|
cgroupName.AppendLiteral("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_LITERAL_CSTRING("/sys/fs/cgroup/memory/") + cgroupName +
|
|
|
|
NS_LITERAL_CSTRING("cgroup.procs");
|
|
|
|
}
|
|
|
|
|
|
|
|
int OpenCpuCGroupProcs()
|
|
|
|
{
|
|
|
|
return open(CpuCGroupProcsFilename().get(), O_WRONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
int OpenMemCGroupProcs()
|
|
|
|
{
|
|
|
|
return open(MemCGroupProcsFilename().get(), O_WRONLY);
|
2015-01-08 17:39:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to create the cgroup for the given PriorityClass, if it doesn't already
|
|
|
|
* exist. This essentially implements mkdir -p; that is, we create parent
|
|
|
|
* cgroups as necessary. The group parameters are also set according to
|
|
|
|
* the corresponding preferences.
|
|
|
|
*
|
|
|
|
* @param aGroup The name of the group.
|
|
|
|
* @return true if we successfully created the cgroup, or if it already
|
|
|
|
* exists. Otherwise, return false.
|
|
|
|
*/
|
|
|
|
static bool
|
2015-01-12 22:19:00 +03:00
|
|
|
EnsureCpuCGroupExists(const nsACString &aGroup)
|
2015-01-08 17:39:43 +03:00
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kDevCpuCtl, "/dev/cpuctl/");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kSlash, "/");
|
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
nsAutoCString groupName(aGroup);
|
|
|
|
HAL_LOG("EnsureCpuCGroupExists for group '%s'", groupName.get());
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
nsAutoCString prefPrefix("hal.processPriorityManager.gonk.cgroups.");
|
|
|
|
|
|
|
|
/* If cgroup is not empty, append the cgroup name and a dot to obtain the
|
|
|
|
* group specific preferences. */
|
|
|
|
if (!aGroup.IsEmpty()) {
|
|
|
|
prefPrefix += aGroup + NS_LITERAL_CSTRING(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString cpuSharesPref(prefPrefix + NS_LITERAL_CSTRING("cpu_shares"));
|
|
|
|
int cpuShares = Preferences::GetInt(cpuSharesPref.get());
|
|
|
|
|
|
|
|
nsAutoCString cpuNotifyOnMigratePref(prefPrefix
|
|
|
|
+ NS_LITERAL_CSTRING("cpu_notify_on_migrate"));
|
|
|
|
int cpuNotifyOnMigrate = Preferences::GetInt(cpuNotifyOnMigratePref.get());
|
|
|
|
|
|
|
|
// Create mCGroup and its parent directories, as necessary.
|
|
|
|
nsCString cgroupIter = aGroup + kSlash;
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
while ((offset = cgroupIter.FindChar('/', offset)) != -1) {
|
|
|
|
nsAutoCString path = kDevCpuCtl + Substring(cgroupIter, 0, offset);
|
|
|
|
int rv = mkdir(path.get(), 0744);
|
|
|
|
|
|
|
|
if (rv == -1 && errno != EEXIST) {
|
|
|
|
HAL_LOG("Could not create the %s control group.", path.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
2015-01-12 22:19:00 +03:00
|
|
|
HAL_LOG("EnsureCpuCGroupExists created group '%s'", groupName.get());
|
2015-01-08 17:39:43 +03:00
|
|
|
|
|
|
|
nsAutoCString pathPrefix(kDevCpuCtl + aGroup + kSlash);
|
|
|
|
nsAutoCString cpuSharesPath(pathPrefix + NS_LITERAL_CSTRING("cpu.shares"));
|
2015-12-29 13:49:50 +03:00
|
|
|
if (cpuShares && !WriteSysFile(cpuSharesPath.get(),
|
|
|
|
nsPrintfCString("%d", cpuShares).get())) {
|
2015-01-08 17:39:43 +03:00
|
|
|
HAL_LOG("Could not set the cpu share for group %s", cpuSharesPath.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString notifyOnMigratePath(pathPrefix
|
|
|
|
+ NS_LITERAL_CSTRING("cpu.notify_on_migrate"));
|
2015-12-29 13:49:50 +03:00
|
|
|
if (!WriteSysFile(notifyOnMigratePath.get(),
|
|
|
|
nsPrintfCString("%d", cpuNotifyOnMigrate).get())) {
|
2015-01-08 17:39:43 +03:00
|
|
|
HAL_LOG("Could not set the cpu migration notification flag for group %s",
|
|
|
|
notifyOnMigratePath.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
static bool
|
|
|
|
EnsureMemCGroupExists(const nsACString &aGroup)
|
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kMemCtl, "/sys/fs/cgroup/memory/");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kSlash, "/");
|
|
|
|
|
|
|
|
nsAutoCString groupName(aGroup);
|
|
|
|
HAL_LOG("EnsureMemCGroupExists for group '%s'", groupName.get());
|
|
|
|
|
|
|
|
nsAutoCString prefPrefix("hal.processPriorityManager.gonk.cgroups.");
|
|
|
|
|
|
|
|
/* If cgroup is not empty, append the cgroup name and a dot to obtain the
|
|
|
|
* group specific preferences. */
|
|
|
|
if (!aGroup.IsEmpty()) {
|
|
|
|
prefPrefix += aGroup + NS_LITERAL_CSTRING(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString memSwappinessPref(prefPrefix + NS_LITERAL_CSTRING("memory_swappiness"));
|
|
|
|
int memSwappiness = Preferences::GetInt(memSwappinessPref.get());
|
|
|
|
|
|
|
|
// Create mCGroup and its parent directories, as necessary.
|
|
|
|
nsCString cgroupIter = aGroup + kSlash;
|
|
|
|
|
|
|
|
int32_t offset = 0;
|
|
|
|
while ((offset = cgroupIter.FindChar('/', offset)) != -1) {
|
|
|
|
nsAutoCString path = kMemCtl + Substring(cgroupIter, 0, offset);
|
|
|
|
int rv = mkdir(path.get(), 0744);
|
|
|
|
|
|
|
|
if (rv == -1 && errno != EEXIST) {
|
|
|
|
HAL_LOG("Could not create the %s control group.", path.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
HAL_LOG("EnsureMemCGroupExists created group '%s'", groupName.get());
|
|
|
|
|
|
|
|
nsAutoCString pathPrefix(kMemCtl + aGroup + kSlash);
|
|
|
|
nsAutoCString memSwappinessPath(pathPrefix + NS_LITERAL_CSTRING("memory.swappiness"));
|
2015-12-29 13:49:50 +03:00
|
|
|
if (!WriteSysFile(memSwappinessPath.get(),
|
|
|
|
nsPrintfCString("%d", memSwappiness).get())) {
|
2015-01-12 22:19:00 +03:00
|
|
|
HAL_LOG("Could not set the memory.swappiness for group %s", memSwappinessPath.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
HAL_LOG("Set memory.swappiness for group %s to %d", memSwappinessPath.get(), memSwappiness);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
PriorityClass::PriorityClass(ProcessPriority aPriority)
|
|
|
|
: mPriority(aPriority)
|
|
|
|
, mOomScoreAdj(0)
|
|
|
|
, mKillUnderKB(0)
|
2015-01-12 22:19:00 +03:00
|
|
|
, mCpuCGroupProcsFd(-1)
|
|
|
|
, mMemCGroupProcsFd(-1)
|
2015-01-08 17:39:43 +03:00
|
|
|
{
|
|
|
|
DebugOnly<nsresult> rv;
|
|
|
|
|
|
|
|
rv = Preferences::GetInt(PriorityPrefName("OomScoreAdjust").get(),
|
|
|
|
&mOomScoreAdj);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Missing oom_score_adj preference");
|
|
|
|
|
|
|
|
rv = Preferences::GetInt(PriorityPrefName("KillUnderKB").get(),
|
|
|
|
&mKillUnderKB);
|
|
|
|
|
|
|
|
rv = Preferences::GetCString(PriorityPrefName("cgroup").get(), &mGroup);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv), "Missing control group preference");
|
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
if (EnsureCpuCGroupExists(mGroup)) {
|
|
|
|
mCpuCGroupProcsFd = OpenCpuCGroupProcs();
|
|
|
|
}
|
|
|
|
if (EnsureMemCGroupExists(mGroup)) {
|
|
|
|
mMemCGroupProcsFd = OpenMemCGroupProcs();
|
2015-01-08 17:39:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PriorityClass::~PriorityClass()
|
|
|
|
{
|
2015-05-26 20:36:51 +03:00
|
|
|
if (mCpuCGroupProcsFd != -1) {
|
|
|
|
close(mCpuCGroupProcsFd);
|
|
|
|
}
|
|
|
|
if (mMemCGroupProcsFd != -1) {
|
|
|
|
close(mMemCGroupProcsFd);
|
|
|
|
}
|
2015-01-08 17:39:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PriorityClass::PriorityClass(const PriorityClass& aOther)
|
|
|
|
: mPriority(aOther.mPriority)
|
|
|
|
, mOomScoreAdj(aOther.mOomScoreAdj)
|
|
|
|
, mKillUnderKB(aOther.mKillUnderKB)
|
|
|
|
, mGroup(aOther.mGroup)
|
|
|
|
{
|
2015-01-12 22:19:00 +03:00
|
|
|
mCpuCGroupProcsFd = OpenCpuCGroupProcs();
|
|
|
|
mMemCGroupProcsFd = OpenMemCGroupProcs();
|
2015-01-08 17:39:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PriorityClass& PriorityClass::operator=(const PriorityClass& aOther)
|
|
|
|
{
|
|
|
|
mPriority = aOther.mPriority;
|
|
|
|
mOomScoreAdj = aOther.mOomScoreAdj;
|
|
|
|
mKillUnderKB = aOther.mKillUnderKB;
|
|
|
|
mGroup = aOther.mGroup;
|
2015-01-12 22:19:00 +03:00
|
|
|
mCpuCGroupProcsFd = OpenCpuCGroupProcs();
|
|
|
|
mMemCGroupProcsFd = OpenMemCGroupProcs();
|
2015-01-08 17:39:43 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityClass::AddProcess(int aPid)
|
|
|
|
{
|
2015-01-12 22:19:00 +03:00
|
|
|
if (mCpuCGroupProcsFd >= 0) {
|
|
|
|
nsPrintfCString str("%d", aPid);
|
2015-01-08 17:39:43 +03:00
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
if (write(mCpuCGroupProcsFd, str.get(), strlen(str.get())) < 0) {
|
|
|
|
HAL_ERR("Couldn't add PID %d to the %s cpu control group", aPid, mGroup.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mMemCGroupProcsFd >= 0) {
|
|
|
|
nsPrintfCString str("%d", aPid);
|
2015-01-08 17:39:43 +03:00
|
|
|
|
2015-01-12 22:19:00 +03:00
|
|
|
if (write(mMemCGroupProcsFd, str.get(), strlen(str.get())) < 0) {
|
|
|
|
HAL_ERR("Couldn't add PID %d to the %s memory control group", aPid, mGroup.get());
|
|
|
|
}
|
2015-01-08 17:39:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the PriorityClass associated with the given ProcessPriority.
|
|
|
|
*
|
|
|
|
* If you pass an invalid ProcessPriority value, we return null.
|
|
|
|
*
|
|
|
|
* The pointers returned here are owned by GetPriorityClass (don't free them
|
|
|
|
* yourself). They are guaranteed to stick around until shutdown.
|
|
|
|
*/
|
|
|
|
PriorityClass*
|
|
|
|
GetPriorityClass(ProcessPriority aPriority)
|
|
|
|
{
|
|
|
|
static StaticAutoPtr<nsTArray<PriorityClass>> priorityClasses;
|
|
|
|
|
|
|
|
// Initialize priorityClasses if this is the first time we're running this
|
|
|
|
// method.
|
|
|
|
if (!priorityClasses) {
|
|
|
|
priorityClasses = new nsTArray<PriorityClass>();
|
|
|
|
ClearOnShutdown(&priorityClasses);
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < NUM_PROCESS_PRIORITY; i++) {
|
|
|
|
priorityClasses->AppendElement(PriorityClass(ProcessPriority(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPriority < 0 ||
|
|
|
|
static_cast<uint32_t>(aPriority) >= priorityClasses->Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &(*priorityClasses)[aPriority];
|
|
|
|
}
|
|
|
|
|
2012-10-31 21:29:14 +04:00
|
|
|
static void
|
|
|
|
EnsureKernelLowMemKillerParamsSet()
|
|
|
|
{
|
|
|
|
static bool kernelLowMemKillerParamsSet;
|
|
|
|
if (kernelLowMemKillerParamsSet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kernelLowMemKillerParamsSet = true;
|
|
|
|
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Setting kernel's low-mem killer parameters.");
|
2012-10-31 21:29:14 +04:00
|
|
|
|
|
|
|
// Set /sys/module/lowmemorykiller/parameters/{adj,minfree,notify_trigger}
|
|
|
|
// according to our prefs. These files let us tune when the kernel kills
|
|
|
|
// processes when we're low on memory, and when it notifies us that we're
|
|
|
|
// running low on available memory.
|
|
|
|
//
|
|
|
|
// adj and minfree are both comma-separated lists of integers. If adj="A,B"
|
|
|
|
// and minfree="X,Y", then the kernel will kill processes with oom_adj
|
|
|
|
// A or higher once we have fewer than X pages of memory free, and will kill
|
|
|
|
// processes with oom_adj B or higher once we have fewer than Y pages of
|
|
|
|
// memory free.
|
|
|
|
//
|
|
|
|
// notify_trigger is a single integer. If we set notify_trigger=Z, then
|
|
|
|
// we'll get notified when there are fewer than Z pages of memory free. (See
|
|
|
|
// GonkMemoryPressureMonitoring.cpp.)
|
|
|
|
|
|
|
|
// Build the adj and minfree strings.
|
|
|
|
nsAutoCString adjParams;
|
|
|
|
nsAutoCString minfreeParams;
|
|
|
|
|
2014-12-18 02:05:28 +03:00
|
|
|
DebugOnly<int32_t> lowerBoundOfNextOomScoreAdj = OOM_SCORE_ADJ_MIN - 1;
|
|
|
|
DebugOnly<int32_t> lowerBoundOfNextKillUnderKB = 0;
|
2013-09-11 11:12:56 +04:00
|
|
|
int32_t countOfLowmemorykillerParametersSets = 0;
|
2013-06-29 08:14:02 +04:00
|
|
|
|
2014-12-17 19:20:09 +03:00
|
|
|
long page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
|
2013-06-29 08:14:02 +04:00
|
|
|
for (int i = NUM_PROCESS_PRIORITY - 1; i >= 0; i--) {
|
2013-02-15 00:41:30 +04:00
|
|
|
// The system doesn't function correctly if we're missing these prefs, so
|
|
|
|
// crash loudly.
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
PriorityClass* pc = GetPriorityClass(static_cast<ProcessPriority>(i));
|
2013-05-10 00:27:06 +04:00
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
int32_t oomScoreAdj = pc->OomScoreAdj();
|
|
|
|
int32_t killUnderKB = pc->KillUnderKB();
|
2012-10-31 21:29:14 +04:00
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
if (killUnderKB == 0) {
|
2014-03-13 11:24:42 +04:00
|
|
|
// ProcessPriority values like PROCESS_PRIORITY_FOREGROUND_KEYBOARD,
|
|
|
|
// which has only OomScoreAdjust but lacks KillUnderMB value, will not
|
|
|
|
// create new LMK parameters.
|
2013-09-11 11:12:56 +04:00
|
|
|
continue;
|
2012-10-31 21:29:14 +04:00
|
|
|
}
|
|
|
|
|
2013-06-29 08:14:02 +04:00
|
|
|
// The LMK in kernel silently malfunctions if we assign the parameters
|
|
|
|
// in non-increasing order, so we add this assertion here. See bug 887192.
|
|
|
|
MOZ_ASSERT(oomScoreAdj > lowerBoundOfNextOomScoreAdj);
|
2014-03-04 14:23:00 +04:00
|
|
|
MOZ_ASSERT(killUnderKB > lowerBoundOfNextKillUnderKB);
|
2013-06-29 08:14:02 +04:00
|
|
|
|
2013-09-11 11:12:56 +04:00
|
|
|
// The LMK in kernel only accept 6 sets of LMK parameters. See bug 914728.
|
|
|
|
MOZ_ASSERT(countOfLowmemorykillerParametersSets < 6);
|
|
|
|
|
2012-10-31 21:29:14 +04:00
|
|
|
// adj is in oom_adj units.
|
|
|
|
adjParams.AppendPrintf("%d,", OomAdjOfOomScoreAdj(oomScoreAdj));
|
|
|
|
|
|
|
|
// minfree is in pages.
|
2014-12-17 19:20:09 +03:00
|
|
|
minfreeParams.AppendPrintf("%ld,", killUnderKB * 1024 / page_size);
|
2013-06-29 08:14:02 +04:00
|
|
|
|
|
|
|
lowerBoundOfNextOomScoreAdj = oomScoreAdj;
|
2014-03-04 14:23:00 +04:00
|
|
|
lowerBoundOfNextKillUnderKB = killUnderKB;
|
2013-09-11 11:12:56 +04:00
|
|
|
countOfLowmemorykillerParametersSets++;
|
2012-10-31 21:29:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off trailing commas.
|
|
|
|
adjParams.Cut(adjParams.Length() - 1, 1);
|
|
|
|
minfreeParams.Cut(minfreeParams.Length() - 1, 1);
|
|
|
|
if (!adjParams.IsEmpty() && !minfreeParams.IsEmpty()) {
|
2015-12-29 13:49:50 +03:00
|
|
|
WriteSysFile("/sys/module/lowmemorykiller/parameters/adj", adjParams.get());
|
|
|
|
WriteSysFile("/sys/module/lowmemorykiller/parameters/minfree",
|
|
|
|
minfreeParams.get());
|
2012-10-31 21:29:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the low-memory-notification threshold.
|
2014-03-04 14:23:00 +04:00
|
|
|
int32_t lowMemNotifyThresholdKB;
|
2012-10-31 21:29:14 +04:00
|
|
|
if (NS_SUCCEEDED(Preferences::GetInt(
|
2014-03-04 14:23:00 +04:00
|
|
|
"hal.processPriorityManager.gonk.notifyLowMemUnderKB",
|
|
|
|
&lowMemNotifyThresholdKB))) {
|
2012-10-31 21:29:14 +04:00
|
|
|
|
|
|
|
// notify_trigger is in pages.
|
2015-12-29 13:49:50 +03:00
|
|
|
WriteSysFile("/sys/module/lowmemorykiller/parameters/notify_trigger",
|
2014-12-17 19:20:09 +03:00
|
|
|
nsPrintfCString("%ld", lowMemNotifyThresholdKB * 1024 / page_size).get());
|
2012-10-31 21:29:14 +04:00
|
|
|
}
|
2014-01-11 10:22:55 +04:00
|
|
|
|
|
|
|
// Ensure OOM events appear in logcat
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OomVictimLogger> oomLogger = new OomVictimLogger();
|
2014-01-11 10:22:55 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->AddObserver(oomLogger, "ipc:content-shutdown", false);
|
|
|
|
}
|
2012-10-31 21:29:14 +04:00
|
|
|
}
|
|
|
|
|
2012-08-05 09:09:39 +04:00
|
|
|
void
|
2015-02-25 11:37:38 +03:00
|
|
|
SetProcessPriority(int aPid, ProcessPriority aPriority, uint32_t aLRU)
|
2012-08-05 09:09:39 +04:00
|
|
|
{
|
2015-02-26 14:43:22 +03:00
|
|
|
HAL_LOG("SetProcessPriority(pid=%d, priority=%d, LRU=%u)",
|
2015-02-25 11:37:38 +03:00
|
|
|
aPid, aPriority, aLRU);
|
2012-08-05 09:09:39 +04:00
|
|
|
|
2012-10-31 21:29:14 +04:00
|
|
|
// If this is the first time SetProcessPriority was called, set the kernel's
|
|
|
|
// OOM parameters according to our prefs.
|
|
|
|
//
|
|
|
|
// We could/should do this on startup instead of waiting for the first
|
|
|
|
// SetProcessPriorityCall. But in practice, the master process needs to set
|
|
|
|
// its priority early in the game, so we can reasonably rely on
|
|
|
|
// SetProcessPriority being called early in startup.
|
|
|
|
EnsureKernelLowMemKillerParamsSet();
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
PriorityClass* pc = GetPriorityClass(aPriority);
|
2014-09-26 18:37:31 +04:00
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
int oomScoreAdj = pc->OomScoreAdj();
|
2012-09-14 22:52:07 +04:00
|
|
|
|
2015-02-25 11:37:38 +03:00
|
|
|
RoundOomScoreAdjUpWithLRU(oomScoreAdj, aLRU);
|
2013-10-01 07:54:59 +04:00
|
|
|
|
2014-09-26 18:37:31 +04:00
|
|
|
// We try the newer interface first, and fall back to the older interface
|
|
|
|
// on failure.
|
2015-12-29 13:49:50 +03:00
|
|
|
if (!WriteSysFile(nsPrintfCString("/proc/%d/oom_score_adj", aPid).get(),
|
|
|
|
nsPrintfCString("%d", oomScoreAdj).get()))
|
2014-09-26 18:37:31 +04:00
|
|
|
{
|
2015-12-29 13:49:50 +03:00
|
|
|
WriteSysFile(nsPrintfCString("/proc/%d/oom_adj", aPid).get(),
|
|
|
|
nsPrintfCString("%d", OomAdjOfOomScoreAdj(oomScoreAdj)).get());
|
2013-05-10 00:27:06 +04:00
|
|
|
}
|
|
|
|
|
2015-01-08 17:39:43 +03:00
|
|
|
HAL_LOG("Assigning pid %d to cgroup %s", aPid, pc->CGroup().get());
|
|
|
|
pc->AddProcess(aPid);
|
2012-08-05 09:09:39 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 19:37:00 +04:00
|
|
|
static bool
|
|
|
|
IsValidRealTimePriority(int aValue, int aSchedulePolicy)
|
|
|
|
{
|
|
|
|
return (aValue >= sched_get_priority_min(aSchedulePolicy)) &&
|
|
|
|
(aValue <= sched_get_priority_max(aSchedulePolicy));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetThreadNiceValue(pid_t aTid, ThreadPriority aThreadPriority, int aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aThreadPriority < NUM_THREAD_PRIORITY);
|
|
|
|
MOZ_ASSERT(aThreadPriority >= 0);
|
|
|
|
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Setting thread %d to priority level %s; nice level %d",
|
|
|
|
aTid, ThreadPriorityToString(aThreadPriority), aValue);
|
2014-05-05 19:37:00 +04:00
|
|
|
int rv = setpriority(PRIO_PROCESS, aTid, aValue);
|
|
|
|
|
|
|
|
if (rv) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to set thread %d to priority level %s; error %s", aTid,
|
|
|
|
ThreadPriorityToString(aThreadPriority), strerror(errno));
|
2014-05-05 19:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetRealTimeThreadPriority(pid_t aTid,
|
|
|
|
ThreadPriority aThreadPriority,
|
|
|
|
int aValue)
|
|
|
|
{
|
|
|
|
int policy = SCHED_FIFO;
|
|
|
|
|
|
|
|
MOZ_ASSERT(aThreadPriority < NUM_THREAD_PRIORITY);
|
|
|
|
MOZ_ASSERT(aThreadPriority >= 0);
|
|
|
|
MOZ_ASSERT(IsValidRealTimePriority(aValue, policy), "Invalid real time priority");
|
|
|
|
|
|
|
|
// Setting real time priorities requires using sched_setscheduler
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Setting thread %d to priority level %s; Real Time priority %d, "
|
|
|
|
"Schedule FIFO", aTid, ThreadPriorityToString(aThreadPriority),
|
|
|
|
aValue);
|
2014-05-05 19:37:00 +04:00
|
|
|
sched_param schedParam;
|
|
|
|
schedParam.sched_priority = aValue;
|
|
|
|
int rv = sched_setscheduler(aTid, policy, &schedParam);
|
|
|
|
|
|
|
|
if (rv) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to set thread %d to real time priority level %s; error %s",
|
|
|
|
aTid, ThreadPriorityToString(aThreadPriority), strerror(errno));
|
2014-05-05 19:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:37:31 +04:00
|
|
|
/*
|
|
|
|
* Used to store the nice value adjustments and real time priorities for the
|
|
|
|
* various thread priority levels.
|
|
|
|
*/
|
|
|
|
struct ThreadPriorityPrefs {
|
|
|
|
bool initialized;
|
|
|
|
struct {
|
|
|
|
int nice;
|
|
|
|
int realTime;
|
|
|
|
} priorities[NUM_THREAD_PRIORITY];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads the preferences for the various process priority levels and sets up
|
|
|
|
* watchers so that if they're dynamically changed the change is reflected on
|
|
|
|
* the appropriate variables.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
EnsureThreadPriorityPrefs(ThreadPriorityPrefs* prefs)
|
|
|
|
{
|
|
|
|
if (prefs->initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = THREAD_PRIORITY_COMPOSITOR; i < NUM_THREAD_PRIORITY; i++) {
|
|
|
|
ThreadPriority priority = static_cast<ThreadPriority>(i);
|
|
|
|
|
|
|
|
// Read the nice values
|
|
|
|
const char* threadPriorityStr = ThreadPriorityToString(priority);
|
|
|
|
nsPrintfCString niceStr("hal.gonk.%s.nice", threadPriorityStr);
|
|
|
|
Preferences::AddIntVarCache(&prefs->priorities[i].nice, niceStr.get());
|
|
|
|
|
|
|
|
// Read the real-time priorities
|
|
|
|
nsPrintfCString realTimeStr("hal.gonk.%s.rt_priority", threadPriorityStr);
|
|
|
|
Preferences::AddIntVarCache(&prefs->priorities[i].realTime,
|
|
|
|
realTimeStr.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
prefs->initialized = true;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:37:00 +04:00
|
|
|
static void
|
2015-05-07 16:53:47 +03:00
|
|
|
DoSetThreadPriority(pid_t aTid, hal::ThreadPriority aThreadPriority)
|
2014-05-05 19:37:00 +04:00
|
|
|
{
|
|
|
|
// See bug 999115, we can only read preferences on the main thread otherwise
|
|
|
|
// we create a race condition in HAL
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Can only set thread priorities on main thread");
|
|
|
|
MOZ_ASSERT(aThreadPriority >= 0);
|
|
|
|
|
2014-09-26 18:37:31 +04:00
|
|
|
static ThreadPriorityPrefs prefs = { 0 };
|
|
|
|
EnsureThreadPriorityPrefs(&prefs);
|
|
|
|
|
2014-05-05 19:37:00 +04:00
|
|
|
switch (aThreadPriority) {
|
|
|
|
case THREAD_PRIORITY_COMPOSITOR:
|
|
|
|
break;
|
|
|
|
default:
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_ERR("Unrecognized thread priority %d; Doing nothing",
|
|
|
|
aThreadPriority);
|
2014-05-05 19:37:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:37:31 +04:00
|
|
|
int realTimePriority = prefs.priorities[aThreadPriority].realTime;
|
2014-05-05 19:37:00 +04:00
|
|
|
|
|
|
|
if (IsValidRealTimePriority(realTimePriority, SCHED_FIFO)) {
|
|
|
|
SetRealTimeThreadPriority(aTid, aThreadPriority, realTimePriority);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:37:31 +04:00
|
|
|
SetThreadNiceValue(aTid, aThreadPriority,
|
|
|
|
prefs.priorities[aThreadPriority].nice);
|
2014-05-05 19:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class sets the priority of threads given the kernel thread's id and a
|
|
|
|
* value taken from hal::ThreadPriority.
|
|
|
|
*
|
|
|
|
* This runnable must always be dispatched to the main thread otherwise it will fail.
|
|
|
|
* We have to run this from the main thread since preferences can only be read on
|
|
|
|
* main thread.
|
|
|
|
*/
|
|
|
|
class SetThreadPriorityRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SetThreadPriorityRunnable(pid_t aThreadId, hal::ThreadPriority aThreadPriority)
|
|
|
|
: mThreadId(aThreadId)
|
|
|
|
, mThreadPriority(aThreadPriority)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Can only set thread priorities on main thread");
|
2015-05-07 16:53:47 +03:00
|
|
|
hal_impl::DoSetThreadPriority(mThreadId, mThreadPriority);
|
2014-05-05 19:37:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
pid_t mThreadId;
|
|
|
|
hal::ThreadPriority mThreadPriority;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-05-05 19:37:00 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
SetCurrentThreadPriority(ThreadPriority aThreadPriority)
|
2015-05-07 16:53:47 +03:00
|
|
|
{
|
|
|
|
pid_t threadId = gettid();
|
|
|
|
hal_impl::SetThreadPriority(threadId, aThreadPriority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetThreadPriority(PlatformThreadId aThreadId,
|
|
|
|
ThreadPriority aThreadPriority)
|
2014-05-05 19:37:00 +04:00
|
|
|
{
|
|
|
|
switch (aThreadPriority) {
|
|
|
|
case THREAD_PRIORITY_COMPOSITOR: {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2015-05-07 16:53:47 +03:00
|
|
|
new SetThreadPriorityRunnable(aThreadId, aThreadPriority);
|
2014-05-05 19:37:00 +04:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Unrecognized thread priority %d; Doing nothing",
|
|
|
|
aThreadPriority);
|
2014-05-05 19:37:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 13:28:34 +04:00
|
|
|
void
|
2014-07-18 05:30:47 +04:00
|
|
|
FactoryReset(FactoryResetReason& aReason)
|
2012-10-04 13:28:34 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRecoveryService> recoveryService =
|
|
|
|
do_GetService("@mozilla.org/recovery-service;1");
|
|
|
|
if (!recoveryService) {
|
|
|
|
NS_WARNING("Could not get recovery service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-18 05:30:47 +04:00
|
|
|
if (aReason == FactoryResetReason::Wipe) {
|
|
|
|
recoveryService->FactoryReset("wipe");
|
2014-11-26 14:46:00 +03:00
|
|
|
} else if (aReason == FactoryResetReason::Root) {
|
|
|
|
recoveryService->FactoryReset("root");
|
2014-07-18 05:30:47 +04:00
|
|
|
} else {
|
|
|
|
recoveryService->FactoryReset("normal");
|
|
|
|
}
|
2012-10-04 13:28:34 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 23:31:53 +04:00
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|