Skip to content

Commit

Permalink
runtime/pprof: include example uses of pprof with PMU.
Browse files Browse the repository at this point in the history
Add four examples loop fusion, matrix transpose, concurrent gorotuines, and a weighted serial
execution of functions to demonstrate profiling use cases with different metrics.
  • Loading branch information
chabbimilind committed Apr 21, 2021
1 parent e9ad5de commit 045b8fb
Show file tree
Hide file tree
Showing 10 changed files with 1,021 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/net/http/pprof/examples/fusion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// +build ignore

package main

import (
"log"
_ "math/rand"
"net/http"
_ "net/http/pprof"
)

const N = (1 << 20)

var a []float32
var b []float32
var c []float32
var d []float32

func init() {
a = make([]float32, N)
b = make([]float32, N)
c = make([]float32, N)
d = make([]float32, N)
// for i := 0; i < N; i++ {
// a[i] = rand.Float32()
// b[i] = rand.Float32()
// }
}

//go:noinline
func addSub() {
for i := 0; i < N; i++ {
c[i] = b[i] + c[i]
}
for i := 0; i < N; i++ {
d[i] = b[i] - c[i]
}
}

//go:noinline
func addSubFuse() {
for i := 0; i < N; i++ {
c[i] = b[i] + c[i]
d[i] = b[i] - c[i]
}
}

func run() error {
addSub()
addSubFuse()
return nil
}

func main() {
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
for {
if err := run(); err != nil {
log.Fatal(err)
}
}
}
190 changes: 190 additions & 0 deletions src/net/http/pprof/examples/goroutine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// +build ignore

package main

import (
"log"
"net/http"
_ "net/http/pprof"
"sync"
)

var wg sync.WaitGroup

//go:noinline
func f1() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f2() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f3() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f4() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f5() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f6() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f7() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f8() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f9() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func f10() int {
defer wg.Done()

var sum int
for i := 0; i < 500000000; i++ {
sum -= i / 2
sum *= i
sum /= i/3 + 1
sum -= i / 4
}

return sum
}

//go:noinline
func run() error {
wg.Add(10)
defer wg.Wait()

go f1()
go f2()
go f3()
go f4()
go f5()
go f6()
go f7()
go f8()
go f9()
go f10()

return nil
}

func main() {
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
for {
if err := run(); err != nil {
log.Fatal(err)
}
}
}
28 changes: 28 additions & 0 deletions src/net/http/pprof/examples/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set -ex
#GOROOT=<set me>
#PATH=<set me>
#if needed and supported
#GO_PPROF_PRECISION=3

HOST=localhost
PORT=6060
L1MISS="r08d1"

for f in fusion.go goroutine.go serial.go transpose.go
do
name=`basename $f .go`
go run $f &
pid=$!
sleep 2
# simple timer profile
curl -o ${name}.timer.prof ${HOST}:${PORT}/debug/pprof/profile?seconds=10
# CPU cycles @ 10M
curl -o ${name}.cycles.prof ${HOST}:${PORT}/debug/pprof/profile?event=cycles\&period=10000000\&seconds=20
# CPU instructions @ 1M
curl -o ${name}.ins.prof ${HOST}:${PORT}/debug/pprof/profile?event=instructions\&period=1000000\&seconds=20
# CPU L1 D-cache miss @ 10000
curl -o ${name}.l1miss.prof ${HOST}:${PORT}/debug/pprof/profile?event=${L1MISS}\&period=10000\&seconds=20
# CPU LLC cache miss @ 10000
curl -o ${name}.llcmiss.prof ${HOST}:${PORT}/debug/pprof/profile?event=cacheMisses\&period=10000\&seconds=20
killall $name || "cannot kill"
done
Loading

0 comments on commit 045b8fb

Please sign in to comment.