You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/09/21 08:28:55 UTC
[GitHub] mkiiskila closed pull request #1410: Log statistics
mkiiskila closed pull request #1410: Log statistics
URL: https://github.com/apache/mynewt-core/pull/1410
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/fs/fcb/include/fcb/fcb.h b/fs/fcb/include/fcb/fcb.h
index d6677b23a0..e9a6e85c06 100644
--- a/fs/fcb/include/fcb/fcb.h
+++ b/fs/fcb/include/fcb/fcb.h
@@ -144,6 +144,13 @@ fcb_offset_last_n(struct fcb *fcb, uint8_t entries,
*/
int fcb_clear(struct fcb *fcb);
+/**
+ * Usage report for a given FCB area. Returns number of elements and the
+ * number of bytes stored in them.
+ */
+int fcb_area_info(struct fcb *fcb, struct flash_area *fa, int *elemsp,
+ int *bytesp);
+
#ifdef __cplusplus
}
diff --git a/fs/fcb/src/fcb_area_info.c b/fs/fcb/src/fcb_area_info.c
new file mode 100644
index 0000000000..174d3b7e23
--- /dev/null
+++ b/fs/fcb/src/fcb_area_info.c
@@ -0,0 +1,54 @@
+/*
+ * 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 "fcb/fcb.h"
+#include "fcb_priv.h"
+
+int
+fcb_area_info(struct fcb *fcb, struct flash_area *fa, int *elemsp, int *bytesp)
+{
+ struct fcb_entry loc;
+ int rc;
+ int elems = 0;
+ int bytes = 0;
+
+ loc.fe_area = fa;
+ loc.fe_elem_off = 0;
+
+ while (1) {
+ rc = fcb_getnext(fcb, &loc);
+ if (rc) {
+ break;
+ }
+ if (!fa) {
+ fa = loc.fe_area;
+ } else if (fa != loc.fe_area) {
+ break;
+ }
+ elems++;
+ bytes += loc.fe_data_len;
+ }
+ if (elemsp) {
+ *elemsp = elems;
+ }
+ if (bytesp) {
+ *bytesp = bytes;
+ }
+ return 0;
+}
diff --git a/fs/fcb/test/src/fcb_test.c b/fs/fcb/test/src/fcb_test.c
index c2bd84fb7e..d79694d5f6 100644
--- a/fs/fcb/test/src/fcb_test.c
+++ b/fs/fcb/test/src/fcb_test.c
@@ -158,6 +158,7 @@ TEST_CASE_DECL(fcb_test_reset)
TEST_CASE_DECL(fcb_test_rotate)
TEST_CASE_DECL(fcb_test_multiple_scratch)
TEST_CASE_DECL(fcb_test_last_of_n)
+TEST_CASE_DECL(fcb_test_area_info)
TEST_SUITE(fcb_test_all)
{
@@ -190,6 +191,10 @@ TEST_SUITE(fcb_test_all)
tu_case_set_pre_cb(fcb_tc_pretest, (void*)4);
fcb_test_last_of_n();
+
+ tu_case_set_pre_cb(fcb_tc_pretest, (void*)2);
+ fcb_test_area_info();
+
}
#if MYNEWT_VAL(SELFTEST)
diff --git a/fs/fcb/test/src/testcases/fcb_test_area_info.c b/fs/fcb/test/src/testcases/fcb_test_area_info.c
new file mode 100644
index 0000000000..212a88ac87
--- /dev/null
+++ b/fs/fcb/test/src/testcases/fcb_test_area_info.c
@@ -0,0 +1,99 @@
+/*
+ * 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 "fcb_test.h"
+
+TEST_CASE(fcb_test_area_info)
+{
+ struct fcb *fcb;
+ int rc;
+ int i;
+ struct fcb_entry loc;
+ uint8_t test_data[128];
+ int elem_cnts[2] = {0, 0};
+ int area_elems[2];
+ int area_bytes[2];
+
+ fcb = &test_fcb;
+
+ /*
+ * Should be empty, with elem count and byte count zero.
+ */
+ for (i = 0; i < 2; i++) {
+ rc = fcb_area_info(fcb, &fcb->f_sectors[i], &area_elems[i],
+ &area_bytes[i]);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(area_elems[i] == 0);
+ TEST_ASSERT(area_bytes[i] == 0);
+ }
+
+ /*
+ * Fill up the areas, make sure that reporting is ok.
+ */
+ while (1) {
+ rc = fcb_append(fcb, sizeof(test_data), &loc);
+ if (rc == FCB_ERR_NOSPACE) {
+ break;
+ }
+ if (loc.fe_area == &test_fcb_area[0]) {
+ elem_cnts[0]++;
+ } else if (loc.fe_area == &test_fcb_area[1]) {
+ elem_cnts[1]++;
+ } else {
+ TEST_ASSERT(0);
+ }
+
+ rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+ sizeof(test_data));
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_append_finish(fcb, &loc);
+ TEST_ASSERT(rc == 0);
+
+ for (i = 0; i < 2; i++) {
+ rc = fcb_area_info(fcb, &fcb->f_sectors[i], &area_elems[i],
+ &area_bytes[i]);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(area_elems[i] == elem_cnts[i]);
+ TEST_ASSERT(area_bytes[i] == elem_cnts[i] * sizeof(test_data));
+ }
+ }
+
+ /*
+ * Wipe out the oldest, should report zeroes for that area now.
+ */
+ rc = fcb_rotate(fcb);
+ TEST_ASSERT(rc == 0);
+
+ rc = fcb_area_info(fcb, &fcb->f_sectors[0], &area_elems[0], &area_bytes[0]);
+ TEST_ASSERT(rc == 0);
+ rc = fcb_area_info(fcb, &fcb->f_sectors[1], &area_elems[1], &area_bytes[1]);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(area_elems[0] == 0);
+ TEST_ASSERT(area_bytes[0] == 0);
+ TEST_ASSERT(area_elems[1] == elem_cnts[1]);
+ TEST_ASSERT(area_bytes[1] == elem_cnts[1] * sizeof(test_data));
+
+ /*
+ * If area pointer is NULL, should check the oldest area (area 1).
+ */
+ rc = fcb_area_info(fcb, NULL, &area_elems[0], &area_bytes[0]);
+ TEST_ASSERT(rc == 0);
+ TEST_ASSERT(area_elems[0] == elem_cnts[1]);
+ TEST_ASSERT(area_bytes[0] == elem_cnts[1] * sizeof(test_data));
+}
diff --git a/sys/log/full/include/log/log.h b/sys/log/full/include/log/log.h
index a2d028c354..ac7bc11909 100644
--- a/sys/log/full/include/log/log.h
+++ b/sys/log/full/include/log/log.h
@@ -22,6 +22,9 @@
#include "os/mynewt.h"
#include "cbmem/cbmem.h"
#include "log_common/log_common.h"
+#if MYNEWT_VAL(LOG_STATS)
+#include "stats/stats.h"
+#endif
#ifdef __cplusplus
extern "C" {
@@ -171,6 +174,21 @@ struct log_entry_hdr {
#define LOG_CRITICAL(__l, __mod, ...) IGNORE(__VA_ARGS__)
#endif
+#if MYNEWT_VAL(LOG_STATS)
+STATS_SECT_START(logs)
+ STATS_SECT_ENTRY(writes)
+ STATS_SECT_ENTRY(drops)
+ STATS_SECT_ENTRY(errs)
+ STATS_SECT_ENTRY(lost)
+STATS_SECT_END
+
+#define LOG_STATS_INC(log, name) STATS_INC(log->l_stats, name)
+#define LOG_STATS_INCN(log, name, cnt) STATS_INCN(log->l_stats, name, cnt)
+#else
+#define LOG_STATS_INC(log, name)
+#define LOG_STATS_INCN(log, name, cnt)
+#endif
+
struct log {
char *l_name;
const struct log_handler *l_log;
@@ -178,6 +196,9 @@ struct log {
STAILQ_ENTRY(log) l_next;
log_append_cb *l_append_cb;
uint8_t l_level;
+#if MYNEWT_VAL(LOG_STATS)
+ STATS_SECT_DECL(logs) l_stats;
+#endif
};
/* Log system level functions (for all logs.) */
diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c
index dec5dabcf0..c432beea12 100644
--- a/sys/log/full/src/log.c
+++ b/sys/log/full/src/log.c
@@ -63,6 +63,15 @@ struct shell_cmd g_shell_storage_cmd = {
#endif
#endif
+#if MYNEWT_VAL(LOG_STATS)
+STATS_NAME_START(logs)
+ STATS_NAME(logs, writes)
+ STATS_NAME(logs, drops)
+ STATS_NAME(logs, errs)
+ STATS_NAME(logs, lost)
+STATS_NAME_END(logs)
+#endif
+
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
static int log_conf_set(int argc, char **argv, char *val);
@@ -339,6 +348,12 @@ log_register(char *name, struct log *log, const struct log_handler *lh,
if (!log_registered(log)) {
STAILQ_INSERT_TAIL(&g_log_list, log, l_next);
+#if MYNEWT_VAL(LOG_STATS)
+ stats_init(STATS_HDR(log->l_stats),
+ STATS_SIZE_INIT_PARMS(log->l_stats, STATS_SIZE_32),
+ STATS_NAME_INIT_PARMS(logs));
+ stats_register(log->l_name, STATS_HDR(log->l_stats));
+#endif
}
/* Call registered handler now - log structure is set and put on list */
@@ -459,15 +474,18 @@ log_append_typed(struct log *log, uint8_t module, uint8_t level, uint8_t etype,
struct log_entry_hdr *hdr;
int rc;
- hdr = (struct log_entry_hdr *)data;
+ LOG_STATS_INC(log, writes);
+ hdr = (struct log_entry_hdr *)data;
rc = log_append_prepare(log, module, level, etype, hdr);
if (rc != 0) {
+ LOG_STATS_INC(log, drops);
goto err;
}
rc = log->l_log->log_append(log, data, len + LOG_ENTRY_HDR_SIZE);
if (rc != 0) {
+ LOG_STATS_INC(log, errs);
goto err;
}
@@ -485,13 +503,16 @@ log_append_body(struct log *log, uint8_t module, uint8_t level, uint8_t etype,
struct log_entry_hdr hdr;
int rc;
+ LOG_STATS_INC(log, writes);
rc = log_append_prepare(log, module, level, etype, &hdr);
if (rc != 0) {
+ LOG_STATS_INC(log, drops);
return rc;
}
rc = log->l_log->log_append_body(log, &hdr, body, body_len);
if (rc != 0) {
+ LOG_STATS_INC(log, errs);
return rc;
}
@@ -511,8 +532,9 @@ log_append_mbuf_typed_no_free(struct log *log, uint8_t module, uint8_t level,
/* Remove a loyer of indirection for convenience. */
om = *om_ptr;
+ LOG_STATS_INC(log, writes);
if (!log->l_log->log_append_mbuf) {
- rc = -1;
+ rc = SYS_ENOTSUP;
goto err;
}
@@ -526,7 +548,8 @@ log_append_mbuf_typed_no_free(struct log *log, uint8_t module, uint8_t level,
rc = log_append_prepare(log, module, level, etype, hdr);
if (rc != 0) {
- goto err;
+ LOG_STATS_INC(log, drops);
+ goto drop;
}
rc = log->l_log->log_append_mbuf(log, om);
@@ -541,6 +564,8 @@ log_append_mbuf_typed_no_free(struct log *log, uint8_t module, uint8_t level,
return 0;
err:
+ LOG_STATS_INC(log, errs);
+drop:
if (om) {
os_mbuf_free_chain(om);
*om_ptr = NULL;
@@ -571,23 +596,30 @@ log_append_mbuf_body_no_free(struct log *log, uint8_t module, uint8_t level,
struct log_entry_hdr hdr;
int rc;
+ LOG_STATS_INC(log, writes);
if (!log->l_log->log_append_mbuf_body) {
- return SYS_ENOTSUP;
+ rc = SYS_ENOTSUP;
+ goto err;
}
rc = log_append_prepare(log, module, level, etype, &hdr);
if (rc != 0) {
- return rc;
+ LOG_STATS_INC(log, drops);
+ goto drop;
}
rc = log->l_log->log_append_mbuf_body(log, &hdr, om);
if (rc != 0) {
- return rc;
+ goto err;
}
log_call_append_cb(log, hdr.ue_index);
return 0;
+err:
+ LOG_STATS_INC(log, errs);
+drop:
+ return rc;
}
int
diff --git a/sys/log/full/src/log_fcb.c b/sys/log/full/src/log_fcb.c
index 7a7184e863..e6b39ad558 100644
--- a/sys/log/full/src/log_fcb.c
+++ b/sys/log/full/src/log_fcb.c
@@ -41,6 +41,9 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
struct fcb_log *fcb_log;
struct flash_area *old_fa;
int rc = 0;
+#if MYNEWT_VAL(LOG_STATS)
+ int cnt;
+#endif
fcb_log = (struct fcb_log *)log->l_arg;
fcb = &fcb_log->fl_fcb;
@@ -66,6 +69,12 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
old_fa = fcb->f_oldest;
(void)old_fa; /* to avoid #ifdefs everywhere... */
+#if MYNEWT_VAL(LOG_STATS)
+ rc = fcb_area_info(fcb, NULL, &cnt, NULL);
+ if (rc == 0) {
+ LOG_STATS_INCN(log, lost, cnt);
+ }
+#endif
rc = fcb_rotate(fcb);
if (rc) {
goto err;
diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml
index 66e861dcd6..a5aa3e261b 100644
--- a/sys/log/full/syscfg.yml
+++ b/sys/log/full/syscfg.yml
@@ -72,6 +72,10 @@ syscfg.defs:
discarded. Enabling this setting requires 128 bytes of bss.
value: 1
+ LOG_STATS:
+ description: 'Log statistics'
+ value: 0
+
LOG_STORAGE_INFO:
description: >
Enable "storage_info" API which keeps track of log storage usage and
diff --git a/sys/log/modlog/src/modlog.c b/sys/log/modlog/src/modlog.c
index 6135547cc8..87ae4d7355 100644
--- a/sys/log/modlog/src/modlog.c
+++ b/sys/log/modlog/src/modlog.c
@@ -227,6 +227,9 @@ modlog_append_one(struct modlog_mapping *mm, uint8_t module, uint8_t level,
if (rc != 0) {
return SYS_EIO;
}
+ } else {
+ LOG_STATS_INC(mm->desc.log, writes);
+ LOG_STATS_INC(mm->desc.log, drops);
}
return 0;
@@ -282,6 +285,9 @@ modlog_append_mbuf_one(struct modlog_mapping *mm, uint8_t module,
if (rc != 0) {
return SYS_EIO;
}
+ } else {
+ LOG_STATS_INC(mm->desc.log, writes);
+ LOG_STATS_INC(mm->desc.log, drops);
}
return 0;
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services