]> git.proxmox.com Git - mirror_zfs.git/commitdiff
spl: Add cmn_err_once() to log a message only on the first call
authorAttila Fülöp <attila@fueloep.org>
Tue, 7 Mar 2023 21:44:11 +0000 (22:44 +0100)
committerGitHub <noreply@github.com>
Tue, 7 Mar 2023 21:44:11 +0000 (13:44 -0800)
Reviewed-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Brian Atkinson <batkinson@lanl.gov>
Signed-off-by: Attila Fülöp <attila@fueloep.org>
Closes #14567

include/os/freebsd/spl/sys/cmn_err.h
include/os/linux/spl/sys/cmn_err.h
lib/libspl/include/sys/cmn_err.h

index a8f9a88247cd636b8111ae9c1e0e02bc1c405389..dd3da7da205a6b6e920d0a67dc0dd02fe18a6f99 100644 (file)
@@ -33,6 +33,7 @@
 
 #if !defined(_ASM)
 #include <sys/_stdarg.h>
+#include <sys/atomic.h>
 #endif
 
 #ifdef __cplusplus
@@ -73,6 +74,38 @@ 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, ...)                    \
+{                                                      \
+       static volatile uint32_t printed = 0;           \
+       if (atomic_cas_32(&printed, 0, 1) == 0) {       \
+               zcmn_err(zone, ce, __VA_ARGS__);        \
+       }                                               \
+}
+
+#define        vzcmn_err_once(zone, ce, fmt, ap)               \
+{                                                      \
+       static volatile uint32_t printed = 0;           \
+       if (atomic_cas_32(&printed, 0, 1) == 0) {       \
+               vzcmn_err(zone, ce, fmt, ap);           \
+       }                                               \
+}
+
 #endif /* !_ASM */
 
 #ifdef __cplusplus
index 161bcf9b3a46a771fdd4628a35e6cd32567d0b3e..7ca2a86be9e841719d6eca5bc5cb1e2e68480c1b 100644 (file)
@@ -29,6 +29,7 @@
 #else
 #include <stdarg.h>
 #endif
+#include <sys/atomic.h>
 
 #define        CE_CONT         0 /* continuation */
 #define        CE_NOTE         1 /* notice */
@@ -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 */
index f4db082c22a4bb3933d7ec78ecb6f223f3343948..6c71dcb8ec134dfb1ef8ae880bed7d3332e70f28 100644 (file)
 #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, ...)                    \
+{                                                      \
+       static volatile uint32_t printed = 0;           \
+       if (atomic_cas_32(&printed, 0, 1) == 0) {       \
+               zcmn_err(zone, ce, __VA_ARGS__);        \
+       }                                               \
+}
+
+#define        vzcmn_err_once(zone, ce, fmt, ap)               \
+{                                                      \
+       static volatile uint32_t printed = 0;           \
+       if (atomic_cas_32(&printed, 0, 1) == 0) {       \
+               vzcmn_err(zone, ce, fmt, ap);           \
+       }                                               \
+}
+
 #endif