gpu_plugin: add -shared-dev-num option

The DRM driver of Intel i915 GPUs allows sharing one device
between many containers.

Make it possible to use the same device from different containers.
The exact number of containers sharing the same device can be limited
with the new option -shared-dev-num set to 1 by default.

closes #53
This commit is contained in:
Dmitry Rozhkov 2018-08-14 14:54:49 +03:00
parent 850ca06121
commit 40246f64ad
2 changed files with 24 additions and 9 deletions

View File

@ -47,14 +47,17 @@ type devicePlugin struct {
sysfsDir string
devfsDir string
sharedDevNum int
gpuDeviceReg *regexp.Regexp
controlDeviceReg *regexp.Regexp
}
func newDevicePlugin(sysfsDir string, devfsDir string) *devicePlugin {
func newDevicePlugin(sysfsDir, devfsDir string, sharedDevNum int) *devicePlugin {
return &devicePlugin{
sysfsDir: sysfsDir,
devfsDir: devfsDir,
sharedDevNum: sharedDevNum,
gpuDeviceReg: regexp.MustCompile(gpuDeviceRE),
controlDeviceReg: regexp.MustCompile(controlDeviceRE),
}
@ -112,9 +115,11 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) {
}
if len(nodes) > 0 {
for i := 0; i < dp.sharedDevNum; i++ {
devID := fmt.Sprintf("%s-%d", f.Name(), i)
// Currently only one device type (i915) is supported.
// TODO: check model ID to differentiate device models.
devTree.AddDevice(deviceType, f.Name(), dpapi.DeviceInfo{
devTree.AddDevice(deviceType, devID, dpapi.DeviceInfo{
State: pluginapi.Healthy,
Nodes: nodes,
})
@ -122,15 +127,25 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) {
}
}
}
}
return devTree, nil
}
func main() {
var sharedDevNum int
flag.IntVar(&sharedDevNum, "shared-dev-num", 1, "number of containers sharing the same GPU device")
flag.Parse()
if sharedDevNum < 1 {
glog.Error("The number of containers sharing the same GPU must greater than zero")
os.Exit(1)
}
glog.Info("GPU device plugin started")
plugin := newDevicePlugin(sysfsDrmDirectory, devfsDriDirectory)
plugin := newDevicePlugin(sysfsDrmDirectory, devfsDriDirectory, sharedDevNum)
manager := dpapi.NewManager(namespace, plugin)
manager.Run()
}

View File

@ -75,7 +75,7 @@ func TestScan(t *testing.T) {
},
}
testPlugin := newDevicePlugin(sysfs, devfs)
testPlugin := newDevicePlugin(sysfs, devfs, 1)
if testPlugin == nil {
t.Fatal("Failed to create a deviceManager")