OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [bench/] [garbage/] [tree2.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// Copyright 2012 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
 
5
package main
6
 
7
import (
8
        "flag"
9
        "fmt"
10
        "log"
11
        "os"
12
        "runtime"
13
        "runtime/pprof"
14
        "unsafe"
15
)
16
 
17
const BranchingFactor = 4
18
 
19
type Object struct {
20
        child [BranchingFactor]*Object
21
}
22
 
23
var (
24
        cpus       = flag.Int("cpus", 1, "number of cpus to use")
25
        heapsize   = flag.Int64("heapsize", 100*1024*1024, "size of the heap in bytes")
26
        cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
27
 
28
        lastPauseNs uint64 = 0
29
        lastFree    uint64 = 0
30
        heap        *Object
31
        calls       [20]int
32
        numobjects  int64
33
)
34
 
35
func buildHeap() {
36
        objsize := int64(unsafe.Sizeof(Object{}))
37
        heap, _ = buildTree(float64(objsize), float64(*heapsize), 0)
38
        fmt.Printf("*** built heap: %.0f MB; (%d objects * %d bytes)\n",
39
                float64(*heapsize)/1048576, numobjects, objsize)
40
}
41
 
42
func buildTree(objsize, size float64, depth int) (*Object, float64) {
43
        calls[depth]++
44
        x := &Object{}
45
        numobjects++
46
        subtreeSize := (size - objsize) / BranchingFactor
47
        alloc := objsize
48
        for i := 0; i < BranchingFactor && alloc < size; i++ {
49
                c, n := buildTree(objsize, subtreeSize, depth+1)
50
                x.child[i] = c
51
                alloc += n
52
        }
53
        return x, alloc
54
}
55
 
56
func gc() {
57
        runtime.GC()
58
        runtime.UpdateMemStats()
59
        pause := runtime.MemStats.PauseTotalNs
60
        inuse := runtime.MemStats.Alloc
61
        free := runtime.MemStats.TotalAlloc - inuse
62
        fmt.Printf("gc pause: %8.3f ms; collect: %8.0f MB; heapsize: %8.0f MB\n",
63
                float64(pause-lastPauseNs)/1e6,
64
                float64(free-lastFree)/1048576,
65
                float64(inuse)/1048576)
66
        lastPauseNs = pause
67
        lastFree = free
68
}
69
 
70
func main() {
71
        flag.Parse()
72
        buildHeap()
73
        runtime.GOMAXPROCS(*cpus)
74
        runtime.UpdateMemStats()
75
        lastPauseNs = runtime.MemStats.PauseTotalNs
76
        lastFree = runtime.MemStats.TotalAlloc - runtime.MemStats.Alloc
77
        if *cpuprofile != "" {
78
                f, err := os.Create(*cpuprofile)
79
                if err != nil {
80
                        log.Fatal(err)
81
                }
82
                pprof.StartCPUProfile(f)
83
                defer pprof.StopCPUProfile()
84
        }
85
        for i := 0; i < 10; i++ {
86
                gc()
87
        }
88
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.