Commit 75389ed6 authored by Jim Harris's avatar Jim Harris Committed by Tomasz Zawadzki
Browse files

log: add SPDK_ERRLOG_RATELIMIT()



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: default avatarJim 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: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 7014f640
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -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 {											\