基准说明 / 测量源码
preempt 测量源码
这里逐字展示测量所用的源码,保留原有注释、缩进和换行。
历史无调用循环 · main.go
// preempt measures the GC/scheduling cost of goroutines stuck in long,
// call-free loops. The busy kernels below are pure numeric loops with no
// function calls, no allocations and no pointer writes, so a compiler that
// marks the whole function async-unsafe leaves the goroutine unreachable by
// signal-based async preemption until the kernel returns. Allocator workers
// drive GC concurrently. runtime/metrics histograms quantify the resulting
// STW and scheduling latencies. Go 1.22 compatible.
package main
import (
"flag"
"fmt"
"math"
"runtime"
"runtime/metrics"
"sync"
"time"
)
// ---- call-free busy kernels (no calls, no allocs, no pointer ops) ----
//go:noinline
func busyFNV(data []byte, iters int) uint64 {
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
h := uint64(offset64)
n := len(data)
for i := 0; i < iters; i++ {
b := data[i%n]
h ^= uint64(b)
h *= prime64
}
return h
}
//go:noinline
func busySum(x []float64, iters int) float64 {
mask := len(x) - 1 // x is power-of-two length
var s float64
for i := 0; i < iters; i++ {
s += x[i&mask] * 1.000001
}
return s
}
//go:noinline
func busyXorshift(seed uint64, iters int) uint64 {
x := seed
var acc uint64 = 0x9e3779b97f4a7c15
for i := 0; i < iters; i++ {
x ^= x << 13
x ^= x >> 7
x ^= x << 17
acc ^= x
}
return acc
}
// ---- GC-driving allocators (call-rich, like the gcload mutators) ----
type node struct {
l, r *node
v [24]byte
}
var ring [2048]*node
//go:noinline
func allocBatch(id, base int) {
for k := 0; k < 64; k++ {
p := &node{}
p.v[0] = byte(k)
p.l = &node{v: [24]byte{byte(id), byte(k)}}
ring[(base+k)&2047] = p
}
}
// ---- metrics plumbing ----
var metricNames = []string{
"/sched/pauses/stopping/gc:seconds",
"/sched/pauses/total/gc:seconds",
"/sched/latencies:seconds",
"/gc/cycles/total:gc-cycles",
"/cpu/classes/gc/mark/assist:cpu-seconds",
"/cpu/classes/gc/total:cpu-seconds",
"/cpu/classes/total:cpu-seconds",
"/gc/heap/live:bytes",
"/gc/heap/allocs:bytes",
"/gc/heap/allocs:objects",
}
func readMetrics(s []metrics.Sample) {
for i := range s {
s[i].Name = metricNames[i]
}
metrics.Read(s)
}
func counter(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return float64(s[i].Value.Uint64())
}
}
return 0
}
// histDelta subtracts two cumulative histogram readings and returns the
// quantile (0..1) of the delta distribution in microseconds, plus the max
// bucket's finite lower edge.
func histQuant(before, after *metrics.Float64Histogram, q float64) float64 {
var total uint64
deltas := make([]uint64, len(after.Counts))
for i, c := range after.Counts {
d := c
if i < len(before.Counts) {
d -= before.Counts[i]
}
deltas[i] = d
total += d
}
if total == 0 {
return 0
}
rank := uint64(float64(total)*q + 0.5)
if rank < 1 {
rank = 1
}
var cum uint64
for i, d := range deltas {
cum += d
if cum >= rank {
ub := after.Buckets[i+1]
if math.IsInf(ub, 1) { // +Inf bucket: report its finite lower edge
return after.Buckets[i] * 1e6
}
return ub * 1e6
}
}
return after.Buckets[len(after.Buckets)-2] * 1e6
}
func histMax(before, after *metrics.Float64Histogram) float64 {
return histQuant(before, after, 1.0)
}
// ---- globals keep the busy results observable without calls in the loop ----
var (
sinkU64 uint64
sinkF64 float64
)
func calibrate(targetChunkUS int) int {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
const probe = 2000
t0 := time.Now()
sinkU64 = busyFNV(data, probe)
us := time.Since(t0).Microseconds()
if us <= 0 {
us = 1
}
iters := int(float64(probe) * float64(targetChunkUS) / float64(us))
if iters < 1000 {
iters = 1000
}
return iters
}
//go:noinline
func busyWorker(id, chunkIters int, dur time.Duration, iters *uint64, chunkUS *float64) {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
xf := make([]float64, 1024)
for i := range xf {
xf[i] = float64(i) * 1.0001
}
seed := uint64(0x9e3779b97f4a7c15) + uint64(id)*7919
var totalIters, chunks, chunkNS uint64
start := time.Now()
for time.Since(start) < dur {
t0 := time.Now()
switch chunks % 3 {
case 0:
sinkU64 = busyFNV(data, chunkIters)
case 1:
sinkF64 = busySum(xf, chunkIters)
default:
seed = busyXorshift(seed, chunkIters)
sinkU64 = seed
}
chunkNS += uint64(time.Since(t0))
chunks++
totalIters += uint64(chunkIters)
}
*iters = totalIters
*chunkUS = float64(chunkNS) / float64(chunks) / 1e3
}
func main() {
defer bs2Metrics()()
procs := flag.Int("p", 4, "GOMAXPROCS")
busyN := flag.Int("busy", 0, "busy goroutines (default = GOMAXPROCS)")
allocN := flag.Int("alloc", 0, "allocator goroutines (default = GOMAXPROCS)")
dur := flag.Duration("d", 8*time.Second, "duration")
chunkUS := flag.Int("chunkus", 2000, "target wall time of one call-free chunk")
flag.Parse()
runtime.GOMAXPROCS(*procs)
if *busyN == 0 {
*busyN = *procs
}
if *allocN == 0 {
*allocN = *procs
}
chunkIters := calibrate(*chunkUS)
runtime.GC()
beforeSamples := make([]metrics.Sample, len(metricNames))
readMetrics(beforeSamples)
before := map[string]*metrics.Float64Histogram{}
for i := range beforeSamples {
if beforeSamples[i].Value.Kind() == metrics.KindFloat64Histogram {
before[beforeSamples[i].Name] = beforeSamples[i].Value.Float64Histogram()
}
}
start := time.Now()
var wg sync.WaitGroup
busyIters := make([]uint64, *busyN)
busyChunks := make([]float64, *busyN)
for w := 0; w < *busyN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
busyWorker(id, chunkIters, *dur, &busyIters[id], &busyChunks[id])
}(w)
}
allocOps := make([]uint64, *allocN)
for w := 0; w < *allocN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
n := 0
base := id * 64
t0 := time.Now()
for time.Since(t0) < *dur {
allocBatch(id, base+n)
n += 64
}
allocOps[id] = uint64(n)
}(w)
}
wg.Wait()
el := time.Since(start).Seconds()
afterSamples := make([]metrics.Sample, len(metricNames))
readMetrics(afterSamples)
afterHist := map[string]*metrics.Float64Histogram{}
var gcCycles, assistCPU, gcCPU, totalCPU, liveHeap, allocBytes, allocObjs float64
for i := range afterSamples {
switch afterSamples[i].Value.Kind() {
case metrics.KindFloat64Histogram:
afterHist[afterSamples[i].Name] = afterSamples[i].Value.Float64Histogram()
case metrics.KindUint64:
v := float64(afterSamples[i].Value.Uint64())
switch afterSamples[i].Name {
case "/gc/cycles/total:gc-cycles":
gcCycles = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/live:bytes":
liveHeap = v
case "/gc/heap/allocs:bytes":
allocBytes = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/allocs:objects":
allocObjs = v - counter(beforeSamples, afterSamples[i].Name)
}
case metrics.KindFloat64:
switch afterSamples[i].Name {
case "/cpu/classes/gc/mark/assist:cpu-seconds":
assistCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/gc/total:cpu-seconds":
gcCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/total:cpu-seconds":
totalCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
}
}
}
var totalBusyIters, totalAllocOps uint64
var chunkAvg float64
for _, v := range busyIters {
totalBusyIters += v
}
for _, v := range allocOps {
totalAllocOps += v
}
for _, v := range busyChunks {
chunkAvg += v
}
chunkAvg /= float64(len(busyChunks))
stopB := before["/sched/pauses/stopping/gc:seconds"]
stopA := afterHist["/sched/pauses/stopping/gc:seconds"]
totB := before["/sched/pauses/total/gc:seconds"]
totA := afterHist["/sched/pauses/total/gc:seconds"]
latB := before["/sched/latencies:seconds"]
latA := afterHist["/sched/latencies:seconds"]
fmt.Printf("busy-Miters/s %.3f\n", float64(totalBusyIters)/el/1e6)
fmt.Printf("alloc-op/s %.0f\n", float64(totalAllocOps)/el)
fmt.Printf("chunk-us %.1f\n", chunkAvg)
fmt.Printf("gc-cycles %.0f\n", gcCycles)
fmt.Printf("gc-cpu-s %.3f\n", gcCPU)
fmt.Printf("gc-cpu-frac %.4f\n", gcCPU/totalCPU)
fmt.Printf("assist-cpu-s %.3f\n", assistCPU)
fmt.Printf("pause-stop-p50-us %.3f\n", histQuant(stopB, stopA, 0.50))
fmt.Printf("pause-stop-p99-us %.3f\n", histQuant(stopB, stopA, 0.99))
fmt.Printf("pause-stop-max-us %.3f\n", histMax(stopB, stopA))
fmt.Printf("pause-total-p50-us %.3f\n", histQuant(totB, totA, 0.50))
fmt.Printf("pause-total-p99-us %.3f\n", histQuant(totB, totA, 0.99))
fmt.Printf("pause-total-max-us %.3f\n", histMax(totB, totA))
fmt.Printf("schedlat-p50-us %.3f\n", histQuant(latB, latA, 0.50))
fmt.Printf("schedlat-p99-us %.3f\n", histQuant(latB, latA, 0.99))
fmt.Printf("schedlat-max-us %.3f\n", histMax(latB, latA))
fmt.Printf("live-heap-B %.0f\n", liveHeap)
fmt.Printf("alloc-B/s %.0f\n", allocBytes/el)
fmt.Printf("alloc-obj/s %.0f\n", allocObjs/el)
}
func beforeVal(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return s[i].Value.Float64()
}
}
return 0
}
无调用版本对照 · main.go
// preempt measures the GC/scheduling cost of goroutines stuck in long,
// call-free loops. The busy kernels below are pure numeric loops with no
// function calls, no allocations and no pointer writes, so a compiler that
// marks the whole function async-unsafe leaves the goroutine unreachable by
// signal-based async preemption until the kernel returns. Allocator workers
// drive GC concurrently. runtime/metrics histograms quantify the resulting
// STW and scheduling latencies. Go 1.22 compatible.
package main
import (
"flag"
"fmt"
"math"
"runtime"
"runtime/metrics"
"sync"
"time"
)
// ---- call-free busy kernels (no calls, no allocs, no pointer ops) ----
//go:noinline
func busyFNV(data []byte, iters int) uint64 {
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
h := uint64(offset64)
n := len(data)
for i := 0; i < iters; i++ {
b := data[i%n]
h ^= uint64(b)
h *= prime64
}
return h
}
//go:noinline
func busySum(x []float64, iters int) float64 {
mask := len(x) - 1 // x is power-of-two length
var s float64
for i := 0; i < iters; i++ {
s += x[i&mask] * 1.000001
}
return s
}
//go:noinline
func busyXorshift(seed uint64, iters int) uint64 {
x := seed
var acc uint64 = 0x9e3779b97f4a7c15
for i := 0; i < iters; i++ {
x ^= x << 13
x ^= x >> 7
x ^= x << 17
acc ^= x
}
return acc
}
// ---- GC-driving allocators (call-rich, like the gcload mutators) ----
type node struct {
l, r *node
v [24]byte
}
var ring [2048]*node
//go:noinline
func allocBatch(id, base int) {
for k := 0; k < 64; k++ {
p := &node{}
p.v[0] = byte(k)
p.l = &node{v: [24]byte{byte(id), byte(k)}}
ring[(base+k)&2047] = p
}
}
// ---- metrics plumbing ----
var metricNames = []string{
"/sched/pauses/stopping/gc:seconds",
"/sched/pauses/total/gc:seconds",
"/sched/latencies:seconds",
"/gc/cycles/total:gc-cycles",
"/cpu/classes/gc/mark/assist:cpu-seconds",
"/cpu/classes/gc/total:cpu-seconds",
"/cpu/classes/total:cpu-seconds",
"/gc/heap/live:bytes",
"/gc/heap/allocs:bytes",
"/gc/heap/allocs:objects",
}
func readMetrics(s []metrics.Sample) {
for i := range s {
s[i].Name = metricNames[i]
}
metrics.Read(s)
}
func counter(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return float64(s[i].Value.Uint64())
}
}
return 0
}
// histDelta subtracts two cumulative histogram readings and returns the
// quantile (0..1) of the delta distribution in microseconds, plus the max
// bucket's finite lower edge.
func histQuant(before, after *metrics.Float64Histogram, q float64) float64 {
var total uint64
deltas := make([]uint64, len(after.Counts))
for i, c := range after.Counts {
d := c
if i < len(before.Counts) {
d -= before.Counts[i]
}
deltas[i] = d
total += d
}
if total == 0 {
return 0
}
rank := uint64(float64(total)*q + 0.5)
if rank < 1 {
rank = 1
}
var cum uint64
for i, d := range deltas {
cum += d
if cum >= rank {
ub := after.Buckets[i+1]
if math.IsInf(ub, 1) { // +Inf bucket: report its finite lower edge
return after.Buckets[i] * 1e6
}
return ub * 1e6
}
}
return after.Buckets[len(after.Buckets)-2] * 1e6
}
func histMax(before, after *metrics.Float64Histogram) float64 {
return histQuant(before, after, 1.0)
}
// ---- globals keep the busy results observable without calls in the loop ----
var (
sinkU64 uint64
sinkF64 float64
)
func calibrate(targetChunkUS int) int {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
const probe = 2000
t0 := time.Now()
sinkU64 = busyFNV(data, probe)
us := time.Since(t0).Microseconds()
if us <= 0 {
us = 1
}
iters := int(float64(probe) * float64(targetChunkUS) / float64(us))
if iters < 1000 {
iters = 1000
}
return iters
}
//go:noinline
func busyWorker(id, chunkIters int, dur time.Duration, iters *uint64, chunkUS *float64) {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
xf := make([]float64, 1024)
for i := range xf {
xf[i] = float64(i) * 1.0001
}
seed := uint64(0x9e3779b97f4a7c15) + uint64(id)*7919
var totalIters, chunks, chunkNS uint64
start := time.Now()
for time.Since(start) < dur {
t0 := time.Now()
switch chunks % 3 {
case 0:
sinkU64 = busyFNV(data, chunkIters)
case 1:
sinkF64 = busySum(xf, chunkIters)
default:
seed = busyXorshift(seed, chunkIters)
sinkU64 = seed
}
chunkNS += uint64(time.Since(t0))
chunks++
totalIters += uint64(chunkIters)
}
*iters = totalIters
*chunkUS = float64(chunkNS) / float64(chunks) / 1e3
}
func main() {
defer bs2Metrics()()
procs := flag.Int("p", 4, "GOMAXPROCS")
busyN := flag.Int("busy", 0, "busy goroutines (default = GOMAXPROCS)")
allocN := flag.Int("alloc", 0, "allocator goroutines (default = GOMAXPROCS)")
dur := flag.Duration("d", 8*time.Second, "duration")
chunkUS := flag.Int("chunkus", 2000, "target wall time of one call-free chunk")
flag.Parse()
runtime.GOMAXPROCS(*procs)
if *busyN == 0 {
*busyN = *procs
}
if *allocN == 0 {
*allocN = *procs
}
chunkIters := calibrate(*chunkUS)
runtime.GC()
beforeSamples := make([]metrics.Sample, len(metricNames))
readMetrics(beforeSamples)
before := map[string]*metrics.Float64Histogram{}
for i := range beforeSamples {
if beforeSamples[i].Value.Kind() == metrics.KindFloat64Histogram {
before[beforeSamples[i].Name] = beforeSamples[i].Value.Float64Histogram()
}
}
start := time.Now()
var wg sync.WaitGroup
busyIters := make([]uint64, *busyN)
busyChunks := make([]float64, *busyN)
for w := 0; w < *busyN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
busyWorker(id, chunkIters, *dur, &busyIters[id], &busyChunks[id])
}(w)
}
allocOps := make([]uint64, *allocN)
for w := 0; w < *allocN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
n := 0
base := id * 64
t0 := time.Now()
for time.Since(t0) < *dur {
allocBatch(id, base+n)
n += 64
}
allocOps[id] = uint64(n)
}(w)
}
wg.Wait()
el := time.Since(start).Seconds()
afterSamples := make([]metrics.Sample, len(metricNames))
readMetrics(afterSamples)
afterHist := map[string]*metrics.Float64Histogram{}
var gcCycles, assistCPU, gcCPU, totalCPU, liveHeap, allocBytes, allocObjs float64
for i := range afterSamples {
switch afterSamples[i].Value.Kind() {
case metrics.KindFloat64Histogram:
afterHist[afterSamples[i].Name] = afterSamples[i].Value.Float64Histogram()
case metrics.KindUint64:
v := float64(afterSamples[i].Value.Uint64())
switch afterSamples[i].Name {
case "/gc/cycles/total:gc-cycles":
gcCycles = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/live:bytes":
liveHeap = v
case "/gc/heap/allocs:bytes":
allocBytes = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/allocs:objects":
allocObjs = v - counter(beforeSamples, afterSamples[i].Name)
}
case metrics.KindFloat64:
switch afterSamples[i].Name {
case "/cpu/classes/gc/mark/assist:cpu-seconds":
assistCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/gc/total:cpu-seconds":
gcCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/total:cpu-seconds":
totalCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
}
}
}
var totalBusyIters, totalAllocOps uint64
var chunkAvg float64
for _, v := range busyIters {
totalBusyIters += v
}
for _, v := range allocOps {
totalAllocOps += v
}
for _, v := range busyChunks {
chunkAvg += v
}
chunkAvg /= float64(len(busyChunks))
stopB := before["/sched/pauses/stopping/gc:seconds"]
stopA := afterHist["/sched/pauses/stopping/gc:seconds"]
totB := before["/sched/pauses/total/gc:seconds"]
totA := afterHist["/sched/pauses/total/gc:seconds"]
latB := before["/sched/latencies:seconds"]
latA := afterHist["/sched/latencies:seconds"]
fmt.Printf("busy-Miters/s %.3f\n", float64(totalBusyIters)/el/1e6)
fmt.Printf("gc-us/req %.6f\n",gcCPU*1e6/float64(totalAllocOps))
fmt.Printf("alloc-op/s %.0f\n", float64(totalAllocOps)/el)
fmt.Printf("chunk-us %.1f\n", chunkAvg)
fmt.Printf("gc-cycles %.0f\n", gcCycles)
fmt.Printf("gc-cpu-s %.3f\n", gcCPU)
fmt.Printf("gc-cpu-frac %.4f\n", gcCPU/totalCPU)
fmt.Printf("assist-cpu-s %.3f\n", assistCPU)
fmt.Printf("pause-stop-p50-us %.3f\n", histQuant(stopB, stopA, 0.50))
fmt.Printf("pause-stop-p99-us %.3f\n", histQuant(stopB, stopA, 0.99))
fmt.Printf("pause-stop-max-us %.3f\n", histMax(stopB, stopA))
fmt.Printf("pause-total-p50-us %.3f\n", histQuant(totB, totA, 0.50))
fmt.Printf("pause-total-p99-us %.3f\n", histQuant(totB, totA, 0.99))
fmt.Printf("pause-total-max-us %.3f\n", histMax(totB, totA))
fmt.Printf("schedlat-p50-us %.3f\n", histQuant(latB, latA, 0.50))
fmt.Printf("schedlat-p99-us %.3f\n", histQuant(latB, latA, 0.99))
fmt.Printf("schedlat-max-us %.3f\n", histMax(latB, latA))
fmt.Printf("live-heap-B %.0f\n", liveHeap)
fmt.Printf("alloc-B/s %.0f\n", allocBytes/el)
fmt.Printf("alloc-obj/s %.0f\n", allocObjs/el)
}
func beforeVal(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return s[i].Value.Float64()
}
}
return 0
}
密集调用版本对照 · main.go
// preempt measures the GC/scheduling cost of goroutines stuck in long,
// call-free loops. The busy kernels below are pure numeric loops with no
// function calls, no allocations and no pointer writes, so a compiler that
// marks the whole function async-unsafe leaves the goroutine unreachable by
// signal-based async preemption until the kernel returns. Allocator workers
// drive GC concurrently. runtime/metrics histograms quantify the resulting
// STW and scheduling latencies. Go 1.22 compatible.
package main
import (
"flag"
"fmt"
"math"
"runtime"
"runtime/metrics"
"sync"
"sync/atomic"
"time"
)
// ---- call-free busy kernels (no calls, no allocs, no pointer ops) ----
//go:noinline
func busyFNV(data []byte, iters int) uint64 {
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
h := uint64(offset64)
n := len(data)
for i := 0; i < iters; i++ {
b := data[i%n]
h ^= uint64(b)
h = denseStep(h * prime64)
}
return h
}
//go:noinline
func busySum(x []float64, iters int) float64 {
mask := len(x) - 1 // x is power-of-two length
var s float64
for i := 0; i < iters; i++ {
s += x[denseStep(uint64(i)) & uint64(mask)] * 1.000001
}
return s
}
//go:noinline
func denseStep(x uint64) uint64 { return x ^ atomic.LoadUint64(&denseSalt) }
var denseSalt uint64
//go:noinline
func busyXorshift(seed uint64, iters int) uint64 {
x := seed
var acc uint64 = 0x9e3779b97f4a7c15
for i := 0; i < iters; i++ {
x ^= x << 13
x ^= x >> 7
x ^= x << 17
acc ^= denseStep(x)
}
return acc
}
// ---- GC-driving allocators (call-rich, like the gcload mutators) ----
type node struct {
l, r *node
v [24]byte
}
var ring [2048]*node
//go:noinline
func allocBatch(id, base int) {
for k := 0; k < 64; k++ {
p := &node{}
p.v[0] = byte(k)
p.l = &node{v: [24]byte{byte(id), byte(k)}}
ring[(base+k)&2047] = p
}
}
// ---- metrics plumbing ----
var metricNames = []string{
"/sched/pauses/stopping/gc:seconds",
"/sched/pauses/total/gc:seconds",
"/sched/latencies:seconds",
"/gc/cycles/total:gc-cycles",
"/cpu/classes/gc/mark/assist:cpu-seconds",
"/cpu/classes/gc/total:cpu-seconds",
"/cpu/classes/total:cpu-seconds",
"/gc/heap/live:bytes",
"/gc/heap/allocs:bytes",
"/gc/heap/allocs:objects",
}
func readMetrics(s []metrics.Sample) {
for i := range s {
s[i].Name = metricNames[i]
}
metrics.Read(s)
}
func counter(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return float64(s[i].Value.Uint64())
}
}
return 0
}
// histDelta subtracts two cumulative histogram readings and returns the
// quantile (0..1) of the delta distribution in microseconds, plus the max
// bucket's finite lower edge.
func histQuant(before, after *metrics.Float64Histogram, q float64) float64 {
var total uint64
deltas := make([]uint64, len(after.Counts))
for i, c := range after.Counts {
d := c
if i < len(before.Counts) {
d -= before.Counts[i]
}
deltas[i] = d
total += d
}
if total == 0 {
return 0
}
rank := uint64(float64(total)*q + 0.5)
if rank < 1 {
rank = 1
}
var cum uint64
for i, d := range deltas {
cum += d
if cum >= rank {
ub := after.Buckets[i+1]
if math.IsInf(ub, 1) { // +Inf bucket: report its finite lower edge
return after.Buckets[i] * 1e6
}
return ub * 1e6
}
}
return after.Buckets[len(after.Buckets)-2] * 1e6
}
func histMax(before, after *metrics.Float64Histogram) float64 {
return histQuant(before, after, 1.0)
}
// ---- globals keep the busy results observable without calls in the loop ----
var (
sinkU64 uint64
sinkF64 float64
)
func calibrate(targetChunkUS int) int {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
const probe = 2000
t0 := time.Now()
sinkU64 = busyFNV(data, probe)
us := time.Since(t0).Microseconds()
if us <= 0 {
us = 1
}
iters := int(float64(probe) * float64(targetChunkUS) / float64(us))
if iters < 1000 {
iters = 1000
}
return iters
}
//go:noinline
func busyWorker(id, chunkIters int, dur time.Duration, iters *uint64, chunkUS *float64) {
data := make([]byte, 4096)
for i := range data {
data[i] = byte(i)
}
xf := make([]float64, 1024)
for i := range xf {
xf[i] = float64(i) * 1.0001
}
seed := uint64(0x9e3779b97f4a7c15) + uint64(id)*7919
var totalIters, chunks, chunkNS uint64
start := time.Now()
for time.Since(start) < dur {
t0 := time.Now()
switch chunks % 3 {
case 0:
sinkU64 = busyFNV(data, chunkIters)
case 1:
sinkF64 = busySum(xf, chunkIters)
default:
seed = busyXorshift(seed, chunkIters)
sinkU64 = seed
}
chunkNS += uint64(time.Since(t0))
chunks++
totalIters += uint64(chunkIters)
}
*iters = totalIters
*chunkUS = float64(chunkNS) / float64(chunks) / 1e3
}
func main() {
defer bs2Metrics()()
procs := flag.Int("p", 4, "GOMAXPROCS")
busyN := flag.Int("busy", 0, "busy goroutines (default = GOMAXPROCS)")
allocN := flag.Int("alloc", 0, "allocator goroutines (default = GOMAXPROCS)")
dur := flag.Duration("d", 8*time.Second, "duration")
chunkUS := flag.Int("chunkus", 2000, "target wall time of one call-free chunk")
flag.Parse()
runtime.GOMAXPROCS(*procs)
if *busyN == 0 {
*busyN = *procs
}
if *allocN == 0 {
*allocN = *procs
}
chunkIters := calibrate(*chunkUS)
runtime.GC()
beforeSamples := make([]metrics.Sample, len(metricNames))
readMetrics(beforeSamples)
before := map[string]*metrics.Float64Histogram{}
for i := range beforeSamples {
if beforeSamples[i].Value.Kind() == metrics.KindFloat64Histogram {
before[beforeSamples[i].Name] = beforeSamples[i].Value.Float64Histogram()
}
}
start := time.Now()
var wg sync.WaitGroup
busyIters := make([]uint64, *busyN)
busyChunks := make([]float64, *busyN)
for w := 0; w < *busyN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
busyWorker(id, chunkIters, *dur, &busyIters[id], &busyChunks[id])
}(w)
}
allocOps := make([]uint64, *allocN)
for w := 0; w < *allocN; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
n := 0
base := id * 64
t0 := time.Now()
for time.Since(t0) < *dur {
allocBatch(id, base+n)
n += 64
}
allocOps[id] = uint64(n)
}(w)
}
wg.Wait()
el := time.Since(start).Seconds()
afterSamples := make([]metrics.Sample, len(metricNames))
readMetrics(afterSamples)
afterHist := map[string]*metrics.Float64Histogram{}
var gcCycles, assistCPU, gcCPU, totalCPU, liveHeap, allocBytes, allocObjs float64
for i := range afterSamples {
switch afterSamples[i].Value.Kind() {
case metrics.KindFloat64Histogram:
afterHist[afterSamples[i].Name] = afterSamples[i].Value.Float64Histogram()
case metrics.KindUint64:
v := float64(afterSamples[i].Value.Uint64())
switch afterSamples[i].Name {
case "/gc/cycles/total:gc-cycles":
gcCycles = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/live:bytes":
liveHeap = v
case "/gc/heap/allocs:bytes":
allocBytes = v - counter(beforeSamples, afterSamples[i].Name)
case "/gc/heap/allocs:objects":
allocObjs = v - counter(beforeSamples, afterSamples[i].Name)
}
case metrics.KindFloat64:
switch afterSamples[i].Name {
case "/cpu/classes/gc/mark/assist:cpu-seconds":
assistCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/gc/total:cpu-seconds":
gcCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
case "/cpu/classes/total:cpu-seconds":
totalCPU = afterSamples[i].Value.Float64() - beforeVal(beforeSamples, afterSamples[i].Name)
}
}
}
var totalBusyIters, totalAllocOps uint64
var chunkAvg float64
for _, v := range busyIters {
totalBusyIters += v
}
for _, v := range allocOps {
totalAllocOps += v
}
for _, v := range busyChunks {
chunkAvg += v
}
chunkAvg /= float64(len(busyChunks))
stopB := before["/sched/pauses/stopping/gc:seconds"]
stopA := afterHist["/sched/pauses/stopping/gc:seconds"]
totB := before["/sched/pauses/total/gc:seconds"]
totA := afterHist["/sched/pauses/total/gc:seconds"]
latB := before["/sched/latencies:seconds"]
latA := afterHist["/sched/latencies:seconds"]
fmt.Printf("busy-Miters/s %.3f\n", float64(totalBusyIters)/el/1e6)
fmt.Printf("gc-us/req %.6f\n",gcCPU*1e6/float64(totalAllocOps))
fmt.Printf("alloc-op/s %.0f\n", float64(totalAllocOps)/el)
fmt.Printf("chunk-us %.1f\n", chunkAvg)
fmt.Printf("gc-cycles %.0f\n", gcCycles)
fmt.Printf("gc-cpu-s %.3f\n", gcCPU)
fmt.Printf("gc-cpu-frac %.4f\n", gcCPU/totalCPU)
fmt.Printf("assist-cpu-s %.3f\n", assistCPU)
fmt.Printf("pause-stop-p50-us %.3f\n", histQuant(stopB, stopA, 0.50))
fmt.Printf("pause-stop-p99-us %.3f\n", histQuant(stopB, stopA, 0.99))
fmt.Printf("pause-stop-max-us %.3f\n", histMax(stopB, stopA))
fmt.Printf("pause-total-p50-us %.3f\n", histQuant(totB, totA, 0.50))
fmt.Printf("pause-total-p99-us %.3f\n", histQuant(totB, totA, 0.99))
fmt.Printf("pause-total-max-us %.3f\n", histMax(totB, totA))
fmt.Printf("schedlat-p50-us %.3f\n", histQuant(latB, latA, 0.50))
fmt.Printf("schedlat-p99-us %.3f\n", histQuant(latB, latA, 0.99))
fmt.Printf("schedlat-max-us %.3f\n", histMax(latB, latA))
fmt.Printf("live-heap-B %.0f\n", liveHeap)
fmt.Printf("alloc-B/s %.0f\n", allocBytes/el)
fmt.Printf("alloc-obj/s %.0f\n", allocObjs/el)
}
func beforeVal(s []metrics.Sample, name string) float64 {
for i := range s {
if s[i].Name == name {
return s[i].Value.Float64()
}
}
return 0
}
共用采集 · metrics.go
// Copy into a staged workload package and defer bs2Metrics()() around the workload.
package main
import (
"fmt"
"math"
"os"
"runtime/metrics"
"runtime/pprof"
)
func bs2Metrics() func() {
s := []metrics.Sample{
{Name: "/cpu/classes/gc/total:cpu-seconds"},
{Name: "/gc/cycles/total:gc-cycles"},
{Name: "/gc/scan/heap:bytes"},
{Name: "/gc/scan/stack:bytes"},
{Name: "/gc/heap/live:bytes"},
{Name: "/sched/pauses/stopping/gc:seconds"},
{Name: "/sched/pauses/total/gc:seconds"},
}
var profile *os.File
if name := os.Getenv("BS2_CPUPROFILE"); name != "" {
var err error
profile, err = os.Create(name)
if err != nil {
panic(err)
}
if err = pprof.StartCPUProfile(profile); err != nil {
panic(err)
}
}
metrics.Read(s)
cpu, cycles := s[0].Value.Float64(), s[1].Value.Uint64()
var before [2][]uint64
for i := range before {
before[i] = append([]uint64(nil), s[5+i].Value.Float64Histogram().Counts...)
}
return func() {
if profile != nil {
defer profile.Close()
defer pprof.StopCPUProfile()
}
metrics.Read(s)
fmt.Printf("bs2-gc-cpu-s %.9f\nbs2-gc-cycles %d\n", s[0].Value.Float64()-cpu, s[1].Value.Uint64()-cycles)
for i, label := range []string{"heap-scan", "stack-scan", "live-heap"} {
fmt.Printf("bs2-%s-B %d\n", label, s[2+i].Value.Uint64())
}
for i, label := range []string{"stopping", "total"} {
h := s[5+i].Value.Float64Histogram()
var count uint64
for j, n := range h.Counts {
count += n - before[i][j]
}
for _, q := range []int{50, 99, 100} {
value := math.NaN()
var sum uint64
for j, n := range h.Counts {
sum += n - before[i][j]
if count > 0 && sum*100 >= count*uint64(q) {
value = h.Buckets[j+1] * 1e6
break
}
}
fmt.Printf("bs2-%s-p%d-upper-us %.3f\n", label, q, value)
}
}
}
}