-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (178 loc) · 3.53 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"github.com/pnsafonov/pind/pkg"
"github.com/pnsafonov/pind/pkg/numa"
log "github.com/sirupsen/logrus"
"os"
"strings"
)
var (
version = dev
commit = "none"
date = "unknown"
builtBy = "manual"
)
func main() {
doMain(os.Args)
}
func doMain(args []string) {
configPath := pkg.ConfPathBuiltIn
service0 := false
printConf0 := false
pkg.InitConsoleLogger()
l0 := len(args)
for i := 1; i < l0; i++ {
arg := args[i]
switch arg {
case "-v", "--version":
{
printVersion()
}
case "--version-only":
{
printVersionOnly()
}
case "-h", "--help":
{
printHelp()
}
case "--print-numa":
{
printNuma()
}
case "--print-numa-phys":
{
printNumaPhys()
}
case "--print-topology":
{
printTopology()
}
case "--print-procs":
{
patterns := ""
i0 := i + 1
if i0 < l0 {
patterns = args[i0]
}
printProcs1(patterns)
}
case "-c", "--config":
{
i0 := i + 1
if i0 < l0 {
configPath = args[i0]
}
i++
continue
}
case "-s", "--service", "--daemon":
{
service0 = true
continue
}
case "--print-conf":
{
printConf0 = true
continue
}
case "--print-name":
{
printName()
continue
}
}
}
version0, gitHash := GetVersion0()
ctx := pkg.NewContext(version0, gitHash, service0)
ctx.Service = service0
ctx.ConfigPath = configPath
ctx.PrintConfig = printConf0
if printConf0 {
printConf(ctx)
}
if service0 {
runService(ctx)
}
// default action
printNumaPhys()
}
func printHelp() {
helpMsg := `Usage: pind [OPTIONS]
pin programs to CPU (affinity)
-s, --service, --daemon run service (daemon)
-c, --config config file location, default is /etc/pind/pind.conf
-h, --help display this help and exit
-v, --version output version information and exit
--version-only print numeric version information and exit
--print-procs "pattern0,pattern1" print process filtered by ps aux | grep pattern0 | grep pattern1
--print-numa print information about numa and exit
--print-numa-phys print information about numa topology and exit
--print-topology print information about cpu's topology
--print-conf print config file content
--print-name print pind and exit`
fmt.Println(helpMsg)
os.Exit(0)
}
func printVersion() {
version0, gitHash := GetVersion0()
fmt.Printf("pind version %s %s\n", version0, gitHash)
os.Exit(0)
}
func printVersionOnly() {
version0 := GetVersion()
fmt.Println(version0)
os.Exit(0)
}
func printNuma() {
err := numa.PrintNuma0()
exit0(err)
}
func printName() {
fmt.Println("pind")
os.Exit(0)
}
func printNumaPhys() {
err := numa.PrintNumaPhys0()
exit0(err)
}
func printTopology() {
err := numa.PrintTopology()
exit0(err)
}
func printProcs1(pattern string) {
var err error
if pattern != "" {
patterns := strings.Split(pattern, ",")
err = pkg.PrintProcs1(patterns)
} else {
err = pkg.PrintProcs2()
}
exit1(err)
}
func exit0(err error) {
if err == nil {
os.Exit(0)
}
os.Exit(1)
}
func exitMoreArgs() {
log.Errorf("Please specify more args.")
os.Exit(1)
}
func exit1(err error) {
if err == nil {
os.Exit(0)
}
log.Errorf("err = %v", err)
os.Exit(1)
}
func runService(ctx *pkg.Context) {
err := pkg.RunService(ctx)
exit1(err)
}
func printConf(ctx *pkg.Context) {
err := pkg.PrintConf0(ctx)
exit1(err)
}