#include <inttypes.h>
#include <math.h>
+#include <sched.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/stat.h>
#define SENSOR_SERVICE_DIR "/data/system/sensor_service"
#define SENSOR_SERVICE_HMAC_KEY_FILE SENSOR_SERVICE_DIR "/hmac_key"
+#define SENSOR_SERVICE_SCHED_FIFO_PRIORITY 10
// Permissions.
static const String16 sDump("android.permission.DUMP");
return true;
}
+// Set main thread to SCHED_FIFO to lower sensor event latency when system is under load
+void SensorService::enableSchedFifoMode() {
+ struct sched_param param = {0};
+ param.sched_priority = SENSOR_SERVICE_SCHED_FIFO_PRIORITY;
+ if (sched_setscheduler(getTid(), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m) != 0) {
+ ALOGE("Couldn't set SCHED_FIFO for SensorService thread");
+ }
+}
+
void SensorService::onFirstRef() {
ALOGD("nuSensorService starting...");
SensorDevice& dev(SensorDevice::getInstance());
mAckReceiver = new SensorEventAckReceiver(this);
mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
run("SensorService", PRIORITY_URGENT_DISPLAY);
+
+ // priority can only be changed after run
+ enableSchedFifoMode();
}
}
}
// Either read from storage or create a new one.
static bool initializeHmacKey();
+ // Enable SCHED_FIFO priority for thread
+ void enableSchedFifoMode();
static uint8_t sHmacGlobalKey[128];
static bool sHmacGlobalKeyIsValid;