forked from Dirbaio/gominer
-
Notifications
You must be signed in to change notification settings - Fork 80
/
main.go
100 lines (89 loc) · 2.03 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"net"
"net/http"
"os"
"runtime"
"runtime/pprof"
"time"
)
var (
cfg *config
)
func gominerMain() error {
// Load configuration and parse command line. This function also
// initializes logging and configures it accordingly.
tcfg, _, err := loadConfig()
if err != nil {
return err
}
cfg = tcfg
defer func() {
if logRotator != nil {
logRotator.Close()
}
}()
// Show version at startup.
mainLog.Infof("Version %s %s (Go version %s %s/%s)", Version, gpuLib(),
runtime.Version(), runtime.GOOS, runtime.GOARCH)
// Enable http profiling server if requested.
if cfg.Profile != "" {
go func() {
listenAddr := net.JoinHostPort("", cfg.Profile)
mainLog.Infof("Creating profiling server "+
"listening on %s", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof",
http.StatusSeeOther)
http.Handle("/", profileRedirect)
err := http.ListenAndServe(listenAddr, nil)
if err != nil {
mainLog.Errorf("Unable to create profiler: %v", err)
os.Exit(1)
}
}()
}
// Write cpu profile if requested.
if cfg.CPUProfile != "" {
f, err := os.Create(cfg.CPUProfile)
if err != nil {
mainLog.Errorf("Unable to create cpu profile: %v", err)
return err
}
pprof.StartCPUProfile(f)
defer f.Close()
defer pprof.StopCPUProfile()
}
// Write mem profile if requested.
if cfg.MemProfile != "" {
f, err := os.Create(cfg.MemProfile)
if err != nil {
mainLog.Errorf("Unable to create cpu profile: %v", err)
return err
}
timer := time.NewTimer(time.Minute * 20) // 20 minutes
go func() {
<-timer.C
pprof.WriteHeapProfile(f)
f.Close()
}()
}
ctx := shutdownListener()
m, err := NewMiner(ctx)
if err != nil {
mainLog.Criticalf("Error initializing miner: %v", err)
return err
}
if len(cfg.APIListeners) != 0 {
go RunMonitor(m)
}
m.Run(ctx)
return nil
}
func main() {
// Use all processor cores.
runtime.GOMAXPROCS(runtime.NumCPU())
// Work around defer not working after os.Exit()
if err := gominerMain(); err != nil {
os.Exit(1)
}
}