From 27e6f2684b7b43710c36853258938c58c8c60a8f Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 15 Dec 2019 11:14:10 +0100 Subject: [PATCH] Step 3: time_used --- src/console.zig | 1 - src/main.zig | 6 ++---- src/task.zig | 28 ++++++++++++---------------- src/time.zig | 11 +++++------ src/vga.zig | 12 ++++++------ 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/console.zig b/src/console.zig index 369bd84..13e9e91 100644 --- a/src/console.zig +++ b/src/console.zig @@ -15,7 +15,6 @@ fn execute(input: []u8) void { if (eql(u8, input, "tasks")) return task.introspect(); if (eql(u8, input, "lspci")) return pci.lspci(); if (eql(u8, input, "uptime")) return time.uptime(); - if (eql(u8, input, "topbar")) return topbar(); println("{}: command not found", input); } diff --git a/src/main.zig b/src/main.zig index 82cf7a0..0145594 100644 --- a/src/main.zig +++ b/src/main.zig @@ -26,9 +26,7 @@ export fn kmain(magic: u32, info: *const multiboot.MultibootInfo) noreturn { pci.scan(); task.new(@ptrToInt(topbar)) catch unreachable; - // task.new(@ptrToInt(console.loop)) catch unreachable; + task.new(@ptrToInt(console.loop)) catch unreachable; - // task.schedule(); - console.loop(); - while (true) asm volatile ("hlt"); + while (true) task.schedule(); } diff --git a/src/task.zig b/src/task.zig index 281eba3..d9c1b47 100644 --- a/src/task.zig +++ b/src/task.zig @@ -1,5 +1,6 @@ pub usingnamespace @import("index.zig"); +var timer_last_count: u64 = 0; var boot_task = Task{ .tid = 0, .esp = 0x47 }; const ListOfTasks = std.TailQueue(*Task); var first_task = ListOfTasks.Node.init(&boot_task); @@ -16,9 +17,17 @@ var tid_counter: u16 = 1; ///ASM extern fn switch_tasks(new_esp: u32, old_esp_addr: u32) void; +pub fn update_time_used() void { + const current_count = time.offset_us; + const elapsed = current_count - timer_last_count; + timer_last_count = current_count; + current_task.data.time_used += elapsed; +} + pub const Task = packed struct { esp: usize, tid: u16, + time_used: u64 = 0, //context: isr.Context, //cr3: usize, @@ -26,6 +35,7 @@ pub const Task = packed struct { // Allocate and initialize the thread structure. var t = try vmem.create(Task); + t.time_used = 0; t.tid = tid_counter; tid_counter +%= 1; assert(tid_counter != 0); //overflow @@ -49,15 +59,9 @@ pub const Task = packed struct { }; pub fn new(entrypoint: usize) !void { - // println("currently: {}", current_task.data.tid); - // println("first: {}", tasks.first.?.data.tid); - // println("last: {}", tasks.last.?.data.tid); const node = try vmem.create(ListOfTasks.Node); node.data = try Task.create(entrypoint); tasks.append(node); - // println("currently: {}", current_task.data.tid); - // println("first: {}", tasks.first.?.data.tid); - // println("last: {}", tasks.last.?.data.tid); } pub fn switch_to(new_task: *ListOfTasks.Node) void { @@ -72,26 +76,18 @@ pub fn switch_to(new_task: *ListOfTasks.Node) void { } pub fn schedule() void { - // println("currently: {}", current_task.data.tid); - // println("first: {}", tasks.first.?.data.tid); - // println("last: {}", tasks.last.?.data.tid); + update_time_used(); if (current_task.next) |next| { - // println("switching to {}", next.data.tid); switch_to(next); } else if (tasks.first) |head| { - // println("switching to {}", head.data.tid); if (head.data != current_task.data) switch_to(head); - } else { - introspect(); } - // if (current_task.data.tid == 0) switch_to(tasks.last.?.*); - // if (current_task.data.tid == 1) switch_to(tasks.first.?.*); - // if (current_task.tid == 2) tasks[0].?.switch_to(); } pub fn introspect() void { var it = tasks.first; println("{} tasks", tasks.len); + update_time_used(); while (it) |node| : (it = node.next) { if (node.data != current_task.data) println("{}", node.data); if (node.data == current_task.data) println("*{}", node.data); diff --git a/src/time.zig b/src/time.zig index 834205c..f75b92f 100644 --- a/src/time.zig +++ b/src/time.zig @@ -1,14 +1,13 @@ usingnamespace @import("index.zig"); -pub var offset_s: u32 = 0; -pub var offset_us: u32 = 0; +pub var offset_us: u64 = 0; pub fn increment(value: u32) void { - const sum = offset_us + value; - offset_s += sum / 1000000; - offset_us = sum % 1000000; + offset_us += value; } pub fn uptime() void { - const offset_ms = offset_us / 1000; + var offset_ms: u64 = offset_us / 1000; + const offset_s: u64 = offset_ms / 1000; + offset_ms = @mod(offset_ms, 1000); println("{}.{:.3}", offset_s, offset_ms); } diff --git a/src/vga.zig b/src/vga.zig index ecf1349..5aee204 100644 --- a/src/vga.zig +++ b/src/vga.zig @@ -68,9 +68,11 @@ pub fn topbar() void { const cursor = vga.cursor; vga.cursor = 0; vga.background = Color.Red; + vga.cursor_enabled = false; time.uptime(); + vga.cursor_enabled = true; vga.cursor = cursor; vga.background = bg; task.schedule(); @@ -85,6 +87,7 @@ fn printCallback(context: void, string: []const u8) Errors!void { const VGA = struct { vram: []VGAEntry, cursor: usize, + cursor_enabled: bool = true, foreground: Color, background: Color, @@ -132,7 +135,7 @@ const VGA = struct { self.cursor += 1; }, } - self.updateCursor(); + if (self.cursor_enabled) self.updateCursor(); } //// @@ -142,11 +145,8 @@ const VGA = struct { // string: String to be printed. // pub fn writeString(self: *VGA, string: []const u8) void { - for (string) |char| { - self.writeChar(char); - } - - self.updateCursor(); + for (string) |char| self.writeChar(char); + if (self.cursor_enabled) self.updateCursor(); } ////