sys/stats; move the stats module to sys/stats/full. Tell that sys/stats/full package exports API 'stats'. Packages list 'stats' as required API. Update apps to depend on sys/stats/full.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/002424b2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/002424b2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/002424b2 Branch: refs/heads/develop Commit: 002424b24b683f4b54952967f42cb14122a3ecc7 Parents: 514c189 Author: Marko Kiiskila <[email protected]> Authored: Tue Jan 17 14:26:40 2017 -0800 Committer: Marko Kiiskila <[email protected]> Committed: Tue Jan 17 14:26:40 2017 -0800 ---------------------------------------------------------------------- apps/blehci/pkg.yml | 1 + apps/bletest/pkg.yml | 2 +- apps/bletiny/pkg.yml | 1 + apps/ffs2native/pkg.yml | 1 + apps/ocf_sample/pkg.yml | 1 + apps/slinky/pkg.yml | 4 +- apps/slinky_oic/pkg.yml | 4 +- apps/spitest/pkg.yml | 2 +- apps/splitty/pkg.yml | 2 +- apps/testbench/pkg.yml | 2 +- apps/timtest/pkg.yml | 2 +- fs/fatfs/pkg.yml | 3 +- fs/nffs/pkg.yml | 3 +- net/nimble/controller/pkg.yml | 2 +- net/nimble/host/pkg.yml | 2 +- net/oic/pkg.yml | 3 +- net/oic/test/pkg.yml | 2 +- sys/stats/full/include/stats/stats.h | 140 +++++++++++ sys/stats/full/pkg.yml | 37 +++ sys/stats/full/src/stats.c | 390 ++++++++++++++++++++++++++++++ sys/stats/full/src/stats_nmgr.c | 185 ++++++++++++++ sys/stats/full/src/stats_shell.c | 119 +++++++++ sys/stats/full/syscfg.yml | 32 +++ sys/stats/include/stats/stats.h | 140 ----------- sys/stats/pkg.yml | 35 --- sys/stats/src/stats.c | 390 ------------------------------ sys/stats/src/stats_nmgr.c | 185 -------------- sys/stats/src/stats_shell.c | 119 --------- sys/stats/syscfg.yml | 32 --- 29 files changed, 925 insertions(+), 916 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/blehci/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/blehci/pkg.yml b/apps/blehci/pkg.yml index 975d779..0e52317 100644 --- a/apps/blehci/pkg.yml +++ b/apps/blehci/pkg.yml @@ -25,6 +25,7 @@ pkg.keywords: pkg.deps: - libc/baselibc - sys/console/stub + - sys/stats/full - kernel/os - net/nimble/controller - net/nimble/transport/uart http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/bletest/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/bletest/pkg.yml b/apps/bletest/pkg.yml index 76a3536..bc4a111 100644 --- a/apps/bletest/pkg.yml +++ b/apps/bletest/pkg.yml @@ -33,5 +33,5 @@ pkg.deps: - sys/shell - sys/config - sys/log - - sys/stats + - sys/stats/full pkg.cflags: -DBLETEST http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/bletiny/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml index 703914d..7bb5047 100644 --- a/apps/bletiny/pkg.yml +++ b/apps/bletiny/pkg.yml @@ -33,4 +33,5 @@ pkg.deps: - net/nimble/host/store/ram - net/nimble/transport/ram - sys/console/full + - sys/stats/full - sys/shell http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/ffs2native/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/ffs2native/pkg.yml b/apps/ffs2native/pkg.yml index 0825d3c..94c38fd 100644 --- a/apps/ffs2native/pkg.yml +++ b/apps/ffs2native/pkg.yml @@ -28,4 +28,5 @@ pkg.deps: - fs/nffs - hw/hal - sys/console/full + - sys/stats/stub - kernel/os http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/ocf_sample/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/ocf_sample/pkg.yml b/apps/ocf_sample/pkg.yml index 7cf3510..339811b 100644 --- a/apps/ocf_sample/pkg.yml +++ b/apps/ocf_sample/pkg.yml @@ -30,6 +30,7 @@ pkg.deps: - net/oic - encoding/cborattr - sys/console/full + - sys/stats/full pkg.deps.OC_TRANSPORT_SERIAL: - sys/shell http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/slinky/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/slinky/pkg.yml b/apps/slinky/pkg.yml index f0785b9..41f44c5 100644 --- a/apps/slinky/pkg.yml +++ b/apps/slinky/pkg.yml @@ -25,7 +25,6 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - sys/console/full - test/flash_test - mgmt/imgmgr - mgmt/newtmgr @@ -34,9 +33,10 @@ pkg.deps: - boot/bootutil - sys/shell - sys/config + - sys/console/full - sys/id - sys/log - - sys/stats + - sys/stats/full - boot/split pkg.deps.CONFIG_NFFS: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/slinky_oic/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/slinky_oic/pkg.yml b/apps/slinky_oic/pkg.yml index 3a66a89..1c778de 100644 --- a/apps/slinky_oic/pkg.yml +++ b/apps/slinky_oic/pkg.yml @@ -25,16 +25,16 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - sys/console/full - mgmt/imgmgr - mgmt/oicmgr - kernel/os - boot/bootutil - sys/shell - sys/config + - sys/console/full - sys/id - sys/log - - sys/stats + - sys/stats/full - boot/split pkg.deps.CONFIG_NFFS: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/spitest/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/spitest/pkg.yml b/apps/spitest/pkg.yml index d850163..dfab7e9 100644 --- a/apps/spitest/pkg.yml +++ b/apps/spitest/pkg.yml @@ -30,4 +30,4 @@ pkg.deps: - sys/shell - sys/config - sys/log - - sys/stats + - sys/stats/full http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/splitty/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/splitty/pkg.yml b/apps/splitty/pkg.yml index 0bacc60..15ec2dd 100644 --- a/apps/splitty/pkg.yml +++ b/apps/splitty/pkg.yml @@ -37,4 +37,4 @@ pkg.deps: - sys/id - sys/log - sys/shell - - sys/stats + - sys/stats/full http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/testbench/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/testbench/pkg.yml b/apps/testbench/pkg.yml index 4e16304..720700d 100644 --- a/apps/testbench/pkg.yml +++ b/apps/testbench/pkg.yml @@ -36,7 +36,7 @@ pkg.deps: - sys/config - sys/id - sys/log - - sys/stats + - sys/stats/full - sys/flash_map - sys/flash_map/test - test/testutil http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/apps/timtest/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/timtest/pkg.yml b/apps/timtest/pkg.yml index 39505cb..be42735 100644 --- a/apps/timtest/pkg.yml +++ b/apps/timtest/pkg.yml @@ -30,4 +30,4 @@ pkg.deps: - sys/shell - sys/config - sys/log - - sys/stats + - sys/stats/full http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/fs/fatfs/pkg.yml ---------------------------------------------------------------------- diff --git a/fs/fatfs/pkg.yml b/fs/fatfs/pkg.yml index 7318674..61ac6f8 100644 --- a/fs/fatfs/pkg.yml +++ b/fs/fatfs/pkg.yml @@ -33,7 +33,8 @@ pkg.deps: - test/testutil - sys/flash_map - sys/log - - sys/stats +pkg.req_apis: + - stats pkg.init_function: fatfs_pkg_init pkg.init_stage: 2 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/fs/nffs/pkg.yml ---------------------------------------------------------------------- diff --git a/fs/nffs/pkg.yml b/fs/nffs/pkg.yml index 6010aae..1be62cf 100644 --- a/fs/nffs/pkg.yml +++ b/fs/nffs/pkg.yml @@ -34,7 +34,8 @@ pkg.deps: - test/testutil - sys/flash_map - sys/log - - sys/stats +pkg.req_apis: + - stats pkg.init_function: nffs_pkg_init pkg.init_stage: 2 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/net/nimble/controller/pkg.yml ---------------------------------------------------------------------- diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml index 7761c53..2a88df6 100644 --- a/net/nimble/controller/pkg.yml +++ b/net/nimble/controller/pkg.yml @@ -28,10 +28,10 @@ pkg.keywords: pkg.req_apis: - ble_driver - ble_transport + - stats pkg.deps: - kernel/os - - sys/stats - net/nimble pkg.init_function: ble_ll_init http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/net/nimble/host/pkg.yml ---------------------------------------------------------------------- diff --git a/net/nimble/host/pkg.yml b/net/nimble/host/pkg.yml index 65b4284..1ae103f 100644 --- a/net/nimble/host/pkg.yml +++ b/net/nimble/host/pkg.yml @@ -29,7 +29,6 @@ pkg.deps: - kernel/os - net/nimble - sys/log - - sys/stats - util/mem pkg.deps.BLE_SM_LEGACY: @@ -41,6 +40,7 @@ pkg.deps.BLE_SM_SC: pkg.req_apis: - ble_transport - console + - stats pkg.init_function: "ble_hs_init" pkg.init_stage: 2 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/net/oic/pkg.yml ---------------------------------------------------------------------- diff --git a/net/oic/pkg.yml b/net/oic/pkg.yml index 9ffaffc..a5c9bae 100644 --- a/net/oic/pkg.yml +++ b/net/oic/pkg.yml @@ -28,7 +28,8 @@ pkg.deps: - "encoding/tinycbor" - "kernel/os" - "sys/log" - - "sys/stats" +pkg.req_apis: + - stats pkg.deps.OC_TRANSPORT_GATT: - "net/nimble/host" http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/net/oic/test/pkg.yml ---------------------------------------------------------------------- diff --git a/net/oic/test/pkg.yml b/net/oic/test/pkg.yml index ce98a54..9790b31 100644 --- a/net/oic/test/pkg.yml +++ b/net/oic/test/pkg.yml @@ -27,7 +27,7 @@ pkg.deps: - encoding/tinycbor - encoding/cborattr - test/testutil - - sys/stats + - sys/stats/full pkg.deps.SELFTEST: - sys/console/stub http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/include/stats/stats.h ---------------------------------------------------------------------- diff --git a/sys/stats/full/include/stats/stats.h b/sys/stats/full/include/stats/stats.h new file mode 100644 index 0000000..f7bb5f8 --- /dev/null +++ b/sys/stats/full/include/stats/stats.h @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 <stdint.h> +#include "syscfg/syscfg.h" +#include "os/queue.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct stats_name_map { + uint16_t snm_off; + char *snm_name; +} __attribute__((packed)); + +struct stats_hdr { + char *s_name; + uint8_t s_size; + uint8_t s_cnt; + uint16_t s_pad1; +#if MYNEWT_VAL(STATS_NAMES) + const struct stats_name_map *s_map; + int s_map_cnt; +#endif + STAILQ_ENTRY(stats_hdr) s_next; +}; + +#define STATS_SECT_DECL(__name) \ + struct stats_ ## __name + +#define STATS_SECT_START(__name) \ +STATS_SECT_DECL(__name) { \ + struct stats_hdr s_hdr; + +#define STATS_SECT_END }; + +#define STATS_SECT_VAR(__var) \ + s##__var + +#define STATS_HDR(__sectname) &(__sectname).s_hdr + +#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(__sectvarname, __size) \ + (__size), \ + ((sizeof (__sectvarname)) - sizeof (struct stats_hdr)) / (__size) + +#define STATS_INC(__sectvarname, __var) \ + ((__sectvarname).STATS_SECT_VAR(__var)++) + +#define STATS_INCN(__sectvarname, __var, __n) \ + ((__sectvarname).STATS_SECT_VAR(__var) += (__n)) + +#define STATS_CLEAR(__sectvarname, __var) \ + ((__sectvarname).STATS_SECT_VAR(__var) = 0) + +#if MYNEWT_VAL(STATS_NAMES) + +#define STATS_NAME_MAP_NAME(__sectname) g_stats_map_ ## __sectname + +#define STATS_NAME_START(__sectname) \ +const struct stats_name_map STATS_NAME_MAP_NAME(__sectname)[] = { + +#define STATS_NAME(__sectname, __entry) \ + { offsetof(STATS_SECT_DECL(__sectname), STATS_SECT_VAR(__entry)), \ + #__entry }, + +#define STATS_NAME_END(__sectname) \ +}; + +#define STATS_NAME_INIT_PARMS(__name) \ + &(STATS_NAME_MAP_NAME(__name)[0]), \ + (sizeof(STATS_NAME_MAP_NAME(__name)) / sizeof(struct stats_name_map)) + +#else /* MYNEWT_VAL(STATS_NAME) */ + +#define STATS_NAME_START(__name) +#define STATS_NAME(__name, __entry) +#define STATS_NAME_END(__name) +#define STATS_NAME_INIT_PARMS(__name) NULL, 0 + +#endif /* MYNEWT_VAL(STATS_NAME) */ + +void stats_module_init(void); +int stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, + const struct stats_name_map *map, uint8_t map_cnt); +int stats_register(char *name, struct stats_hdr *shdr); +int stats_init_and_reg(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, + const struct stats_name_map *map, uint8_t map_cnt, + char *name); +void stats_reset(struct stats_hdr *shdr); + +typedef int (*stats_walk_func_t)(struct stats_hdr *, void *, char *, + uint16_t); +int stats_walk(struct stats_hdr *, stats_walk_func_t, void *); + +typedef int (*stats_group_walk_func_t)(struct stats_hdr *, void *); +int stats_group_walk(stats_group_walk_func_t, void *); + +struct stats_hdr *stats_group_find(char *name); + +/* Private */ +#if MYNEWT_VAL(STATS_NEWTMGR) +int stats_nmgr_register_group(void); +#endif +#if MYNEWT_VAL(STATS_CLI) +int stats_shell_register(void); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __UTIL_STATS_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/pkg.yml ---------------------------------------------------------------------- diff --git a/sys/stats/full/pkg.yml b/sys/stats/full/pkg.yml new file mode 100644 index 0000000..ee80238 --- /dev/null +++ b/sys/stats/full/pkg.yml @@ -0,0 +1,37 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +pkg.name: sys/stats/full +pkg.description: Statistic tracking utilities for embedded systems. +pkg.author: "Apache Mynewt <[email protected]>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - statistics + +pkg.deps: + - kernel/os +pkg.apis: + - stats +pkg.deps.STATS_CLI: + - sys/shell +pkg.deps.STATS_NEWTMGR: + - mgmt/mgmt + +pkg.init_function: stats_module_init +pkg.init_stage: 0 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/src/stats.c ---------------------------------------------------------------------- diff --git a/sys/stats/full/src/stats.c b/sys/stats/full/src/stats.c new file mode 100644 index 0000000..b2009f6 --- /dev/null +++ b/sys/stats/full/src/stats.c @@ -0,0 +1,390 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 <assert.h> +#include <string.h> +#include <stdio.h> + +#include "sysinit/sysinit.h" +#include "syscfg/syscfg.h" +#include "os/os.h" +#include "stats/stats.h" + +/** + * Declare an example statistics section, which is fittingly, the number + * statistics registered in the system. There are two, largely duplicated, + * statistics sections here, in order to provide the optional ability to + * name statistics. + * + * STATS_SECT_START/END actually declare the statistics structure definition, + * STATS_SECT_DECL() creates the structure declaration so you can declare + * these statistics as a global structure, and STATS_NAME_START/END are how + * you name the statistics themselves. + * + * Statistics entries can be declared as any of the following values, however, + * all statistics in a given structure must be of the same size, and they are + * all unsigned. + * + * - STATS_SECT_ENTRY(): default statistic entry, 32-bits. This + * is the good stuff. Two factors to consider: + * - With 16-bit numbers, rollovers happen, frequently. Whether its + * testing a pathological condition, or just a long time since you've + * collected a statistic: it really sucks to not have a crucial piece + * of information. + * - 64-bit numbers are wonderful things. However, the gods did not see + * fit to bless us with unlimited memory. 64-bit statistics are useful + * when you want to store non-statistics in a statistics entry (i.e. time), + * because its convenient... + * + * - STATS_SECT_ENTRY16(): 16-bits. Smaller statistics if you need to fit into + * specific RAM or code size numbers. + * + * - STATS_SECT_ENTRY32(): 32-bits, if you want to force it. + * + * - STATS_SECT_ENTRY64(): 64-bits. Useful for storing chunks of data. + * + * Following the statics entry declaration is the statistic names declaration. + * This is compiled out when STATS_NAME_ENABLE is set to 0. This declaration + * is const, and therefore can be located in .text, not .data. + * + * In cases where the system configuration variable STATS_NAME_ENABLE is set + * to 1, the statistics names are stored and returned to both the console + * and management APIs. Whereas, when STATS_NAME_ENABLE = 0, these statistics + * are numbered, s0, s1, etc. + */ +STATS_SECT_START(stats) + STATS_SECT_ENTRY(num_registered) +STATS_SECT_END + +STATS_SECT_DECL(stats) g_stats_stats; + +STATS_NAME_START(stats) + STATS_NAME(stats, num_registered) +STATS_NAME_END(stats) + +STAILQ_HEAD(, stats_hdr) g_stats_registry = + STAILQ_HEAD_INITIALIZER(g_stats_registry); + + +/** + * Walk a specific statistic entry, and call walk_func with arg for + * each field within that entry. + * + * Walk func takes the following parameters: + * + * - The header of the statistics section (stats_hdr) + * - The user supplied argument + * - The name of the statistic (if STATS_NAME_ENABLE = 0, this is + * ("s%d", n), where n is the number of the statistic in the structure. + * - A pointer to the current entry. + * + * @return 0 on success, the return code of the walk_func on abort. + * + */ +int +stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg) +{ + char *name; + char name_buf[12]; + uint16_t cur; + uint16_t end; + int ent_n; + int len; + int rc; +#if MYNEWT_VAL(STATS_NAMES) + int i; +#endif + + cur = sizeof(*hdr); + end = sizeof(*hdr) + (hdr->s_size * hdr->s_cnt); + + while (cur < end) { + /* + * Access and display the statistic name. Pass that to the + * walk function + */ + name = NULL; +#if MYNEWT_VAL(STATS_NAMES) + /* The stats name map contains two elements, an offset into the + * statistics entry structure, and the name corresponding with that + * offset. This annotation allows for naming only certain statistics, + * and doesn't enforce ordering restrictions on the stats name map. + */ + for (i = 0; i < hdr->s_map_cnt; ++i) { + if (hdr->s_map[i].snm_off == cur) { + name = hdr->s_map[i].snm_name; + break; + } + } +#endif + /* Do this check irrespective of whether MYNEWT_VALUE(STATS_NAMES) + * is set. Users may only partially name elements in the statistics + * structure. + */ + if (name == NULL) { + ent_n = (cur - sizeof(*hdr)) / hdr->s_size; + len = snprintf(name_buf, sizeof(name_buf), "s%d", ent_n); + name_buf[len] = '\0'; + name = name_buf; + } + + rc = walk_func(hdr, arg, name, cur); + if (rc != 0) { + goto err; + } + + /* Statistics are variable sized, move forward either 16, 32 or 64 + * bits in the structure. + */ + cur += hdr->s_size; + } + + return (0); +err: + return (rc); +} + +/** + * Initialize the stastics module. Called before any of the statistics get + * registered to initialize global structures, and register the default + * statistics "stat." + * + * ASSERT's if it fails, as something is likely h0rked system wide. + */ +void +stats_module_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + STAILQ_INIT(&g_stats_registry); + +#if MYNEWT_VAL(STATS_CLI) + rc = stats_shell_register(); + SYSINIT_PANIC_ASSERT(rc == 0); +#endif + +#if MYNEWT_VAL(STATS_NEWTMGR) + rc = stats_nmgr_register_group(); + SYSINIT_PANIC_ASSERT(rc == 0); +#endif + + rc = stats_init(STATS_HDR(g_stats_stats), + STATS_SIZE_INIT_PARMS(g_stats_stats, STATS_SIZE_32), + STATS_NAME_INIT_PARMS(stats)); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = stats_register("stat", STATS_HDR(g_stats_stats)); + SYSINIT_PANIC_ASSERT(rc == 0); +} + + +/** + * Initialize a statistics structure, pointed to by hdr. + * + * @param hdr The header of the statistics structure, contains things + * like statistic section name, size of statistics entries, + * number of statistics, etc. + * @param size The size of the individual statistics elements, either + * 2 (16-bits), 4 (32-bits) or 8 (64-bits). + * @param cnt The number of elements in the statistics structure + * @param map The mapping of statistics name to statistic entry + * @param map_cnt The number of items in the statistics map + * + * @return 0 on success, non-zero error code on failure. + */ +int +stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, + const struct stats_name_map *map, uint8_t map_cnt) +{ + memset((uint8_t *) shdr+sizeof(*shdr), 0, size * cnt); + + shdr->s_size = size; + shdr->s_cnt = cnt; +#if MYNEWT_VAL(STATS_NAMES) + shdr->s_map = map; + shdr->s_map_cnt = map_cnt; +#endif + + return (0); +} + +/** + * Walk the group of registered statistics and call walk_func() for + * each element in the list. This function _DOES NOT_ lock the statistics + * list, and assumes that the list is not being changed by another task. + * (assumption: all statistics are registered prior to OS start.) + * + * @param walk_func The walk function to call, with a statistics header + * and arg. + * @param arg The argument to call the walk function with. + * + * @return 0 on success, non-zero error code on failure + */ +int +stats_group_walk(stats_group_walk_func_t walk_func, void *arg) +{ + struct stats_hdr *hdr; + int rc; + + STAILQ_FOREACH(hdr, &g_stats_registry, s_next) { + rc = walk_func(hdr, arg); + if (rc != 0) { + goto err; + } + } + return (0); +err: + return (rc); +} + +/** + * Find a statistics structure by name, this is not thread-safe. + * (assumption: all statistics are registered prior ot OS start.) + * + * @param name The statistic structure name to find + * + * @return statistic structure if found, NULL if not found. + */ +struct stats_hdr * +stats_group_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); +} + +/** + * Register the statistics pointed to by shdr, with the name of "name." + * + * @param name The name of the statistic to register. This name is guaranteed + * unique in the statistics map. If already exists, this function + * will return an error. + * @param shdr The statistics header to register into the statistic map under + * name. + * + * @return 0 on success, non-zero error code on failure. + */ +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(g_stats_stats, num_registered); + + return (0); +err: + return (rc); +} + +/** + * Initializes and registers the specified statistics section. + * + * @param shdr The statistics header to register + * @param size The entry size of the statistics to register either 2 (16-bit), + * 4 (32-bit) or 8 (64-bit). + * @param cnt The number of statistics entries in the statistics structure. + * @param map The map of statistics entry to statistics name, only used when + * MYNEWT_VAL(STATS_NAME) = 1. + * @param map_cnt The number of elements in the statistics name map. + * @param name The name of the statistics element to register with the system. + * + * @return 0 on success, non-zero error code on failure. + */ +int +stats_init_and_reg(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, + const struct stats_name_map *map, uint8_t map_cnt, + char *name) +{ + int rc; + + rc = stats_init(shdr, size, cnt, map, map_cnt); + if (rc != 0) { + return rc; + } + + rc = stats_register(name, shdr); + if (rc != 0) { + return rc; + } + + return rc; +} + +/** + * Resets and zeroes the specified statistics section. + * + * @param shdr The statistics header to zero + */ +void +stats_reset(struct stats_hdr *hdr) +{ + uint16_t cur; + uint16_t end; + void *stat_val; + + cur = sizeof(*hdr); + end = sizeof(*hdr) + (hdr->s_size * hdr->s_cnt); + + while (cur < end) { + stat_val = (uint8_t*)hdr + cur; + switch (hdr->s_size) { + case sizeof(uint16_t): + *(uint16_t *)stat_val = 0; + break; + case sizeof(uint32_t): + *(unsigned long *)stat_val = 0; + break; + case sizeof(uint64_t): + *(uint64_t *)stat_val = 0; + break; + } + + /* + * Statistics are variable sized, move forward either 16, 32 or 64 + * bits in the structure. + */ + cur += hdr->s_size; + } + return; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/src/stats_nmgr.c ---------------------------------------------------------------------- diff --git a/sys/stats/full/src/stats_nmgr.c b/sys/stats/full/src/stats_nmgr.c new file mode 100644 index 0000000..5df6e7c --- /dev/null +++ b/sys/stats/full/src/stats_nmgr.c @@ -0,0 +1,185 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 <string.h> +#include <stdio.h> + +#include "syscfg/syscfg.h" + +#if MYNEWT_VAL(STATS_NEWTMGR) + +#include "os/os.h" +#include "mgmt/mgmt.h" +#include "cborattr/cborattr.h" +#include "stats/stats.h" + +/* Source code is only included if the newtmgr library is enabled. Otherwise + * this file is compiled out for code size. + */ +static int stats_nmgr_read(struct mgmt_cbuf *cb); +static int stats_nmgr_list(struct mgmt_cbuf *cb); + +static struct mgmt_group shell_nmgr_group; + +#define STATS_NMGR_ID_READ (0) +#define STATS_NMGR_ID_LIST (1) + +/* ORDER MATTERS HERE. + * Each element represents the command ID, referenced from newtmgr. + */ +static struct mgmt_handler shell_nmgr_group_handlers[] = { + [STATS_NMGR_ID_READ] = {stats_nmgr_read, stats_nmgr_read}, + [STATS_NMGR_ID_LIST] = {stats_nmgr_list, stats_nmgr_list} +}; + +static int +stats_nmgr_walk_func(struct stats_hdr *hdr, void *arg, char *sname, + uint16_t stat_off) +{ + void *stat_val; + CborEncoder *penc = (CborEncoder *) arg; + CborError g_err = CborNoError; + + stat_val = (uint8_t *)hdr + stat_off; + + g_err |= cbor_encode_text_stringz(penc, sname); + + switch (hdr->s_size) { + case sizeof(uint16_t): + g_err |= cbor_encode_uint(penc, *(uint16_t *) stat_val); + break; + case sizeof(uint32_t): + g_err |= cbor_encode_uint(penc, *(uint32_t *) stat_val); + break; + case sizeof(uint64_t): + g_err |= cbor_encode_uint(penc, *(uint64_t *) stat_val); + break; + } + + return (g_err); +} + +static int +stats_nmgr_encode_name(struct stats_hdr *hdr, void *arg) +{ + CborEncoder *penc = (CborEncoder *) arg; + + return cbor_encode_text_stringz(penc, hdr->s_name); +} + +static int +stats_nmgr_read(struct mgmt_cbuf *cb) +{ + struct stats_hdr *hdr; +#define STATS_NMGR_NAME_LEN (32) + char stats_name[STATS_NMGR_NAME_LEN]; + struct cbor_attr_t attrs[] = { + { "name", CborAttrTextStringType, .addr.string = &stats_name[0], + .len = sizeof(stats_name) }, + { NULL }, + }; + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp, stats; + + g_err = cbor_read_object(&cb->it, attrs); + if (g_err != 0) { + g_err = MGMT_ERR_EINVAL; + goto err; + } + + hdr = stats_group_find(stats_name); + if (!hdr) { + g_err = MGMT_ERR_EINVAL; + goto err; + } + + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + + g_err |= cbor_encode_text_stringz(&rsp, "name"); + g_err |= cbor_encode_text_stringz(&rsp, stats_name); + + g_err |= cbor_encode_text_stringz(&rsp, "group"); + g_err |= cbor_encode_text_string(&rsp, "sys", sizeof("sys")-1); + + g_err |= cbor_encode_text_stringz(&rsp, "fields"); + + g_err |= cbor_encoder_create_map(&rsp, &stats, CborIndefiniteLength); + + stats_walk(hdr, stats_nmgr_walk_func, &stats); + + g_err |= cbor_encoder_close_container(&rsp, &stats); + g_err |= cbor_encoder_close_container(penc, &rsp); + + if (g_err) { + return MGMT_ERR_ENOMEM; + } + return (0); +err: + mgmt_cbuf_setoerr(cb, g_err); + + return (0); +} + +static int +stats_nmgr_list(struct mgmt_cbuf *cb) +{ + CborError g_err = CborNoError; + CborEncoder *penc = &cb->encoder; + CborEncoder rsp, stats; + + g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); + g_err |= cbor_encode_text_stringz(&rsp, "rc"); + g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); + g_err |= cbor_encode_text_stringz(&rsp, "stat_list"); + g_err |= cbor_encoder_create_array(&rsp, &stats, CborIndefiniteLength); + stats_group_walk(stats_nmgr_encode_name, &stats); + g_err |= cbor_encoder_close_container(&rsp, &stats); + g_err |= cbor_encoder_close_container(penc, &rsp); + + if (g_err) { + return MGMT_ERR_ENOMEM; + } + return (0); +} + +/** + * Register nmgr group handlers + */ +int +stats_nmgr_register_group(void) +{ + int rc; + + MGMT_GROUP_SET_HANDLERS(&shell_nmgr_group, shell_nmgr_group_handlers); + shell_nmgr_group.mg_group_id = MGMT_GROUP_ID_STATS; + + rc = mgmt_group_register(&shell_nmgr_group); + if (rc != 0) { + goto err; + } + + return (0); +err: + return (rc); +} + +#endif /* MYNEWT_VAL(STATS_NEWTMGR) */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/src/stats_shell.c ---------------------------------------------------------------------- diff --git a/sys/stats/full/src/stats_shell.c b/sys/stats/full/src/stats_shell.c new file mode 100644 index 0000000..84b80ac --- /dev/null +++ b/sys/stats/full/src/stats_shell.c @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 "syscfg/syscfg.h" + +/* Source code is only included if the shell library is enabled. Otherwise + * this file is compiled out for code size. + */ +#if MYNEWT_VAL(STATS_CLI) + +#include <string.h> +#include "shell/shell.h" +#include "console/console.h" +#include "os/os.h" +#include "stats/stats.h" + +static int shell_stats_display(int argc, char **argv); +static struct shell_cmd shell_stats_cmd = { + .sc_cmd = "stat", + .sc_cmd_func = shell_stats_display +}; +uint8_t stats_shell_registered; + +static int +stats_shell_display_entry(struct stats_hdr *hdr, void *arg, char *name, + uint16_t stat_off) +{ + void *stat_val; + + stat_val = (uint8_t *)hdr + stat_off; + switch (hdr->s_size) { + case sizeof(uint16_t): + console_printf("%s: %u\n", name, *(uint16_t *) stat_val); + break; + case sizeof(uint32_t): + console_printf("%s: %lu\n", name, *(unsigned long *) stat_val); + break; + case sizeof(uint64_t): + console_printf("%s: %llu\n", name, *(uint64_t *) stat_val); + break; + default: + console_printf("Unknown stat size for %s %u\n", name, + hdr->s_size); + break; + } + + return (0); +} + +static int +stats_shell_display_group(struct stats_hdr *hdr, void *arg) +{ + console_printf("\t%s\n", hdr->s_name); + return (0); +} + +static int +shell_stats_display(int argc, char **argv) +{ + struct stats_hdr *hdr; + char *name; + int rc; + + name = argv[1]; + if (name == NULL || !strcmp(name, "")) { + console_printf("Must specify a statistic name to dump, " + "possible names are:\n"); + stats_group_walk(stats_shell_display_group, NULL); + rc = OS_EINVAL; + goto err; + } + + hdr = stats_group_find(name); + if (!hdr) { + console_printf("Could not find statistic group %s\n", name); + rc = OS_EINVAL; + goto err; + } + + rc = stats_walk(hdr, stats_shell_display_entry, NULL); + if (rc != 0) { + goto err; + } + + return (0); +err: + return (rc); +} + + +int +stats_shell_register(void) +{ + if (!stats_shell_registered) { + stats_shell_registered = 1; + shell_cmd_register(&shell_stats_cmd); + } + + return (0); +} + + +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/full/syscfg.yml ---------------------------------------------------------------------- diff --git a/sys/stats/full/syscfg.yml b/sys/stats/full/syscfg.yml new file mode 100644 index 0000000..419cfad --- /dev/null +++ b/sys/stats/full/syscfg.yml @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +# Package: sys/stats/full + +syscfg.defs: + STATS_NAMES: + description: 'Include and report the textual name of each statistic.' + value: 0 + STATS_CLI: + description: 'Expose the "stat" shell command.' + value: 0 + restrictions: + - SHELL_TASK + STATS_NEWTMGR: + description: 'Expose the "stat" newtmgr command.' + value: 0 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/include/stats/stats.h ---------------------------------------------------------------------- diff --git a/sys/stats/include/stats/stats.h b/sys/stats/include/stats/stats.h deleted file mode 100644 index f7bb5f8..0000000 --- a/sys/stats/include/stats/stats.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 <stdint.h> -#include "syscfg/syscfg.h" -#include "os/queue.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct stats_name_map { - uint16_t snm_off; - char *snm_name; -} __attribute__((packed)); - -struct stats_hdr { - char *s_name; - uint8_t s_size; - uint8_t s_cnt; - uint16_t s_pad1; -#if MYNEWT_VAL(STATS_NAMES) - const struct stats_name_map *s_map; - int s_map_cnt; -#endif - STAILQ_ENTRY(stats_hdr) s_next; -}; - -#define STATS_SECT_DECL(__name) \ - struct stats_ ## __name - -#define STATS_SECT_START(__name) \ -STATS_SECT_DECL(__name) { \ - struct stats_hdr s_hdr; - -#define STATS_SECT_END }; - -#define STATS_SECT_VAR(__var) \ - s##__var - -#define STATS_HDR(__sectname) &(__sectname).s_hdr - -#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(__sectvarname, __size) \ - (__size), \ - ((sizeof (__sectvarname)) - sizeof (struct stats_hdr)) / (__size) - -#define STATS_INC(__sectvarname, __var) \ - ((__sectvarname).STATS_SECT_VAR(__var)++) - -#define STATS_INCN(__sectvarname, __var, __n) \ - ((__sectvarname).STATS_SECT_VAR(__var) += (__n)) - -#define STATS_CLEAR(__sectvarname, __var) \ - ((__sectvarname).STATS_SECT_VAR(__var) = 0) - -#if MYNEWT_VAL(STATS_NAMES) - -#define STATS_NAME_MAP_NAME(__sectname) g_stats_map_ ## __sectname - -#define STATS_NAME_START(__sectname) \ -const struct stats_name_map STATS_NAME_MAP_NAME(__sectname)[] = { - -#define STATS_NAME(__sectname, __entry) \ - { offsetof(STATS_SECT_DECL(__sectname), STATS_SECT_VAR(__entry)), \ - #__entry }, - -#define STATS_NAME_END(__sectname) \ -}; - -#define STATS_NAME_INIT_PARMS(__name) \ - &(STATS_NAME_MAP_NAME(__name)[0]), \ - (sizeof(STATS_NAME_MAP_NAME(__name)) / sizeof(struct stats_name_map)) - -#else /* MYNEWT_VAL(STATS_NAME) */ - -#define STATS_NAME_START(__name) -#define STATS_NAME(__name, __entry) -#define STATS_NAME_END(__name) -#define STATS_NAME_INIT_PARMS(__name) NULL, 0 - -#endif /* MYNEWT_VAL(STATS_NAME) */ - -void stats_module_init(void); -int stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, - const struct stats_name_map *map, uint8_t map_cnt); -int stats_register(char *name, struct stats_hdr *shdr); -int stats_init_and_reg(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, - const struct stats_name_map *map, uint8_t map_cnt, - char *name); -void stats_reset(struct stats_hdr *shdr); - -typedef int (*stats_walk_func_t)(struct stats_hdr *, void *, char *, - uint16_t); -int stats_walk(struct stats_hdr *, stats_walk_func_t, void *); - -typedef int (*stats_group_walk_func_t)(struct stats_hdr *, void *); -int stats_group_walk(stats_group_walk_func_t, void *); - -struct stats_hdr *stats_group_find(char *name); - -/* Private */ -#if MYNEWT_VAL(STATS_NEWTMGR) -int stats_nmgr_register_group(void); -#endif -#if MYNEWT_VAL(STATS_CLI) -int stats_shell_register(void); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* __UTIL_STATS_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/pkg.yml ---------------------------------------------------------------------- diff --git a/sys/stats/pkg.yml b/sys/stats/pkg.yml deleted file mode 100644 index 5d122dd..0000000 --- a/sys/stats/pkg.yml +++ /dev/null @@ -1,35 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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. -# - -pkg.name: sys/stats -pkg.description: Statistic tracking utilities for embedded systems. -pkg.author: "Apache Mynewt <[email protected]>" -pkg.homepage: "http://mynewt.apache.org/" -pkg.keywords: - - statistics - -pkg.deps: - - kernel/os -pkg.deps.STATS_CLI: - - sys/shell -pkg.deps.STATS_NEWTMGR: - - mgmt/mgmt - -pkg.init_function: stats_module_init -pkg.init_stage: 0 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/src/stats.c ---------------------------------------------------------------------- diff --git a/sys/stats/src/stats.c b/sys/stats/src/stats.c deleted file mode 100644 index b2009f6..0000000 --- a/sys/stats/src/stats.c +++ /dev/null @@ -1,390 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 <assert.h> -#include <string.h> -#include <stdio.h> - -#include "sysinit/sysinit.h" -#include "syscfg/syscfg.h" -#include "os/os.h" -#include "stats/stats.h" - -/** - * Declare an example statistics section, which is fittingly, the number - * statistics registered in the system. There are two, largely duplicated, - * statistics sections here, in order to provide the optional ability to - * name statistics. - * - * STATS_SECT_START/END actually declare the statistics structure definition, - * STATS_SECT_DECL() creates the structure declaration so you can declare - * these statistics as a global structure, and STATS_NAME_START/END are how - * you name the statistics themselves. - * - * Statistics entries can be declared as any of the following values, however, - * all statistics in a given structure must be of the same size, and they are - * all unsigned. - * - * - STATS_SECT_ENTRY(): default statistic entry, 32-bits. This - * is the good stuff. Two factors to consider: - * - With 16-bit numbers, rollovers happen, frequently. Whether its - * testing a pathological condition, or just a long time since you've - * collected a statistic: it really sucks to not have a crucial piece - * of information. - * - 64-bit numbers are wonderful things. However, the gods did not see - * fit to bless us with unlimited memory. 64-bit statistics are useful - * when you want to store non-statistics in a statistics entry (i.e. time), - * because its convenient... - * - * - STATS_SECT_ENTRY16(): 16-bits. Smaller statistics if you need to fit into - * specific RAM or code size numbers. - * - * - STATS_SECT_ENTRY32(): 32-bits, if you want to force it. - * - * - STATS_SECT_ENTRY64(): 64-bits. Useful for storing chunks of data. - * - * Following the statics entry declaration is the statistic names declaration. - * This is compiled out when STATS_NAME_ENABLE is set to 0. This declaration - * is const, and therefore can be located in .text, not .data. - * - * In cases where the system configuration variable STATS_NAME_ENABLE is set - * to 1, the statistics names are stored and returned to both the console - * and management APIs. Whereas, when STATS_NAME_ENABLE = 0, these statistics - * are numbered, s0, s1, etc. - */ -STATS_SECT_START(stats) - STATS_SECT_ENTRY(num_registered) -STATS_SECT_END - -STATS_SECT_DECL(stats) g_stats_stats; - -STATS_NAME_START(stats) - STATS_NAME(stats, num_registered) -STATS_NAME_END(stats) - -STAILQ_HEAD(, stats_hdr) g_stats_registry = - STAILQ_HEAD_INITIALIZER(g_stats_registry); - - -/** - * Walk a specific statistic entry, and call walk_func with arg for - * each field within that entry. - * - * Walk func takes the following parameters: - * - * - The header of the statistics section (stats_hdr) - * - The user supplied argument - * - The name of the statistic (if STATS_NAME_ENABLE = 0, this is - * ("s%d", n), where n is the number of the statistic in the structure. - * - A pointer to the current entry. - * - * @return 0 on success, the return code of the walk_func on abort. - * - */ -int -stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg) -{ - char *name; - char name_buf[12]; - uint16_t cur; - uint16_t end; - int ent_n; - int len; - int rc; -#if MYNEWT_VAL(STATS_NAMES) - int i; -#endif - - cur = sizeof(*hdr); - end = sizeof(*hdr) + (hdr->s_size * hdr->s_cnt); - - while (cur < end) { - /* - * Access and display the statistic name. Pass that to the - * walk function - */ - name = NULL; -#if MYNEWT_VAL(STATS_NAMES) - /* The stats name map contains two elements, an offset into the - * statistics entry structure, and the name corresponding with that - * offset. This annotation allows for naming only certain statistics, - * and doesn't enforce ordering restrictions on the stats name map. - */ - for (i = 0; i < hdr->s_map_cnt; ++i) { - if (hdr->s_map[i].snm_off == cur) { - name = hdr->s_map[i].snm_name; - break; - } - } -#endif - /* Do this check irrespective of whether MYNEWT_VALUE(STATS_NAMES) - * is set. Users may only partially name elements in the statistics - * structure. - */ - if (name == NULL) { - ent_n = (cur - sizeof(*hdr)) / hdr->s_size; - len = snprintf(name_buf, sizeof(name_buf), "s%d", ent_n); - name_buf[len] = '\0'; - name = name_buf; - } - - rc = walk_func(hdr, arg, name, cur); - if (rc != 0) { - goto err; - } - - /* Statistics are variable sized, move forward either 16, 32 or 64 - * bits in the structure. - */ - cur += hdr->s_size; - } - - return (0); -err: - return (rc); -} - -/** - * Initialize the stastics module. Called before any of the statistics get - * registered to initialize global structures, and register the default - * statistics "stat." - * - * ASSERT's if it fails, as something is likely h0rked system wide. - */ -void -stats_module_init(void) -{ - int rc; - - /* Ensure this function only gets called by sysinit. */ - SYSINIT_ASSERT_ACTIVE(); - - STAILQ_INIT(&g_stats_registry); - -#if MYNEWT_VAL(STATS_CLI) - rc = stats_shell_register(); - SYSINIT_PANIC_ASSERT(rc == 0); -#endif - -#if MYNEWT_VAL(STATS_NEWTMGR) - rc = stats_nmgr_register_group(); - SYSINIT_PANIC_ASSERT(rc == 0); -#endif - - rc = stats_init(STATS_HDR(g_stats_stats), - STATS_SIZE_INIT_PARMS(g_stats_stats, STATS_SIZE_32), - STATS_NAME_INIT_PARMS(stats)); - SYSINIT_PANIC_ASSERT(rc == 0); - - rc = stats_register("stat", STATS_HDR(g_stats_stats)); - SYSINIT_PANIC_ASSERT(rc == 0); -} - - -/** - * Initialize a statistics structure, pointed to by hdr. - * - * @param hdr The header of the statistics structure, contains things - * like statistic section name, size of statistics entries, - * number of statistics, etc. - * @param size The size of the individual statistics elements, either - * 2 (16-bits), 4 (32-bits) or 8 (64-bits). - * @param cnt The number of elements in the statistics structure - * @param map The mapping of statistics name to statistic entry - * @param map_cnt The number of items in the statistics map - * - * @return 0 on success, non-zero error code on failure. - */ -int -stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, - const struct stats_name_map *map, uint8_t map_cnt) -{ - memset((uint8_t *) shdr+sizeof(*shdr), 0, size * cnt); - - shdr->s_size = size; - shdr->s_cnt = cnt; -#if MYNEWT_VAL(STATS_NAMES) - shdr->s_map = map; - shdr->s_map_cnt = map_cnt; -#endif - - return (0); -} - -/** - * Walk the group of registered statistics and call walk_func() for - * each element in the list. This function _DOES NOT_ lock the statistics - * list, and assumes that the list is not being changed by another task. - * (assumption: all statistics are registered prior to OS start.) - * - * @param walk_func The walk function to call, with a statistics header - * and arg. - * @param arg The argument to call the walk function with. - * - * @return 0 on success, non-zero error code on failure - */ -int -stats_group_walk(stats_group_walk_func_t walk_func, void *arg) -{ - struct stats_hdr *hdr; - int rc; - - STAILQ_FOREACH(hdr, &g_stats_registry, s_next) { - rc = walk_func(hdr, arg); - if (rc != 0) { - goto err; - } - } - return (0); -err: - return (rc); -} - -/** - * Find a statistics structure by name, this is not thread-safe. - * (assumption: all statistics are registered prior ot OS start.) - * - * @param name The statistic structure name to find - * - * @return statistic structure if found, NULL if not found. - */ -struct stats_hdr * -stats_group_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); -} - -/** - * Register the statistics pointed to by shdr, with the name of "name." - * - * @param name The name of the statistic to register. This name is guaranteed - * unique in the statistics map. If already exists, this function - * will return an error. - * @param shdr The statistics header to register into the statistic map under - * name. - * - * @return 0 on success, non-zero error code on failure. - */ -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(g_stats_stats, num_registered); - - return (0); -err: - return (rc); -} - -/** - * Initializes and registers the specified statistics section. - * - * @param shdr The statistics header to register - * @param size The entry size of the statistics to register either 2 (16-bit), - * 4 (32-bit) or 8 (64-bit). - * @param cnt The number of statistics entries in the statistics structure. - * @param map The map of statistics entry to statistics name, only used when - * MYNEWT_VAL(STATS_NAME) = 1. - * @param map_cnt The number of elements in the statistics name map. - * @param name The name of the statistics element to register with the system. - * - * @return 0 on success, non-zero error code on failure. - */ -int -stats_init_and_reg(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, - const struct stats_name_map *map, uint8_t map_cnt, - char *name) -{ - int rc; - - rc = stats_init(shdr, size, cnt, map, map_cnt); - if (rc != 0) { - return rc; - } - - rc = stats_register(name, shdr); - if (rc != 0) { - return rc; - } - - return rc; -} - -/** - * Resets and zeroes the specified statistics section. - * - * @param shdr The statistics header to zero - */ -void -stats_reset(struct stats_hdr *hdr) -{ - uint16_t cur; - uint16_t end; - void *stat_val; - - cur = sizeof(*hdr); - end = sizeof(*hdr) + (hdr->s_size * hdr->s_cnt); - - while (cur < end) { - stat_val = (uint8_t*)hdr + cur; - switch (hdr->s_size) { - case sizeof(uint16_t): - *(uint16_t *)stat_val = 0; - break; - case sizeof(uint32_t): - *(unsigned long *)stat_val = 0; - break; - case sizeof(uint64_t): - *(uint64_t *)stat_val = 0; - break; - } - - /* - * Statistics are variable sized, move forward either 16, 32 or 64 - * bits in the structure. - */ - cur += hdr->s_size; - } - return; -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/src/stats_nmgr.c ---------------------------------------------------------------------- diff --git a/sys/stats/src/stats_nmgr.c b/sys/stats/src/stats_nmgr.c deleted file mode 100644 index 5df6e7c..0000000 --- a/sys/stats/src/stats_nmgr.c +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 <string.h> -#include <stdio.h> - -#include "syscfg/syscfg.h" - -#if MYNEWT_VAL(STATS_NEWTMGR) - -#include "os/os.h" -#include "mgmt/mgmt.h" -#include "cborattr/cborattr.h" -#include "stats/stats.h" - -/* Source code is only included if the newtmgr library is enabled. Otherwise - * this file is compiled out for code size. - */ -static int stats_nmgr_read(struct mgmt_cbuf *cb); -static int stats_nmgr_list(struct mgmt_cbuf *cb); - -static struct mgmt_group shell_nmgr_group; - -#define STATS_NMGR_ID_READ (0) -#define STATS_NMGR_ID_LIST (1) - -/* ORDER MATTERS HERE. - * Each element represents the command ID, referenced from newtmgr. - */ -static struct mgmt_handler shell_nmgr_group_handlers[] = { - [STATS_NMGR_ID_READ] = {stats_nmgr_read, stats_nmgr_read}, - [STATS_NMGR_ID_LIST] = {stats_nmgr_list, stats_nmgr_list} -}; - -static int -stats_nmgr_walk_func(struct stats_hdr *hdr, void *arg, char *sname, - uint16_t stat_off) -{ - void *stat_val; - CborEncoder *penc = (CborEncoder *) arg; - CborError g_err = CborNoError; - - stat_val = (uint8_t *)hdr + stat_off; - - g_err |= cbor_encode_text_stringz(penc, sname); - - switch (hdr->s_size) { - case sizeof(uint16_t): - g_err |= cbor_encode_uint(penc, *(uint16_t *) stat_val); - break; - case sizeof(uint32_t): - g_err |= cbor_encode_uint(penc, *(uint32_t *) stat_val); - break; - case sizeof(uint64_t): - g_err |= cbor_encode_uint(penc, *(uint64_t *) stat_val); - break; - } - - return (g_err); -} - -static int -stats_nmgr_encode_name(struct stats_hdr *hdr, void *arg) -{ - CborEncoder *penc = (CborEncoder *) arg; - - return cbor_encode_text_stringz(penc, hdr->s_name); -} - -static int -stats_nmgr_read(struct mgmt_cbuf *cb) -{ - struct stats_hdr *hdr; -#define STATS_NMGR_NAME_LEN (32) - char stats_name[STATS_NMGR_NAME_LEN]; - struct cbor_attr_t attrs[] = { - { "name", CborAttrTextStringType, .addr.string = &stats_name[0], - .len = sizeof(stats_name) }, - { NULL }, - }; - CborError g_err = CborNoError; - CborEncoder *penc = &cb->encoder; - CborEncoder rsp, stats; - - g_err = cbor_read_object(&cb->it, attrs); - if (g_err != 0) { - g_err = MGMT_ERR_EINVAL; - goto err; - } - - hdr = stats_group_find(stats_name); - if (!hdr) { - g_err = MGMT_ERR_EINVAL; - goto err; - } - - g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); - g_err |= cbor_encode_text_stringz(&rsp, "rc"); - g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); - - g_err |= cbor_encode_text_stringz(&rsp, "name"); - g_err |= cbor_encode_text_stringz(&rsp, stats_name); - - g_err |= cbor_encode_text_stringz(&rsp, "group"); - g_err |= cbor_encode_text_string(&rsp, "sys", sizeof("sys")-1); - - g_err |= cbor_encode_text_stringz(&rsp, "fields"); - - g_err |= cbor_encoder_create_map(&rsp, &stats, CborIndefiniteLength); - - stats_walk(hdr, stats_nmgr_walk_func, &stats); - - g_err |= cbor_encoder_close_container(&rsp, &stats); - g_err |= cbor_encoder_close_container(penc, &rsp); - - if (g_err) { - return MGMT_ERR_ENOMEM; - } - return (0); -err: - mgmt_cbuf_setoerr(cb, g_err); - - return (0); -} - -static int -stats_nmgr_list(struct mgmt_cbuf *cb) -{ - CborError g_err = CborNoError; - CborEncoder *penc = &cb->encoder; - CborEncoder rsp, stats; - - g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength); - g_err |= cbor_encode_text_stringz(&rsp, "rc"); - g_err |= cbor_encode_int(&rsp, MGMT_ERR_EOK); - g_err |= cbor_encode_text_stringz(&rsp, "stat_list"); - g_err |= cbor_encoder_create_array(&rsp, &stats, CborIndefiniteLength); - stats_group_walk(stats_nmgr_encode_name, &stats); - g_err |= cbor_encoder_close_container(&rsp, &stats); - g_err |= cbor_encoder_close_container(penc, &rsp); - - if (g_err) { - return MGMT_ERR_ENOMEM; - } - return (0); -} - -/** - * Register nmgr group handlers - */ -int -stats_nmgr_register_group(void) -{ - int rc; - - MGMT_GROUP_SET_HANDLERS(&shell_nmgr_group, shell_nmgr_group_handlers); - shell_nmgr_group.mg_group_id = MGMT_GROUP_ID_STATS; - - rc = mgmt_group_register(&shell_nmgr_group); - if (rc != 0) { - goto err; - } - - return (0); -err: - return (rc); -} - -#endif /* MYNEWT_VAL(STATS_NEWTMGR) */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/src/stats_shell.c ---------------------------------------------------------------------- diff --git a/sys/stats/src/stats_shell.c b/sys/stats/src/stats_shell.c deleted file mode 100644 index 84b80ac..0000000 --- a/sys/stats/src/stats_shell.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 "syscfg/syscfg.h" - -/* Source code is only included if the shell library is enabled. Otherwise - * this file is compiled out for code size. - */ -#if MYNEWT_VAL(STATS_CLI) - -#include <string.h> -#include "shell/shell.h" -#include "console/console.h" -#include "os/os.h" -#include "stats/stats.h" - -static int shell_stats_display(int argc, char **argv); -static struct shell_cmd shell_stats_cmd = { - .sc_cmd = "stat", - .sc_cmd_func = shell_stats_display -}; -uint8_t stats_shell_registered; - -static int -stats_shell_display_entry(struct stats_hdr *hdr, void *arg, char *name, - uint16_t stat_off) -{ - void *stat_val; - - stat_val = (uint8_t *)hdr + stat_off; - switch (hdr->s_size) { - case sizeof(uint16_t): - console_printf("%s: %u\n", name, *(uint16_t *) stat_val); - break; - case sizeof(uint32_t): - console_printf("%s: %lu\n", name, *(unsigned long *) stat_val); - break; - case sizeof(uint64_t): - console_printf("%s: %llu\n", name, *(uint64_t *) stat_val); - break; - default: - console_printf("Unknown stat size for %s %u\n", name, - hdr->s_size); - break; - } - - return (0); -} - -static int -stats_shell_display_group(struct stats_hdr *hdr, void *arg) -{ - console_printf("\t%s\n", hdr->s_name); - return (0); -} - -static int -shell_stats_display(int argc, char **argv) -{ - struct stats_hdr *hdr; - char *name; - int rc; - - name = argv[1]; - if (name == NULL || !strcmp(name, "")) { - console_printf("Must specify a statistic name to dump, " - "possible names are:\n"); - stats_group_walk(stats_shell_display_group, NULL); - rc = OS_EINVAL; - goto err; - } - - hdr = stats_group_find(name); - if (!hdr) { - console_printf("Could not find statistic group %s\n", name); - rc = OS_EINVAL; - goto err; - } - - rc = stats_walk(hdr, stats_shell_display_entry, NULL); - if (rc != 0) { - goto err; - } - - return (0); -err: - return (rc); -} - - -int -stats_shell_register(void) -{ - if (!stats_shell_registered) { - stats_shell_registered = 1; - shell_cmd_register(&shell_stats_cmd); - } - - return (0); -} - - -#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/002424b2/sys/stats/syscfg.yml ---------------------------------------------------------------------- diff --git a/sys/stats/syscfg.yml b/sys/stats/syscfg.yml deleted file mode 100644 index 5d477f8..0000000 --- a/sys/stats/syscfg.yml +++ /dev/null @@ -1,32 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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. -# - -# Package: sys/stats - -syscfg.defs: - STATS_NAMES: - description: 'Include and report the textual name of each statistic.' - value: 0 - STATS_CLI: - description: 'Expose the "stat" shell command.' - value: 0 - restrictions: - - SHELL_TASK - STATS_NEWTMGR: - description: 'Expose the "stat" newtmgr command.' - value: 0
