Android: Replace the java based tick timer implemented with a not as bloated and more accurate linux hrtimer based one. Further reduces idle cpu usage (0% on my phone but still 1-2% on a Samsung Galaxy S).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28784 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2010-12-10 15:14:18 +00:00
parent 44cdce9b29
commit c47d813451
4 changed files with 66 additions and 130 deletions

View file

@ -1,76 +0,0 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2010 Thomas Martitz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
package org.rockbox;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.util.Log;
public class RockboxTimer extends Timer
{
private class RockboxTimerTask extends TimerTask {
private RockboxTimer timer;
public RockboxTimerTask(RockboxTimer parent)
{
super();
timer = parent;
}
@Override
public void run()
{
timerTask();
synchronized(timer) {
timer.notify();
}
}
}
public RockboxTimer(Context c, long period_inverval_in_ms)
{
super("tick timer");
schedule(new RockboxTimerTask(this), 0, period_inverval_in_ms);
}
@SuppressWarnings("unused")
private void LOG(CharSequence text)
{
Log.d("Rockbox", (String) text);
}
/* methods called from native, keep them simple */
public void java_wait_for_interrupt()
{
synchronized(this)
{
try {
this.wait();
} catch (InterruptedException e) {
/* Not an error: wakeup and return */
}
}
}
public native void timerTask();
}

View file

@ -20,73 +20,83 @@
****************************************************************************/
#include <jni.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <semaphore.h>
#include "config.h"
#include "system.h"
#include "button.h"
#include "audio.h"
#include "panic.h"
extern JNIEnv *env_ptr;
extern jobject RockboxService_instance;
static jclass RockboxTimer_class;
static jobject RockboxTimer_instance;
static jmethodID java_wait_for_interrupt;
static bool initialized = false;
static sem_t wfi_sem;
/*
* This is called from the separate Timer java thread. I have not added any
* interrupt simulation to it (like the sdl counterpart does),
* I think this is probably not needed, unless code calls disable_interrupt()
* in order to be protected from tick tasks, but I can't remember a place right
* now.
*
* No synchronisation mechanism either. This could possibly be problematic,
* but we'll see :)
*/
JNIEXPORT void JNICALL
Java_org_rockbox_RockboxTimer_timerTask(JNIEnv *env, jobject this)
* call tick tasks and wake the scheduler up */
void timer_signal(int sig)
{
(void)env;
(void)this;
(void)sig;
call_tick_tasks();
interrupt();
}
void tick_start(unsigned int interval_in_ms)
{
JNIEnv e = *env_ptr;
/* first, create a new Timer instance */
RockboxTimer_class = e->FindClass(env_ptr, "org/rockbox/RockboxTimer");
jmethodID constructor = e->GetMethodID(env_ptr,
RockboxTimer_class,
"<init>",
"(Landroid/content/Context;J)V");
/* the constructor will do the tick_start */
RockboxTimer_instance = e->NewObject(env_ptr,
RockboxTimer_class,
constructor,
RockboxService_instance,
(jlong)interval_in_ms);
/* get our wfi func also */
java_wait_for_interrupt = e->GetMethodID(env_ptr,
RockboxTimer_class,
"java_wait_for_interrupt",
"()V");
/* it's now safe to call java_wait_for_interrupt */
initialized = true;
}
/*
* wait on the sem which the signal handler posts to save cpu time (aka sleep)
*
* other mechanisms could use them as well */
void wait_for_interrupt(void)
{
if (LIKELY(initialized))
{
(*env_ptr)->CallVoidMethod(env_ptr,
RockboxTimer_instance,
java_wait_for_interrupt);
}
sem_wait(&wfi_sem);
}
void interrupt(void)
{
int s;
/* unless idling, we usually have more interrupt() than wait_for_interrupt()
* don't post unecessarily because wait_for_interrupt() would need to
* decrement for each wasted sem_post(), instead of sleeping directly */
sem_getvalue(&wfi_sem, &s);
if (s <= 0)
sem_post(&wfi_sem);
}
/*
* setup a hrtimer to send a signal to our process every tick */
void tick_start(unsigned int interval_in_ms)
{
int ret = 0;
sigset_t proc_set;
timer_t timerid;
struct itimerspec ts;
sigevent_t sigev = {
.sigev_notify = SIGEV_SIGNAL,
.sigev_signo = SIGUSR2,
};
ts.it_value.tv_sec = ts.it_interval.tv_sec = 0;
ts.it_value.tv_nsec = ts.it_interval.tv_nsec = interval_in_ms*1000*1000;
/* add the signal handler */
signal(SIGUSR2, timer_signal);
/* add the timer */
ret |= timer_create(CLOCK_REALTIME, &sigev, &timerid);
ret |= timer_settime(timerid, 0, &ts, NULL);
/* unblock SIGUSR2 so the handler can run */
ret |= sigprocmask(0, NULL, &proc_set);
ret |= sigdelset(&proc_set, SIGUSR2);
ret |= sigprocmask(SIG_SETMASK, &proc_set, NULL);
ret |= sem_init(&wfi_sem, 0, 0);
if (ret != 0)
panicf("%s(): %s\n", __func__, strerror(errno));
}
bool timer_register(int reg_prio, void (*unregister_callback)(void),
long cycles, void (*timer_callback)(void))
{

View file

@ -27,6 +27,8 @@
#define restore_irq(level) (void)level
void power_off(void);
void wait_for_interrupt(void);
void interrupt(void);
#endif /* __SYSTEM_TARGET_H__ */

View file

@ -23,6 +23,7 @@
****************************************************************************/
#include <jni.h>
#include <system.h>
/*---------------------------------------------------------------------------
* Start the thread running and terminate it if it returns
*---------------------------------------------------------------------------
@ -89,7 +90,6 @@ static inline void load_context(const void* addr)
* wait_for_interrupt is implemented in kernel-android.c
**/
extern void wait_for_interrupt(void);
static inline void core_sleep(void)
{
wait_for_interrupt();