Memory profiling code. Updates to reduce memory footprint.
This commit is contained in:
+1
-1
@@ -152,7 +152,7 @@ func eventloop() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
ctx.Reset(&e)
|
||||
|
||||
mth.SetText(fmt.Sprintf("%s %d", sm.Month.String(), sm.Year))
|
||||
|
||||
|
||||
+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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+54
-29
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
gio "git.wow.st/gmp/giowrap"
|
||||
|
||||
@@ -99,18 +101,21 @@ func eventloop() {
|
||||
lbar := gio.NewLabel(" ", gio.Face(face))
|
||||
f2 := gio.NewFlex(gio.Axis(layout.Vertical))
|
||||
topbar := gio.NewLabel("Scroll X and Y. Click to select", gio.Face(face))
|
||||
f3 := gio.NewFlex(gio.Axis(layout.Horizontal))
|
||||
f4 := gio.NewFlex(gio.Axis(layout.Vertical))
|
||||
|
||||
sh := gio.HScroll(gio.NewFlex(gio.Axis(layout.Horizontal)))
|
||||
sv := gio.VScroll(gio.NewFlex(gio.Axis(layout.Vertical)))
|
||||
|
||||
numlabs := 50
|
||||
labs := make([][]gio.Widget, numlabs)
|
||||
sels := make([][]bool, numlabs)
|
||||
for i := 0; i < 16; i++ {
|
||||
labs[i] = make([]gio.Widget, numlabs)
|
||||
sels[i] = make([]bool, numlabs)
|
||||
for j := 0; j < numlabs; j++ {
|
||||
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i*16+j), &sels[i][j])
|
||||
numrows := 16
|
||||
numcols := 50
|
||||
labs := make([][]gio.Widget, numrows)
|
||||
sels := make([][]bool, numrows)
|
||||
for i := 0; i < numrows; i++ {
|
||||
labs[i] = make([]gio.Widget, numcols)
|
||||
sels[i] = make([]bool, numcols)
|
||||
for j := 0; j < numcols; j++ {
|
||||
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i+j*numrows), &sels[i][j])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +125,30 @@ func eventloop() {
|
||||
gio.Left(x.Left), gio.Right(x.Right))
|
||||
}
|
||||
|
||||
page := sysbg(sysinset(bg(margin(
|
||||
f1(lbar, f2(topbar, sh(sv(f3(
|
||||
f4(labs[0]...),
|
||||
f4(labs[1]...),
|
||||
f4(labs[2]...),
|
||||
f4(labs[3]...),
|
||||
f4(labs[4]...),
|
||||
f4(labs[5]...),
|
||||
f4(labs[6]...),
|
||||
f4(labs[7]...),
|
||||
f4(labs[8]...),
|
||||
f4(labs[9]...),
|
||||
f4(labs[10]...),
|
||||
f4(labs[11]...),
|
||||
f4(labs[12]...),
|
||||
f4(labs[13]...),
|
||||
f4(labs[14]...),
|
||||
f4(labs[15]...),
|
||||
)))))))))
|
||||
|
||||
var oldInsets app.Insets
|
||||
|
||||
stime := time.Now()
|
||||
sm := runtime.MemStats{}
|
||||
for {
|
||||
select {
|
||||
case e := <-w.Events():
|
||||
@@ -129,32 +156,30 @@ func eventloop() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
profileNow := time.Since(stime) > time.Second*2
|
||||
if profileNow {
|
||||
stime = time.Now()
|
||||
//gio.RecordMallocs()
|
||||
runtime.ReadMemStats(&sm)
|
||||
}
|
||||
gio.Mallocs("start")
|
||||
ctx.Reset(&e)
|
||||
if diffInsets(e.Insets, oldInsets) {
|
||||
oldInsets = e.Insets
|
||||
resetSysinset(e.Insets)
|
||||
}
|
||||
|
||||
sysbg(sysinset(bg(margin(
|
||||
f1(lbar, f2(topbar, sh(
|
||||
sv(labs[0]...),
|
||||
sv(labs[1]...),
|
||||
sv(labs[2]...),
|
||||
sv(labs[3]...),
|
||||
sv(labs[4]...),
|
||||
sv(labs[5]...),
|
||||
sv(labs[6]...),
|
||||
sv(labs[7]...),
|
||||
sv(labs[8]...),
|
||||
sv(labs[9]...),
|
||||
sv(labs[10]...),
|
||||
sv(labs[11]...),
|
||||
sv(labs[12]...),
|
||||
sv(labs[13]...),
|
||||
sv(labs[14]...),
|
||||
sv(labs[15]...),
|
||||
))))))).Layout(ctx)
|
||||
page.Layout(ctx)
|
||||
ctx.Update()
|
||||
gio.Mallocs("end")
|
||||
if profileNow {
|
||||
for _, m := range gio.AllMallocs {
|
||||
fmt.Printf("Mallocs: %d (%s)\n", m.Value, m.Text)
|
||||
}
|
||||
gio.AllMallocs = nil
|
||||
oldMallocs := sm.Mallocs
|
||||
runtime.ReadMemStats(&sm)
|
||||
fmt.Printf("Mallocs = %d\n", sm.Mallocs-oldMallocs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user