Skip to content

Commit

Permalink
log: add SPDK_ERRLOG_RATELIMIT()
Browse files Browse the repository at this point in the history
This will only print the specified error message once per second.

Some caveats:

1) This uses spdk_get_ticks() and friends which is technically a circular
   dependency since env_dpdk depends on log. log.h doesn't include env.h,
   and I'm not sure we should add it due to this dependency. It means caller
   needs to have env.h included before using this new macro.
2) The ratelimit is global, so if thread A prints it as time 0s, and thread
   B prints it at time 0.5s, the thread B one will get squashed. I think this
   is the desired behavior.
3) Similar to #2, it means there is small race where two threads do the
   ERRLOG at the exact same time after the 1 second has expired. In this case
   both will read the expired value and do the print. Again, I think this is
   fine, let's not overcomplicate this.
4) This doesn't take argument values into account. So if this macro is getting
   called many many times but each time with different parameters (i.e. a
   controller name) then the log will only show one of them.

Signed-off-by: Jim Harris <jim.harris@samsung.com>
Change-Id: I642350f39bacaaa79358308511dab511e6570cae
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/22615
Community-CI: Mellanox Build Bot
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: Shuhei Matsumoto <smatsumoto@nvidia.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
  • Loading branch information
jimharris authored and tomzawadzki committed Apr 5, 2024
1 parent 7014f64 commit 75389ed
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/spdk/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ enum spdk_log_level spdk_log_get_print_level(void);
} \
} while (0)

#define SPDK_ERRLOG_RATELIMIT(...) \
do { \
static uint64_t last_tsc = 0; \
static uint64_t squashed = 0; \
uint64_t tsc = spdk_get_ticks(); \
if (tsc > last_tsc + spdk_get_ticks_hz()) { \
last_tsc = tsc; \
SPDK_ERRLOG(__VA_ARGS__); \
if (squashed > 0) { \
SPDK_ERRLOG("(same message squashed %" PRIu64 " times)\n", \
squashed); \
squashed = 0; \
} \
} else { \
squashed++; \
} \
} while (0)

#ifdef DEBUG
#define SPDK_DEBUGLOG(flag, ...) \
do { \
Expand Down

0 comments on commit 75389ed

Please sign in to comment.