diff --git a/cyborg/accelerator/common/utils.py b/cyborg/accelerator/common/utils.py new file mode 100644 index 00000000..fd758bc7 --- /dev/null +++ b/cyborg/accelerator/common/utils.py @@ -0,0 +1,27 @@ +# Copyright 2019 Intel, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslo_serialization import jsonutils +import collections + + +def pci_str_to_json(pci_address): + dbs, func = pci_address.split('.') + domain, bus, slot = dbs.split(':') + keys = ["domain", "bus", "device", "function"] + values = [domain, bus, slot, func] + bdf_dict = dict(zip(keys, values)) + ordered_dict = collections.OrderedDict(sorted(bdf_dict.items())) + bdf_json = jsonutils.dumps(ordered_dict) + return bdf_json diff --git a/cyborg/accelerator/drivers/fpga/intel/sysinfo.py b/cyborg/accelerator/drivers/fpga/intel/sysinfo.py index b64b838b..90c74b0b 100644 --- a/cyborg/accelerator/drivers/fpga/intel/sysinfo.py +++ b/cyborg/accelerator/drivers/fpga/intel/sysinfo.py @@ -17,13 +17,13 @@ Cyborg Intel FPGA driver implementation. """ -# from cyborg.accelerator.drivers.fpga.base import FPGADriver import glob import os import re from oslo_serialization import jsonutils +from cyborg.accelerator.common import utils from cyborg.agent import rc_fields from cyborg.objects.driver_objects import driver_deployable, driver_device,\ driver_attach_handle, driver_controlpath_id, driver_attribute @@ -208,7 +208,7 @@ def _generate_driver_device(fpga, pf_has_vf): def _generate_controlpath_id(fpga): driver_cpid = driver_controlpath_id.DriverControlPathID() driver_cpid.cpid_type = "PCI" - driver_cpid.cpid_info = fpga["devices"] + driver_cpid.cpid_info = utils.pci_str_to_json(fpga["devices"]) return driver_cpid @@ -221,7 +221,7 @@ def _generate_dep_list(fpga, pf_has_vf): if not pf_has_vf: driver_dep.num_accelerators = 1 driver_dep.attach_handle_list = \ - [_generate_attach_handle(fpga, pf_has_vf)] + [_generate_attach_handle(fpga)] driver_dep.name = fpga["name"] # pf with sriov enabled, may have several regions and several vfs. # For now, there is only region, this maybe improve in next release. @@ -230,15 +230,15 @@ def _generate_dep_list(fpga, pf_has_vf): for vf in fpga["regions"]: # Only vfs in regions can be attach, no pf. driver_dep.attach_handle_list.append( - _generate_attach_handle(vf, False)) + _generate_attach_handle(vf)) driver_dep.name = vf["name"] return [driver_dep] -def _generate_attach_handle(fpga, pf_has_vf): +def _generate_attach_handle(fpga): driver_ah = driver_attach_handle.DriverAttachHandle() driver_ah.attach_type = "PCI" - driver_ah.attach_info = fpga["devices"] + driver_ah.attach_info = utils.pci_str_to_json(fpga["devices"]) driver_ah.in_use = False return driver_ah diff --git a/cyborg/accelerator/drivers/gpu/utils.py b/cyborg/accelerator/drivers/gpu/utils.py index 3c9b2c58..aeacdb03 100644 --- a/cyborg/accelerator/drivers/gpu/utils.py +++ b/cyborg/accelerator/drivers/gpu/utils.py @@ -25,6 +25,7 @@ import subprocess from cyborg.objects.driver_objects import driver_deployable, driver_device, \ driver_attach_handle, driver_controlpath_id from cyborg.common import constants +from cyborg.accelerator.common import utils LOG = logging.getLogger(__name__) @@ -91,7 +92,7 @@ def _generate_driver_device(gpu): def _generate_controlpath_id(gpu): driver_cpid = driver_controlpath_id.DriverControlPathID() driver_cpid.cpid_type = "PCI" - driver_cpid.cpid_info = gpu["devices"] + driver_cpid.cpid_info = utils.pci_str_to_json(gpu["devices"]) return driver_cpid @@ -115,5 +116,5 @@ def _generate_attach_handle(gpu): driver_ah = driver_attach_handle.DriverAttachHandle() driver_ah.attach_type = "PCI" driver_ah.in_use = False - driver_ah.attach_info = gpu["devices"] + driver_ah.attach_info = utils.pci_str_to_json(gpu["devices"]) return driver_ah diff --git a/cyborg/agent/resource_tracker.py b/cyborg/agent/resource_tracker.py index ce2efcd2..54dc914c 100644 --- a/cyborg/agent/resource_tracker.py +++ b/cyborg/agent/resource_tracker.py @@ -72,5 +72,4 @@ class ResourceTracker(object): acc_list.extend(acc_driver.discover()) # Call conductor_api here to diff and report acc data. Now, we actually # do not have the method report_data. - if acc_list: - self.conductor_api.report_data(context, self.host, acc_list) + self.conductor_api.report_data(context, self.host, acc_list) diff --git a/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py b/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py index 88bd5ad3..5bb19d2f 100644 --- a/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py +++ b/cyborg/tests/unit/accelerator/drivers/fpga/intel/test_driver.py @@ -43,12 +43,18 @@ class TestIntelFPGADriver(base.TestCase): attach_handle_list = [ [ {'attach_type': 'PCI', - 'attach_info': '0000:be:00.0', + 'attach_info': '{"bus": "be", ' + '"device": "00", ' + '"domain": "0000", ' + '"function": "0"}', 'in_use': False} ], [ {'attach_type': 'PCI', - 'attach_info': '0000:5e:00.1', + 'attach_info': '{"bus": "5e", ' + '"device": "00", ' + '"domain": "0000", ' + '"function": "1"}', 'in_use': False} ] ] @@ -63,8 +69,13 @@ class TestIntelFPGADriver(base.TestCase): }, ], 'controlpath_id': - {'cpid_info': '0000:be:00.0', - 'cpid_type': 'PCI'}}, + { + 'cpid_info': '{"bus": "be", ' + '"device": "00", ' + '"domain": "0000", ' + '"function": "0"}', + 'cpid_type': 'PCI'} + }, {'vendor': '0x8086', 'type': 'FPGA', 'model': '0xbcc0', @@ -76,8 +87,14 @@ class TestIntelFPGADriver(base.TestCase): }, ], 'controlpath_id': - {'cpid_info': '0000:5e:00.0', - 'cpid_type': 'PCI'}}] + { + 'cpid_info': '{"bus": "5e", ' + '"device": "00", ' + '"domain": "0000", ' + '"function": "0"}', + 'cpid_type': 'PCI'} + } + ] intel = IntelFPGADriver() fpgas = intel.discover() list.sort(fpgas, key=lambda x: x._obj_deployable_list[0].name) diff --git a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py index 5fc47c65..249a6784 100644 --- a/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py +++ b/cyborg/tests/unit/accelerator/drivers/gpu/test_utils.py @@ -59,7 +59,10 @@ class TestGPUDriverUtils(base.TestCase): self.assertEqual(1, len(gpu_list)) attach_handle_list = [ {'attach_type': 'PCI', - 'attach_info': '0000:00:06.0', + 'attach_info': '{"bus": "00", ' + '"device": "06", ' + '"domain": "0000", ' + '"function": "0"}', 'in_use': False} ] expected = { @@ -76,8 +79,13 @@ class TestGPUDriverUtils(base.TestCase): 'attach_handle_list': attach_handle_list }, ], - 'controlpath_id': {'cpid_info': '0000:00:06.0', 'cpid_type': 'PCI'} - } + + 'controlpath_id': {'cpid_info': '{"bus": "00", ' + '"device": "06", ' + '"domain": "0000", ' + '"function": "0"}', + 'cpid_type': 'PCI'} + } gpu_obj = gpu_list[0] gpu_dict = gpu_obj.as_dict() gpu_dep_list = gpu_dict['deployable_list']