Reduce the overhead of the trace compare routines by checking if kcov is
authoranton <anton@openbsd.org>
Sat, 18 Dec 2021 08:24:31 +0000 (08:24 +0000)
committeranton <anton@openbsd.org>
Sat, 18 Dec 2021 08:24:31 +0000 (08:24 +0000)
enabled for the current thread before doing anything else.

sys/dev/kcov.c

index 800c380..b49c6d0 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: kcov.c,v 1.38 2021/07/05 05:50:19 anton Exp $ */
+/*     $OpenBSD: kcov.c,v 1.39 2021/12/18 08:24:31 anton Exp $ */
 
 /*
  * Copyright (c) 2018 Anton Lindqvist <anton@openbsd.org>
@@ -157,15 +157,11 @@ __sanitizer_cov_trace_pc(void)
  * its corresponding coverage buffer.
  */
 void
-trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uintptr_t pc)
+trace_cmp(struct kcov_dev *kd, uint64_t type, uint64_t arg1, uint64_t arg2,
+    uintptr_t pc)
 {
-       struct kcov_dev *kd;
        uint64_t idx;
 
-       kd = kd_curproc(KCOV_MODE_TRACE_CMP);
-       if (kd == NULL)
-               return;
-
        if ((idx = kd_claim(kd, KCOV_STRIDE_TRACE_CMP, 1))) {
                kd->kd_buf[idx] = type;
                kd->kd_buf[idx + 1] = arg1;
@@ -174,68 +170,73 @@ trace_cmp(uint64_t type, uint64_t arg1, uint64_t arg2, uintptr_t pc)
        }
 }
 
+#define TRACE_CMP(type, arg1, arg2) do {                               \
+       struct kcov_dev *kd;                                            \
+       if ((kd = kd_curproc(KCOV_MODE_TRACE_CMP)) == NULL)             \
+               return;                                                 \
+       trace_cmp(kd, (type), (arg1), (arg2),                           \
+           (uintptr_t)__builtin_return_address(0));                    \
+} while (0)
+
 void
 __sanitizer_cov_trace_cmp1(uint8_t arg1, uint8_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(0), arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(0), arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_cmp2(uint16_t arg1, uint16_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(1), arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(1), arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_cmp4(uint32_t arg1, uint32_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(2), arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(2), arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_cmp8(uint64_t arg1, uint64_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(3), arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(3), arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_const_cmp1(uint8_t arg1, uint8_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(0) | KCOV_CMP_CONST, arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_const_cmp2(uint16_t arg1, uint16_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(1) | KCOV_CMP_CONST, arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_const_cmp4(uint32_t arg1, uint32_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(2) | KCOV_CMP_CONST, arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_const_cmp8(uint64_t arg1, uint64_t arg2)
 {
-       trace_cmp(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2,
-           (uintptr_t)__builtin_return_address(0));
+       TRACE_CMP(KCOV_CMP_SIZE(3) | KCOV_CMP_CONST, arg1, arg2);
 }
 
 void
 __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
 {
+       struct kcov_dev *kd;
        uint64_t i, nbits, ncases, type;
        uintptr_t pc;
 
+       kd = kd_curproc(KCOV_MODE_TRACE_CMP);
+       if (kd == NULL)
+               return;
+
        pc = (uintptr_t)__builtin_return_address(0);
        ncases = cases[0];
        nbits = cases[1];
@@ -259,7 +260,7 @@ __sanitizer_cov_trace_switch(uint64_t val, uint64_t *cases)
        type |= KCOV_CMP_CONST;
 
        for (i = 0; i < ncases; i++)
-               trace_cmp(type, cases[i + 2], val, pc);
+               trace_cmp(kd, type, cases[i + 2], val, pc);
 }
 
 void