Sorted out proc refcounting with ARCs
[akaros.git] / kern / src / monitor.c
index 7985c43..f116501 100644 (file)
 #include <pmap.h>
 #include <kdebug.h>
 #include <testing.h>
-#include <kfs.h>
 #include <manager.h>
 #include <schedule.h>
 #include <resource.h>
 #include <kdebug.h>
+#include <syscall.h>
+#include <kmalloc.h>
+#include <elf.h>
 
+#include <ros/timer.h>
 #include <ros/memlayout.h>
 
 #define CMDBUF_SIZE    80      // enough for one VGA text line
@@ -40,18 +43,22 @@ static command_t (RO commands)[] = {
        { "kerninfo", "Display information about the kernel", mon_kerninfo },
        { "backtrace", "Dump a backtrace", mon_backtrace },
        { "reboot", "Take a ride to the South Bay", mon_reboot },
-       { "showmapping", "Shows VA->PA mappings between two virtual addresses (parameters)", mon_showmapping},
+       { "showmapping", "Shows VA->PA mappings", mon_showmapping},
        { "setmapperm", "Sets permissions on a VA->PA mapping", mon_setmapperm},
        { "cpuinfo", "Prints CPU diagnostics", mon_cpuinfo},
        { "ps", "Prints process list", mon_ps},
        { "nanwan", "Meet Nanwan!!", mon_nanwan},
-       { "kfs_ls", "List files in KFS", mon_kfs_ls},
-       { "kfs_run", "Create and run a program from KFS", mon_kfs_run},
-       { "kfs_cat", "Dumps text from a file from KFS", mon_kfs_cat},
+       { "bin_ls", "List files in /bin", mon_bin_ls},
+       { "bin_run", "Create and run a program from /bin", mon_bin_run},
        { "manager", "Run the manager", mon_manager},
        { "procinfo", "Show information about processes", mon_procinfo},
        { "exit", "Leave the monitor", mon_exit},
        { "kfunc", "Run a kernel function directly (!!!)", mon_kfunc},
+       { "notify", "Notify a process.  Vcoreid will skip their prefs", mon_notify},
+       { "measure", "Run a specific measurement", mon_measure},
+       { "trace", "Run a specific measurement", mon_trace},
+       { "monitor", "Run the monitor on another core", mon_monitor},
+       { "fs", "Filesystem Diagnostics", mon_fs},
 };
 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
 
@@ -244,55 +251,60 @@ int mon_nanwan(int argc, char **argv, trapframe_t *tf)
        return 0;
 }
 
-int mon_kfs_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
-       printk("Files in KFS:\n-------------------------------\n");
-       for (int i = 0; i < MAX_KFS_FILES; i++)
-               if (kfs[i].name[0])
-                       printk("%s\n", kfs[i].name);
+       struct dirent dir = {0};
+       struct file *bin_dir;
+       int retval = 0;
+
+       bin_dir = do_file_open("/bin", 0, 0);
+       if (!bin_dir) {
+               printk("No /bin directory!\n");
+               return 1;
+       }
+       printk("Files in /bin:\n-------------------------------\n");
+       do {
+               retval = bin_dir->f_op->readdir(bin_dir, &dir); 
+               printk("%s\n", dir.d_name);
+               dir.d_off++;
+       } while (retval == 1);
+       kref_put(&bin_dir->f_kref);
        return 0;
 }
 
-int mon_kfs_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        if (argc != 2) {
-               printk("Usage: kfs_run FILENAME\n");
+               printk("Usage: bin_run FILENAME\n");
                return 1;
        }
-       int kfs_inode = kfs_lookup_path(argv[1]);
-       if (kfs_inode < 0) {
-               printk("Bad filename!\n");
+       struct file *program;
+       int retval = 0;
+       char buf[6 + MAX_FILENAME_SZ] = "/bin/";        /* /bin/ + max + \0 */
+       strncpy(buf + 5, argv[1], MAX_FILENAME_SZ);
+       program = do_file_open(buf, 0, 0);
+       if (!program) {
+               printk("No such program!\n");
                return 1;
        }
-       struct proc *p = kfs_proc_create(kfs_inode);
-       // go from PROC_CREATED->PROC_RUNNABLE_S
-       spin_lock(&p->proc_lock); // might not be necessary for a mon function
+       char *p_argv[] = {0, 0, 0};
+       char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
+       p_argv[0] = file_name(program);
+       struct proc *p = proc_create(program, p_argv, p_envp);
+
+       spin_lock(&p->proc_lock);
        __proc_set_state(p, PROC_RUNNABLE_S);
        schedule_proc(p);
        spin_unlock(&p->proc_lock);
-       proc_decref(p, 1); // let go of the reference created in proc_create()
-       // Should never return from schedule (env_pop in there)
-       // also note you may not get the process you created, in the event there
-       // are others floating around that are runnable
+       kref_put(&p->kref); /* let go of the reference created in proc_create() */
+       kref_put(&program->f_kref);
+       /* Should never return from schedule (env_pop in there) also note you may
+        * not get the process you created, in the event there are others floating
+        * around that are runnable */
        schedule();
        return 0;
 }
 
-int mon_kfs_cat(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
-{
-       if (argc != 2) {
-               printk("Usage: kfs_cat FILENAME\n");
-               return 1;
-       }
-       int kfs_inode = kfs_lookup_path(argv[1]);
-       if (kfs_inode < 0) {
-               printk("Bad filename!\n");
-               return 1;
-       }
-       kfs_cat(kfs_inode);
-       return 0;
-}
-
 int mon_procinfo(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        if (argc < 2) {
@@ -302,7 +314,7 @@ int mon_procinfo(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("\trunnable: show proc_runnablelist\n");
                printk("\tresources: show resources wanted/granted for all procs\n");
                printk("\tpid NUM: show a lot of info for proc NUM\n");
-               printk("\tunlock NUM: unlock the lock for proc NUM (OMG!!!)\n");
+               printk("\tunlock: unlock the lock for the ADDR (OMG!!!)\n");
                printk("\tkill NUM: destroy proc NUM\n");
                return 1;
        }
@@ -322,16 +334,15 @@ int mon_procinfo(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                print_proc_info(strtol(argv[2], 0, 0));
        } else if (!strcmp(argv[1], "unlock")) {
                if (argc != 3) {
-                       printk("Give me a pid number.\n");
+                       printk("Gimme lock address!  Me want lock address!.\n");
                        return 1;
                }
-               struct proc *p = pid2proc(strtol(argv[2], 0, 0));
-               if (!p) {
-                       printk("No such proc\n");
+               spinlock_t *lock = (spinlock_t*)strtol(argv[2], 0, 16);
+               if (!lock) {
+                       printk("Null address...\n");
                        return 1;
                }
-               spin_unlock(&p->proc_lock);
-               proc_decref(p, 1);
+               spin_unlock(lock);
        } else if (!strcmp(argv[1], "kill")) {
                if (argc != 3) {
                        printk("Give me a pid number.\n");
@@ -343,7 +354,7 @@ int mon_procinfo(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        return 1;
                }
                proc_destroy(p);
-               proc_decref(p, 1);
+               kref_put(&p->kref);
        } else {
                printk("Bad option\n");
                return 1;
@@ -421,6 +432,262 @@ int mon_kfunc(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        return 0;
 }
 
+int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       struct proc *p;
+       unsigned int num;
+       uint32_t vcoreid;
+
+       if (argc < 3) {
+               printk("Usage: notify PID NUM [VCOREID]\n");
+               return 1;
+       }
+       p = pid2proc(strtol(argv[1], 0, 0));
+       if (!p) {
+               printk("No such proc\n");
+               return 1;
+       }
+       num = strtol(argv[2], 0, 0);
+       if (argc == 4) {
+               vcoreid = strtol(argv[3], 0, 0);
+               do_notify(p, vcoreid, num, 0);
+       } else {
+               proc_notify(p, num, 0);
+       }
+       kref_put(&p->kref);
+       return 0;
+}
+
+/* Micro-benchmarky Measurements.  This is really fragile code that probably
+ * won't work perfectly, esp as the kernel evolves. */
+int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       uint64_t begin = 0, diff = 0;
+       bool self_ipi_pending = FALSE;
+       uint32_t end_refcnt = 0;
+
+       if (argc < 2) {
+               printk("Usage: measure OPTION\n");
+               printk("\tkill PID : kill proc PID\n");
+               printk("\tpreempt PID : preempt proc PID (no delay)\n");
+               printk("\tpreempt PID [pcore] : preempt PID's pcore (no delay)\n");
+               printk("\tpreempt-warn PID : warn-preempt proc PID (pending)\n");
+               printk("\tpreempt-warn PID [pcore] : warn-preempt proc PID's pcore\n");
+               printk("\tpreempt-raw PID : raw-preempt proc PID\n");
+               printk("\tpreempt-raw PID [pcore] : raw-preempt proc PID's pcore\n");
+               return 1;
+       }
+       if (!strcmp(argv[1], "kill")) {
+               if (argc < 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               struct proc *p = pid2proc(strtol(argv[2], 0, 0));
+               if (!p) {
+                       printk("No such proc\n");
+                       return 1;
+               }
+               begin = start_timing();
+#ifdef __CONFIG_APPSERVER__
+               printk("Warning: this will be inaccurate due to the appserver.\n");
+               end_refcnt = kref_refcnt(&p->kref) - p->procinfo->num_vcores - 1;
+#endif /* __CONFIG_APPSERVER__ */
+               proc_destroy(p);
+               kref_put(&p->kref);
+#ifdef __CONFIG_APPSERVER__
+               /* Won't be that accurate, since it's not actually going through the
+                * __proc_free() path. */
+               spin_on(kref_refcnt(&p->kref) != end_refcnt);   
+#else
+               /* this is a little ghetto. it's not fully free yet, but we are also
+                * slowing it down by messing with it, esp with the busy waiting on a
+                * hyperthreaded core. */
+               spin_on(p->env_cr3);
+#endif /* __CONFIG_APPSERVER__ */
+               /* No noticeable difference using stop_timing instead of read_tsc() */
+               diff = stop_timing(begin);
+       } else if (!strcmp(argv[1], "preempt")) {
+               if (argc < 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               struct proc *p = pid2proc(strtol(argv[2], 0, 0));
+               if (!p) {
+                       printk("No such proc\n");
+                       return 1;
+               }
+               if (argc == 4) { /* single core being preempted, warned but no delay */
+                       uint32_t pcoreid = strtol(argv[3], 0, 0);
+                       begin = start_timing();
+                       proc_preempt_core(p, pcoreid, 1000000); // 1 sec warning
+                       /* done when unmapped (right before abandoning) */
+                       spin_on(p->procinfo->pcoremap[pcoreid].valid);
+                       diff = stop_timing(begin);
+               } else { /* preempt all cores, warned but no delay */
+                       end_refcnt = kref_refcnt(&p->kref) - p->procinfo->num_vcores;
+                       begin = start_timing();
+                       proc_preempt_all(p, 1000000);
+                       /* a little ghetto, implies no one is using p */
+                       spin_on(kref_refcnt(&p->kref) != end_refcnt);
+                       diff = stop_timing(begin);
+               }
+               kref_put(&p->kref);
+       } else if (!strcmp(argv[1], "preempt-warn")) {
+               if (argc < 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               struct proc *p = pid2proc(strtol(argv[2], 0, 0));
+               if (!p) {
+                       printk("No such proc\n");
+                       return 1;
+               }
+               if (argc == 4) { /* single core being preempted-warned */
+                       uint32_t pcoreid = strtol(argv[3], 0, 0);
+                       spin_lock(&p->proc_lock);
+                       uint32_t vcoreid = p->procinfo->pcoremap[pcoreid].vcoreid;
+                       if (!p->procinfo->pcoremap[pcoreid].valid) {
+                               printk("Pick a mapped pcore\n");
+                               spin_unlock(&p->proc_lock);
+                               return 1;
+                       }
+                       begin = start_timing();
+                       __proc_preempt_warn(p, vcoreid, 1000000); // 1 sec
+                       spin_unlock(&p->proc_lock);
+                       /* done when unmapped (right before abandoning) */
+                       spin_on(p->procinfo->pcoremap[pcoreid].valid);
+                       diff = stop_timing(begin);
+               } else { /* preempt-warn all cores */
+                       printk("Warning, this won't work if they can't yield their "
+                              "last vcore, will stop at 1!\n");
+                       spin_lock(&p->proc_lock);
+                       begin = start_timing();
+                       __proc_preempt_warnall(p, 1000000);
+                       spin_unlock(&p->proc_lock);
+                       /* target cores do the unmapping / changing of the num_vcores */
+                       spin_on(p->procinfo->num_vcores > 1);
+                       diff = stop_timing(begin);
+               }
+               kref_put(&p->kref);
+       } else if (!strcmp(argv[1], "preempt-raw")) {
+               if (argc < 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               struct proc *p = pid2proc(strtol(argv[2], 0, 0));
+               if (!p) {
+                       printk("No such proc\n");
+                       return 1;
+               }
+               if (argc == 4) { /* single core preempted, no warning or waiting */
+                       uint32_t pcoreid = strtol(argv[3], 0, 0);
+                       spin_lock(&p->proc_lock);
+                       if (!p->procinfo->pcoremap[pcoreid].valid) {
+                               printk("Pick a mapped pcore\n");
+                               spin_unlock(&p->proc_lock);
+                               return 1;
+                       }
+                       begin = start_timing();
+                       self_ipi_pending = __proc_preempt_core(p, pcoreid);
+                       spin_unlock(&p->proc_lock);
+                       __proc_kmsg_pending(p, self_ipi_pending);
+                       /* done when unmapped (right before abandoning) */
+                       spin_on(p->procinfo->pcoremap[pcoreid].valid);
+                       diff = stop_timing(begin);
+                       /* TODO: (RMS), if num_vcores == 0, RUNNABLE_M, schedule */
+               } else { /* preempt all cores, no warning or waiting */
+                       spin_lock(&p->proc_lock);
+                       end_refcnt = kref_refcnt(&p->kref) - p->procinfo->num_vcores;
+                       begin = start_timing();
+                       self_ipi_pending = __proc_preempt_all(p);
+                       /* TODO: (RMS), RUNNABLE_M, schedule */
+                       spin_unlock(&p->proc_lock);
+                       __proc_kmsg_pending(p, self_ipi_pending);
+                       /* a little ghetto, implies no one else is using p */
+                       spin_on(kref_refcnt(&p->kref) != end_refcnt);
+                       diff = stop_timing(begin);
+               }
+               kref_put(&p->kref);
+       } else {
+               printk("Bad option\n");
+               return 1;
+       }
+       printk("[Tired Giraffe Accent] Took %llu usec (%llu nsec) to finish.\n",
+              diff * 1000000 / system_timing.tsc_freq,
+              diff * 1000000000 / system_timing.tsc_freq);
+       return 0;
+}
+
+int mon_trace(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       if (argc < 2) {
+               printk("Usage: trace OPTION\n");
+               printk("\tsyscall start [silent] [pid]: starts tracing\n");
+               printk("\tsyscall stop: stops tracing, prints if it was silent\n");
+               return 1;
+       }
+       if (!strcmp(argv[1], "syscall")) {
+               if (argc < 3) {
+                       printk("Need a start or stop.\n");
+                       return 1;
+               }
+               if (!strcmp(argv[2], "start")) {
+                       bool all = TRUE;
+                       bool silent = FALSE;
+                       struct proc *p = NULL;
+                       if (argc >= 4) {
+                               silent = (bool)strtol(argv[3], 0, 0);
+                       }
+                       if (argc >= 5) {
+                               all = FALSE;
+                               p = pid2proc(strtol(argv[4], 0, 0));
+                               if (!p) {
+                                       printk("No such process\n");
+                                       return 1;
+                               }
+                       }
+                       systrace_start(silent);
+                       if (systrace_reg(all, p))
+                               printk("No room to trace more processes\n");
+               } else if (!strcmp(argv[2], "stop")) {
+                       /* Stop and print for all processes */
+                       systrace_stop();
+                       systrace_print(TRUE, 0);
+                       systrace_clear_buffer();
+               }
+       } else if (!strcmp(argv[1], "opt2")) {
+               if (argc != 3) {
+                       printk("ERRRRRRRRRR.\n");
+                       return 1;
+               }
+               print_proc_info(strtol(argv[2], 0, 0));
+       } else {
+               printk("Bad option\n");
+               return 1;
+       }
+       return 0;
+}
+
+int mon_monitor(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       if (argc < 2) {
+               printk("Usage: monitor COREID\n");
+               return 1;
+       }
+       uint32_t core = strtol(argv[1], 0, 0);
+       if (core >= num_cpus) {
+               printk("No such core!  Maybe it's in another cell...\n");
+               return 1;
+       }
+       void run_mon(struct trapframe *tf, uint32_t srcid, void *a0, void *a1,
+                    void *a2)
+       {
+               monitor(0); // TODO consider passing the tf
+       }
+       send_kernel_message(core, run_mon, 0, 0, 0, KMSG_ROUTINE);
+       return 0;
+}
+
 /***** Kernel monitor command interpreter *****/
 
 #define WHITESPACE "\t\r\n "
@@ -467,22 +734,71 @@ static int runcmd(char *NTS real_buf, trapframe_t *tf) {
 
 void monitor(trapframe_t *tf) {
        static spinlock_t monitor_lock = SPINLOCK_INITIALIZER;
-       spin_lock(&monitor_lock);
+       spin_lock_irqsave(&monitor_lock);
 
        char *buf;
 
-       printk("Welcome to the ROS kernel monitor on core %d!\n", core_id());
+       /* they are always disabled, since we have this irqsave lock */
+       if (irq_is_enabled())
+               printk("Entering Nanwan's Dungeon on Core %d (Ints on):\n", core_id());
+       else
+               printk("Entering Nanwan's Dungeon on Core %d (Ints off):\n", core_id());
        printk("Type 'help' for a list of commands.\n");
 
        if (tf != NULL)
                print_trapframe(tf);
 
+       spin_unlock_irqsave(&monitor_lock);
        while (1) {
-               buf = readline("K> ");
-               if (buf != NULL)
+               spin_lock_irqsave(&monitor_lock);
+               buf = readline("ROS(Core %d)> ", core_id());
+               if (buf != NULL) {
+                       spin_unlock_irqsave(&monitor_lock);
                        if (runcmd(buf, tf) < 0)
                                break;
+               }
        }
+}
 
-       spin_unlock(&monitor_lock);
+int mon_fs(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       /* this assumes one mounted FS at the NS root */
+       struct super_block *sb = default_ns.root->mnt_root->d_inode->i_sb;
+       struct file *i;
+       if (argc < 2) {
+               printk("Usage: fs OPTION\n");
+               printk("\topen: show all open files\n");
+               printk("\tls DIR: print the dir tree starting with DIR\n");
+               printk("\tpid: proc PID's fs crap placeholder\n");
+               return 1;
+       }
+       if (!strcmp(argv[1], "open")) {
+               printk("Open Files:\n----------------------------\n");
+               TAILQ_FOREACH(i, &sb->s_files, f_list)
+                       printk("File: %08p, %s, Refs: %d, Drefs: %d, Irefs: %d\n", i,
+                              file_name(i), kref_refcnt(&i->f_kref),
+                                  kref_refcnt(&i->f_dentry->d_kref),
+                                  kref_refcnt(&i->f_dentry->d_inode->i_kref));
+       } else if (!strcmp(argv[1], "ls")) {
+               if (argc != 3) {
+                       printk("Give me a dir.\n");
+                       return 1;
+               }
+               if (argv[2][0] != '/') {
+                       printk("Dear fellow giraffe lover, Use absolute paths.\n");
+                       return 1;
+               }
+               ls_dash_r(argv[2]);
+               /* whatever.  placeholder. */
+       } else if (!strcmp(argv[1], "pid")) {
+               if (argc != 3) {
+                       printk("Give me a pid number.\n");
+                       return 1;
+               }
+               /* whatever.  placeholder. */
+       } else {
+               printk("Bad option\n");
+               return 1;
+       }
+       return 0;
 }