-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
176 lines (162 loc) · 4.18 KB
/
server.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
package main
import (
"flag"
pb "gpu-rpc/proto"
"log"
"net"
"os"
"time"
nvml "github.com/mindprince/gonvml"
grpc "google.golang.org/grpc"
)
const defaultSocket = "/var/lib/kubelet/gpu.sock"
type GPUInfoServer struct {
socket string
server *grpc.Server
}
func NewGPUInfoServer(s string) *GPUInfoServer {
return &GPUInfoServer{
socket: s,
}
}
func getGPUMemoryCapacityAndUsed() ([]int64, []int64, error) {
cap := make([]int64, 0)
used := make([]int64, 0)
if err := nvml.Initialize(); err != nil {
log.Printf("Can't initialize NVML: %v \n", err)
return cap, used, err
}
n, err := nvml.DeviceCount()
if err != nil {
log.Printf("Failed to fetch Can't initialize NVML: %v \n", err)
return cap, used, err
}
cap = make([]int64, n)
used = make([]int64, n)
for i := uint(0); i < n; i++ {
d, err := nvml.DeviceHandleByIndex(i)
if err != nil {
log.Printf("Failed to get device(%d): %v", i, err)
return cap, used, err
}
t, u, err := d.MemoryInfo()
if err != nil {
log.Printf("Failed to get device(%d) GPU info: %v", i, err)
return cap, used, err
}
cap[i] = int64(t) / (1024 * 1024)
used[i] = int64(u) / (1024 * 1024)
}
return cap, used, nil
}
func (s *GPUInfoServer) GetGPUMemoryCapacityAndUsed(rect *pb.Request, stream pb.GPUInfoService_GetGPUMemoryCapacityAndUsedServer) error {
for {
time.Sleep(4 * time.Second)
cap, used, err := getGPUMemoryCapacityAndUsed()
if err != nil {
log.Printf("Failed to get node GPU memory:%v\n", err)
continue
}
err = stream.Send(&pb.GPUMemoryResponse{Cap: cap, Used: used})
if err != nil {
log.Printf("Failed to send GPU memory response: %v\n", err)
break
}
log.Printf("Cap:%v, Used: %v\n", cap, used)
}
return nil
}
func (s *GPUInfoServer) cleanup() error {
if err := os.Remove(s.socket); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
c, err := grpc.Dial(unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithTimeout(timeout),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
return nil, err
}
return c, nil
}
func (s *GPUInfoServer) Start() error {
err := s.cleanup()
if err != nil {
return err
}
sock, err := net.Listen("unix", s.socket)
if err != nil {
return err
}
s.server = grpc.NewServer([]grpc.ServerOption{}...)
pb.RegisterGPUInfoServiceServer(s.server, s)
go s.server.Serve(sock)
// Wait for server to start by launching a blocking connexion
conn, err := dial(s.socket, 5*time.Second)
if err != nil {
return err
}
conn.Close()
return nil
}
// Stop stops the gRPC server
func (s *GPUInfoServer) Stop() error {
if s.server == nil {
return nil
}
s.server.Stop()
s.server = nil
return s.cleanup()
}
func (s *GPUInfoServer) Serve() error {
err := s.Start()
if err != nil {
log.Printf("Could not start GPUInfoServer: %s\n", err)
s.Stop()
return err
}
log.Println("Starting to serve on", s.socket)
return nil
}
func main() {
var socketPath string
flag.StringVar(&socketPath, "socket", defaultSocket, "GPU grpc server socket path")
flag.Parse()
log.Println("Loading NVML")
if err := nvml.Initialize(); err != nil {
log.Printf("Failed to initialize NVML: %s.", err)
log.Printf("If this is a GPU node, did you set the docker default runtime to `nvidia`?")
log.Printf("You can check the prerequisites at: https://github.com/NVIDIA/k8s-device-plugin#prerequisites")
log.Printf("You can learn how to set the runtime at: https://github.com/NVIDIA/k8s-device-plugin#quick-start")
log.Println("Waiting indefinitely.")
select {}
}
defer func() { log.Println("Shutdown of NVML returned:", nvml.Shutdown()) }()
log.Println("Fetching devices.")
n, err := nvml.DeviceCount()
if err != nil || n == 0 {
log.Println("No devices found. Waiting indefinitely.")
select {}
}
restart := true
var server *GPUInfoServer
for {
if restart {
if server != nil {
server.Stop()
}
server = NewGPUInfoServer(socketPath)
if err := server.Serve(); err != nil {
time.Sleep(2 * time.Second)
} else {
restart = false
}
}
time.Sleep(30 * time.Second)
}
}