1

add a shnitton of loggers

This commit is contained in:
2022-07-16 03:30:20 +02:00
parent 60d746af11
commit 2f7a2a219b
25 changed files with 177 additions and 173 deletions

View File

@ -48,8 +48,6 @@ extern Scheduler scheduler;
extern SerialOut serial;
constexpr bool DEBUG = false;
extern unsigned int total_mem; // RAM total
extern unsigned long systime; // wird all 10ms hochgezaehlt

View File

@ -160,10 +160,10 @@ void pg_init() {
// sodass genau der physikalische Adressraum abgedeckt ist?
num_pages = total_mem / (4096 * 1024);
kout << "pg_init: " << total_mem << endl;
kout << " total_mem: " << total_mem << endl;
kout << " #pages: " << total_mem / (4096 * 1024) << endl;
Logger log("Paging"); // This is only called one time
log << INFO << "pg_init: " << total_mem << endl;
log << INFO << " total_mem: " << total_mem << endl;
log << INFO << " #pages: " << total_mem / (4096 * 1024) << endl;
//
// Aufbau des Page-Directory

View File

@ -52,6 +52,8 @@
#ifndef __Paging_include__
#define __Paging_include__
#include "user/lib/Logger.h"
// Externe Funktionen in startup.asm
extern "C" {
void paging_on(unsigned int* p_pdir); // Paging einschalten

View File

@ -25,7 +25,7 @@ void BumpAllocator::init() {
this->allocations = 0;
this->next = (unsigned char*)heap_start;
if constexpr (DEBUG) { kout << "Initialized Bump Allocator" << endl; }
log << INFO << "Initialized Bump Allocator" << endl;
}
/*****************************************************************************
@ -51,10 +51,10 @@ void* BumpAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
if constexpr (DEBUG) { kout << "Requested " << hex << req_size << " Bytes" << endl; }
log << DEBUG << "Requested " << hex << req_size << " Bytes" << endl;
if (req_size + (unsigned int)this->next > this->heap_end) {
if constexpr (DEBUG) { kout << " - More memory requested than available :(" << endl; }
log << ERROR << " - More memory requested than available :(" << endl;
return NULL;
}
@ -62,7 +62,7 @@ void* BumpAllocator::alloc(unsigned int req_size) {
this->next = (unsigned char*)((unsigned int)this->next + req_size);
this->allocations = this->allocations + 1;
if constexpr (DEBUG) { kout << " - Allocated " << hex << req_size << " Bytes." << endl; }
log << TRACE << " - Allocated " << hex << req_size << " Bytes." << endl;
return allocated;
}
@ -73,5 +73,5 @@ void* BumpAllocator::alloc(unsigned int req_size) {
* Beschreibung: Nicht implementiert. *
*****************************************************************************/
void BumpAllocator::free(void* ptr) {
if constexpr (DEBUG) { kout << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl; }
log << ERROR << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl;
}

View File

@ -13,6 +13,7 @@
#define __BumpAllocator_include__
#include "kernel/Allocator.h"
#include "user/lib/Logger.h"
class BumpAllocator : Allocator {
@ -20,10 +21,12 @@ private:
unsigned char* next;
unsigned int allocations;
Logger log;
BumpAllocator(Allocator& copy) = delete; // Verhindere Kopieren
public:
BumpAllocator() {}; // Allocator() called implicitely in C++
BumpAllocator() : log("BMP-Alloc") {}; // Allocator() called implicitely in C++
void init() override;
void dump_free_memory() override;

View File

@ -11,6 +11,7 @@
#include "kernel/allocator/LinkedListAllocator.h"
#include "kernel/Globals.h"
#include "user/lib/Logger.h"
#include <stddef.h>
// I don't order the list by size so that the block order corresponds to the location in memory
@ -37,7 +38,7 @@ void LinkedListAllocator::init() {
this->free_start->size = this->heap_size - sizeof(free_block_t);
this->free_start->next = this->free_start; // Only one block, points to itself
if constexpr (DEBUG) { kout << "Initialized LinkedList Allocator" << endl; }
log << INFO << "Initialized LinkedList Allocator" << endl;
}
/*****************************************************************************
@ -75,10 +76,10 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
// NOTE: next pointer zeigt auf headeranfang, returned wird zeiger auf anfang des nutzbaren freispeichers
if constexpr (DEBUG) { kout << "Requested " << hex << req_size << " Bytes" << endl; }
log << DEBUG << "Requested " << hex << req_size << " Bytes" << endl;
if (this->free_start == NULL) {
if constexpr (DEBUG) { kout << " - No free memory remaining :(" << endl; }
log << ERROR << " - No free memory remaining :(" << endl;
return NULL;
}
@ -86,7 +87,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
unsigned int req_size_diff = (BASIC_ALIGN - req_size % BASIC_ALIGN) % BASIC_ALIGN;
unsigned int rreq_size = req_size + req_size_diff;
if (req_size_diff > 0) {
if constexpr (DEBUG) { kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; }
log << TRACE << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl;
}
free_block_t* current = this->free_start;
@ -117,7 +118,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
// Next-fit
this->free_start = new_next;
if constexpr (DEBUG) { kout << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl; }
log << TRACE << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl;
} else {
// Block too small to be cut, allocate whole block
@ -125,11 +126,11 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
this->free_start = current->next; // Pointer keeps pointing to current if last block
if (this->free_start == current) {
// No free block remaining
if constexpr (DEBUG) { kout << " - Disabled freelist" << endl; }
log << TRACE << " - Disabled freelist" << endl;
this->free_start = NULL;
}
if constexpr (DEBUG) { kout << " - Allocated " << hex << current->size << " Bytes without cutting" << endl; }
log << TRACE << " - Allocated " << hex << current->size << " Bytes without cutting" << endl;
}
// Block aushängen
@ -146,7 +147,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
current = current->next;
} while (current != this->free_start); // Stop when arriving at the first block again
if constexpr (DEBUG) { kout << " - More memory requested than available :(" << endl; }
log << ERROR << " - More memory requested than available :(" << endl;
return NULL;
}
@ -159,7 +160,7 @@ void LinkedListAllocator::free(void* ptr) {
/* Hier muess Code eingefuegt werden */
if constexpr (DEBUG) { kout << "Freeing " << hex << (unsigned int)ptr << endl; }
log << DEBUG << "Freeing " << hex << (unsigned int)ptr << endl;
free_block_t* block_start = (free_block_t*)((unsigned int)ptr - sizeof(free_block_t));
@ -170,8 +171,7 @@ void LinkedListAllocator::free(void* ptr) {
block_start->allocated = false;
block_start->next = block_start;
if constexpr (DEBUG) { kout << " - Enabling freelist with one block" << endl; }
log << TRACE << " - Enabling freelist with one block" << endl;
return;
}
@ -201,7 +201,7 @@ void LinkedListAllocator::free(void* ptr) {
// Try to merge forward ========================================================================
if (next_block == next_free) {
if constexpr (DEBUG) { kout << " - Merging block forward" << endl; }
log << TRACE << " - Merging block forward" << endl;
// Current and next adjacent block can be merged
// [previous_free | previous_free_next | <> | block_start | next_free]
@ -220,7 +220,7 @@ void LinkedListAllocator::free(void* ptr) {
if (this->free_start == next_free) {
// next_free is now invalid after merge
if constexpr (DEBUG) { kout << " - Moving freelist start to " << hex << (unsigned int)block_start << endl; }
log << TRACE << " - Moving freelist start to " << hex << (unsigned int)block_start << endl;
this->free_start = block_start;
}
} else {
@ -236,7 +236,7 @@ void LinkedListAllocator::free(void* ptr) {
// Try to merge backward =====================================================================
if (previous_free_next == block_start) {
if constexpr (DEBUG) { kout << " - Merging block backward" << endl; }
log << TRACE << " - Merging block backward" << endl;
// Current and previous adjacent block can be merged
// [previous_free | block_start]
@ -249,7 +249,7 @@ void LinkedListAllocator::free(void* ptr) {
if (this->free_start == block_start) {
// block_start is now invalid after merge
if constexpr (DEBUG) { kout << " - Moving freelist start to " << hex << (unsigned int)previous_free << endl; }
log << TRACE << " - Moving freelist start to " << hex << (unsigned int)previous_free << endl;
this->free_start = previous_free;
}
}

View File

@ -13,6 +13,7 @@
#define __LinkedListAllocator_include__
#include "kernel/Allocator.h"
#include "user/lib/Logger.h"
// Format eines freien Blocks, 4 + 4 + 4 Byte
typedef struct free_block {
@ -28,21 +29,21 @@ typedef struct free_block {
} free_block_t;
class LinkedListAllocator : Allocator {
private:
// freie Bloecke werden verkettet
struct free_block* free_start;
LinkedListAllocator(Allocator& copy) = delete; // Verhindere Kopieren
// NOTE: I added this
// Traverses the whole list forward till previous block is reached.
// This can only be called on free blocks as allocated blocks
// aren't reachable from the freelist.
static struct free_block* find_previous_block(struct free_block*);
Logger log;
public:
LinkedListAllocator() {}
LinkedListAllocator() : log("LL-Alloc") {}
void init() override;
void dump_free_memory() override;

View File

@ -33,6 +33,9 @@ void int_disp(unsigned int vector) {
/* hier muss Code eingefuegt werden */
// 32 = Timer Interrupt
// kout << "Interrupt: " << dec << vector << endl;
if (vector < 32) {
bs_dump(vector);
cpu.halt();
@ -45,17 +48,6 @@ void int_disp(unsigned int vector) {
}
}
/*****************************************************************************
* Konstruktor: IntDispatcher::IntDispatcher *
*---------------------------------------------------------------------------*
* Beschreibung: Initialisierung der ISR map mit einer Default-ISR. *
*****************************************************************************/
IntDispatcher::IntDispatcher() {
for (unsigned int slot = 0; slot < size; slot++) {
map[slot] = 0;
}
}
/*****************************************************************************
* Methode: IntDispatcher::assign *
*---------------------------------------------------------------------------*
@ -72,12 +64,12 @@ int IntDispatcher::assign(unsigned int vector, ISR& isr) {
/* hier muss Code eingefuegt werden */
if (vector >= this->size) {
if constexpr (DEBUG) { kout << "Invalid vector number when assigning" << endl; }
log << ERROR << "Invalid vector number when assigning" << endl;
return -1;
}
this->map[vector] = &isr;
if constexpr (DEBUG) { kout << "Registered ISR for vector " << dec << vector << endl; }
log << INFO << "Registered ISR for vector " << dec << vector << endl;
return 0;
}
@ -103,7 +95,7 @@ int IntDispatcher::report(unsigned int vector) {
ISR* isr = this->map[vector];
if (isr == 0) {
if constexpr (DEBUG) { kout << "No ISR registered for vector " << vector << endl; }
log << ERROR << "No ISR registered for vector " << vector << endl;
return -1;
}

View File

@ -14,12 +14,15 @@
#define __IntDispatcher_include__
#include "kernel/interrupts/ISR.h"
#include "user/lib/Logger.h"
class IntDispatcher {
private:
IntDispatcher(const IntDispatcher& copy) = delete; // Verhindere Kopieren
Logger log;
enum { size = 256 };
ISR* map[size];
@ -32,7 +35,11 @@ public:
};
// Initialisierung der ISR map mit einer Default-ISR.
IntDispatcher();
IntDispatcher() : log("IntDis") {
for (unsigned int slot = 0; slot < size; slot++) {
map[slot] = 0;
}
}
// Registrierung einer ISR. (Rueckgabewert: 0 = Erfolg, -1 = Fehler)
int assign(unsigned int vector, ISR& isr);

View File

@ -55,8 +55,8 @@ void Scheduler::ready(Thread* that) {
// Thread-Wechsel durch PIT verhindern
cpu.disable_int();
this->ready_queue.insert(that);
kout << "Scheduler :: Adding to ready_queue" << endl;
this->ready_queue.print();
log << TRACE << "Scheduler :: Adding to ready_queue" << endl;
this->ready_queue.print(log << TRACE);
cpu.enable_int();
}
@ -75,8 +75,8 @@ void Scheduler::exit() {
// Thread-Wechsel durch PIT verhindern
cpu.disable_int();
Thread& next = *(Thread*)this->ready_queue.remove_first();
kout << "Scheduler :: Exiting thread" << endl;
this->ready_queue.print();
log << TRACE << "Scheduler :: Exiting thread" << endl;
this->ready_queue.print(log << TRACE);
this->dispatch(next);
// Interrupts werden in Thread_switch in Thread.asm wieder zugelassen
@ -101,8 +101,8 @@ void Scheduler::kill(Thread* that) {
// Thread-Wechsel durch PIT verhindern
cpu.disable_int();
this->ready_queue.remove(that);
kout << "Scheduler :: Killing thread" << endl;
this->ready_queue.print();
log << TRACE << "Scheduler :: Killing thread" << endl;
this->ready_queue.print(log << TRACE);
cpu.enable_int();
}
@ -125,8 +125,8 @@ void Scheduler::yield() {
cpu.disable_int();
this->ready_queue.insert(this->get_active());
Thread& next = *(Thread*)this->ready_queue.remove_first();
kout << "Scheduler :: Yield" << endl;
this->ready_queue.print();
log << TRACE << "Scheduler :: Yield" << endl;
this->ready_queue.print(log << TRACE);
this->dispatch(next);
}

View File

@ -18,6 +18,7 @@
#include "lib/Queue.h"
#include "lib/SpinLock.h"
#include "user/lib/ArrayList.h"
#include "user/lib/Logger.h"
class Scheduler : public Dispatcher {
private:
@ -29,6 +30,8 @@ private:
// bevor er initialisiert wurde
bool has_idle_thread;
Logger log;
// NOTE: I would have to release the lock when switching threads but I don't know exactly how to do this
// in the assembly function
// SpinLock lock; // Use spinlock instead of cpu.disable_int() because it still allows preemption
@ -39,7 +42,7 @@ private:
ArrayList<Thread*> ready_queue;
public:
Scheduler() : has_idle_thread(false) {}
Scheduler() : has_idle_thread(false), log("SCHED") {}
void init() {
this->ready_queue.init();