Add the helper strtoul_from_ubuf()
[akaros.git] / kern / drivers / dev / kprof.c
index 71889ec..cb777db 100644 (file)
@@ -26,6 +26,7 @@
 #include <umem.h>
 #include <profiler.h>
 #include <kprof.h>
+#include <ros/procinfo.h>
 
 #define KTRACE_BUFFER_SIZE (128 * 1024)
 #define TRACE_PRINTK_BUFFER_SIZE (8 * 1024)
@@ -71,6 +72,7 @@ static spinlock_t ktrace_lock = SPINLOCK_INITIALIZER_IRQSAVE;
 static struct circular_buffer ktrace_data;
 static char ktrace_buffer[KTRACE_BUFFER_SIZE];
 static int kprof_timer_period = 1000;
+static char kprof_control_usage[128];
 
 static size_t mpstat_len(void)
 {
@@ -164,14 +166,14 @@ static void kprof_start_profiler(void)
 static void kprof_fetch_profiler_data(void)
 {
        size_t psize = kprof.psize + profiler_size();
-       char *ndata = krealloc(kprof.pdata, psize, KMALLOC_WAIT);
+       char *ndata = krealloc(kprof.pdata, psize, MEM_WAIT);
 
        if (!ndata)
                error(ENOMEM, ERROR_FIXME);
        kprof.pdata = ndata;
        while (kprof.psize < psize) {
                size_t csize = profiler_read(kprof.pdata + kprof.psize,
-                                                                        psize - kprof.psize);
+                                            psize - kprof.psize);
 
                if (csize == 0)
                        break;
@@ -229,7 +231,7 @@ static void kprof_init(void)
        kprof.psize = 0;
 
        kprof.alarms = kzmalloc(sizeof(struct alarm_waiter) * num_cores,
-                                                       KMALLOC_WAIT);
+                               MEM_WAIT);
        if (!kprof.alarms)
                error(ENOMEM, ERROR_FIXME);
        if (waserror()) {
@@ -247,6 +249,11 @@ static void kprof_init(void)
        kproftab[Kmpstatqid].length = mpstat_len();
        kproftab[Kmpstatrawqid].length = mpstatraw_len();
 
+       strlcpy(kprof_control_usage, "clear|start|stop|flush|timer",
+               sizeof(kprof_control_usage));
+       profiler_append_configure_usage(kprof_control_usage,
+                                       sizeof(kprof_control_usage));
+
        poperror();
 }
 
@@ -267,7 +274,7 @@ static void kprofclear(void)
 }
 
 static struct walkqid *kprof_walk(struct chan *c, struct chan *nc, char **name,
-                                                                int nname)
+                                  int nname)
 {
        return devwalk(c, nc, name, nname, kproftab, ARRAY_SIZE(kproftab), devgen);
 }
@@ -318,7 +325,7 @@ static void kprof_close(struct chan *c)
 static long mpstat_read(void *va, long n, int64_t off)
 {
        size_t bufsz = mpstat_len();
-       char *buf = kmalloc(bufsz, KMALLOC_WAIT);
+       char *buf = kmalloc(bufsz, MEM_WAIT);
        int len = 0;
        struct per_cpu_info *pcpui;
        uint64_t cpu_total;
@@ -356,7 +363,7 @@ static long mpstat_read(void *va, long n, int64_t off)
 static long mpstatraw_read(void *va, long n, int64_t off)
 {
        size_t bufsz = mpstatraw_len();
-       char *buf = kmalloc(bufsz, KMALLOC_WAIT);
+       char *buf = kmalloc(bufsz, MEM_WAIT);
        int len = 0;
        struct per_cpu_info *pcpui;
 
@@ -365,7 +372,7 @@ static long mpstatraw_read(void *va, long n, int64_t off)
 
        /* header line: version, num_cores, tsc freq, state names */
        len += snprintf(buf + len, bufsz - len, "v%03d %5d %16llu", 1, num_cores,
-                       system_timing.tsc_freq);
+                       __proc_global_info.tsc_freq);
        for (int j = 0; j < NR_CPU_STATES; j++)
                len += snprintf(buf + len, bufsz - len, " %6s", cpu_state_names[j]);
        len += snprintf(buf + len, bufsz - len, "\n");
@@ -427,21 +434,6 @@ static void kprof_manage_timer(int coreid, struct cmdbuf *cb)
        }
 }
 
-static void kprof_usage_fail(void)
-{
-       static const char *ctlstring = "clear|start|stop|flush|timer";
-       const char * const *cmds = profiler_configure_cmds();
-       char msgbuf[128];
-
-       strlcpy(msgbuf, ctlstring, sizeof(msgbuf));
-       for (int i = 0; cmds[i]; i++) {
-               strlcat(msgbuf, "|", sizeof(msgbuf));
-               strlcat(msgbuf, cmds[i], sizeof(msgbuf));
-       }
-
-       error(EFAIL, msgbuf);
-}
-
 static long kprof_write(struct chan *c, void *a, long n, int64_t unused)
 {
        ERRSTACK(1);
@@ -454,7 +446,7 @@ static long kprof_write(struct chan *c, void *a, long n, int64_t unused)
        switch ((int) c->qid.path) {
        case Kprofctlqid:
                if (cb->nf < 1)
-                       kprof_usage_fail();
+                       error(EFAIL, kprof_control_usage);
                if (profiler_configure(cb))
                        break;
                if (!strcmp(cb->f[0], "clear")) {
@@ -481,7 +473,7 @@ static long kprof_write(struct chan *c, void *a, long n, int64_t unused)
                } else if (!strcmp(cb->f[0], "stop")) {
                        kprof_stop_profiler();
                } else {
-                       kprof_usage_fail();
+                       error(EFAIL, kprof_control_usage);
                }
                break;
        case Kprofdataqid:
@@ -563,7 +555,7 @@ void kprof_tracedata_write(const char *pretty_buf, size_t len)
        spin_lock_irqsave(&ktrace_lock);
        if (unlikely(!ktrace_init_done)) {
                circular_buffer_init(&ktrace_data, sizeof(ktrace_buffer),
-                                                        ktrace_buffer);
+                                    ktrace_buffer);
                ktrace_init_done = TRUE;
        }
        circular_buffer_write(&ktrace_data, pretty_buf, len);
@@ -632,10 +624,10 @@ void trace_vprintk(bool btrace, const char *fmt, va_list args)
 
        if (!atomic_cas(&tpb->in_use, 0, 1))
                return;
-       if (likely(system_timing.tsc_freq))
+       if (likely(__proc_global_info.tsc_freq))
                tsc2timespec(read_tsc(), &ts_now);
        snprintf(hdr, sizeof(hdr), "[%lu.%09lu]:cpu%d: ", ts_now.tv_sec,
-                        ts_now.tv_nsec, core_id_early());
+                ts_now.tv_nsec, core_id_early());
 
        pb.ptr = usrbuf + vsnprintf(usrbuf, usr_bufsz, fmt, args);
        pb.top = usrbuf + usr_bufsz;