Selaa lähdekoodia

add schedstat, improve schedule algorithm

greatbridf 9 kuukautta sitten
vanhempi
commit
6c5408c96f

+ 3 - 0
include/kernel/process.hpp

@@ -288,6 +288,9 @@ public:
     }
 
     void kill(pid_t pid, int exit_code);
+
+    constexpr auto begin() const { return m_procs.begin(); }
+    constexpr auto end() const { return m_procs.end(); }
 };
 
 void NORETURN init_scheduler(kernel::mem::paging::pfn_t kernel_stack_pfn);

+ 2 - 2
include/kernel/task/thread.hpp

@@ -52,8 +52,8 @@ public:
     int* __user clear_child_tid {};
 
     std::string name {};
-
-    uint64_t tls_desc32;
+    uint64_t tls_desc32 {};
+    std::size_t elected_times {};
 
     explicit thread(std::string name, pid_t owner);
     thread(const thread& val, pid_t owner);

+ 1 - 1
init_script.sh

@@ -19,7 +19,7 @@ export PATH="/bin"
 echo ok > /dev/console
 
 mkdir -p /etc /root /proc
-mount -t proc proc proc
+mount -t procfs proc proc
 
 cat > /etc/passwd <<EOF
 root:x:0:0:root:/root:/mnt/busybox sh

+ 25 - 0
src/fs/procfs.cc

@@ -5,7 +5,9 @@
 #include <sys/mount.h>
 #include <unistd.h>
 
+#include <kernel/hw/timer.hpp>
 #include <kernel/module.hpp>
+#include <kernel/process.hpp>
 #include <kernel/vfs.hpp>
 #include <kernel/vfs/vfs.hpp>
 
@@ -62,6 +64,28 @@ static ssize_t mounts_read(char* page, size_t n)
     return orig_n - n;
 }
 
+static ssize_t schedstat_read(char* page, size_t n)
+{
+    auto orig_n = n;
+
+    if (n == 0)
+        return n;
+
+    int nw = snprintf(page, n, "%d\n", kernel::hw::timer::current_ticks());
+    n -= nw, page += nw;
+
+    for (const auto& proc : *procs) {
+        for (const auto& thd : proc.second.thds) {
+            int nwrote = snprintf(page, n, "%d %x %d\n", proc.first, thd.tid(), thd.elected_times);
+
+            n -= nwrote;
+            page += nwrote;
+        }
+    }
+
+    return orig_n - n;
+}
+
 namespace fs::proc {
 
 struct proc_file {
@@ -105,6 +129,7 @@ public:
         auto* ind = cache_inode(0, 0, S_IFDIR | 0777, 0, 0);
 
         create_file("mounts", mounts_read, nullptr);
+        create_file("schedstat", schedstat_read, nullptr);
 
         register_root_node(ind);
     }

+ 17 - 0
src/kernel/task/readyqueue.cc

@@ -28,11 +28,28 @@ void dispatcher::dequeue(thread* thd)
 thread* dispatcher::next()
 {
     lock_guard_irq lck(dispatcher_mtx);
+    auto back = dispatcher_thds.back();
+
+    if (dispatcher_thds.size() == 1) {
+        back->elected_times++;
+        return back;
+    }
+
+    if (dispatcher_thds.size() == 2) {
+        if (back->owner == 0) {
+            auto front = dispatcher_thds.front();
+            front->elected_times++;
+            return front;
+        }
+        back->elected_times++;
+        return back;
+    }
 
     auto* retval = dispatcher_thds.front();
 
     dispatcher_thds.pop_front();
     dispatcher_thds.push_back(retval);
 
+    retval->elected_times++;
     return retval;
 }