Skip to content

Extending Logging Interface#75

Open
zbelinsk wants to merge 4 commits into
masterfrom
wip/log-spam-control
Open

Extending Logging Interface#75
zbelinsk wants to merge 4 commits into
masterfrom
wip/log-spam-control

Conversation

@zbelinsk

@zbelinsk zbelinsk commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Logging system improvements: symbolic level names, spam-control opt-out, and memory optimization.

  • Accept symbolic level names (err, warn, info, dbg) in LOGBUF syntax alongside numeric values
  • Add H2K_LOG_NO_SPAM mode to disable spam-control macros (once/throttle), making them no-ops
  • Optimize H2K_log_once_ht() to use a single u32 bitmask instead of an array (4 bytes vs. 4×MAX_HTHREADS bytes per callsite)

Examples

make LOGBUF=1[warn]                    # whole kernel, WARN and above
make LOGBUF=sched[info],futex[err]     # mixed levels per module
make LOGBUF=1[warn] H2K_LOG_NO_SPAM=1  # WARN and above; once/throttle macros disabled (no-op)

Details

Symbolic Levels

  • err → 0 (ERROR)
  • warn → 1 (WARN)
  • info → 2 (INFO)
  • dbg → 3 (DEBUG)
  • Numeric values (0-3) still work for backward compatibility

H2K_LOG_NO_SPAM Mode

When enabled, H2K_log_once*() and H2K_log_throttle*() macros become empty (no-op). Eliminates static state bloat:

  • H2K_log_once() allocates a u32 flag per callsite (tracks if already logged)
  • H2K_log_throttle() allocates a u64 timestamp per callsite (tracks last emit time)
  • H2K_log_throttle_ht() allocates u64[MAX_HTHREADS] per callsite (per-HT timestamps)
  • H2K_log_once_ht() uses a single u32 bitmask per callsite. Each hardware thread's once-flag is one bit, accessed atomically via H2K_atomic_setbit().

With the flag, these allocations are skipped entirely.

@github-actions github-actions Bot added the untested Mark untested PRs label Jul 7, 2026
zbelinsk added 2 commits July 7, 2026 15:40
Signed-off-by: Zeev Belinsky <zbelinsk@qti.qualcomm.com>
Expands the logging system from a simple H2K_log() macro into a complete
structured logging API with severity levels, spam control, and build-time
filtering.

## New API (all three build modes: LOGBUF, LOG_PRINTF, silent)

Severity variants:
  H2K_log_err/warn/info/dbg(fmt, ...)
  H2K_log_string_at(level, S), H2K_log_string_err/warn/info/dbg(S)

Spam control — log once per callsite:
  H2K_log_once[_err/warn/info/dbg](...)
  H2K_log_once_ht[_err/warn/info/dbg](...)   per hardware thread

Spam control — rate limited by PCycle interval:
  H2K_log_throttle[_err/warn/info/dbg](interval, ...)
  H2K_log_throttle_ht[_err/warn/info/dbg](interval, ...)   per hardware thread

All string variants (_string_once, _string_throttle, _ht) follow the same
pattern, giving a full NxM matrix of combinations.

## Log prefix format (same in both LOGBUF and LOG_PRINTF)

  [ht<id>][LEVEL][<pcycles>][<file>:<line>] message

## Build-time level filtering

  make LOGBUF=1[2]          whole kernel, INFO and above only
  make LOGBUF=sched[1]      sched directory, WARN and above
  make LOGBUF=dosched.ref.c[0]  single file, ERROR only
  make LOGBUF=sched,futex[2]    mixed, no level = default (DBG)

The [level] syntax works for both kernel (LOGBUF) and standalone test
(LOG_PRINTF) builds via Makefile.inc.test.

## Implementation notes

- LOGBUF: lockless per-htid ring buffer; spinlock only for Angel flush.
  Formatting (emit_prefix + format parse) happens outside the lock.
  H2K_log_once uses H2K_atomic_setbit; H2K_log_throttle uses logbuf_lock.
  H2K_log_throttle_ht / H2K_log_once_ht are lockless (per-slot ownership).

- LOG_PRINTF: static inline helpers (H2K_printf_now via H2K_get_pcycle_reg,
  H2K_printf_basename via strrchr) give full prefix with zero link deps.
  H2K_printf_log_once (atomic) and H2K_printf_log_throttle (spinlock) live
  in log.ref.c under #ifdef H2K_LOG_PRINTF.

- H2K_log_string now has identical semantics in both modes (full prefix +
  level check), replacing the previous puts() shortcut in LOG_PRINTF.

Signed-off-by: Zeev Belinsky <zbelinsk@qti.qualcomm.com>
@zbelinsk zbelinsk force-pushed the wip/log-spam-control branch 4 times, most recently from 5e1b8a1 to 0654e7c Compare July 7, 2026 22:51
Three improvements to the logging system:

1. Accept symbolic level names in LOGBUF syntax (err, warn, info, dbg) in
   addition to numeric values (0, 1, 2, 3):
     make LOGBUF=1[warn]           whole kernel, WARN and above
     make LOGBUF=sched[info]       sched directory, INFO and above
     make LOGBUF=futex[err],sched[dbg]  mixed symbolic and numeric

2. Add H2K_LOG_NO_SPAM mode to strip spam-control macros (once/throttle) and
   make them no-ops. Eliminates static state bloat:
     make LOGBUF=1[warn] H2K_LOG_NO_SPAM=1

   In this mode, H2K_log_once/throttle macros become empty (no-op), while
   base H2K_log/err/warn/info/dbg macros still emit normally.

3. Optimize H2K_log_once_ht to use a single u32 bitmask instead of an array.
   Each hardware thread's once-flag is now a single bit in a shared u32,
   reducing per-callsite memory from MAX_HTHREADS*4 bytes to 4 bytes.
   Uses H2K_atomic_setbit for race-free per-bit access. Works in both
   H2K_LOGBUF and H2K_LOG_PRINTF modes.

Implemented via Makefile function for level mapping, conditional macro
redefinition in log.h, and bitmask logic in log.ref.c. Works in both kernel
(LOGBUF) and standalone test (LOG_PRINTF) builds.

Signed-off-by: Zeev Belinsky <zbelinsk@qti.qualcomm.com>
@zbelinsk zbelinsk force-pushed the wip/log-spam-control branch from 0654e7c to 5f0098b Compare July 7, 2026 22:52
Signed-off-by: Zeev Belinsky <zbelinsk@qti.qualcomm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untested Mark untested PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant