don't spam serial

This commit is contained in:
2024-03-23 10:54:01 +01:00
parent ccd5678d5a
commit d41d620172
6 changed files with 90 additions and 69 deletions

View File

@@ -66,70 +66,6 @@ void ktask() {
(new Task(Task::TaskMode::TASKMODE_KERN, ktask2, "two"))->start();
}
void freeprinter() {
while (1) {
String buf;
buf += "=====\n";
buf += "Free mem: ";
buf += BytesFormatter::formatStr(get_free() * 1024);
buf += "\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap allocated: ";
buf += BytesFormatter::formatStr(get_heap_allocated());
buf += "\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap used: ";
buf += BytesFormatter::formatStr(get_heap_used());
buf += "\n";
buf += "=====\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
Scheduler::sleep_self(1000000);
}
}
void statprinter() {
SkipList<uint64_t, std::pair<String, uint64_t>> last_times = Scheduler::getTaskTimePerPid();
std::atomic<uint64_t> last_print_time = micros;
while (1) {
Scheduler::sleep_self(1000000);
uint64_t prev_print_time = last_print_time;
last_print_time = micros;
SkipList<uint64_t, std::pair<String, uint64_t>> prev_times = std::move(last_times);
last_times = Scheduler::getTaskTimePerPid();
uint64_t slice = last_print_time - prev_print_time;
if (slice == 0) continue;
for (const auto &t: prev_times) {
auto f = last_times.find(t.key);
if (!f->end && f->key == t.key) {
assert(f->data.second >= t.data.second);
String buf;
buf += "PID: ";
buf += t.key;
buf += " ";
buf += t.data.first;
buf += " usage: ";
buf += (((f->data.second - t.data.second) * 100ULL) / slice);
buf += "%\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
} else {
String buf;
buf += "PID: ";
buf += t.key;
buf += " ";
buf += t.data.first;
buf += " dead \n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
}
}
}
}
static Mutex testmutex;
void mtest1() {
@@ -206,8 +142,6 @@ void ktask_main() {
GlobalTtyManager.add_tty(new SerialTty());
(new Task(Task::TaskMode::TASKMODE_KERN, ktask, "one"))->start();
(new Task(Task::TaskMode::TASKMODE_KERN, freeprinter, "freeprinter"))->start();
(new Task(Task::TaskMode::TASKMODE_KERN, statprinter, "statprinter"))->start();
(new Task(Task::TaskMode::TASKMODE_KERN, mtest1, "mtest1"))->start();
(new Task(Task::TaskMode::TASKMODE_KERN, mtest2, "mtest2"))->start();
(new Task(Task::TaskMode::TASKMODE_KERN, mtest3, "mtest3"))->start();

View File

@@ -12,8 +12,11 @@
#include "gdt.hpp"
#include "misc.hpp"
#include "BytesFormatter.hpp"
#include "FDT.hpp"
#include "File.hpp"
#include "memman.hpp"
#include "timer.hpp"
// Don't forget the correct order
// Shockingly, it doesn't immediately break and even something simple as putchar works
@@ -91,6 +94,68 @@ uint64_t syscall_lseek(uint64_t fd, uint64_t off, uint64_t whence) {
return f->seek(off);
}
uint64_t syscall_print_tasks() {
static SkipList<uint64_t, std::pair<String, uint64_t>> last_times = Scheduler::getTaskTimePerPid();
static std::atomic<uint64_t> last_print_time = micros;
uint64_t prev_print_time = last_print_time;
last_print_time = micros;
SkipList<uint64_t, std::pair<String, uint64_t>> prev_times = std::move(last_times);
last_times = Scheduler::getTaskTimePerPid();
uint64_t slice = last_print_time - prev_print_time;
if (slice == 0) return 0;
for (const auto &t: prev_times) {
auto f = last_times.find(t.key);
if (!f->end && f->key == t.key) {
assert(f->data.second >= t.data.second);
String buf;
buf += "PID: ";
buf += t.key;
buf += " ";
buf += t.data.first;
buf += " usage: ";
buf += (((f->data.second - t.data.second) * 100ULL) / slice);
buf += "%\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
} else {
String buf;
buf += "PID: ";
buf += t.key;
buf += " ";
buf += t.data.first;
buf += " dead \n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
}
}
return 0;
}
uint64_t syscall_print_mem() {
String buf;
buf += "=====\n";
buf += "Free mem: ";
buf += BytesFormatter::formatStr(get_free() * 1024);
buf += "\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap allocated: ";
buf += BytesFormatter::formatStr(get_heap_allocated());
buf += "\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
buf = "";
buf += "Heap used: ";
buf += BytesFormatter::formatStr(get_heap_used());
buf += "\n";
buf += "=====\n";
GlobalTtyManager.all_tty_putstr(buf.c_str());
return 0;
}
extern "C" uint64_t syscall_impl(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t a3_rcx) {
assert2(are_interrupts_enabled(), "why wouldn't they be?");
switch (id_rdi) {
@@ -117,6 +182,10 @@ extern "C" uint64_t syscall_impl(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_r
case SYSCALL_CLOSEDIR_ID:
case SYSCALL_MKDIR_ID:
case SYSCALL_UNLINK_ID:
case SYSCALL_PRINT_TASKS:
return syscall_print_tasks();
case SYSCALL_PRINT_MEM:
return syscall_print_mem();
default:
return -1;
}

View File

@@ -23,8 +23,12 @@ void _start() {
while (1) {
// putchar('h');
// putchar('i');
// putchar('\n');
putchar(readchar());
putchar('\n');
char read = readchar();
if (read == 'm') print_mem();
if (read == 't') print_tasks();
putchar('\n');
putchar(read);
// sleep(100000);
}
}

View File

@@ -31,6 +31,10 @@ extern "C" {
#define SYSCALL_MKDIR_ID 12
#define SYSCALL_UNLINK_ID 13
#define SYSCALL_PRINT_MEM 1000
#define SYSCALL_PRINT_TASKS 1001
#ifdef __cplusplus
}
#endif

View File

@@ -46,4 +46,11 @@ uint64_t write(uint64_t fd, const char *buf, uint64_t len) {
uint64_t lseek(uint64_t fd, uint64_t off, uint64_t whence) {
return do_syscall(SYSCALL_LSEEK_ID, fd, off, whence);
}
}
void print_mem() {
do_syscall(SYSCALL_PRINT_MEM, 0, 0, 0);
}
void print_tasks() {
do_syscall(SYSCALL_PRINT_TASKS, 0, 0, 0);
}

View File

@@ -27,6 +27,9 @@ uint64_t read(uint64_t fd, char *buf, uint64_t len);
uint64_t write(uint64_t fd, const char *buf, uint64_t len);
uint64_t lseek(uint64_t fd, uint64_t off, uint64_t whence);
void print_mem();
void print_tasks();
#ifdef __cplusplus
}
#endif