You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2015/11/13 01:18:36 UTC

incubator-mynewt-larva git commit: add basic statistics module. still under development, rough first cut.

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 3fea5a999 -> 777d91e9b


add basic statistics module.  still under development, rough first cut.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/777d91e9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/777d91e9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/777d91e9

Branch: refs/heads/master
Commit: 777d91e9b76c188ae347716270bb47a121574983
Parents: 3fea5a9
Author: Sterling Hughes <st...@apache.org>
Authored: Thu Nov 12 16:18:18 2015 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Nov 12 16:18:30 2015 -0800

----------------------------------------------------------------------
 libs/shell/include/shell/shell.h |   2 +-
 libs/shell/src/shell.c           |   4 +-
 libs/util/include/util/stats.h   | 108 +++++++++++++++++
 libs/util/src/cbmem.c            |   1 -
 libs/util/src/log.c              |   4 +-
 libs/util/src/stats.c            | 213 ++++++++++++++++++++++++++++++++++
 project/blinky/src/main.c        |   4 +
 7 files changed, 330 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --git a/libs/shell/include/shell/shell.h b/libs/shell/include/shell/shell.h
index b7d5dd3..1bb9c8e 100644
--- a/libs/shell/include/shell/shell.h
+++ b/libs/shell/include/shell/shell.h
@@ -16,7 +16,7 @@
 #ifndef __SHELL_H__ 
 #define __SHELL_H__
 
-typedef int (*shell_cmd_func_t)(char **argv, int argc);
+typedef int (*shell_cmd_func_t)(int argc, char **argv);
 struct shell_cmd {
     char *sc_cmd;
     shell_cmd_func_t sc_cmd_func;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 56f99a5..87361ad 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -126,7 +126,7 @@ shell_cmd(char *cmd, char **argv, int argc)
     }
 
     if (sc) {
-        sc->sc_cmd_func(argv, argc);
+        sc->sc_cmd_func(argc, argv);
     } else {
         console_printf("Unknown command %s\n", cmd);
     }
@@ -219,7 +219,7 @@ shell_console_rx_cb(int full_line)
 }
 
 static int
-shell_echo_cmd(char **argv, int argc)
+shell_echo_cmd(int argc, char **argv)
 {
     int i; 
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/util/include/util/stats.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/stats.h b/libs/util/include/util/stats.h
new file mode 100644
index 0000000..f35e0c0
--- /dev/null
+++ b/libs/util/include/util/stats.h
@@ -0,0 +1,108 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __UTIL_STATS_H__ 
+#define __UTIL_STATS_H__ 
+
+#include <os/queue.h>
+#include <stdint.h>
+
+struct stats_name_map {
+    void *snm_off;
+    char *snm_name;
+};
+
+struct stats_hdr {
+    char *s_name;
+    uint8_t s_size;
+    uint8_t s_cnt;
+    uint16_t s_pad1;
+#ifdef STATS_NAME_ENABLE
+    struct stats_name_map *s_map;
+    int s_map_cnt;
+#endif
+    STAILQ_ENTRY(stats_hdr) s_next;
+};
+
+
+#define STATS_SECT_START(__name)           \
+struct stats_ ## __name {                  \
+    struct stats_hdr s_hdr;
+
+
+#define STATS_SECT_END(__name)           \
+} g_stats_ ## __name;
+
+#define STATS_SECT_NAME(__name) \
+    g_stats_ ## __name
+
+#define STATS_HDR(__name) ((struct stats_hdr *) &STATS_SECT_NAME(__name))
+
+#define STATS_SECT_VAR(__var) \
+    s##__var 
+
+#define STATS_SIZE_16 (sizeof(uint16_t))
+#define STATS_SIZE_32 (sizeof(uint32_t))
+#define STATS_SIZE_64 (sizeof(uint64_t))
+
+#define STATS_SECT_ENTRY(__var) uint32_t STATS_SECT_VAR(__var);
+#define STATS_SECT_ENTRY16(__var) uint16_t STATS_SECT_VAR(__var);
+#define STATS_SECT_ENTRY32(__var) uint32_t STATS_SECT_VAR(__var);
+#define STATS_SECT_ENTRY64(__var) uint64_t STATS_SECT_VAR(__var);
+
+#define STATS_SIZE_INIT_PARMS(__name, __size)                              \
+    __size,                                                                \
+    ((sizeof(STATS_SECT_NAME(__name)) - sizeof(struct stats_hdr)) / __size)
+
+
+#define STATS_INC(__name, __var) \
+    (STATS_SECT_NAME(__name).STATS_SECT_VAR(__var)++)
+
+#define STATS_INCN(__name, __var, __n) \
+    (STATS_SECT_NAME(__name).STATS_SECT_VAR(__var) += (__n))
+
+#ifdef STATS_NAME_ENABLE
+
+#define STATS_NAME_MAP_NAME(__name) g_stats_map_ ## __name
+
+#define STATS_NAME_START(__name)                      \
+struct stats_name_map STATS_NAME_MAP_NAME(__name)[] = {
+
+#define STATS_NAME(__name, __entry)                   \
+    { &STATS_SECT_NAME(__name).STATS_SECT_VAR(__entry), #__entry },
+
+#define STATS_NAME_END(__name)                        \
+};
+
+#define STATS_NAME_INIT_PARMS(__name)                                    \
+    &(STATS_NAME_MAP_NAME(__name)[0]),                                   \
+    (sizeof(STATS_NAME_MAP_NAME(__name)) / sizeof(struct stats_name_map))
+
+#else /* STATS_NAME_ENABLE */
+
+#define STATS_NAME_START(__name)
+#define STATS_NAME(__name, __entry)
+#define STATS_NAME_END(__name)
+#define STATS_NAME_INIT_PARMS(__name) NULL, 0
+
+#endif /* STATS_NAME_ENABLE */
+
+int stats_module_init(void);
+int stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, 
+    struct stats_name_map *map, uint8_t map_cnt);
+int stats_register(char *name, struct stats_hdr *shdr);
+struct stats_hdr *stats_find(char *name);
+
+#endif /* __UTIL_STATS_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/util/src/cbmem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/cbmem.c b/libs/util/src/cbmem.c
index 07968e1..84d161f 100644
--- a/libs/util/src/cbmem.c
+++ b/libs/util/src/cbmem.c
@@ -233,7 +233,6 @@ err:
     return (-1);
 }
 
-
 int 
 cbmem_walk(struct cbmem *cbmem, cbmem_walk_func_t walk_func, void *arg)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/util/src/log.c
----------------------------------------------------------------------
diff --git a/libs/util/src/log.c b/libs/util/src/log.c
index c553673..5d16364 100644
--- a/libs/util/src/log.c
+++ b/libs/util/src/log.c
@@ -29,7 +29,7 @@
 #include <console/console.h> 
 
 struct shell_cmd shell_log_cmd;
-int shell_registered;
+uint8_t shell_registered;
 
 #endif 
 
@@ -72,7 +72,7 @@ err:
 }
 
 static int 
-shell_log_dump_all(char **argv, int argc)
+shell_log_dump_all(int argc, char **argv)
 {
     struct util_log *log;
     int rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/libs/util/src/stats.c
----------------------------------------------------------------------
diff --git a/libs/util/src/stats.c b/libs/util/src/stats.c
new file mode 100644
index 0000000..1cdd337
--- /dev/null
+++ b/libs/util/src/stats.c
@@ -0,0 +1,213 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <os/os.h>
+
+#include <string.h>
+
+#include "util/stats.h" 
+
+#ifdef SHELL_PRESENT
+#include "shell/shell.h"
+#include "console/console.h"
+#endif
+
+#include <stdio.h>
+
+STATS_SECT_START(stats)
+    STATS_SECT_ENTRY(num_registered)
+    STATS_SECT_ENTRY(garbage1)
+    STATS_SECT_ENTRY(garbage2)
+STATS_SECT_END(stats)
+
+STATS_NAME_START(stats)
+    STATS_NAME(stats, num_registered)
+    STATS_NAME(stats, garbage1)
+    STATS_NAME(stats, garbage2)
+STATS_NAME_END(stats)
+
+STAILQ_HEAD(, stats_hdr) g_stats_registry = 
+    STAILQ_HEAD_INITIALIZER(g_stats_registry);
+
+#ifdef SHELL_PRESENT
+uint8_t stats_shell_registered;
+struct shell_cmd shell_stats_cmd;
+#endif
+
+#ifdef SHELL_PRESENT 
+
+static void 
+shell_stats_display_entry(struct stats_hdr *hdr, uint8_t *ptr)
+{
+    char *name;
+    char buf[12];
+    int ent_n;
+    int len;
+#ifdef STATS_NAME_ENABLE
+    int i;
+#endif
+
+    name = NULL;
+
+#ifdef STATS_NAME_ENABLE
+    for (i = 0; i < hdr->s_map_cnt; i++) {
+        if (hdr->s_map[i].snm_off == ptr) {
+            name = hdr->s_map[i].snm_name;
+            break;
+        }
+    }
+#endif 
+
+    if (name == NULL) {
+        ent_n = (ptr - ((uint8_t *) hdr + sizeof(*hdr))) / hdr->s_size;
+
+        len = snprintf(buf, sizeof(buf), "s%d", ent_n);
+        buf[len] = 0;
+        name = buf;
+    }
+
+    switch (hdr->s_size) {
+        case sizeof(uint16_t):
+            console_printf("%s: %u\n", name, *(uint16_t *) ptr);
+            break;
+        case sizeof(uint32_t):
+            console_printf("%s: %u\n", name, *(uint32_t *) ptr);
+            break;
+        case sizeof(uint64_t):
+            console_printf("%s: %llu\n", name, *(uint64_t *) ptr);
+            break;
+        default:
+            console_printf("Unknown stat size for %s %u\n", name, 
+                    hdr->s_size);
+            break;
+    }
+}
+
+static int 
+shell_stats_display(int argc, char **argv)
+{
+    struct stats_hdr *hdr;
+    char *name;
+    uint8_t *cur;
+    uint8_t *end;
+
+    name = argv[1];
+
+    hdr = stats_find(name);
+    if (!hdr) {
+        console_printf("Could not find statistic %s\n", name);
+        goto done;
+    }
+
+    cur = (uint8_t *) hdr + sizeof(*hdr);
+    end = (uint8_t *) hdr + sizeof(*hdr) + (hdr->s_size * hdr->s_cnt);
+    while (cur < end) {
+        shell_stats_display_entry(hdr, (uint8_t *) cur);
+        cur += hdr->s_size;
+    }
+
+done:
+    return (0);
+}
+
+#endif
+
+int 
+stats_module_init(void)
+{
+    int rc;
+#ifdef SHELL_PRESENT
+    if (!stats_shell_registered) {
+        stats_shell_registered = 1;
+        shell_cmd_register(&shell_stats_cmd, "stat", shell_stats_display);
+    }
+#endif
+
+    rc = stats_init(STATS_HDR(stats), STATS_SIZE_INIT_PARMS(stats, STATS_SIZE_32), 
+            STATS_NAME_INIT_PARMS(stats));
+    if (rc != 0) {
+        goto err;
+    }
+    
+    rc = stats_register("stat", STATS_HDR(stats));
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+int
+stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, 
+        struct stats_name_map *map, uint8_t map_cnt)
+{
+    memset((uint8_t *) shdr, 0, sizeof(*shdr) + (size * cnt));
+
+    shdr->s_size = size;
+    shdr->s_cnt = cnt;
+#ifdef STATS_NAME_ENABLE
+    shdr->s_map = map;
+    shdr->s_map_cnt = map_cnt;
+#endif
+
+    return (0);
+}
+
+int
+stats_register(char *name, struct stats_hdr *shdr)
+{
+    struct stats_hdr *cur;
+    int rc;
+
+    /* Don't allow duplicate entries, return an error if this stat 
+     * is already registered.
+     */
+    STAILQ_FOREACH(cur, &g_stats_registry, s_next) {
+        if (!strcmp(cur->s_name, name)) {
+            rc = -1;
+            goto err;
+        }
+    }
+
+    shdr->s_name = name;
+
+    STAILQ_INSERT_TAIL(&g_stats_registry, shdr, s_next);
+
+    STATS_INC(stats, num_registered);
+
+    return (0);
+err:
+    return (rc);
+}
+
+struct stats_hdr * 
+stats_find(char *name)
+{
+    struct stats_hdr *cur;
+
+    cur = NULL;
+    STAILQ_FOREACH(cur, &g_stats_registry, s_next) {
+        if (!strcmp(cur->s_name, name)) {
+            break;
+        }
+    }
+
+    return (cur);
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/777d91e9/project/blinky/src/main.c
----------------------------------------------------------------------
diff --git a/project/blinky/src/main.c b/project/blinky/src/main.c
index 70f9a52..b5d5fb1 100755
--- a/project/blinky/src/main.c
+++ b/project/blinky/src/main.c
@@ -19,6 +19,7 @@
 #include "console/console.h" 
 #include "shell/shell.h"
 #include "util/log.h"
+#include "util/stats.h" 
 #include <assert.h>
 #include <string.h>
 
@@ -155,6 +156,9 @@ main(void)
     shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE);
 
     (void) console_init(shell_console_rx_cb);
+
+    stats_module_init();
+
     rc = init_tasks();
     os_start();