From 4672f514591f0f7110103c4cd898909aef95b635 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 14 Mar 2015 14:17:03 +0100 Subject: Fix region allocator (see changes in region.c!) ; debug spam control. --- src/kernel/core/frame.c | 1 + src/kernel/core/region.c | 14 ++++++++++++-- src/kernel/core/thread.c | 27 +++++++++++++++------------ 3 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src/kernel/core') diff --git a/src/kernel/core/frame.c b/src/kernel/core/frame.c index 255abb6..e74f9ad 100644 --- a/src/kernel/core/frame.c +++ b/src/kernel/core/frame.c @@ -54,6 +54,7 @@ uint32_t frame_alloc(size_t n) { mutex_unlock(&frame_allocator_mutex); /*dbg_printf("AF 0x%p\n", i * 32 + j);*/ + return i * 32 + j; } } diff --git a/src/kernel/core/region.c b/src/kernel/core/region.c index 886c9fc..6a4c7b4 100644 --- a/src/kernel/core/region.c +++ b/src/kernel/core/region.c @@ -55,6 +55,9 @@ void remove_free_region(descriptor_t *d) { first_free_region_by_size = d->free.next_by_size; } else { for (descriptor_t *i = first_free_region_by_size; i != 0; i = i->free.next_by_size) { + if (i->free.first_bigger == d) { + i->free.first_bigger = d->free.next_by_size; + } if (i->free.next_by_size == d) { i->free.next_by_size = d->free.next_by_size; break; @@ -382,7 +385,9 @@ void dbg_print_region_info() { } dbg_printf("- Free kernel regions, by size:\n"); for (descriptor_t *d = first_free_region_by_size; d != 0; d = d->free.next_by_size) { - dbg_printf("| 0x%p - 0x%p\n", d->free.addr, d->free.addr + d->free.size); + dbg_printf("| 0x%p - 0x%p ", d->free.addr, d->free.addr + d->free.size); + dbg_printf("(0x%p, next in size: 0x%p)\n", d->free.size, + (d->free.first_bigger == 0 ? 0 : d->free.first_bigger->free.addr)); ASSERT(d != d->free.next_by_size); } dbg_printf("- Used kernel regions:\n"); @@ -390,7 +395,12 @@ void dbg_print_region_info() { dbg_printf("| 0x%p - 0x%p %s\n", d->used.i.addr, d->used.i.addr + d->used.i.size, d->used.i.type); ASSERT(d != d->used.next_by_addr); } - dbg_printf("\\\n"); + + int nfreerd = 0; + for (descriptor_t *i = first_unused_descriptor; i != 0; i = i->unused_descriptor.next) + nfreerd++; + + dbg_printf("\\ Free region descriptors: %d (%d)\n", nfreerd, n_unused_descriptors); mutex_unlock(&ra_mutex); } diff --git a/src/kernel/core/thread.c b/src/kernel/core/thread.c index c4d4f93..4a3722d 100644 --- a/src/kernel/core/thread.c +++ b/src/kernel/core/thread.c @@ -129,7 +129,7 @@ void run_scheduler() { } current_thread = dequeue_thread(); - if (current_thread != prev_thread) dbg_printf("[0x%p]\n", current_thread); + if (current_thread != prev_thread && SPAM_CONTEXT_SWITCH) dbg_printf("[0x%p]\n", current_thread); if (current_thread != 0) { thread_t *ptr = current_thread; @@ -149,7 +149,7 @@ void run_scheduler() { void run_thread(void (*entry)(void*), void* data) { ASSERT(current_thread->state == T_STATE_RUNNING); - dbg_printf("Begin thread 0x%p (in process %d)\n", + if (SPAM_BEGIN_EXIT) dbg_printf("Begin thread 0x%p (in process %d)\n", current_thread, (current_thread->proc ? current_thread->proc->pid : 0)); switch_pagedir(get_kernel_pagedir()); @@ -176,6 +176,7 @@ thread_t *new_thread(entry_t entry, void* data) { uint32_t f; int tries = 0; while ((f = frame_alloc(1)) == 0 && (tries++) < 3) { + dbg_printf("thread stack alloc OOM\n"); free_some_memory(); } if (f == 0) { @@ -220,7 +221,7 @@ thread_t *new_thread(entry_t entry, void* data) { void delete_thread(thread_t *t) { ASSERT(t->state == T_STATE_FINISHED); - dbg_printf("Deleting thread 0x%p\n", t); + if (SPAM_BEGIN_EXIT) dbg_printf("Deleting thread 0x%p\n", t); region_free_unmap_free(t->stack_region->addr); free(t); @@ -289,11 +290,13 @@ bool wait_on_many(void** x, size_t n) { // ---- Set ourselves as the waiting thread for all the requested objets - /*dbg_printf("Wait on many:");*/ - /*for (size_t i = 0; i < n; i++) {*/ - /*dbg_printf(" 0x%p", x[i]);*/ - /*}*/ - /*dbg_printf("\n");*/ + if (SPAM_WAIT_RESUME_ON) { + dbg_printf("Wait on many:"); + for (size_t i = 0; i < n; i++) { + dbg_printf(" 0x%p", x[i]); + } + dbg_printf("\n"); + } current_thread->waiting_on = x; current_thread->n_waiting_on = n; @@ -363,7 +366,7 @@ void exit() { // (it may switch before adding the delete_thread task), but once the task is added // no other switch may happen, therefore this thread will not get re-enqueued - dbg_printf("Thread 0x%p exiting.\n", current_thread); + if (SPAM_BEGIN_EXIT) dbg_printf("Thread 0x%p exiting.\n", current_thread); worker_push(exit_cleanup_task, current_thread); @@ -379,12 +382,12 @@ bool resume_on(void* x) { int st = enter_critical(CL_NOINT); - /*dbg_printf("Resume on 0x%p:", x);*/ + if (SPAM_WAIT_RESUME_ON) dbg_printf("Resume on 0x%p:", x); for (thread_t *t = waiters; t != 0; t = t->next_waiter) { for (int i = 0; i < t->n_waiting_on; i++) { if (t->waiting_on[i] == x) { - /*dbg_printf(" 0x%p", t);*/ + if (SPAM_WAIT_RESUME_ON) dbg_printf(" 0x%p", t); if (t->state == T_STATE_PAUSED) { t->state = T_STATE_RUNNING; @@ -397,7 +400,7 @@ bool resume_on(void* x) { } } } - /*dbg_printf("\n");*/ + if (SPAM_WAIT_RESUME_ON) dbg_printf("\n"); exit_critical(st); -- cgit v1.2.3