-
Notifications
You must be signed in to change notification settings - Fork 5
/
acc.go
212 lines (187 loc) · 5.74 KB
/
acc.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
210
211
212
package main
import (
"io/ioutil"
"github.com/gogo/protobuf/proto"
pb "./device_proto"
k8sPluginApi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
"os"
"log"
"github.com/fsnotify/fsnotify"
"os/signal"
"syscall"
)
const (
ACC_K8S_DIR_DEFAULT = "/etc/accelerator-docker"
USR_LIST_DEFAULT = "/etc/accelerator-docker/device.pbtxt"
STAT_LIST_DEFAULT = "/etc/accelerator-docker/stat.pb"
accManagerName = "acc-manager"
)
type AccManager struct {
userListPath string
statListPath string
restartChan chan interface{}
dpWatcher *fsnotify.Watcher
osWatcher chan os.Signal
healthWatcher *fsnotify.Watcher
}
func initializeAccManager() (*AccManager,error){
//TODO : fetch list files from ACC-Manager
/*accManagerPath, err := exec.LookPath(accManagerName)
if err != nil {
log.Println("No "+accManagerName+" found\nFirst install Accelerator-Docker")
}
listPaths, err := exec.Command(accManagerPath,"paths").Output()
if err != nil {
log.Println("Cannot execute "+accManagerName)
}
fmt.Println(string(listPaths))*/
tmp_acc_k8s_dir_path := ACC_K8S_DIR_DEFAULT
tmp_user_list_path := USR_LIST_DEFAULT
tmp_stat_list_path := STAT_LIST_DEFAULT
//Handle change of device plugin path
tmpDpWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println("Cannot initialize file watcher : "+err.Error())
return nil, err
}
err = tmpDpWatcher.Add(k8sPluginApi.DevicePluginPath)
if err != nil {
tmpDpWatcher.Close()
log.Println("Cannot watch ["+k8sPluginApi.DevicePluginPath+"] : "+err.Error())
return nil, err
}
//Handle OS interrupts
tmpOsWatcher := make(chan os.Signal,1)
signal.Notify(tmpOsWatcher, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
//Health Watcher
tmpHealthWatcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println("Cannot initialize file watcher for health check : "+err.Error())
return nil, err
}
if err := tmpHealthWatcher.Add(tmp_acc_k8s_dir_path); err != nil {
log.Println("Cannot watch ["+tmp_acc_k8s_dir_path+"] : "+err.Error())
return nil, err
}
if err := tmpHealthWatcher.Add(tmp_user_list_path); err != nil {
log.Println("Cannot watch ["+tmp_user_list_path+"] : "+err.Error())
//return nil, err
}
os.OpenFile(tmp_stat_list_path, os.O_RDONLY|os.O_CREATE, 0666)
if err := tmpHealthWatcher.Add(tmp_stat_list_path); err != nil {
log.Println("Cannot watch ["+tmp_stat_list_path+"] : "+err.Error())
//return nil, err
}
return &AccManager{
userListPath: tmp_user_list_path,
statListPath: tmp_stat_list_path,
restartChan: make(chan interface{}),
dpWatcher: tmpDpWatcher,
osWatcher: tmpOsWatcher,
healthWatcher: tmpHealthWatcher,
},nil
}
func (m *AccManager) getAccelerators() []*pb.Accelerator{
statListPath := m.statListPath
usrListPath := m.userListPath
statListTxt, err := ioutil.ReadFile(statListPath)
if err != nil {
log.Println("cannot read ["+statListPath+"] : "+err.Error())
}
statList := &pb.DeviceList{}
if err := proto.Unmarshal(statListTxt, statList); err != nil {
log.Println("cannot parse ["+ statListPath +"] : "+err.Error())
}
usrListTxt, err := ioutil.ReadFile(usrListPath)
if err != nil {
log.Println("cannot read ["+usrListPath+"] : "+err.Error())
}
usrList := &pb.AcceleratorList{}
if err := proto.UnmarshalText(string(usrListTxt), usrList); err != nil {
log.Println("cannot parse ["+ usrListPath +"] : "+err.Error())
}
accByTypes := make(map[string]*pb.Accelerator)
for _, acc := range usrList.Accelerators{
for _, dev := range acc.Devices{
tmpStatus := pb.Device_IDLE
dev.Status = &tmpStatus
dev.Pid = proto.Int32(0)
for _, devStat := range statList.Devices{
if dev.GetName() == devStat.GetName(){
dev.Id = devStat.Id
dev.Status = devStat.Status
dev.Pid = proto.Int32(devStat.GetPid())
// Fix me : We should trust status protobuf
// as we cannot check other processes' status from inside container
/*if _,err := os.Stat(path.Join("/proc",strconv.Itoa(int(devStat.GetPid())))); os.IsNotExist(err) {
tmpStatus := pb.Device_IDLE
dev.Status = &tmpStatus
dev.Pid = proto.Int32(0)
}*/
break
}
}
}
if _, ok := accByTypes[acc.GetType()] ; ok {
accByTypes[acc.GetType()].Devices = append(accByTypes[acc.GetType()].Devices, acc.Devices...)
}else{
accByTypes[acc.GetType()] = acc
}
}
accs := []*pb.Accelerator{}
for _,v := range accByTypes {
accs = append(accs,v)
}
/*for _, elem := range usrList.GetDevices(){
name := strings.Trim(strings.ToLower(*elem.Name)," ")
dev := proto.Clone(elem).(*pb.Device)
for _,elemStat := range statList.GetDevices(){
nameStat := strings.Trim(strings.ToLower(*elemStat.Name)," ")
if nameStat == name {
if _,err := os.Stat(path.Join("/proc",string(*elemStat.Pid))); os.IsNotExist(err) {
tmpStatus := pb.Device_IDLE
dev.Status = &tmpStatus
dev.Pid = proto.Int32(0)
}else{
dev.Status = elemStat.Status
dev.Pid = proto.Int32(*elemStat.Pid)
}
if _,ok := devices[name]; !ok {
devices[name] = []*pb.Device{}
}
devices[name] = append(devices[name],dev)
break
}
}
}*/
return accs
}
func (m *AccManager) HealthCheck() {
//TODO: Cleanup file watcher
//TODO: fix from per dp to overall
/*for{
select {
case <-watcher.Events:
var tmpDevs []*k8sPluginApi.Device
devs := getDevices()
if _,ok := devs[dp.resName]; !ok {
log.Println("Resource ["+dp.resName+"] not exists anymore")
dp.health <- nil
return
}
tmpDevs = convertDeviceVar(dp.resName,devs[dp.resName])
dp.devs = tmpDevs
dp.health <- dp.devs
case e := <-watcher.Errors:
log.Println("Error while watching status files: "+e.Error())
}
}*/
}
func (m *AccManager) Shutdown(){
if m.dpWatcher != nil {
m.dpWatcher.Close()
}
if m.healthWatcher != nil {
m.healthWatcher.Close()
}
}