2005-06-13 16:59:11 +04:00
|
|
|
/*
|
2010-01-04 19:23:14 +03:00
|
|
|
* Copyright 2003-2010, Axel Dörfler, axeld@pinc-software.de.
|
2005-06-13 16:59:11 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <kernel_daemon.h>
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
#include <new>
|
|
|
|
#include <signal.h>
|
2005-06-13 16:59:11 +04:00
|
|
|
#include <stdlib.h>
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
#include <KernelExport.h>
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
#include <elf.h>
|
2008-05-08 19:08:14 +04:00
|
|
|
#include <lock.h>
|
|
|
|
#include <util/AutoLock.h>
|
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
|
2005-06-13 16:59:11 +04:00
|
|
|
|
|
|
|
// The use of snooze() in the kernel_daemon() function is very inaccurate, of
|
|
|
|
// course - the time the daemons need to execute add up in each iteration.
|
|
|
|
// But since the kernel daemon is documented to be very inaccurate, this
|
|
|
|
// actually might be okay (and that's why it's implemented this way now :-).
|
|
|
|
// BeOS R5 seems to do it in the same way, anyway.
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
struct daemon : DoublyLinkedListLinkImpl<struct daemon> {
|
2003-01-27 16:53:10 +03:00
|
|
|
daemon_hook function;
|
2008-05-08 19:08:14 +04:00
|
|
|
void* arg;
|
2003-01-26 20:31:33 +03:00
|
|
|
int32 frequency;
|
|
|
|
int32 offset;
|
2010-04-13 16:09:56 +04:00
|
|
|
bool executing;
|
2003-01-26 20:31:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
typedef DoublyLinkedList<struct daemon> DaemonList;
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
class KernelDaemon {
|
|
|
|
public:
|
|
|
|
status_t Init(const char* name);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t Register(daemon_hook function, void* arg,
|
|
|
|
int frequency);
|
|
|
|
status_t Unregister(daemon_hook function, void* arg);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
void Dump();
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
private:
|
|
|
|
static status_t _DaemonThreadEntry(void* data);
|
2008-11-07 15:22:05 +03:00
|
|
|
struct daemon* _NextDaemon(struct daemon& marker);
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t _DaemonThread();
|
2010-04-13 16:09:56 +04:00
|
|
|
bool _IsDaemon() const;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
private:
|
2008-11-07 15:22:05 +03:00
|
|
|
recursive_lock fLock;
|
2008-08-22 05:13:18 +04:00
|
|
|
DaemonList fDaemons;
|
|
|
|
thread_id fThread;
|
2010-04-13 16:09:56 +04:00
|
|
|
ConditionVariable fUnregisterCondition;
|
|
|
|
int32 fUnregisterWaiters;
|
2008-08-22 05:13:18 +04:00
|
|
|
};
|
2008-05-08 19:08:14 +04:00
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
static KernelDaemon sKernelDaemon;
|
|
|
|
static KernelDaemon sResourceResizer;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
KernelDaemon::Init(const char* name)
|
|
|
|
{
|
2008-11-07 15:22:05 +03:00
|
|
|
recursive_lock_init(&fLock, name);
|
2008-08-22 05:13:18 +04:00
|
|
|
|
|
|
|
fThread = spawn_kernel_thread(&_DaemonThreadEntry, name, B_LOW_PRIORITY,
|
|
|
|
this);
|
|
|
|
if (fThread < 0)
|
|
|
|
return fThread;
|
|
|
|
|
|
|
|
send_signal_etc(fThread, SIGCONT, B_DO_NOT_RESCHEDULE);
|
2010-04-13 16:09:56 +04:00
|
|
|
fUnregisterCondition.Init(this, name);
|
2008-05-08 19:08:14 +04:00
|
|
|
|
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t
|
|
|
|
KernelDaemon::Register(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
if (function == NULL || frequency < 1)
|
|
|
|
return B_BAD_VALUE;
|
2008-05-08 19:08:14 +04:00
|
|
|
|
2009-02-01 23:48:02 +03:00
|
|
|
struct ::daemon* daemon = new(std::nothrow) (struct ::daemon);
|
2008-08-22 05:13:18 +04:00
|
|
|
if (daemon == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2008-05-08 19:08:14 +04:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
daemon->function = function;
|
|
|
|
daemon->arg = arg;
|
|
|
|
daemon->frequency = frequency;
|
2010-04-13 16:09:56 +04:00
|
|
|
daemon->executing = false;
|
2008-08-22 05:13:18 +04:00
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
RecursiveLocker _(fLock);
|
2008-08-22 05:13:18 +04:00
|
|
|
|
|
|
|
if (frequency > 1) {
|
|
|
|
// we try to balance the work-load for each daemon run
|
|
|
|
// (beware, it's a very simple algorithm, yet effective)
|
|
|
|
|
|
|
|
DaemonList::Iterator iterator = fDaemons.GetIterator();
|
|
|
|
int32 num = 0;
|
|
|
|
|
|
|
|
while (iterator.HasNext()) {
|
|
|
|
if (iterator.Next()->frequency == frequency)
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon->offset = num % frequency;
|
|
|
|
} else
|
|
|
|
daemon->offset = 0;
|
|
|
|
|
|
|
|
fDaemons.Add(daemon);
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
KernelDaemon::Unregister(daemon_hook function, void* arg)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
2010-04-13 16:09:56 +04:00
|
|
|
RecursiveLocker locker(fLock);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
DaemonList::Iterator iterator = fDaemons.GetIterator();
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
// search for the daemon and remove it from the list
|
2008-05-08 19:08:14 +04:00
|
|
|
while (iterator.HasNext()) {
|
|
|
|
struct daemon* daemon = iterator.Next();
|
|
|
|
|
2003-01-26 20:31:33 +03:00
|
|
|
if (daemon->function == function && daemon->arg == arg) {
|
|
|
|
// found it!
|
2010-04-13 16:09:56 +04:00
|
|
|
if (!_IsDaemon()) {
|
|
|
|
// wait if it's busy
|
|
|
|
while (daemon->executing) {
|
|
|
|
fUnregisterWaiters++;
|
|
|
|
|
|
|
|
ConditionVariableEntry entry;
|
|
|
|
fUnregisterCondition.Add(&entry);
|
|
|
|
|
|
|
|
locker.Unlock();
|
|
|
|
|
|
|
|
entry.Wait();
|
|
|
|
|
|
|
|
locker.Lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
iterator.Remove();
|
2008-05-22 15:37:20 +04:00
|
|
|
delete daemon;
|
2008-05-08 19:08:14 +04:00
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
void
|
|
|
|
KernelDaemon::Dump()
|
|
|
|
{
|
|
|
|
DaemonList::Iterator iterator = fDaemons.GetIterator();
|
|
|
|
|
|
|
|
while (iterator.HasNext()) {
|
|
|
|
struct daemon* daemon = iterator.Next();
|
2010-01-04 19:23:14 +03:00
|
|
|
const char* imageName;
|
|
|
|
const char* symbol;
|
2008-11-07 15:22:05 +03:00
|
|
|
bool exactMatch;
|
|
|
|
|
|
|
|
status_t status = elf_debug_lookup_symbol_address(
|
|
|
|
(addr_t)daemon->function, NULL, &symbol, &imageName, &exactMatch);
|
|
|
|
if (status == B_OK && exactMatch) {
|
|
|
|
if (strchr(imageName, '/') != NULL)
|
|
|
|
imageName = strrchr(imageName, '/') + 1;
|
|
|
|
|
2010-04-13 16:09:56 +04:00
|
|
|
kprintf("\t%s:%s (%p)", imageName, symbol, daemon->function);
|
|
|
|
} else
|
|
|
|
kprintf("\t%p", daemon->function);
|
|
|
|
|
|
|
|
kprintf(", arg %p%s\n", daemon->arg,
|
|
|
|
daemon->executing ? " (running) " : "");
|
2008-11-07 15:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
/*static*/ status_t
|
|
|
|
KernelDaemon::_DaemonThreadEntry(void* data)
|
2003-01-26 20:31:33 +03:00
|
|
|
{
|
2008-08-22 05:13:18 +04:00
|
|
|
return ((KernelDaemon*)data)->_DaemonThread();
|
|
|
|
}
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
struct daemon*
|
|
|
|
KernelDaemon::_NextDaemon(struct daemon& marker)
|
|
|
|
{
|
|
|
|
struct daemon* daemon;
|
|
|
|
|
2010-01-03 21:54:52 +03:00
|
|
|
if (marker.arg == NULL) {
|
|
|
|
// The marker is not part of the list yet, just return the first entry
|
2008-11-07 15:22:05 +03:00
|
|
|
daemon = fDaemons.Head();
|
|
|
|
} else {
|
2010-01-04 03:11:56 +03:00
|
|
|
daemon = fDaemons.GetNext(&marker);
|
2008-11-07 15:22:05 +03:00
|
|
|
fDaemons.Remove(&marker);
|
|
|
|
}
|
|
|
|
|
2010-01-03 21:54:52 +03:00
|
|
|
marker.arg = daemon;
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
if (daemon != NULL)
|
2010-01-04 03:11:56 +03:00
|
|
|
fDaemons.InsertAfter(daemon, &marker);
|
2008-11-07 15:22:05 +03:00
|
|
|
|
|
|
|
return daemon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
status_t
|
|
|
|
KernelDaemon::_DaemonThread()
|
|
|
|
{
|
2008-11-07 15:22:05 +03:00
|
|
|
struct daemon marker;
|
2008-08-22 05:13:18 +04:00
|
|
|
int32 iteration = 0;
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2010-01-03 21:54:52 +03:00
|
|
|
marker.arg = NULL;
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
while (true) {
|
2008-11-07 15:22:05 +03:00
|
|
|
RecursiveLocker locker(fLock);
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
// iterate through the list and execute each daemon if needed
|
2008-11-07 15:22:05 +03:00
|
|
|
while (struct daemon* daemon = _NextDaemon(marker)) {
|
2010-04-13 16:09:56 +04:00
|
|
|
daemon->executing = true;
|
|
|
|
locker.Unlock();
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
if (((iteration + daemon->offset) % daemon->frequency) == 0)
|
|
|
|
daemon->function(daemon->arg, iteration);
|
2010-04-13 16:09:56 +04:00
|
|
|
|
|
|
|
locker.Lock();
|
|
|
|
daemon->executing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fUnregisterWaiters != 0) {
|
|
|
|
fUnregisterCondition.NotifyAll();
|
|
|
|
fUnregisterWaiters = 0;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|
2008-11-07 15:22:05 +03:00
|
|
|
|
|
|
|
locker.Unlock();
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
iteration++;
|
|
|
|
snooze(100000); // 0.1 seconds
|
|
|
|
}
|
2003-01-26 20:31:33 +03:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-13 16:09:56 +04:00
|
|
|
bool
|
|
|
|
KernelDaemon::_IsDaemon() const
|
|
|
|
{
|
|
|
|
return find_thread(NULL) == fThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
2008-08-22 05:13:18 +04:00
|
|
|
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
static int
|
|
|
|
dump_daemons(int argc, char** argv)
|
|
|
|
{
|
|
|
|
kprintf("kernel daemons:\n");
|
|
|
|
sKernelDaemon.Dump();
|
|
|
|
|
|
|
|
kprintf("\nresource resizers:\n");
|
|
|
|
sResourceResizer.Dump();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
extern "C" status_t
|
|
|
|
register_kernel_daemon(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
return sKernelDaemon.Register(function, arg, frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
unregister_kernel_daemon(daemon_hook function, void* arg)
|
|
|
|
{
|
|
|
|
return sKernelDaemon.Unregister(function, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
register_resource_resizer(daemon_hook function, void* arg, int frequency)
|
|
|
|
{
|
|
|
|
return sResourceResizer.Register(function, arg, frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" status_t
|
|
|
|
unregister_resource_resizer(daemon_hook function, void* arg)
|
|
|
|
{
|
|
|
|
return sResourceResizer.Unregister(function, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 15:22:05 +03:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2008-05-08 19:08:14 +04:00
|
|
|
extern "C" status_t
|
2003-01-26 20:31:33 +03:00
|
|
|
kernel_daemon_init(void)
|
|
|
|
{
|
2008-08-22 05:13:18 +04:00
|
|
|
new(&sKernelDaemon) KernelDaemon;
|
|
|
|
if (sKernelDaemon.Init("kernel daemon") != B_OK)
|
|
|
|
panic("kernel_daemon_init(): failed to init kernel daemon");
|
2003-01-26 20:31:33 +03:00
|
|
|
|
2008-08-22 05:13:18 +04:00
|
|
|
new(&sResourceResizer) KernelDaemon;
|
|
|
|
if (sResourceResizer.Init("resource resizer") != B_OK)
|
|
|
|
panic("kernel_daemon_init(): failed to init resource resizer");
|
2004-11-08 17:03:19 +03:00
|
|
|
|
2010-01-04 19:23:14 +03:00
|
|
|
add_debugger_command("daemons", dump_daemons,
|
|
|
|
"Shows registered kernel daemons.");
|
2004-11-08 17:03:19 +03:00
|
|
|
return B_OK;
|
2003-01-26 20:31:33 +03:00
|
|
|
}
|