Skip to content

Commit bafef75

Browse files
committed
Don't print stuff, only return strings.
1 parent b7ca2e9 commit bafef75

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

z/allocator.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package z
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"math"
2223
"math/bits"
@@ -92,18 +93,21 @@ func (a *Allocator) Reset() {
9293
atomic.StoreUint64(&a.compIdx, 0)
9394
}
9495

95-
func PrintAllocators() {
96+
func Allocators() string {
9697
allocsMu.Lock()
9798
tags := make(map[string]uint64)
9899
num := make(map[string]int)
99100
for _, ac := range allocs {
100101
tags[ac.Tag] += ac.Allocated()
101102
num[ac.Tag] += 1
102103
}
104+
105+
var buf bytes.Buffer
103106
for tag, sz := range tags {
104-
fmt.Printf("Allocator Tag: %s Num: %d Size: %s\n", tag, num[tag], humanize.IBytes(sz))
107+
fmt.Fprintf(&buf, "Tag: %s Num: %d Size: %s . ", tag, num[tag], humanize.IBytes(sz))
105108
}
106109
allocsMu.Unlock()
110+
return buf.String()
107111
}
108112

109113
func (a *Allocator) String() string {

z/calloc_jemalloc.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ package z
1313
*/
1414
import "C"
1515
import (
16+
"bytes"
1617
"fmt"
1718
"runtime"
19+
"strconv"
1820
"strings"
1921
"sync"
2022
"sync/atomic"
2123
"unsafe"
24+
25+
"github.com/dustin/go-humanize"
2226
)
2327

2428
// The go:linkname directives provides backdoor access to private functions in
@@ -49,6 +53,11 @@ type dalloc struct {
4953
var dallocsMu sync.Mutex
5054
var dallocs map[unsafe.Pointer]*dalloc
5155

56+
func init() {
57+
// By initializing dallocs, we can start tracking allocations and deallocations via z.Calloc.
58+
dallocs = make(map[unsafe.Pointer]*dalloc)
59+
}
60+
5261
func Calloc(n int) []byte {
5362
if n == 0 {
5463
return make([]byte, 0)
@@ -123,20 +132,25 @@ func Free(b []byte) {
123132
}
124133
}
125134

126-
func PrintLeaks() {
135+
func Leaks() string {
127136
if dallocs == nil {
128-
fmt.Println("Leak detection disabled. Enable with 'leak' build flag.")
129-
return
137+
return "Leak detection disabled. Enable with 'leak' build flag."
130138
}
131139
dallocsMu.Lock()
132140
defer dallocsMu.Unlock()
133141
if len(dallocs) == 0 {
134-
fmt.Println("NO leaks found.")
135-
return
142+
return "NO leaks found."
136143
}
144+
m := make(map[string]int)
137145
for _, da := range dallocs {
138-
fmt.Printf("LEAK: %d at file: %s %d\n", da.sz, da.f, da.no)
146+
m[da.f+":"+strconv.Itoa(da.no)] += da.sz
147+
}
148+
var buf bytes.Buffer
149+
fmt.Fprintf(&buf, "Allocations:\n")
150+
for f, sz := range m {
151+
fmt.Fprintf(&buf, "%s at file: %s\n", humanize.IBytes(uint64(sz)), f)
139152
}
153+
return buf.String()
140154
}
141155

142156
// ReadMemStats populates stats with JE Malloc statistics.

z/calloc_leak.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

z/calloc_nojemalloc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func CallocNoRef(n int) []byte {
2727
// Free does not do anything in this mode.
2828
func Free(b []byte) {}
2929

30-
func PrintLeaks() {}
30+
func Leaks() string { return "Leaks: Using Go memory" }
3131
func StatsPrint() {
3232
fmt.Println("Using Go memory")
3333
}

z/calloc_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package z
1818

1919
import (
20+
"fmt"
2021
"sync"
2122
"testing"
2223
"time"
@@ -98,7 +99,7 @@ func TestCalloc(t *testing.T) {
9899
// _ = buf2
99100
Free(buf2)
100101
require.Equal(t, int64(0), NumAllocBytes())
101-
PrintLeaks()
102+
fmt.Println(Leaks())
102103

103104
// Double free would panic when debug mode is enabled in jemalloc.
104105
// Free(buf2)

0 commit comments

Comments
 (0)