Merge branch 'metrics-count-bug' into 'main'

add metrics counter for BUG() reached

Closes #40839

See merge request tpo/core/tor!760
This commit is contained in:
David Goulet 2023-10-11 18:59:34 +00:00
commit dad173d3be
5 changed files with 80 additions and 9 deletions

View File

@ -9,8 +9,10 @@ lib/intmath/*.h
lib/lock/*.h
lib/log/*.h
lib/malloc/*.h
lib/metrics/*.h
lib/string/*.h
lib/subsys/*.h
lib/testsupport/*.h
lib/thread/threads.h
lib/version/*.h
lib/wallclock/*.h

View File

@ -33,6 +33,7 @@
#define LOG_PRIVATE
#include "lib/log/log.h"
#include "lib/log/log_sys.h"
#include "lib/log/util_bug.h"
#include "lib/version/git_revision.h"
#include "lib/log/ratelim.h"
#include "lib/lock/compat_mutex.h"
@ -912,6 +913,7 @@ init_logging(int disable_startup_queue)
{
if (!log_mutex_initialized) {
tor_mutex_init(&log_mutex);
tor_bug_init_counter();
log_mutex_initialized = 1;
}
#ifdef __GNUC__

View File

@ -11,11 +11,16 @@
#include "lib/log/escape.h"
#include "lib/log/log.h"
#include "lib/log/log_sys.h"
#include "lib/log/util_bug.h"
#include "lib/metrics/metrics_store.h"
static metrics_store_t *the_store;
static int
subsys_logging_initialize(void)
{
init_logging(0);
the_store = metrics_store_new();
return 0;
}
@ -26,6 +31,29 @@ subsys_logging_shutdown(void)
escaped(NULL);
}
static const smartlist_t *
logging_metrics_get_stores(void)
{
static smartlist_t *stores_list = NULL;
metrics_store_reset(the_store);
metrics_store_entry_t *sentry = metrics_store_add(
the_store,
METRICS_TYPE_COUNTER,
METRICS_NAME(bug_reached),
"Total number of BUG() and similar assertion reached",
0, NULL);
metrics_store_entry_update(sentry, tor_bug_get_count());
if (!stores_list) {
stores_list = smartlist_new();
smartlist_add(stores_list, the_store);
}
return stores_list;
}
const subsys_fns_t sys_logging = {
.name = "log",
SUBSYS_DECLARE_LOCATION(),
@ -35,4 +63,5 @@ const subsys_fns_t sys_logging = {
.level = -90,
.initialize = subsys_logging_initialize,
.shutdown = subsys_logging_shutdown,
.get_metrics = logging_metrics_get_stores,
};

View File

@ -18,6 +18,7 @@
#endif
#include "lib/malloc/malloc.h"
#include "lib/string/printf.h"
#include "lib/thread/threads.h"
#include <string.h>
#include <stdlib.h>
@ -101,6 +102,27 @@ tor_assertion_failed_(const char *fname, unsigned int line,
tor_free(buf);
}
static atomic_counter_t total_bug_reached;
void
tor_bug_init_counter(void)
{
atomic_counter_init(&total_bug_reached);
}
/** Helper to update BUG count in metrics. */
void
tor_bug_increment_count_(void)
{
atomic_counter_add(&total_bug_reached, 1);
}
size_t
tor_bug_get_count(void)
{
return atomic_counter_get(&total_bug_reached);
}
/** Helper for tor_assert_nonfatal: report the assertion failure. */
void
tor_bug_occurred_(const char *fname, unsigned int line,
@ -110,6 +132,11 @@ tor_bug_occurred_(const char *fname, unsigned int line,
char *buf = NULL;
const char *once_str = once ?
" (Future instances of this warning will be silenced.)": "";
if (! once) {
// _once assertions count from the macro directly so we count them as many
// time as they are reached, and not just once.
tor_bug_increment_count_();
}
if (! expr) {
if (capturing_bugs()) {
add_captured_bug("This line should not have been reached.");

View File

@ -39,6 +39,7 @@
#include "orconfig.h"
#include "lib/cc/compat_compiler.h"
#include "lib/log/log.h"
#include "lib/smartlist_core/smartlist_core.h"
#include "lib/testsupport/testsupport.h"
/* Replace assert() with a variant that sends failures to the log before
@ -191,6 +192,7 @@
STMT_END
#define tor_assert_nonfatal_unreached_once() STMT_BEGIN \
static int warning_logged__ = 0; \
tor_bug_increment_count_(); \
if (!warning_logged__) { \
warning_logged__ = 1; \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1, NULL); \
@ -198,11 +200,13 @@
STMT_END
#define tor_assert_nonfatal_once(cond) STMT_BEGIN \
static int warning_logged__ = 0; \
if (ASSERT_PREDICT_LIKELY_(cond)) { \
} else if (!warning_logged__) { \
if (!ASSERT_PREDICT_LIKELY_(cond)) { \
tor_bug_increment_count_(); \
if (!warning_logged__) { \
warning_logged__ = 1; \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1, NULL);\
} \
} \
STMT_END
#define BUG(cond) \
(ASSERT_PREDICT_UNLIKELY_(cond) ? \
@ -215,18 +219,22 @@
if (( { \
static int var = 0; \
int bool_result = !!(cond); \
if (bool_result && !var) { \
if (bool_result) { \
tor_bug_increment_count_(); \
if (!var) { \
var = 1; \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
("!("#cond")"), 1, NULL); \
} \
} \
bool_result; } ))
#else /* !defined(__GNUC__) */
#define IF_BUG_ONCE__(cond,var) \
static int var = 0; \
if ((cond) ? \
(var ? 1 : \
(var ? (tor_bug_increment_count_(), 1) : \
(var=1, \
tor_bug_increment_count_(), \
tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, \
("!("#cond")"), 1, NULL), \
1)) \
@ -273,12 +281,15 @@ void tor_assertion_failed_(const char *fname, unsigned int line,
const char *func, const char *expr,
const char *fmt, ...)
CHECK_PRINTF(5,6);
void tor_bug_increment_count_(void);
size_t tor_bug_get_count(void);
void tor_bug_occurred_(const char *fname, unsigned int line,
const char *func, const char *expr,
int once, const char *fmt, ...)
CHECK_PRINTF(6,7);
void tor_abort_(void) ATTR_NORETURN;
void tor_bug_init_counter(void);
#ifdef _WIN32
#define SHORT_FILE__ (tor_fix_source_file(__FILE__))