Files
lneto/internal/debug_heaplog.go
Pat Whittingslow 989bb6a0b9 Reduce heap allocs 2 (#46)
* reduce heap allocations in tcp logging; omit use of AppendFloat which allocates a metric sh*tton

* debugheaplog: better heap statistic logging

* heap: use string for pcap.Frame.Protocol

* add potential to eliminate Flags.String heap alloc, remove incorrect HEAP comments

* add StackAsync.DebugErr and httpraw.SetBytes

* many heap alloc reductions and replacement of bytes.Equal with internal.BytesEqual
2026-02-28 15:20:46 -03:00

61 lines
1.2 KiB
Go

//go:build debugheaplog
package internal
import (
"log/slog"
"runtime"
"time"
"unsafe"
)
const (
HeapAllocDebugging = true
timefmt = "[01-02 15:04:05.000]"
)
var (
timebuf [len(timefmt) * 2]byte
)
func LogEnabled(l *slog.Logger, lvl slog.Level) bool {
return true
}
func LogAttrs(_ *slog.Logger, level slog.Level, msg string, attrs ...slog.Attr) {
now := time.Now()
n := len(now.AppendFormat(timebuf[:0], timefmt))
LogAllocs(msg)
print("time=", unsafe.String(&timebuf[0], n), " ")
if level == LevelTrace {
print("TRACE ")
} else if level < slog.LevelDebug {
print("SEQS ")
} else {
print(level.String(), " ")
}
print(msg)
for _, a := range attrs {
switch a.Value.Kind() {
case slog.KindString:
print(" ", a.Key, "=", a.Value.String())
case slog.KindInt64:
print(" ", a.Key, "=", a.Value.Int64())
case slog.KindUint64:
print(" ", a.Key, "=", a.Value.Uint64())
case slog.KindBool:
print(" ", a.Key, "=", a.Value.Bool())
}
}
println()
allocmu.Lock()
runtime.ReadMemStats(&memstats)
if lastAllocs != memstats.TotalAlloc {
print("alloc increase in heaplog")
}
lastAllocs = memstats.TotalAlloc
lastMallocs = memstats.Mallocs
allocmu.Unlock()
}