基准说明 / 测量源码
gcload 测量源码
这里逐字展示测量所用的源码,保留原有注释、缩进和换行。
历史 GC 矩阵 · main.go
// gcload is a server-shaped GC workload: many goroutines with call stacks that
// hold pointers, request-scoped allocation, JSON encode/decode and a long-lived
// cache. It prints runtime/metrics deltas so GC cost can be split into
// heap scan, stack scan and GC CPU.
package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"runtime"
"os"
"runtime/metrics"
"runtime/pprof"
"strconv"
"sync"
"time"
)
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Attrs map[string]string `json:"attrs"`
Child *Item `json:"child,omitempty"`
}
type cache struct {
mu sync.Mutex
m map[int]*Item
}
func (c *cache) put(it *Item) {
c.mu.Lock()
c.m[it.ID%50000] = it
c.mu.Unlock()
}
func mkItem(r *rand.Rand, depth int) *Item {
it := &Item{ID: r.Int(), Name: "item-" + strconv.Itoa(r.Intn(1000)), Attrs: map[string]string{}}
for i := 0; i < 4; i++ {
it.Tags = append(it.Tags, strconv.Itoa(r.Intn(100)))
it.Attrs["k"+strconv.Itoa(i)] = it.Name
}
if depth > 0 {
it.Child = mkItem(r, depth-1)
}
return it
}
// handle recurses so each goroutine has a deep stack with live pointers in
// every frame, like middleware chains in RPC servers.
//go:noinline
func handle(r *rand.Rand, c *cache, depth int, ctx *Item) int {
if depth == 0 {
it := mkItem(r, 3)
b, _ := json.Marshal(it)
var out Item
_ = json.Unmarshal(b, &out)
c.put(&out)
return len(b) + len(ctx.Name)
}
local := &Item{ID: depth, Name: ctx.Name}
n := handle(r, c, depth-1, local)
return n + local.ID
}
var names = []string{
"/gc/cycles/total:gc-cycles",
"/cpu/classes/gc/total:cpu-seconds",
"/cpu/classes/gc/mark/assist:cpu-seconds",
"/cpu/classes/gc/mark/dedicated:cpu-seconds",
"/cpu/classes/gc/mark/idle:cpu-seconds",
"/cpu/classes/gc/pause:cpu-seconds",
"/cpu/classes/user:cpu-seconds",
"/cpu/classes/total:cpu-seconds",
"/gc/scan/stack:bytes",
"/gc/scan/heap:bytes",
"/gc/scan/globals:bytes",
"/gc/heap/live:bytes",
"/gc/heap/allocs:bytes",
"/gc/heap/allocs:objects",
"/gc/stack/starting-size:bytes",
"/memory/classes/heap/stacks:bytes",
}
func read() map[string]float64 {
s := make([]metrics.Sample, len(names))
for i, n := range names {
s[i].Name = n
}
metrics.Read(s)
m := map[string]float64{}
for _, x := range s {
switch x.Value.Kind() {
case metrics.KindUint64:
m[x.Name] = float64(x.Value.Uint64())
case metrics.KindFloat64:
m[x.Name] = x.Value.Float64()
}
}
return m
}
// sampler records scan bytes of every completed GC cycle, since the
// /gc/scan/* metrics only describe the last cycle.
type sampler struct {
cycles, heap, stack float64
stop chan struct{}
done chan struct{}
}
func startSampler() *sampler {
sp := &sampler{stop: make(chan struct{}), done: make(chan struct{})}
go func() {
defer close(sp.done)
s := []metrics.Sample{{Name: "/gc/cycles/total:gc-cycles"}, {Name: "/gc/scan/heap:bytes"}, {Name: "/gc/scan/stack:bytes"}}
metrics.Read(s)
last := s[0].Value.Uint64()
for {
select {
case <-sp.stop:
return
case <-time.After(200 * time.Microsecond):
}
metrics.Read(s)
if c := s[0].Value.Uint64(); c != last {
last = c
sp.cycles++
sp.heap += float64(s[1].Value.Uint64())
sp.stack += float64(s[2].Value.Uint64())
}
}
}()
return sp
}
func main() {
defer bs2Metrics()()
workers := flag.Int("g", 256, "goroutines")
depth := flag.Int("depth", 32, "stack depth per request")
dur := flag.Duration("d", 10*time.Second, "duration")
cpuprof := flag.String("cpuprofile", "", "write CPU profile to file")
flag.Parse()
if *cpuprof != "" {
f, err := os.Create(*cpuprof)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
c := &cache{m: map[int]*Item{}}
runtime.GC()
before := read()
sp := startSampler()
start := time.Now()
var wg sync.WaitGroup
var mu sync.Mutex
reqs := 0
for w := 0; w < *workers; w++ {
wg.Add(1)
go func(seed int64) {
defer wg.Done()
r := rand.New(rand.NewSource(seed))
n := 0
root := &Item{Name: "root"}
for time.Since(start) < *dur {
handle(r, c, *depth, root)
n++
}
mu.Lock()
reqs += n
mu.Unlock()
}(int64(w))
}
wg.Wait()
el := time.Since(start).Seconds()
after := read()
close(sp.stop)
<-sp.done
d := func(k string) float64 { return after[k] - before[k] }
gcCPU := d("/cpu/classes/gc/total:cpu-seconds")
tot := d("/cpu/classes/total:cpu-seconds")
cycles := d("/gc/cycles/total:gc-cycles")
fmt.Printf("reqs/s %.0f\n", float64(reqs)/el)
fmt.Printf("gc-cycles %.0f\n", cycles)
fmt.Printf("gc-cpu-frac %.4f\n", gcCPU/tot)
fmt.Printf("gc-cpu-s %.3f\n", gcCPU)
fmt.Printf("assist-cpu-s %.3f\n", d("/cpu/classes/gc/mark/assist:cpu-seconds"))
fmt.Printf("stack-scan-B/cycle %.0f\n", sp.stack/sp.cycles)
fmt.Printf("heap-scan-B/cycle %.0f\n", sp.heap/sp.cycles)
fmt.Printf("gc-cpu-ns/scan-KB %.1f\n", gcCPU*1e9/((sp.heap+sp.stack)/1024)*sp.cycles/cycles)
fmt.Printf("live-heap-B %.0f\n", after["/gc/heap/live:bytes"])
fmt.Printf("alloc-B/req %.0f\n", d("/gc/heap/allocs:bytes")/float64(reqs))
fmt.Printf("alloc-obj/req %.1f\n", d("/gc/heap/allocs:objects")/float64(reqs))
fmt.Printf("stacks-B %.0f\n", after["/memory/classes/heap/stacks:bytes"])
fmt.Printf("stack-start-B %.0f\n", after["/gc/stack/starting-size:bytes"])
}
版本对照 · main.go
// gcload is a server-shaped GC workload: many goroutines with call stacks that
// hold pointers, request-scoped allocation, JSON encode/decode and a long-lived
// cache. It prints runtime/metrics deltas so GC cost can be split into
// heap scan, stack scan and GC CPU.
package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"runtime"
"os"
"runtime/metrics"
"runtime/pprof"
"strconv"
"sync"
"time"
)
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Attrs map[string]string `json:"attrs"`
Child *Item `json:"child,omitempty"`
}
type cache struct {
mu sync.Mutex
m map[int]*Item
}
func (c *cache) put(it *Item) {
c.mu.Lock()
c.m[it.ID%50000] = it
c.mu.Unlock()
}
func mkItem(r *rand.Rand, depth int) *Item {
it := &Item{ID: r.Int(), Name: "item-" + strconv.Itoa(r.Intn(1000)), Attrs: map[string]string{}}
for i := 0; i < 4; i++ {
it.Tags = append(it.Tags, strconv.Itoa(r.Intn(100)))
it.Attrs["k"+strconv.Itoa(i)] = it.Name
}
if depth > 0 {
it.Child = mkItem(r, depth-1)
}
return it
}
// handle recurses so each goroutine has a deep stack with live pointers in
// every frame, like middleware chains in RPC servers.
//go:noinline
func handle(r *rand.Rand, c *cache, depth int, ctx *Item) int {
if depth == 0 {
it := mkItem(r, 3)
b, _ := json.Marshal(it)
var out Item
_ = json.Unmarshal(b, &out)
c.put(&out)
return len(b) + len(ctx.Name)
}
local := &Item{ID: depth, Name: ctx.Name}
n := handle(r, c, depth-1, local)
return n + local.ID
}
var names = []string{
"/gc/cycles/total:gc-cycles",
"/cpu/classes/gc/total:cpu-seconds",
"/cpu/classes/gc/mark/assist:cpu-seconds",
"/cpu/classes/gc/mark/dedicated:cpu-seconds",
"/cpu/classes/gc/mark/idle:cpu-seconds",
"/cpu/classes/gc/pause:cpu-seconds",
"/cpu/classes/user:cpu-seconds",
"/cpu/classes/total:cpu-seconds",
"/gc/scan/stack:bytes",
"/gc/scan/heap:bytes",
"/gc/scan/globals:bytes",
"/gc/heap/live:bytes",
"/gc/heap/allocs:bytes",
"/gc/heap/allocs:objects",
"/gc/stack/starting-size:bytes",
"/memory/classes/heap/stacks:bytes",
}
func read() map[string]float64 {
s := make([]metrics.Sample, len(names))
for i, n := range names {
s[i].Name = n
}
metrics.Read(s)
m := map[string]float64{}
for _, x := range s {
switch x.Value.Kind() {
case metrics.KindUint64:
m[x.Name] = float64(x.Value.Uint64())
case metrics.KindFloat64:
m[x.Name] = x.Value.Float64()
}
}
return m
}
// sampler records scan bytes of every completed GC cycle, since the
// /gc/scan/* metrics only describe the last cycle.
type sampler struct {
cycles, heap, stack float64
stop chan struct{}
done chan struct{}
}
func startSampler() *sampler {
sp := &sampler{stop: make(chan struct{}), done: make(chan struct{})}
go func() {
defer close(sp.done)
s := []metrics.Sample{{Name: "/gc/cycles/total:gc-cycles"}, {Name: "/gc/scan/heap:bytes"}, {Name: "/gc/scan/stack:bytes"}}
metrics.Read(s)
last := s[0].Value.Uint64()
for {
select {
case <-sp.stop:
return
case <-time.After(200 * time.Microsecond):
}
metrics.Read(s)
if c := s[0].Value.Uint64(); c != last {
last = c
sp.cycles++
sp.heap += float64(s[1].Value.Uint64())
sp.stack += float64(s[2].Value.Uint64())
}
}
}()
return sp
}
func main() {
defer bs2Metrics()()
workers := flag.Int("g", 256, "goroutines")
depth := flag.Int("depth", 32, "stack depth per request")
dur := flag.Duration("d", 10*time.Second, "duration")
cpuprof := flag.String("cpuprofile", "", "write CPU profile to file")
flag.Parse()
if *cpuprof != "" {
f, err := os.Create(*cpuprof)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
c := &cache{m: map[int]*Item{}}
runtime.GC()
before := read()
sp := startSampler()
start := time.Now()
var wg sync.WaitGroup
var mu sync.Mutex
reqs := 0
for w := 0; w < *workers; w++ {
wg.Add(1)
go func(seed int64) {
defer wg.Done()
r := rand.New(rand.NewSource(seed))
n := 0
root := &Item{Name: "root"}
for time.Since(start) < *dur {
handle(r, c, *depth, root)
n++
}
mu.Lock()
reqs += n
mu.Unlock()
}(int64(w))
}
wg.Wait()
el := time.Since(start).Seconds()
after := read()
close(sp.stop)
<-sp.done
d := func(k string) float64 { return after[k] - before[k] }
gcCPU := d("/cpu/classes/gc/total:cpu-seconds")
tot := d("/cpu/classes/total:cpu-seconds")
cycles := d("/gc/cycles/total:gc-cycles")
fmt.Printf("gc-us/req %.6f\n",gcCPU*1e6/float64(reqs))
fmt.Printf("reqs/s %.0f\n", float64(reqs)/el)
fmt.Printf("gc-cycles %.0f\n", cycles)
fmt.Printf("gc-cpu-frac %.4f\n", gcCPU/tot)
fmt.Printf("gc-cpu-s %.3f\n", gcCPU)
fmt.Printf("assist-cpu-s %.3f\n", d("/cpu/classes/gc/mark/assist:cpu-seconds"))
fmt.Printf("stack-scan-B/cycle %.0f\n", sp.stack/sp.cycles)
fmt.Printf("heap-scan-B/cycle %.0f\n", sp.heap/sp.cycles)
fmt.Printf("gc-cpu-ns/scan-KB %.1f\n", gcCPU*1e9/((sp.heap+sp.stack)/1024)*sp.cycles/cycles)
fmt.Printf("live-heap-B %.0f\n", after["/gc/heap/live:bytes"])
fmt.Printf("alloc-B/req %.0f\n", d("/gc/heap/allocs:bytes")/float64(reqs))
fmt.Printf("alloc-obj/req %.1f\n", d("/gc/heap/allocs:objects")/float64(reqs))
fmt.Printf("stacks-B %.0f\n", after["/memory/classes/heap/stacks:bytes"])
fmt.Printf("stack-start-B %.0f\n", after["/gc/stack/starting-size:bytes"])
}
共用采集 · 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)
}
}
}
}