dedicated kthread interface and enablement of lrud auto-recycler
[lunaix-os.git] / lunaix-os / includes / lunaix / process.h
index fe33fa2136c5f73c731a950d90deba370ac9de6f..d35c2340822805918165189b854d50a30319197d 100644 (file)
@@ -13,6 +13,8 @@
 #include <lunaix/types.h>
 #include <lunaix/spike.h>
 #include <lunaix/hart_state.h>
+#include <lunaix/usrscope.h>
+
 #include <stdint.h>
 
 
 #define PS_GrDT (PS_TERMNAT | PS_DESTROY)
 #define PS_Rn (PS_RUNNING | PS_CREATED)
 
-#define proc_terminated(proc) (((proc)->state) & PS_GrDT)
-#define proc_hanged(proc) (((proc)->state) & PS_BLOCKED)
-#define proc_runnable(proc) (!(proc)->state || !(((proc)->state) & ~PS_Rn))
+#define proc_terminated(proc) \
+            (!(proc) || ((proc)->state) & PS_GrDT)
+#define proc_hanged(proc) \
+            ((proc)  && ((proc)->state) & PS_BLOCKED)
+#define proc_runnable(proc) \
+            ((proc) && (!(proc)->state || !(((proc)->state) & ~PS_Rn)))
 
 
-#define TH_DETACHED 0b0001
+#define TH_DETACHED         0b00000001
+#define TH_STALLED          0b00000010
 
 #define thread_detached(th) ((th)->flags & TH_DETACHED)
 #define detach_thread(th) ((th)->flags |= TH_DETACHED)
 
+#define thread_flags_set(th, flag)      ((th)->flags |= (flag))
+#define thread_flags_clear(th, flag)    ((th)->flags &= ~(flag))
+#define thread_flags_test(th, flag)     ((th)->flags & (flag))
+#define thread_flags_test_all(th, flag) (((th)->flags & (flag)) == (flag))
+
 struct proc_sig
 {
     int sig_num;
@@ -72,11 +83,39 @@ struct haybed {
     time_t alarm_time;
 };
 
+struct thread_stats
+{
+    // number of times the thread entering kernel space involuntarily
+    unsigned long entry_count_invol;
+    // number of times the thread entering kernel space voluntarily
+    unsigned long entry_count_vol;
+    
+    // number of times the thread is preempted in kerenl space
+    unsigned long kpreempt_count;
+
+    // timestamp of last time kernel entry
+    time_t last_entry;
+    // timestamp of last time kernel reentry
+    time_t last_reentry;
+
+    // timestamp of last time kernel leave
+    time_t last_leave;
+    // timestamp of last time the thread is resumed
+    time_t last_resume;
+    
+    union {
+        struct {
+            bool at_user;
+        };
+        int flags;
+    };
+};
+
 struct thread
 {
     /*
         Any change to *critical section*, including layout, size
-        must be reflected in arch/i386/interrupt.S.inc to avoid
+        must be reflected in arch/<arch>/interrupt.S.inc to avoid
         disaster!
      */
     struct
@@ -99,6 +138,8 @@ struct thread
         struct mm_region* ustack;   // process local user stack (NULL for kernel thread)
     };
 
+    struct thread_stats stats;
+
     struct haybed sleep;
 
     struct proc_info* process;
@@ -128,6 +169,14 @@ struct proc_info
         pid_t pgid;
         time_t created;
 
+        uid_t euid;
+        uid_t suid;
+        gid_t egid;
+        gid_t sgid;
+
+        struct user_scope uscope;
+        struct v_dnode* root;
+
         int state;
         int exit_code;
     };
@@ -323,16 +372,6 @@ create_thread(struct proc_info* proc, bool with_ustack);
 void
 start_thread(struct thread* th, ptr_t entry);
 
-static inline void
-spawn_kthread(ptr_t entry) {
-    assert(kernel_process(__current));
-
-    struct thread* th = create_thread(__current, false);
-    
-    assert(th);
-    start_thread(th, entry);
-}
-
 void 
 exit_thread(void* val);
 
@@ -381,5 +420,82 @@ proc_setsignal(struct proc_info* proc, signum_t signum);
 void
 thread_setsignal(struct thread* thread, signum_t signum);
 
+void
+thread_stats_update(bool inbound, bool voluntary);
+
+static inline void
+thread_stats_update_entering(bool voluntary)
+{
+    thread_stats_update(true, voluntary);
+}
+
+static inline void
+thread_stats_update_leaving()
+{
+    thread_stats_update(false, true);
+}
+
+static inline void
+thread_stats_update_kpreempt()
+{
+    current_thread->stats.kpreempt_count++;
+}
+
+static inline void
+thread_stats_reset_kpreempt()
+{
+    current_thread->stats.kpreempt_count = 0;
+}
+
+static inline ticks_t
+thread_stats_kernel_elapse(struct thread* thread)
+{
+    return clock_systime() - thread->stats.last_reentry;
+}
+
+static inline ticks_t
+thread_stats_user_elapse(struct thread* thread)
+{
+    struct thread_stats* stats;
+    stats = &thread->stats;
+
+    return stats->last_entry - stats->last_leave;
+}
+
+static inline struct user_scope*
+current_user_scope()
+{
+    return &__current->uscope;
+}
+
+static inline uid_t must_inline
+current_euid()
+{
+    return __current->euid;
+}
+
+static inline bool must_inline
+current_is_root()
+{
+    return current_euid() == 0;
+}
+
+static inline gid_t must_inline
+current_egid()
+{
+    return __current->egid;
+}
+
+static inline void must_inline
+current_set_egid(gid_t gid)
+{
+    __current->egid = gid;
+}
+
+static inline void must_inline
+current_set_euid(uid_t uid)
+{
+    __current->euid = uid;
+}
 
 #endif /* __LUNAIX_PROCESS_H */