1// Copyright 2010 The Go Authors.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"runtime"
10	"sort"
11	"time"
12)
13
14func gcstats(name string, n int, t time.Duration) {
15	st := new(runtime.MemStats)
16	runtime.ReadMemStats(st)
17	nprocs := runtime.GOMAXPROCS(-1)
18	cpus := ""
19	if nprocs != 1 {
20		cpus = fmt.Sprintf("-%d", nprocs)
21	}
22	fmt.Printf("garbage.%sMem%s Alloc=%d/%d Heap=%d NextGC=%d Mallocs=%d\n", name, cpus, st.Alloc, st.TotalAlloc, st.Sys, st.NextGC, st.Mallocs)
23	fmt.Printf("garbage.%s%s %d %d ns/op\n", name, cpus, n, t.Nanoseconds()/int64(n))
24	fmt.Printf("garbage.%sLastPause%s 1 %d ns/op\n", name, cpus, st.PauseNs[(st.NumGC-1)%uint32(len(st.PauseNs))])
25	fmt.Printf("garbage.%sPause%s %d %d ns/op\n", name, cpus, st.NumGC, int64(st.PauseTotalNs)/int64(st.NumGC))
26	nn := int(st.NumGC)
27	if nn >= len(st.PauseNs) {
28		nn = len(st.PauseNs)
29	}
30	t1, t2, t3, t4, t5 := tukey5(st.PauseNs[0:nn])
31	fmt.Printf("garbage.%sPause5%s: %d %d %d %d %d\n", name, cpus, t1, t2, t3, t4, t5)
32
33	//	fmt.Printf("garbage.%sScan: %v\n", name, st.ScanDist)
34}
35
36type T []uint64
37
38func (t T) Len() int           { return len(t) }
39func (t T) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }
40func (t T) Less(i, j int) bool { return t[i] < t[j] }
41
42func tukey5(raw []uint64) (lo, q1, q2, q3, hi uint64) {
43	x := make(T, len(raw))
44	copy(x, raw)
45	sort.Sort(T(x))
46	lo = x[0]
47	q1 = x[len(x)/4]
48	q2 = x[len(x)/2]
49	q3 = x[len(x)*3/4]
50	hi = x[len(x)-1]
51	return
52}
53