From 3cd731ada3ff29b47539035dde8cbc5df2bcddf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 25 Sep 2007 23:58:31 +0000 Subject: [PATCH] * The low memory handler now runs more often in more critical situations. * vm_low_memory() no longer calls call_handlers() directly, but just triggers a run immediately. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22313 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/kernel/vm/vm_low_memory.cpp | 42 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/system/kernel/vm/vm_low_memory.cpp b/src/system/kernel/vm/vm_low_memory.cpp index ead2791a07..445252b16f 100644 --- a/src/system/kernel/vm/vm_low_memory.cpp +++ b/src/system/kernel/vm/vm_low_memory.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2005-2007, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -26,16 +26,6 @@ // TODO: the priority is currently ignored, and probably can be removed -static const bigtime_t kLowMemoryInterval = 3000000; // 3 secs - -// page limits -static const size_t kNoteLimit = 1024; -static const size_t kWarnLimit = 256; -static const size_t kCriticalLimit = 32; - -static int32 sLowMemoryState = B_NO_LOW_MEMORY; - - struct low_memory_handler : public DoublyLinkedListLinkImpl { low_memory_func function; void *data; @@ -44,6 +34,18 @@ struct low_memory_handler : public DoublyLinkedListLinkImpl typedef DoublyLinkedList HandlerList; + +static const bigtime_t kLowMemoryInterval = 3000000; // 3 secs +static const bigtime_t kWarnMemoryInterval = 500000; // 0.5 secs + +// page limits +static const size_t kNoteLimit = 1024; +static const size_t kWarnLimit = 256; +static const size_t kCriticalLimit = 32; + + +static int32 sLowMemoryState = B_NO_LOW_MEMORY; + static mutex sLowMemoryMutex; static sem_id sLowMemoryWaitSem; static HandlerList sLowMemoryHandlers; @@ -83,8 +85,12 @@ compute_state(void) static int32 low_memory(void *) { + bigtime_t timeout = kLowMemoryInterval; while (true) { - snooze(kLowMemoryInterval); + if (sLowMemoryState != B_LOW_MEMORY_CRITICAL) { + acquire_sem_etc(sLowMemoryWaitSem, 1, B_RELATIVE_TIMEOUT, + timeout); + } sLowMemoryState = compute_state(); @@ -95,6 +101,11 @@ low_memory(void *) continue; call_handlers(sLowMemoryState); + + if (sLowMemoryState == B_LOW_MEMORY_WARNING) + timeout = kWarnMemoryInterval; + else + timeout = kLowMemoryInterval; } return 0; } @@ -103,9 +114,9 @@ low_memory(void *) void vm_low_memory(size_t requirements) { - // ToDo: compute level with requirements in mind + // TODO: take requirements into account - call_handlers(B_LOW_MEMORY_NOTE); + release_sem(sLowMemoryWaitSem); } @@ -132,7 +143,8 @@ vm_low_memory_init(void) // static initializers do not work in the kernel, // so we have to do it here, manually - thread = spawn_kernel_thread(&low_memory, "low memory handler", B_LOW_PRIORITY, NULL); + thread = spawn_kernel_thread(&low_memory, "low memory handler", + B_LOW_PRIORITY, NULL); send_signal_etc(thread, SIGCONT, B_DO_NOT_RESCHEDULE); return B_OK;