-
Notifications
You must be signed in to change notification settings - Fork 19
/
profiling.go
61 lines (53 loc) · 1.4 KB
/
profiling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2015 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements the profiler support.
package golisp
import (
"fmt"
"os"
"time"
)
var profileOutput *os.File = nil
var ProfileEnabled = false
var ProfileGUID int64 = 0
func StartProfiling(fname string) {
ProfileGUID = 0
if fname == "" {
profileOutput = nil
} else {
var err error
profileOutput, err = os.Create(fname)
if err != nil {
panic(fmt.Sprintf("Profiler: %s could not be opened.", fname))
}
}
ProfileEnabled = true
}
func EndProfiling() {
ProfileEnabled = false
if profileOutput != nil {
profileOutput.Close()
}
}
func ProfileEnter(funcType string, name string, guid int64) {
if ProfileEnabled {
msg := fmt.Sprintf("{time: %d guid: %d mode: 'enter type: '%s name: '%s}\n", time.Now().UnixNano(), guid, funcType, name)
if profileOutput == nil {
fmt.Printf(msg)
} else {
fmt.Fprintf(profileOutput, msg)
}
}
}
func ProfileExit(funcType string, name string, guid int64) {
if ProfileEnabled {
msg := fmt.Sprintf("{time: %d guid: %d mode: 'exit type: '%s name: '%s}\n", time.Now().UnixNano(), guid, funcType, name)
if profileOutput == nil {
fmt.Printf(msg)
} else {
fmt.Fprintf(profileOutput, msg)
}
}
}