Clean up profiler configure and usage functions.
authorDan Cross <crossd@gmail.com>
Tue, 8 Mar 2016 16:54:42 +0000 (11:54 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 8 Mar 2016 19:58:19 +0000 (14:58 -0500)
An incidental cleanup that became evident from the last cleanup;
the 'profiler_configure' function was unnecessarily hard to
follow due to lack of an early return.

Also, there was this odd function to return an array of strings
that could be used to construct an error message, but that were
used nowhere else; this was an encapsulation failure.  Change
that to just construct the error message and call it.

Arguably, the configure function should just call 'error()'. Oh
well.

Signed-off-by: Dan Cross <crossd@gmail.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/drivers/dev/kprof.c
kern/include/profiler.h
kern/src/profiler.c

index 51c215b..bc3fe08 100644 (file)
@@ -71,6 +71,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)
 {
@@ -247,6 +248,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();
 }
 
@@ -427,21 +433,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 +445,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 +472,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:
index aedd748..802a830 100644 (file)
@@ -14,7 +14,7 @@ struct file;
 struct cmdbuf;
 
 int profiler_configure(struct cmdbuf *cb);
-const char * const *profiler_configure_cmds(void);
+void profiler_append_configure_usage(char *msgbuf, size_t buflen);
 void profiler_init(void);
 void profiler_setup(void);
 void profiler_cleanup(void);
index 696b990..2f76ca6 100644 (file)
@@ -309,26 +309,30 @@ int profiler_configure(struct cmdbuf *cb)
                        error(EFAIL, "Profiler already running");
                profiler_queue_limit = (int) profiler_get_checked_value(
                        cb->f[1], 1024, 1024 * 1024, max_pmem / 32);
-       } else if (!strcmp(cb->f[0], "prof_cpubufsz")) {
+               return 1;
+       }
+       if (!strcmp(cb->f[0], "prof_cpubufsz")) {
                if (cb->nf < 2)
                        error(EFAIL, "prof_cpubufsz KB");
                profiler_cpu_buffer_size = (size_t) profiler_get_checked_value(
                        cb->f[1], 1024, 16 * 1024, 1024 * 1024);
-       } else {
-               return 0;
+               return 1;
        }
 
-       return 1;
+       return 0;
 }
 
-const char* const *profiler_configure_cmds(void)
+void profiler_append_configure_usage(char *msgbuf, size_t buflen)
 {
-       static const char * const cmds[] = {
-               "prof_qlimit", "prof_cpubufsz",
-               NULL
+       const char * const cmds[] = {
+               "prof_qlimit",
+               "prof_cpubufsz",
        };
 
-       return cmds;
+       for (int i = 0; i < COUNT_OF(cmds); i++) {
+               strlcat(msgbuf, "|", buflen);
+               strlcat(msgbuf, cmds[i], buflen);
+       }
 }
 
 static void profiler_release(struct kref *kref)