Memory profiling code. Updates to reduce memory footprint.
This commit is contained in:
+293
-46
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"git.wow.st/gmp/giowrap"
|
||||
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
@@ -27,12 +28,13 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
FPS int = 100
|
||||
frames int64
|
||||
frametime int64 // nanoseconds
|
||||
maxframetime int64
|
||||
mux sync.Mutex
|
||||
Profile bool = false
|
||||
FPS int = 100
|
||||
frames int64
|
||||
frametime int64 // nanoseconds
|
||||
maxframetime int64
|
||||
mux sync.Mutex
|
||||
Profile bool = true
|
||||
RecordMallocs bool = false
|
||||
)
|
||||
|
||||
func NewButton(face text.Face, t string, c color.RGBA) giowrap.Clickable {
|
||||
@@ -69,7 +71,7 @@ func main() {
|
||||
fallthrough
|
||||
default:
|
||||
log.Fatal(`Usage:
|
||||
hello [main1|main2]
|
||||
hello [main1|main2|main3|main4]
|
||||
`)
|
||||
case os.Args[1] == "main1":
|
||||
log.Print("main1()")
|
||||
@@ -77,10 +79,17 @@ hello [main1|main2]
|
||||
case os.Args[1] == "main2":
|
||||
log.Print("main2()")
|
||||
go main2()
|
||||
case os.Args[1] == "main3":
|
||||
log.Print("main3()")
|
||||
go main3()
|
||||
case os.Args[1] == "main4":
|
||||
log.Print("main4()")
|
||||
go main4()
|
||||
}
|
||||
app.Main()
|
||||
}
|
||||
|
||||
// Example with functional layout
|
||||
func main1() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
@@ -108,17 +117,156 @@ func main1() {
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
page := OuterInset(
|
||||
f1(
|
||||
e1,
|
||||
giowrap.Flexible(0.33),
|
||||
InnerInset(e2),
|
||||
giowrap.Flexible(0.67),
|
||||
f2(
|
||||
InnerInset(btn1),
|
||||
giowrap.Flexible(0.50),
|
||||
InnerInset(btn2),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
mux.Lock()
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
startTime = time.Now()
|
||||
profileNow = true
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
giowrap.Mallocs("pre-reset")
|
||||
ctx.Reset(&e)
|
||||
giowrap.Mallocs("pre-layout")
|
||||
page.Layout(ctx)
|
||||
giowrap.Mallocs("post-layout")
|
||||
ctx.Update()
|
||||
giowrap.Mallocs("post-update")
|
||||
if btn1.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e1.Text())
|
||||
}
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
giowrap.Mallocs("post-clicked()")
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func prof(x int) {
|
||||
names := []string{"memprofile-1.pprof", "memprofile-2.pprof"}
|
||||
f1, err := os.Create(names[x])
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f1); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
f1.Close()
|
||||
log.Print("Memory profile written")
|
||||
}
|
||||
|
||||
// Example showing use of WidgetCombinators in the main loop.
|
||||
func main2() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := giowrap.NewContext(w)
|
||||
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||
e1 := giowrap.NewEditor("text 1", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
e1.Focus()
|
||||
|
||||
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
|
||||
f1 := giowrap.NewFlex(giowrap.Axis(layout.Vertical))
|
||||
OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
InnerInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
|
||||
f2 := giowrap.NewFlex(giowrap.Axis(layout.Horizontal))
|
||||
|
||||
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
btn2 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push2", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
mux.Lock()
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
startTime = time.Now()
|
||||
profileNow = true
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
giowrap.Mallocs("pre-reset")
|
||||
ctx.Reset(&e)
|
||||
giowrap.Mallocs("pre-layout")
|
||||
OuterInset(
|
||||
f1(
|
||||
e1,
|
||||
@@ -127,9 +275,107 @@ func main1() {
|
||||
giowrap.Flexible(0.67),
|
||||
f2(
|
||||
InnerInset(btn1),
|
||||
giowrap.Flexible(0.5),
|
||||
giowrap.Flexible(0.50),
|
||||
InnerInset(btn2),
|
||||
),
|
||||
),
|
||||
).Layout(ctx)
|
||||
giowrap.Mallocs("post-layout")
|
||||
ctx.Update()
|
||||
giowrap.Mallocs("post-update")
|
||||
if btn1.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e1.Text())
|
||||
}
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
giowrap.Mallocs("post-clicked()")
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Example showing widget allocation within the main loop (causes unnecessary
|
||||
// memory allocations)
|
||||
func main3() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := giowrap.NewContext(w)
|
||||
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||
e1 := giowrap.NewEditor("text 1", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
e1.Focus()
|
||||
|
||||
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
|
||||
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
btn2 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push2", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
profileNow = true
|
||||
startTime = time.Now()
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
ctx.Reset(&e)
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(
|
||||
giowrap.NewFlex(giowrap.Axis(layout.Vertical))(
|
||||
e1,
|
||||
//giowrap.Flexible(0.33),
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(e2),
|
||||
//giowrap.Flexible(0.67),
|
||||
giowrap.NewFlex(giowrap.Axis(layout.Horizontal))(
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(btn1),
|
||||
//giowrap.Flexible(0.50),
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(btn2),
|
||||
),
|
||||
)).Layout(ctx)
|
||||
ctx.Update()
|
||||
if btn1.Clicked(ctx) {
|
||||
@@ -138,6 +384,20 @@ func main1() {
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
mux.Lock()
|
||||
@@ -148,28 +408,11 @@ func main1() {
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
if profiled {
|
||||
continue
|
||||
}
|
||||
if time.Since(startTime) < time.Second*10 {
|
||||
continue
|
||||
}
|
||||
if Profile {
|
||||
profiled = true
|
||||
f, err := os.Create("memprofile.pprof")
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
log.Print("Memory profile written")
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main2() {
|
||||
// Example using the base Gio API.
|
||||
func main4() {
|
||||
w := app.NewWindow()
|
||||
q := w.Queue()
|
||||
ops := new(ui.Ops)
|
||||
@@ -215,6 +458,18 @@ func main2() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
profileNow = true
|
||||
startTime = time.Now()
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
}
|
||||
c := &e.Config
|
||||
ops.Reset()
|
||||
faces.Reset(c)
|
||||
@@ -257,6 +512,16 @@ func main2() {
|
||||
dims = ins.End(f1.Layout(c1, c2, c3))
|
||||
}
|
||||
w.Update(ops)
|
||||
if profileNow {
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
mux.Lock()
|
||||
@@ -267,24 +532,6 @@ func main2() {
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
if profiled {
|
||||
continue
|
||||
}
|
||||
if time.Since(startTime) < time.Second*10 {
|
||||
continue
|
||||
}
|
||||
if Profile {
|
||||
profiled = true
|
||||
f, err := os.Create("memprofile.pprof")
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
log.Print("Memory profile written")
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user