Build system to map MMIO into our virtual address space.
[akaros.git] / kern / src / monitor.c
index 7f03f24..4ede378 100644 (file)
@@ -25,6 +25,7 @@
 #include <syscall.h>
 #include <kmalloc.h>
 #include <elf.h>
+#include <event.h>
 
 #include <ros/timer.h>
 #include <ros/memlayout.h>
@@ -59,6 +60,7 @@ static command_t (RO commands)[] = {
        { "trace", "Run a specific measurement", mon_trace},
        { "monitor", "Run the monitor on another core", mon_monitor},
        { "fs", "Filesystem Diagnostics", mon_fs},
+       { "bb", "Try to run busybox (ash)", mon_bb},
 };
 #define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))
 
@@ -257,7 +259,7 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        struct file *bin_dir;
        int retval = 0;
 
-       bin_dir = path_to_file("/bin");
+       bin_dir = do_file_open("/bin", 0, 0);
        if (!bin_dir) {
                printk("No /bin directory!\n");
                return 1;
@@ -266,18 +268,14 @@ int mon_bin_ls(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        do {
                retval = bin_dir->f_op->readdir(bin_dir, &dir); 
                printk("%s\n", dir.d_name);
-               dir.d_off++;
        } while (retval == 1);
-       /* TODO: (REF) need to dealloc when this hits 0, atomic/concurrent/etc */
-       atomic_dec(&bin_dir->f_refcnt);
-       assert(!bin_dir->f_refcnt);
-       bin_dir->f_op->release(bin_dir->f_inode, bin_dir);
+       kref_put(&bin_dir->f_kref);
        return 0;
 }
 
 int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
-       if (argc != 2) {
+       if (argc < 2) {
                printk("Usage: bin_run FILENAME\n");
                return 1;
        }
@@ -285,23 +283,25 @@ int mon_bin_run(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        int retval = 0;
        char buf[6 + MAX_FILENAME_SZ] = "/bin/";        /* /bin/ + max + \0 */
        strncpy(buf + 5, argv[1], MAX_FILENAME_SZ);
-       program = path_to_file(buf);
+       program = do_file_open(buf, 0, 0);
        if (!program) {
                printk("No such program!\n");
                return 1;
        }
-       char *p_argv[] = {0, 0, 0};
-       char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};   /* for /bin/sh, i think */
-       p_argv[0] = file_name(program);
+       char **p_argv = kmalloc(sizeof(char*) * argc, 0);       /* bin_run's argc */
+       for (int i = 0; i < argc - 1; i++)
+               p_argv[i] = argv[i + 1];
+       p_argv[argc - 1] = 0;
+       char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
        struct proc *p = proc_create(program, p_argv, p_envp);
+       kfree(p_argv);
 
        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() */
-       /* TODO: (REF) need to dealloc when this hits 0, atomic/concurrent/etc */
-       atomic_dec(&program->f_refcnt);
+       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 */
@@ -358,7 +358,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;
@@ -436,11 +436,12 @@ int mon_kfunc(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        return 0;
 }
 
+/* Sending a vcoreid forces an event and an IPI/notification */
 int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
 {
        struct proc *p;
-       unsigned int num;
        uint32_t vcoreid;
+       struct event_msg msg = {0};
 
        if (argc < 3) {
                printk("Usage: notify PID NUM [VCOREID]\n");
@@ -451,18 +452,21 @@ int mon_notify(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                printk("No such proc\n");
                return 1;
        }
-       num = strtol(argv[2], 0, 0);
+       msg.ev_type = strtol(argv[2], 0, 0);
        if (argc == 4) {
                vcoreid = strtol(argv[3], 0, 0);
-               do_notify(p, vcoreid, num, 0);
+               post_vcore_event(p, &msg, vcoreid);
+               proc_notify(p, vcoreid);
        } else {
-               proc_notify(p, num, 0);
+               /* o/w, try and do what they want */
+               send_kernel_event(p, &msg, 0);
        }
-       proc_decref(p, 1);
+       kref_put(&p->kref);
        return 0;
 }
 
-/* Micro-benchmarky Measurements */
+/* 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;
@@ -493,14 +497,14 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                begin = start_timing();
 #ifdef __CONFIG_APPSERVER__
                printk("Warning: this will be inaccurate due to the appserver.\n");
-               end_refcnt = p->env_refcnt - p->procinfo->num_vcores - 1;
+               end_refcnt = kref_refcnt(&p->kref) - p->procinfo->num_vcores - 1;
 #endif /* __CONFIG_APPSERVER__ */
                proc_destroy(p);
-               proc_decref(p, 1);
+               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(p->env_refcnt != end_refcnt);   
+               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
@@ -527,14 +531,14 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        spin_on(p->procinfo->pcoremap[pcoreid].valid);
                        diff = stop_timing(begin);
                } else { /* preempt all cores, warned but no delay */
-                       end_refcnt = p->env_refcnt - p->procinfo->num_vcores;
+                       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(p->env_refcnt != end_refcnt);
+                       spin_on(kref_refcnt(&p->kref) != end_refcnt);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               kref_put(&p->kref);
        } else if (!strcmp(argv[1], "preempt-warn")) {
                if (argc < 3) {
                        printk("Give me a pid number.\n");
@@ -571,7 +575,7 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        spin_on(p->procinfo->num_vcores > 1);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               kref_put(&p->kref);
        } else if (!strcmp(argv[1], "preempt-raw")) {
                if (argc < 3) {
                        printk("Give me a pid number.\n");
@@ -600,17 +604,17 @@ int mon_measure(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
                        /* TODO: (RMS), if num_vcores == 0, RUNNABLE_M, schedule */
                } else { /* preempt all cores, no warning or waiting */
                        spin_lock(&p->proc_lock);
-                       end_refcnt = p->env_refcnt - p->procinfo->num_vcores;
+                       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(p->env_refcnt != end_refcnt);
+                       spin_on(kref_refcnt(&p->kref) != end_refcnt);
                        diff = stop_timing(begin);
                }
-               proc_decref(p, 1);
+               kref_put(&p->kref);
        } else {
                printk("Bad option\n");
                return 1;
@@ -735,11 +739,11 @@ static int runcmd(char *NTS real_buf, trapframe_t *tf) {
        return 0;
 }
 
-void monitor(trapframe_t *tf) {
-       static spinlock_t monitor_lock = SPINLOCK_INITIALIZER;
-       spin_lock_irqsave(&monitor_lock);
-
-       char *buf;
+void monitor(struct trapframe *tf)
+{
+       #define MON_CMD_LENGTH 256
+       char buf[MON_CMD_LENGTH];
+       int cnt;
 
        /* they are always disabled, since we have this irqsave lock */
        if (irq_is_enabled())
@@ -751,12 +755,10 @@ void monitor(trapframe_t *tf) {
        if (tf != NULL)
                print_trapframe(tf);
 
-       spin_unlock_irqsave(&monitor_lock);
        while (1) {
-               spin_lock_irqsave(&monitor_lock);
-               buf = readline("ROS(Core %d)> ", core_id());
-               if (buf != NULL) {
-                       spin_unlock_irqsave(&monitor_lock);
+               cnt = readline(buf, MON_CMD_LENGTH, "ROS(Core %d)> ", core_id());
+               if (cnt > 0) {
+                       buf[cnt] = 0;
                        if (runcmd(buf, tf) < 0)
                                break;
                }
@@ -766,18 +768,84 @@ void monitor(trapframe_t *tf) {
 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;
+       struct super_block *sb;
+       struct file *file;
+       struct inode *inode;
+       struct dentry *dentry;
        if (argc < 2) {
                printk("Usage: fs OPTION\n");
                printk("\topen: show all open files\n");
+               printk("\tinodes: show all inodes\n");
+               printk("\tdentries [lru|prune]: show all dentries, opt LRU/prune\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\n", i, file_name(i), i->f_refcnt);
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       TAILQ_FOREACH(file, &sb->s_files, f_list)
+                               printk("File: %08p, %s, Refs: %d, Drefs: %d, Irefs: %d\n", file,
+                                      file_name(file), kref_refcnt(&file->f_kref),
+                                      kref_refcnt(&file->f_dentry->d_kref),
+                                      kref_refcnt(&file->f_dentry->d_inode->i_kref));
+               }
+       } else if (!strcmp(argv[1], "inodes")) {
+               printk("Mounted FS Inodes:\n----------------------------\n");
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       TAILQ_FOREACH(inode, &sb->s_inodes, i_sb_list) {
+                               printk("Inode: %08p, Refs: %d, Nlinks: %d, Size(B): %d\n",
+                                      inode, kref_refcnt(&inode->i_kref), inode->i_nlink,
+                                      inode->i_size);
+                               TAILQ_FOREACH(dentry, &inode->i_dentry, d_alias)
+                                       printk("\t%s: Dentry: %08p, Refs: %d\n",
+                                              dentry->d_name.name, dentry,
+                                              kref_refcnt(&dentry->d_kref));
+                       }
+               }
+       } else if (!strcmp(argv[1], "dentries")) {
+               printk("Dentry Cache:\n----------------------------\n");
+               TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                       printk("Superblock for %s\n", sb->s_name);
+                       if (hashtable_count(sb->s_dcache)) {
+                               hashtable_itr_t *dcache_i = hashtable_iterator(sb->s_dcache);
+                               printk("DENTRY     FLAGS      REFCNT NAME\n");
+                               printk("--------------------------------\n");
+                               do {
+                                       struct dentry *d_i = hashtable_iterator_value(dcache_i);
+                                       printk("%08p %08p %02d     %s\n", d_i, d_i->d_flags,
+                                              kref_refcnt(&d_i->d_kref), d_i->d_name.name);
+                               } while (hashtable_iterator_advance(dcache_i));
+                       }
+               }
+               if (argc < 3)
+                       return 0;
+               if (!strcmp(argv[2], "lru")) {
+                       printk("LRU lists:\n");
+                       TAILQ_FOREACH(sb, &super_blocks, s_list) {
+                               printk("Superblock for %s\n", sb->s_name);
+                               TAILQ_FOREACH(dentry, &sb->s_lru_d, d_lru)
+                                       printk("Dentry: %08p, Name: %s\n", dentry,
+                                              dentry->d_name.name);
+                       }
+               } else if (!strcmp(argv[2], "prune")) {
+                       printk("Pruning unused dentries\n");
+                       TAILQ_FOREACH(sb, &super_blocks, s_list)
+                               dcache_prune(sb, FALSE);
+               }
+       } 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");
@@ -790,3 +858,9 @@ int mon_fs(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
        }
        return 0;
 }
+
+int mon_bb(int argc, char *NTS *NT COUNT(argc) argv, trapframe_t *tf)
+{
+       char *l_argv[3] = {"", "busybox", "ash"};
+       return mon_bin_run(3, l_argv, tf);
+}