Step 3: time_used
This commit is contained in:
parent
ec7ee599a1
commit
27e6f2684b
5 changed files with 25 additions and 33 deletions
|
|
@ -15,7 +15,6 @@ fn execute(input: []u8) void {
|
||||||
if (eql(u8, input, "tasks")) return task.introspect();
|
if (eql(u8, input, "tasks")) return task.introspect();
|
||||||
if (eql(u8, input, "lspci")) return pci.lspci();
|
if (eql(u8, input, "lspci")) return pci.lspci();
|
||||||
if (eql(u8, input, "uptime")) return time.uptime();
|
if (eql(u8, input, "uptime")) return time.uptime();
|
||||||
if (eql(u8, input, "topbar")) return topbar();
|
|
||||||
println("{}: command not found", input);
|
println("{}: command not found", input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,7 @@ export fn kmain(magic: u32, info: *const multiboot.MultibootInfo) noreturn {
|
||||||
pci.scan();
|
pci.scan();
|
||||||
|
|
||||||
task.new(@ptrToInt(topbar)) catch unreachable;
|
task.new(@ptrToInt(topbar)) catch unreachable;
|
||||||
// task.new(@ptrToInt(console.loop)) catch unreachable;
|
task.new(@ptrToInt(console.loop)) catch unreachable;
|
||||||
|
|
||||||
// task.schedule();
|
while (true) task.schedule();
|
||||||
console.loop();
|
|
||||||
while (true) asm volatile ("hlt");
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
src/task.zig
28
src/task.zig
|
|
@ -1,5 +1,6 @@
|
||||||
pub usingnamespace @import("index.zig");
|
pub usingnamespace @import("index.zig");
|
||||||
|
|
||||||
|
var timer_last_count: u64 = 0;
|
||||||
var boot_task = Task{ .tid = 0, .esp = 0x47 };
|
var boot_task = Task{ .tid = 0, .esp = 0x47 };
|
||||||
const ListOfTasks = std.TailQueue(*Task);
|
const ListOfTasks = std.TailQueue(*Task);
|
||||||
var first_task = ListOfTasks.Node.init(&boot_task);
|
var first_task = ListOfTasks.Node.init(&boot_task);
|
||||||
|
|
@ -16,9 +17,17 @@ var tid_counter: u16 = 1;
|
||||||
///ASM
|
///ASM
|
||||||
extern fn switch_tasks(new_esp: u32, old_esp_addr: u32) void;
|
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 {
|
pub const Task = packed struct {
|
||||||
esp: usize,
|
esp: usize,
|
||||||
tid: u16,
|
tid: u16,
|
||||||
|
time_used: u64 = 0,
|
||||||
//context: isr.Context,
|
//context: isr.Context,
|
||||||
//cr3: usize,
|
//cr3: usize,
|
||||||
|
|
||||||
|
|
@ -26,6 +35,7 @@ pub const Task = packed struct {
|
||||||
// Allocate and initialize the thread structure.
|
// Allocate and initialize the thread structure.
|
||||||
var t = try vmem.create(Task);
|
var t = try vmem.create(Task);
|
||||||
|
|
||||||
|
t.time_used = 0;
|
||||||
t.tid = tid_counter;
|
t.tid = tid_counter;
|
||||||
tid_counter +%= 1;
|
tid_counter +%= 1;
|
||||||
assert(tid_counter != 0); //overflow
|
assert(tid_counter != 0); //overflow
|
||||||
|
|
@ -49,15 +59,9 @@ pub const Task = packed struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn new(entrypoint: usize) !void {
|
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);
|
const node = try vmem.create(ListOfTasks.Node);
|
||||||
node.data = try Task.create(entrypoint);
|
node.data = try Task.create(entrypoint);
|
||||||
tasks.append(node);
|
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 {
|
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 {
|
pub fn schedule() void {
|
||||||
// println("currently: {}", current_task.data.tid);
|
update_time_used();
|
||||||
// println("first: {}", tasks.first.?.data.tid);
|
|
||||||
// println("last: {}", tasks.last.?.data.tid);
|
|
||||||
if (current_task.next) |next| {
|
if (current_task.next) |next| {
|
||||||
// println("switching to {}", next.data.tid);
|
|
||||||
switch_to(next);
|
switch_to(next);
|
||||||
} else if (tasks.first) |head| {
|
} else if (tasks.first) |head| {
|
||||||
// println("switching to {}", head.data.tid);
|
|
||||||
if (head.data != current_task.data) switch_to(head);
|
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 {
|
pub fn introspect() void {
|
||||||
var it = tasks.first;
|
var it = tasks.first;
|
||||||
println("{} tasks", tasks.len);
|
println("{} tasks", tasks.len);
|
||||||
|
update_time_used();
|
||||||
while (it) |node| : (it = node.next) {
|
while (it) |node| : (it = node.next) {
|
||||||
if (node.data != current_task.data) println("{}", node.data);
|
if (node.data != current_task.data) println("{}", node.data);
|
||||||
if (node.data == current_task.data) println("*{}", node.data);
|
if (node.data == current_task.data) println("*{}", node.data);
|
||||||
|
|
|
||||||
11
src/time.zig
11
src/time.zig
|
|
@ -1,14 +1,13 @@
|
||||||
usingnamespace @import("index.zig");
|
usingnamespace @import("index.zig");
|
||||||
|
|
||||||
pub var offset_s: u32 = 0;
|
pub var offset_us: u64 = 0;
|
||||||
pub var offset_us: u32 = 0;
|
|
||||||
pub fn increment(value: u32) void {
|
pub fn increment(value: u32) void {
|
||||||
const sum = offset_us + value;
|
offset_us += value;
|
||||||
offset_s += sum / 1000000;
|
|
||||||
offset_us = sum % 1000000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uptime() void {
|
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);
|
println("{}.{:.3}", offset_s, offset_ms);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/vga.zig
12
src/vga.zig
|
|
@ -68,9 +68,11 @@ pub fn topbar() void {
|
||||||
const cursor = vga.cursor;
|
const cursor = vga.cursor;
|
||||||
vga.cursor = 0;
|
vga.cursor = 0;
|
||||||
vga.background = Color.Red;
|
vga.background = Color.Red;
|
||||||
|
vga.cursor_enabled = false;
|
||||||
|
|
||||||
time.uptime();
|
time.uptime();
|
||||||
|
|
||||||
|
vga.cursor_enabled = true;
|
||||||
vga.cursor = cursor;
|
vga.cursor = cursor;
|
||||||
vga.background = bg;
|
vga.background = bg;
|
||||||
task.schedule();
|
task.schedule();
|
||||||
|
|
@ -85,6 +87,7 @@ fn printCallback(context: void, string: []const u8) Errors!void {
|
||||||
const VGA = struct {
|
const VGA = struct {
|
||||||
vram: []VGAEntry,
|
vram: []VGAEntry,
|
||||||
cursor: usize,
|
cursor: usize,
|
||||||
|
cursor_enabled: bool = true,
|
||||||
foreground: Color,
|
foreground: Color,
|
||||||
background: Color,
|
background: Color,
|
||||||
|
|
||||||
|
|
@ -132,7 +135,7 @@ const VGA = struct {
|
||||||
self.cursor += 1;
|
self.cursor += 1;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
self.updateCursor();
|
if (self.cursor_enabled) self.updateCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
@ -142,11 +145,8 @@ const VGA = struct {
|
||||||
// string: String to be printed.
|
// string: String to be printed.
|
||||||
//
|
//
|
||||||
pub fn writeString(self: *VGA, string: []const u8) void {
|
pub fn writeString(self: *VGA, string: []const u8) void {
|
||||||
for (string) |char| {
|
for (string) |char| self.writeChar(char);
|
||||||
self.writeChar(char);
|
if (self.cursor_enabled) self.updateCursor();
|
||||||
}
|
|
||||||
|
|
||||||
self.updateCursor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue