瀏覽代碼

Kernel: Restore state strings for block states

"Blocking" is not terribly informative, but now that everything is
ported over, we can force the blocker to provide us with a reason.

This does mean that to_string(State) needed to become a member, but
that's OK.
Robin Burchell 6 年之前
父節點
當前提交
762333ba95
共有 7 個文件被更改,包括 37 次插入18 次删除
  1. 1 1
      Kernel/Devices/SB16.cpp
  2. 1 1
      Kernel/FileSystem/ProcFS.cpp
  3. 1 1
      Kernel/Net/NetworkTask.cpp
  4. 1 1
      Kernel/Process.cpp
  5. 2 1
      Kernel/Scheduler.cpp
  6. 7 9
      Kernel/Thread.cpp
  7. 24 4
      Kernel/Thread.h

+ 1 - 1
Kernel/Devices/SB16.cpp

@@ -139,7 +139,7 @@ void SB16::handle_irq()
 
 
 void SB16::wait_for_irq()
 void SB16::wait_for_irq()
 {
 {
-    current->block_until([this] {
+    current->block_until("Interrupting", [this] {
         return m_interrupted;
         return m_interrupted;
     });
     });
 }
 }

+ 1 - 1
Kernel/FileSystem/ProcFS.cpp

@@ -572,7 +572,7 @@ ByteBuffer procfs$all(InodeIdentifier)
         process_object.set("sid", process.sid());
         process_object.set("sid", process.sid());
         process_object.set("uid", process.uid());
         process_object.set("uid", process.uid());
         process_object.set("gid", process.gid());
         process_object.set("gid", process.gid());
-        process_object.set("state", to_string(process.state()));
+        process_object.set("state", process.main_thread().state_string());
         process_object.set("ppid", process.ppid());
         process_object.set("ppid", process.ppid());
         process_object.set("nfds", process.number_of_open_file_descriptors());
         process_object.set("nfds", process.number_of_open_file_descriptors());
         process_object.set("name", process.name());
         process_object.set("name", process.name());

+ 1 - 1
Kernel/Net/NetworkTask.cpp

@@ -59,7 +59,7 @@ void NetworkTask_main()
     for (;;) {
     for (;;) {
         auto packet = dequeue_packet();
         auto packet = dequeue_packet();
         if (packet.is_null()) {
         if (packet.is_null()) {
-            current->block_until([] {
+            current->block_until("Networking", [] {
                 if (LoopbackAdapter::the().has_queued_packets())
                 if (LoopbackAdapter::the().has_queued_packets())
                     return true;
                     return true;
                 if (auto* e1000 = E1000NetworkAdapter::the()) {
                 if (auto* e1000 = E1000NetworkAdapter::the()) {

+ 1 - 1
Kernel/Process.cpp

@@ -1384,7 +1384,7 @@ int Process::reap(Process& process)
             }
             }
         }
         }
 
 
-        dbgprintf("reap: %s(%u) {%s}\n", process.name().characters(), process.pid(), to_string(process.state()));
+        dbgprintf("reap: %s(%u) {%s}\n", process.name().characters(), process.pid(), process.main_thread().state_string());
         ASSERT(process.is_dead());
         ASSERT(process.is_dead());
         g_processes->remove(&process);
         g_processes->remove(&process);
     }
     }

+ 2 - 1
Kernel/Scheduler.cpp

@@ -122,8 +122,9 @@ bool Thread::ReadBlocker::should_unblock(Thread&, time_t, long)
     return blocked_description()->can_read();
     return blocked_description()->can_read();
 }
 }
 
 
-Thread::ConditionBlocker::ConditionBlocker(Function<bool()> &condition)
+Thread::ConditionBlocker::ConditionBlocker(const char* state_string, Function<bool()> &condition)
     : m_block_until_condition(move(condition))
     : m_block_until_condition(move(condition))
+    , m_state_string(state_string)
 {
 {
     ASSERT(m_block_until_condition);
     ASSERT(m_block_until_condition);
 }
 }

+ 7 - 9
Kernel/Thread.cpp

@@ -108,9 +108,9 @@ void Thread::unblock()
     set_state(Thread::Runnable);
     set_state(Thread::Runnable);
 }
 }
 
 
-void Thread::block_until(Function<bool()>&& condition)
+void Thread::block_until(const char* state_string, Function<bool()>&& condition)
 {
 {
-    m_blocker = make<ConditionBlocker>(condition);
+    m_blocker = make<ConditionBlocker>(state_string, condition);
     block(Thread::Blocked);
     block(Thread::Blocked);
     Scheduler::yield();
     Scheduler::yield();
 }
 }
@@ -118,9 +118,6 @@ void Thread::block_until(Function<bool()>&& condition)
 void Thread::block(Thread::State new_state)
 void Thread::block(Thread::State new_state)
 {
 {
     bool did_unlock = process().big_lock().unlock_if_locked();
     bool did_unlock = process().big_lock().unlock_if_locked();
-    if (state() != Thread::Running) {
-        dbgprintf("Thread::block: %s(%u) block(%u/%s) with state=%u/%s\n", process().name().characters(), process().pid(), new_state, to_string(new_state), state(), to_string(state()));
-    }
     ASSERT(state() == Thread::Running);
     ASSERT(state() == Thread::Running);
     m_was_interrupted_while_blocked = false;
     m_was_interrupted_while_blocked = false;
     set_state(new_state);
     set_state(new_state);
@@ -143,9 +140,9 @@ u64 Thread::sleep(u32 ticks)
     return wakeup_time;
     return wakeup_time;
 }
 }
 
 
-const char* to_string(Thread::State state)
+const char* Thread::state_string() const
 {
 {
-    switch (state) {
+    switch (state()) {
     case Thread::Invalid:
     case Thread::Invalid:
         return "Invalid";
         return "Invalid";
     case Thread::Runnable:
     case Thread::Runnable:
@@ -163,9 +160,10 @@ const char* to_string(Thread::State state)
     case Thread::Skip0SchedulerPasses:
     case Thread::Skip0SchedulerPasses:
         return "Skip0";
         return "Skip0";
     case Thread::Blocked:
     case Thread::Blocked:
-        return "Blocked";
+        ASSERT(m_blocker);
+        return m_blocker->state_string();
     }
     }
-    kprintf("to_string(Thread::State): Invalid state: %u\n", state);
+    kprintf("to_string(Thread::State): Invalid state: %u\n", state());
     ASSERT_NOT_REACHED();
     ASSERT_NOT_REACHED();
     return nullptr;
     return nullptr;
 }
 }

+ 24 - 4
Kernel/Thread.h

@@ -70,6 +70,7 @@ public:
     public:
     public:
         virtual ~Blocker() {}
         virtual ~Blocker() {}
         virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
         virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
+        virtual const char* state_string() const = 0;
     };
     };
 
 
     class FileDescriptionBlocker : public Blocker {
     class FileDescriptionBlocker : public Blocker {
@@ -85,45 +86,53 @@ public:
     public:
     public:
         AcceptBlocker(const RefPtr<FileDescription>& description);
         AcceptBlocker(const RefPtr<FileDescription>& description);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Accepting"; }
     };
     };
 
 
     class ReceiveBlocker : public FileDescriptionBlocker {
     class ReceiveBlocker : public FileDescriptionBlocker {
     public:
     public:
         ReceiveBlocker(const RefPtr<FileDescription>& description);
         ReceiveBlocker(const RefPtr<FileDescription>& description);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Receiving"; }
     };
     };
 
 
     class ConnectBlocker : public FileDescriptionBlocker {
     class ConnectBlocker : public FileDescriptionBlocker {
     public:
     public:
         ConnectBlocker(const RefPtr<FileDescription>& description);
         ConnectBlocker(const RefPtr<FileDescription>& description);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Connecting"; }
     };
     };
 
 
     class WriteBlocker : public FileDescriptionBlocker {
     class WriteBlocker : public FileDescriptionBlocker {
     public:
     public:
         WriteBlocker(const RefPtr<FileDescription>& description);
         WriteBlocker(const RefPtr<FileDescription>& description);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Writing"; }
     };
     };
 
 
     class ReadBlocker : public FileDescriptionBlocker {
     class ReadBlocker : public FileDescriptionBlocker {
     public:
     public:
         ReadBlocker(const RefPtr<FileDescription>& description);
         ReadBlocker(const RefPtr<FileDescription>& description);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Reading"; }
     };
     };
 
 
     class ConditionBlocker : public Blocker {
     class ConditionBlocker : public Blocker {
     public:
     public:
-        ConditionBlocker(Function<bool()> &condition);
+        ConditionBlocker(const char* state_string, Function<bool()> &condition);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return m_state_string; }
 
 
     private:
     private:
         Function<bool()> m_block_until_condition;
         Function<bool()> m_block_until_condition;
+        const char* m_state_string;
     };
     };
 
 
     class SleepBlocker : public Blocker {
     class SleepBlocker : public Blocker {
     public:
     public:
         SleepBlocker(u64 wakeup_time);
         SleepBlocker(u64 wakeup_time);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Sleeping"; }
 
 
     private:
     private:
         u64 m_wakeup_time { 0 };
         u64 m_wakeup_time { 0 };
@@ -134,6 +143,7 @@ public:
         typedef Vector<int, FD_SETSIZE> FDVector;
         typedef Vector<int, FD_SETSIZE> FDVector;
         SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
         SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Selecting"; }
 
 
     private:
     private:
         timeval m_select_timeout;
         timeval m_select_timeout;
@@ -147,6 +157,7 @@ public:
     public:
     public:
         WaitBlocker(int wait_options, pid_t& waitee_pid);
         WaitBlocker(int wait_options, pid_t& waitee_pid);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override { return "Waiting"; }
 
 
     private:
     private:
         int m_wait_options { 0 };
         int m_wait_options { 0 };
@@ -162,6 +173,16 @@ public:
 
 
         SemiPermanentBlocker(Reason reason);
         SemiPermanentBlocker(Reason reason);
         virtual bool should_unblock(Thread&, time_t, long) override;
         virtual bool should_unblock(Thread&, time_t, long) override;
+        virtual const char* state_string() const override
+        {
+            switch (m_reason) {
+                case Reason::Lurking:
+                    return "Lurking";
+                case Reason::Signal:
+                    return "Signal";
+            }
+            ASSERT_NOT_REACHED();
+        }
 
 
     private:
     private:
         Reason m_reason;
         Reason m_reason;
@@ -183,6 +204,7 @@ public:
     u16 selector() const { return m_far_ptr.selector; }
     u16 selector() const { return m_far_ptr.selector; }
     TSS32& tss() { return m_tss; }
     TSS32& tss() { return m_tss; }
     State state() const { return m_state; }
     State state() const { return m_state; }
+    const char* state_string() const;
     u32 ticks() const { return m_ticks; }
     u32 ticks() const { return m_ticks; }
 
 
     u64 sleep(u32 ticks);
     u64 sleep(u32 ticks);
@@ -190,7 +212,7 @@ public:
     void block(Blocker& blocker);
     void block(Blocker& blocker);
     void unblock();
     void unblock();
 
 
-    void block_until(Function<bool()>&&);
+    void block_until(const char* state_string, Function<bool()>&&);
     KResult wait_for_connect(FileDescription&);
     KResult wait_for_connect(FileDescription&);
 
 
     const FarPtr& far_ptr() const { return m_far_ptr; }
     const FarPtr& far_ptr() const { return m_far_ptr; }
@@ -281,8 +303,6 @@ private:
 
 
 HashTable<Thread*>& thread_table();
 HashTable<Thread*>& thread_table();
 
 
-const char* to_string(Thread::State);
-
 template<typename Callback>
 template<typename Callback>
 inline void Thread::for_each_in_state(State state, Callback callback)
 inline void Thread::for_each_in_state(State state, Callback callback)
 {
 {