forked from decred/gominer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (89 loc) · 2.01 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
101
102
package main
import (
"net"
"net/http"
"os"
"os/signal"
"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 backendLog.Flush()
// Show version at startup.
mainLog.Infof("Version %s %s", version(), gpuLib())
// 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)
backendLog.Flush()
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.Error())
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()
}()
}
m, err := NewMiner()
if err != nil {
mainLog.Criticalf("Error initializing miner: %v", err)
return err
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
mainLog.Warn("Got Control+C, exiting...")
m.Stop()
}()
m.Run()
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)
}
}