Skip to content

Commit

Permalink
spl: Add cmn_err_once() to log a message only on the first call
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Fülöp <attila@fueloep.org>
  • Loading branch information
AttilaFueloep committed Mar 4, 2023
1 parent 0963d64 commit d6eca66
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
25 changes: 25 additions & 0 deletions include/os/freebsd/spl/sys/cmn_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#if !defined(_ASM)
#include <sys/_stdarg.h>
#include <sys/atomic.h>
#endif

#ifdef __cplusplus
Expand Down Expand Up @@ -73,6 +74,30 @@ extern void vuprintf(const char *, __va_list)
extern void panic(const char *, ...)
__attribute__((format(printf, 1, 2), __noreturn__));

#define cmn_err_once(ce, ...) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
cmn_err(ce, __VA_ARGS__); \
} \
}

#define vcmn_err_once(ce, fmt, ap) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
vcmn_err(ce, fmt, ap); \
} \
}

#define zcmn_err_once(zone, ce, fmt, ap) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
zcmn_err(zone, ce, fmt, ap); \
} \
}

#endif /* !_ASM */

#ifdef __cplusplus
Expand Down
17 changes: 17 additions & 0 deletions include/os/linux/spl/sys/cmn_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#else
#include <stdarg.h>
#endif
#include <sys/atomic.h>

#define CE_CONT 0 /* continuation */
#define CE_NOTE 1 /* notice */
Expand All @@ -45,4 +46,20 @@ extern void vpanic(const char *, va_list)

#define fm_panic panic

#define cmn_err_once(ce, ...) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
cmn_err(ce, __VA_ARGS__); \
} \
}

#define vcmn_err_once(ce, fmt, ap) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
vcmn_err(ce, fmt, ap); \
} \
}

#endif /* SPL_CMN_ERR_H */
26 changes: 26 additions & 0 deletions lib/libspl/include/sys/cmn_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,30 @@
#ifndef _LIBSPL_SYS_CMN_ERR_H
#define _LIBSPL_SYS_CMN_ERR_H

#include <atomic.h>

#define cmn_err_once(ce, ...) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
cmn_err(ce, __VA_ARGS__); \
} \
}

#define vcmn_err_once(ce, fmt, ap) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
vcmn_err(ce, fmt, ap); \
} \
}

#define zcmn_err_once(zone, ce, fmt, ap) \
{ \
static volatile uint32_t printed = 0; \
if (atomic_cas_32(&printed, 0, 1) == 0) { \
zcmn_err(zone, ce, fmt, ap); \
} \
}

#endif

0 comments on commit d6eca66

Please sign in to comment.