Exemplo n.º 1
0
    def test_get_sockets(self, lib):
        "Tests get_sockets() method."

        sockets_mock = [ctypes.c_uint(socket) for socket in [0, 1, 2, 3]]
        sockets_arr = ctypes_build_array(sockets_mock)

        def pqos_cpu_get_sockets_m(_p_cpu, count_ref):
            "Mock pqos_cpu_get_sockets()."

            ctypes_ref_set_int(count_ref, len(sockets_arr))
            return ctypes.cast(sockets_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_sockets = MagicMock(side_effect=pqos_cpu_get_sockets_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            sockets = cpu.get_sockets()

        self.assertEqual(len(sockets), 4)
        self.assertEqual(sockets[0], 0)
        self.assertEqual(sockets[1], 1)
        self.assertEqual(sockets[2], 2)
        self.assertEqual(sockets[3], 3)
Exemplo n.º 2
0
    def test_get_cores(self, lib):
        "Tests get_cores() method."

        cores_mock = [ctypes.c_uint(core) for core in [8, 7, 3]]
        cores_arr = ctypes_build_array(cores_mock)

        def pqos_cpu_get_cores_m(_p_cpu, socket, count_ref):
            "Mock pqos_cpu_get_cores()."

            self.assertEqual(socket, 0)
            ctypes_ref_set_int(count_ref, len(cores_arr))
            return ctypes.cast(cores_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_cores = MagicMock(side_effect=pqos_cpu_get_cores_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            cores = cpu.get_cores(0)

        self.assertEqual(len(cores), 3)
        self.assertEqual(cores[0], 8)
        self.assertEqual(cores[1], 7)
        self.assertEqual(cores[2], 3)

        lib.pqos_cpu_get_cores.assert_called_once()
Exemplo n.º 3
0
    def test_get_l2ids(self, lib):
        "Tests get_l2ids() method."

        l2ids_mock = [ctypes.c_uint(l2id) for l2id in [7, 2, 3, 5]]
        l2ids_arr = ctypes_build_array(l2ids_mock)

        def pqos_cpu_get_l2ids_m(_p_cpu, count_ref):
            "Mock pqos_cpu_get_l2ids()."

            ctypes_ref_set_int(count_ref, len(l2ids_arr))
            return ctypes.cast(l2ids_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_l2ids = MagicMock(side_effect=pqos_cpu_get_l2ids_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            l2ids = cpu.get_l2ids()

        self.assertEqual(len(l2ids), 4)
        self.assertEqual(l2ids[0], 7)
        self.assertEqual(l2ids[1], 2)
        self.assertEqual(l2ids[2], 3)
        self.assertEqual(l2ids[3], 5)
Exemplo n.º 4
0
    def test_get_cores(self, lib):
        "Tests get_cores() method."

        cores_mock = [ctypes.c_uint(core) for core in [8, 7, 3]]
        cores_arr = ctypes_build_array(cores_mock)

        def pqos_cpu_get_cores_m(_p_cpu, socket, count_ref):
            "Mock pqos_cpu_get_cores()."

            self.assertEqual(socket, 0)
            ctypes_ref_set_int(count_ref, len(cores_arr))
            return ctypes.cast(cores_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_cores = MagicMock(side_effect=pqos_cpu_get_cores_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            cores = cpu.get_cores(0)

        self.assertEqual(len(cores), 3)
        self.assertEqual(cores[0], 8)
        self.assertEqual(cores[1], 7)
        self.assertEqual(cores[2], 3)

        lib.pqos_cpu_get_cores.assert_called_once()
Exemplo n.º 5
0
    def test_get_l2ids(self, lib):
        "Tests get_l2ids() method."

        l2ids_mock = [ctypes.c_uint(l2id) for l2id in [7, 2, 3, 5]]
        l2ids_arr = ctypes_build_array(l2ids_mock)

        def pqos_cpu_get_l2ids_m(_p_cpu, count_ref):
            "Mock pqos_cpu_get_l2ids()."

            ctypes_ref_set_int(count_ref, len(l2ids_arr))
            return ctypes.cast(l2ids_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_l2ids = MagicMock(side_effect=pqos_cpu_get_l2ids_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            l2ids = cpu.get_l2ids()

        self.assertEqual(len(l2ids), 4)
        self.assertEqual(l2ids[0], 7)
        self.assertEqual(l2ids[1], 2)
        self.assertEqual(l2ids[2], 3)
        self.assertEqual(l2ids[3], 5)
Exemplo n.º 6
0
    def test_get_sockets(self, lib):
        "Tests get_sockets() method."

        sockets_mock = [ctypes.c_uint(socket) for socket in [0, 1, 2, 3]]
        sockets_arr = ctypes_build_array(sockets_mock)

        def pqos_cpu_get_sockets_m(_p_cpu, count_ref):
            "Mock pqos_cpu_get_sockets()."

            ctypes_ref_set_int(count_ref, len(sockets_arr))
            return ctypes.cast(sockets_arr, ctypes.POINTER(ctypes.c_uint))

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_sockets = MagicMock(side_effect=pqos_cpu_get_sockets_m)

        cpu = PqosCpuInfo()

        with patch('pqos.cpuinfo.free_memory'):
            sockets = cpu.get_sockets()

        self.assertEqual(len(sockets), 4)
        self.assertEqual(sockets[0], 0)
        self.assertEqual(sockets[1], 1)
        self.assertEqual(sockets[2], 2)
        self.assertEqual(sockets[3], 3)
Exemplo n.º 7
0
    def test_get_core_info(self, lib):
        "Tests get_core_info() method."

        coreinfo_mock = CPqosCoreInfo(lcore=1,
                                      socket=0,
                                      l3_id=1,
                                      l2_id=7,
                                      l3cat_id=1,
                                      mba_id=1)

        def pqos_get_core_info_m(_p_cpu, core):
            "Mock pqos_cpu_get_core_info()."

            self.assertEqual(core, 1)
            return ctypes.pointer(coreinfo_mock)

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_core_info = MagicMock(
            side_effect=pqos_get_core_info_m)

        cpu = PqosCpuInfo()
        coreinfo = cpu.get_core_info(1)

        self.assertEqual(coreinfo.core, 1)
        self.assertEqual(coreinfo.socket, 0)
        self.assertEqual(coreinfo.l3_id, 1)
        self.assertEqual(coreinfo.l2_id, 7)

        lib.pqos_cpu_get_core_info.assert_called_once()
Exemplo n.º 8
0
    def test_get_vendor(self, lib):

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_get_vendor = MagicMock(return_value=1)

        cpu = PqosCpuInfo()

        self.assertEqual(cpu.get_vendor(), "INTEL")
Exemplo n.º 9
0
def main():
    args = parse_args()

    try:
        with PqosContextManager(args.interface):
            cpu = PqosCpuInfo()
            l2_ids = cpu.get_l2ids()

            set_allocation_class(l2_ids, args.class_id, args.mask)
            print_allocation_config(l2_ids)
    except:
        print("Error!")
        raise
Exemplo n.º 10
0
def main():
    args = parse_args()

    try:
        with PqosContextManager(args.interface):
            cpu = PqosCpuInfo()
            sockets = cpu.get_sockets()

            set_allocation_class(sockets, args.class_id, args.mb_max)
            print_allocation_config(sockets)
    except:
        print("Error!")
        raise
Exemplo n.º 11
0
    def init(self, iface, force_iface=False):
        """
        Initializes libpqos

        Returns:
            0 on success
            -1 otherwise
        """

        if not force_iface and not iface in self.supported_iface():
            log.error("RDT does not support '%s' interface!" % (iface))
            return -1

        # deinitialize lib first
        if self.shared_dict['current_iface']:
            self.fini()

        # umount restcrl to improve caps detection
        if platform.system() == 'FreeBSD':
            result = os.system(
                "/sbin/umount -a -t resctrl")  # nosec - string literal
        else:
            result = os.system(
                "/bin/umount -a -t resctrl")  # nosec - string literal
        if result:
            log.error("Failed to umount resctrl fs! status code: %d"\
                    % (os.WEXITSTATUS(result)))
            return -1

        # attempt to initialize libpqos
        try:
            self.pqos.init(iface.upper())
            self.cap = PqosCap()
            self.l3ca = PqosCatL3()
            self.mba = PqosMba()
            self.alloc = PqosAlloc()
            self.cpuinfo = PqosCpuInfo()
        except Exception as ex:
            log.error(str(ex))
            return -1

        # save current interface type in shared dict
        self.shared_dict['current_iface'] = iface

        # Reread MBA BW status from libpqos
        self.refresh_mba_bw_status()

        return 0
Exemplo n.º 12
0
def get_all_cores():
    """
    Returns all available cores.

    Returns:
        a list of available cores
    """

    cores = []
    cpu = PqosCpuInfo()
    sockets = cpu.get_sockets()

    for socket in sockets:
        cores += cpu.get_cores(socket)

    return cores
Exemplo n.º 13
0
    def test_check_core_invalid(self, lib):
        "Tests check_core() method when the given core is invalid."

        def pqos_cpu_check_core_m(_p_cpu, core):
            "Mock pqos_cpu_check_core()."

            self.assertEqual(core, 99999)
            return 1

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_check_core = MagicMock(side_effect=pqos_cpu_check_core_m)

        cpu = PqosCpuInfo()
        result = cpu.check_core(99999)

        self.assertFalse(result)

        lib.pqos_cpu_check_core.assert_called_once()
Exemplo n.º 14
0
    def test_check_core_invalid(self, lib):
        "Tests check_core() method when the given core is invalid."

        def pqos_cpu_check_core_m(_p_cpu, core):
            "Mock pqos_cpu_check_core()."

            self.assertEqual(core, 99999)
            return 1

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_check_core = MagicMock(side_effect=pqos_cpu_check_core_m)

        cpu = PqosCpuInfo()
        result = cpu.check_core(99999)

        self.assertFalse(result)

        lib.pqos_cpu_check_core.assert_called_once()
Exemplo n.º 15
0
    def test_get_one_core(self, lib):
        "Tests get_one_core() method."

        def pqos_get_one_core_m(_p_cpu, socket, core_ref):
            "Mock pqos_cpu_get_one_core()."

            self.assertEqual(socket, 1)
            ctypes_ref_set_int(core_ref, 5)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_one_core = MagicMock(side_effect=pqos_get_one_core_m,
                                              __name__=u'pqos_cpu_get_one_core')

        cpu = PqosCpuInfo()
        core = cpu.get_one_core(1)

        self.assertEqual(core, 5)

        lib.pqos_cpu_get_one_core.assert_called_once()
Exemplo n.º 16
0
    def test_get_one_core(self, lib):
        "Tests get_one_core() method."

        def pqos_get_one_core_m(_p_cpu, socket, core_ref):
            "Mock pqos_cpu_get_one_core()."

            self.assertEqual(socket, 1)
            ctypes_ref_set_int(core_ref, 5)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_one_core = MagicMock(side_effect=pqos_get_one_core_m,
                                              __name__=u'pqos_cpu_get_one_core')

        cpu = PqosCpuInfo()
        core = cpu.get_one_core(1)

        self.assertEqual(core, 5)

        lib.pqos_cpu_get_one_core.assert_called_once()
Exemplo n.º 17
0
    def test_get_socketid(self, lib):
        "Tests get_socketid() method."

        def pqos_get_socketid_m(_p_cpu, core, socket_ref):
            "Mock pqos_cpu_get_socketid()."

            self.assertEqual(core, 3)
            ctypes_ref_set_int(socket_ref, 4)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_socketid = MagicMock(side_effect=pqos_get_socketid_m,
                                              __name__=u'pqos_cpu_get_socketid')

        cpu = PqosCpuInfo()
        socket = cpu.get_socketid(3)

        self.assertEqual(socket, 4)

        lib.pqos_cpu_get_socketid.assert_called_once()
Exemplo n.º 18
0
    def test_get_socketid(self, lib):
        "Tests get_socketid() method."

        def pqos_get_socketid_m(_p_cpu, core, socket_ref):
            "Mock pqos_cpu_get_socketid()."

            self.assertEqual(core, 3)
            ctypes_ref_set_int(socket_ref, 4)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_socketid = MagicMock(side_effect=pqos_get_socketid_m,
                                              __name__=u'pqos_cpu_get_socketid')

        cpu = PqosCpuInfo()
        socket = cpu.get_socketid(3)

        self.assertEqual(socket, 4)

        lib.pqos_cpu_get_socketid.assert_called_once()
Exemplo n.º 19
0
    def test_get_one_by_l2id(self, lib):
        "Tests get_one_by_l2id() method."

        def pqos_get_by_l2id_m(_p_cpu, l2_id, core_ref):
            "Mock pqos_cpu_get_one_core()."

            self.assertEqual(l2_id, 8)
            ctypes_ref_set_int(core_ref, 15)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        func_mock = MagicMock(side_effect=pqos_get_by_l2id_m,
                              __name__=u'pqos_cpu_get_one_by_l2id')
        lib.pqos_cpu_get_one_by_l2id = func_mock

        cpu = PqosCpuInfo()
        core = cpu.get_one_by_l2id(8)

        self.assertEqual(core, 15)

        lib.pqos_cpu_get_one_by_l2id.assert_called_once()
Exemplo n.º 20
0
    def test_get_clusterid(self, lib):
        "Tests get_clusterid() method."

        def pqos_get_clusterid_m(_p_cpu, core, cluster_ref):
            "Mock pqos_cpu_get_clusterid()."

            self.assertEqual(core, 1)
            ctypes_ref_set_int(cluster_ref, 0)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        func_mock = MagicMock(side_effect=pqos_get_clusterid_m,
                              __name__=u'pqos_cpu_get_clusterid')
        lib.pqos_cpu_get_clusterid = func_mock

        cpu = PqosCpuInfo()
        cluster = cpu.get_clusterid(1)

        self.assertEqual(cluster, 0)

        lib.pqos_cpu_get_clusterid.assert_called_once()
Exemplo n.º 21
0
    def init(self):
        """
        Initializes libpqos

        Returns:
            0 on success
            -1 otherwise
        """

        try:
            self.pqos.init('MSR')
            self.cap = PqosCap()
            self.l3ca = PqosCatL3()
            self.mba = PqosMba()
            self.alloc = PqosAlloc()
            self.cpuinfo = PqosCpuInfo()
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0
Exemplo n.º 22
0
    def test_get_clusterid(self, lib):
        "Tests get_clusterid() method."

        def pqos_get_clusterid_m(_p_cpu, core, cluster_ref):
            "Mock pqos_cpu_get_clusterid()."

            self.assertEqual(core, 1)
            ctypes_ref_set_int(cluster_ref, 0)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        func_mock = MagicMock(side_effect=pqos_get_clusterid_m,
                              __name__=u'pqos_cpu_get_clusterid')
        lib.pqos_cpu_get_clusterid = func_mock

        cpu = PqosCpuInfo()
        cluster = cpu.get_clusterid(1)

        self.assertEqual(cluster, 0)

        lib.pqos_cpu_get_clusterid.assert_called_once()
Exemplo n.º 23
0
    def test_get_one_by_l2id(self, lib):
        "Tests get_one_by_l2id() method."

        def pqos_get_by_l2id_m(_p_cpu, l2_id, core_ref):
            "Mock pqos_cpu_get_one_core()."

            self.assertEqual(l2_id, 8)
            ctypes_ref_set_int(core_ref, 15)
            return 0

        lib.pqos_cap_get = MagicMock(return_value=0)
        func_mock = MagicMock(side_effect=pqos_get_by_l2id_m,
                              __name__=u'pqos_cpu_get_one_by_l2id')
        lib.pqos_cpu_get_one_by_l2id = func_mock

        cpu = PqosCpuInfo()
        core = cpu.get_one_by_l2id(8)

        self.assertEqual(core, 15)

        lib.pqos_cpu_get_one_by_l2id.assert_called_once()
Exemplo n.º 24
0
def print_allocation_config():
    """
    Prints allocation configuration.
    """

    l3ca = PqosCatL3()
    alloc = PqosAlloc()
    cpuinfo = PqosCpuInfo()
    sockets = cpuinfo.get_sockets()

    for socket in sockets:
        try:
            coses = l3ca.get(socket)

            print("L3CA COS definitions for Socket %u:" % socket)

            for cos in coses:
                cos_params = (cos.class_id, cos.mask)
                print("    L3CA COS%u => MASK 0x%x" % cos_params)
        except:
            print("Error")
            raise

    for socket in sockets:
        try:
            print("Core information for socket %u:" % socket)

            cores = cpuinfo.get_cores(socket)

            for core in cores:
                class_id = alloc.assoc_get(core)
                try:
                    print("    Core %u => COS%u" % (core, class_id))
                except:
                    print("    Core %u => ERROR" % core)
        except:
            print("Error")
            raise
Exemplo n.º 25
0
    def test_get_core_info(self, lib):
        "Tests get_core_info() method."

        coreinfo_mock = CPqosCoreInfo(lcore=1, socket=0, l3_id=1, l2_id=7)

        def pqos_get_core_info_m(_p_cpu, core):
            "Mock pqos_cpu_get_core_info()."

            self.assertEqual(core, 1)
            return ctypes.pointer(coreinfo_mock)

        lib.pqos_cap_get = MagicMock(return_value=0)
        lib.pqos_cpu_get_core_info = MagicMock(side_effect=pqos_get_core_info_m)

        cpu = PqosCpuInfo()
        coreinfo = cpu.get_core_info(1)

        self.assertEqual(coreinfo.core, 1)
        self.assertEqual(coreinfo.socket, 0)
        self.assertEqual(coreinfo.l3_id, 1)
        self.assertEqual(coreinfo.l2_id, 7)

        lib.pqos_cpu_get_core_info.assert_called_once()
Exemplo n.º 26
0
def print_allocation_config():
    """
    Prints allocation configuration.
    """

    alloc = PqosAlloc()
    cpuinfo = PqosCpuInfo()
    sockets = cpuinfo.get_sockets()

    for socket in sockets:
        try:
            print("Core information for socket %u:" % socket)

            cores = cpuinfo.get_cores(socket)

            for core in cores:
                class_id = alloc.assoc_get(core)
                try:
                    print("    Core %u => COS%u" % (core, class_id))
                except:
                    print("    Core %u => ERROR" % core)
        except:
            print("Error")
            raise
Exemplo n.º 27
0
    def test_init(self, lib):
        """
        Tests if the pointer to CPU information object given
        to PQoS library APIs is the same returned from pqos_cap_get() API during
        an initialization of PqosCpuInfo.
        """

        builder = PqosCpuInfoMockBuilder()
        p_cpu = builder.build()

        def pqos_cap_get_mock(_cap_ref, cpu_ref):
            "Mock pqos_cap_get()."

            ctypes.memmove(cpu_ref, ctypes.addressof(p_cpu),
                           ctypes.sizeof(p_cpu))
            return 0

        def pqos_socketid_m(cpu_ref, _core, _socket_ref):
            "Mock pqos_cpu_get_socketid()."

            cpu_ref_addr = ctypes.addressof(cpu_ref.contents)
            p_cpu_addr = ctypes.addressof(p_cpu.contents)
            self.assertEqual(cpu_ref_addr, p_cpu_addr)
            return 1

        lib.pqos_cap_get = MagicMock(side_effect=pqos_cap_get_mock)
        lib.pqos_cpu_get_socketid = MagicMock(side_effect=pqos_socketid_m,
                                              __name__=u'pqos_cpu_get_socketid')

        pqos_cpu = PqosCpuInfo()

        with self.assertRaises(PqosError):
            pqos_cpu.get_socketid(0)

        lib.pqos_cpu_get_socketid.assert_called_once()
        lib.pqos_cap_get.assert_called_once()
Exemplo n.º 28
0
    def test_init(self, lib):
        """
        Tests if the pointer to CPU information object given
        to PQoS library APIs is the same returned from pqos_cap_get() API during
        an initialization of PqosCpuInfo.
        """

        builder = PqosCpuInfoMockBuilder()
        p_cpu = builder.build()

        def pqos_cap_get_mock(_cap_ref, cpu_ref):
            "Mock pqos_cap_get()."

            ctypes.memmove(cpu_ref, ctypes.addressof(p_cpu),
                           ctypes.sizeof(p_cpu))
            return 0

        def pqos_socketid_m(cpu_ref, _core, _socket_ref):
            "Mock pqos_cpu_get_socketid()."

            cpu_ref_addr = ctypes.addressof(cpu_ref.contents)
            p_cpu_addr = ctypes.addressof(p_cpu.contents)
            self.assertEqual(cpu_ref_addr, p_cpu_addr)
            return 1

        lib.pqos_cap_get = MagicMock(side_effect=pqos_cap_get_mock)
        lib.pqos_cpu_get_socketid = MagicMock(side_effect=pqos_socketid_m,
                                              __name__=u'pqos_cpu_get_socketid')

        pqos_cpu = PqosCpuInfo()

        with self.assertRaises(PqosError):
            pqos_cpu.get_socketid(0)

        lib.pqos_cpu_get_socketid.assert_called_once()
        lib.pqos_cap_get.assert_called_once()
Exemplo n.º 29
0
class PqosApi:
    # pylint: disable=too-many-instance-attributes
    """
    Wrapper for libpqos wrapper.
    """
    def __init__(self):
        self.pqos = Pqos()
        self.cap = None
        self.l3ca = None
        self.mba = None
        self.alloc = None
        self.cpuinfo = None
        self._supported_iface = []

        # dict to share interface type and MBA BW status
        # between REST API process and "backend"
        self.shared_dict = common.MANAGER.dict()
        self.shared_dict['current_iface'] = None
        self.shared_dict['mba_bw_supported'] = None
        self.shared_dict['mba_bw_enabled'] = None

    def detect_supported_ifaces(self):
        """
        Detects supported RDT interfaces
        """
        for iface in ["msr", "os"]:
            if not self.init(iface, True):
                log.info("Interface %s, MBA BW: %ssupported."\
                        % (iface.upper(), "un" if not self.is_mba_bw_supported() else ""))
                self.fini()
                self._supported_iface.append(iface)

        log.info("Supported RDT interfaces: " + str(self.supported_iface()))

    def init(self, iface, force_iface=False):
        """
        Initializes libpqos

        Returns:
            0 on success
            -1 otherwise
        """

        if not force_iface and not iface in self.supported_iface():
            log.error("RDT does not support '%s' interface!" % (iface))
            return -1

        # deinitialize lib first
        if self.shared_dict['current_iface']:
            self.fini()

        # umount restcrl to improve caps detection
        if platform.system() == 'FreeBSD':
            result = os.system(
                "/sbin/umount -a -t resctrl")  # nosec - string literal
        else:
            result = os.system(
                "/bin/umount -a -t resctrl")  # nosec - string literal
        if result:
            log.error("Failed to umount resctrl fs! status code: %d"\
                    % (os.WEXITSTATUS(result)))
            return -1

        # attempt to initialize libpqos
        try:
            self.pqos.init(iface.upper())
            self.cap = PqosCap()
            self.l3ca = PqosCatL3()
            self.mba = PqosMba()
            self.alloc = PqosAlloc()
            self.cpuinfo = PqosCpuInfo()
        except Exception as ex:
            log.error(str(ex))
            return -1

        # save current interface type in shared dict
        self.shared_dict['current_iface'] = iface

        # Reread MBA BW status from libpqos
        self.refresh_mba_bw_status()

        return 0

    def refresh_mba_bw_status(self):
        """
        Reads MBA BW status from libpqos
        and save results in shared dict

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            supported, enabled = self.cap.is_mba_ctrl_enabled()
            # convert None to False
            supported = bool(supported)

            self.shared_dict['mba_bw_supported'] = supported
            self.shared_dict['mba_bw_enabled'] = enabled
        except Exception as ex:
            log.error("libpqos is_mba_ctrl_enabled(..) call failed!")
            log.error(str(ex))
            return -1

        return 0

    def current_iface(self):
        """
        Returns current RDT interface

        Returns:
            interface name on success
            None when libpqos is not initialized
        """
        return self.shared_dict['current_iface']

    def supported_iface(self):
        """
        Returns list of supported RDT interfaces

        Returns:
            list of supported interfaces
        """
        # no need to keep it in shared dict as it does not changed
        # during runtime.
        return self._supported_iface

    def is_mba_bw_supported(self):
        """
        Returns MBA BW support status

        Returns:
            MBA BW support status
        """
        return self.shared_dict['mba_bw_supported']

    def is_mba_bw_enabled(self):
        """
        Returns MBA BW enabled status

        Returns:
            MBA BW enabled status
        """
        return self.shared_dict['mba_bw_enabled']

    def enable_mba_bw(self, enable):
        """
        Change MBA BW enabled status

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            # call libpqos alloc reset
            self.alloc.reset("any", "any", "ctrl" if enable else "default")
        except Exception as ex:
            log.error("libpqos reset(..) call failed!")
            log.error(str(ex))
            return -1

        # Reread MBA BW status from libpqos
        self.refresh_mba_bw_status()

        return 0

    def fini(self):
        """
        De-initializes libpqos
        """
        self.pqos.fini()
        self.shared_dict['current_iface'] = None

        return 0

    def release(self, cores):
        """
        Release cores, assigns cores to CoS#0

        Parameters:
            cores: list of cores to be released

        Returns:
            0 on success
            -1 otherwise
        """
        if cores is None:
            return 0

        try:
            self.alloc.release(cores)
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0

    def alloc_assoc_set(self, cores, cos):
        """
        Assigns cores to CoS

        Parameters:
            cores: list of cores to be assigned to cos
            cos: Class of Service

        Returns:
            0 on success
            -1 otherwise
        """
        if not cores:
            return 0

        try:
            for core in cores:
                self.alloc.assoc_set(core, cos)
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0

    def l3ca_set(self, sockets, cos_id, ways_mask):
        """
        Configures L3 CAT for CoS

        Parameters:
            sockets: sockets list on which to configure L3 CAT
            cos_id: Class of Service
            ways_mask: L3 CAT CBM to set

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            cos = self.l3ca.COS(cos_id, ways_mask)
            for socket in sockets:
                self.l3ca.set(socket, [cos])
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0

    def mba_set(self, sockets, cos_id, mb_max, ctrl=False):
        """
        Configures MBA rate for CoS

        Parameters:
            sockets: sockets list on which to configure L3 CAT
            cos_id: Class of Service
            mb_max: MBA rate to set

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            cos = self.mba.COS(cos_id, mb_max, ctrl)
            for socket in sockets:
                self.mba.set(socket, [cos])
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0

    def is_mba_supported(self):
        """
        Checks for MBA support

        Returns:
            True if supported
            False otherwise
        """
        try:
            return bool(self.get_mba_num_cos())
        except Exception as ex:
            log.error(str(ex))
            return False

    def is_l3_cat_supported(self):
        """
        Checks for L3 CAT support

        Returns:
            True if supported
            False otherwise
        """
        try:
            return bool(self.get_l3ca_num_cos())
        except Exception as ex:
            log.error(str(ex))
            return False

    def is_multicore(self):
        """
        Checks if system is multicore

        Returns:
            True if multicore
            False otherwise
        """
        return self.get_num_cores() > 1

    def get_num_cores(self):
        """
        Gets number of cores in system

        Returns:
            num of cores
            None otherwise
        """
        try:
            sockets = self.cpuinfo.get_sockets()
            return sum(
                [len(self.cpuinfo.get_cores(socket)) for socket in sockets])
        except Exception as ex:
            log.error(str(ex))
            return None

    def check_core(self, core):
        """
        Verifies if a specified core is a valid logical core ID.

        Parameters:
            core: core ID

        Returns:
            True/False a given core number is valid/invalid
            None otherwise
        """
        try:
            return self.cpuinfo.check_core(core)
        except Exception as ex:
            log.error(str(ex))
            return None

    def get_sockets(self):
        """
        Gets list of sockets

        Returns:
            sockets list,
            None otherwise
        """

        try:
            return self.cpuinfo.get_sockets()
        except Exception as ex:
            log.error(str(ex))
            return None

    def get_l3ca_num_cos(self):
        """
        Gets number of COS for L3 CAT

        Returns:
            num of COS for L3 CAT
            None otherwise
        """
        try:
            return self.cap.get_l3ca_cos_num()
        except Exception as ex:
            log.error(str(ex))
            return None

    def get_mba_num_cos(self):
        """
        Gets number of COS for MBA

        Returns:
            num of COS for MBA
            None otherwise
        """
        try:
            return self.cap.get_mba_cos_num()
        except Exception as ex:
            log.error(str(ex))
            return None

    def get_max_cos_id(self, alloc_type):
        """
        Gets max COS# (id) that can be used to configure set of allocation technologies

        Returns:
            Available COS# to be used
            None otherwise
        """
        max_cos_num = None
        max_cos_cat = self.get_l3ca_num_cos()
        max_cos_mba = self.get_mba_num_cos()

        if common.CAT_CAP not in alloc_type and common.MBA_CAP not in alloc_type:
            return None

        if common.CAT_CAP in alloc_type and not max_cos_cat:
            return None

        if common.MBA_CAP in alloc_type and not max_cos_mba:
            return None

        if common.CAT_CAP in alloc_type:
            max_cos_num = max_cos_cat

        if common.MBA_CAP in alloc_type:
            if max_cos_num is not None:
                max_cos_num = min(max_cos_mba, max_cos_num)
            else:
                max_cos_num = max_cos_mba

        return max_cos_num - 1

    def get_max_l3_cat_cbm(self):
        """
        Gets Max L3 CAT CBM

        Returns:
            Max L3 CAT CBM
            None otherwise
        """

        if not self.is_l3_cat_supported():
            return None

        try:
            l3ca_caps = self.cap.get_type("l3ca")
            return 2**l3ca_caps.num_ways - 1
        except Exception as ex:
            log.error(str(ex))
            return None
Exemplo n.º 30
0
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

from pqos import Pqos
from pqos.cpuinfo import PqosCpuInfo

# Initialize PQoS library
pqos = Pqos()
pqos.init('MSR')

cpuinfo = PqosCpuInfo()

# Get number of sockets
sockets = cpuinfo.get_sockets()
print('Number of sockets: %d' % len(sockets))

# Get number of cores
num_cores = sum([len(cpuinfo.get_cores(socket)) for socket in sockets])
print('Number of cores: %d' % num_cores)

# Check if core 89 is a valid core
result = cpuinfo.check_core(89)
print('Is 89 a valid core? %s' % ('Yes' if result else 'No'))

# Finalize PQoS library
pqos.fini()
Exemplo n.º 31
0
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

from pqos import Pqos
from pqos.cpuinfo import PqosCpuInfo


# Initialize PQoS library
pqos = Pqos()
pqos.init('MSR')

cpuinfo = PqosCpuInfo()

# Get number of sockets
sockets = cpuinfo.get_sockets()
print('Number of sockets: %d' % len(sockets))

# Get number of cores
num_cores = sum([len(cpuinfo.get_cores(socket)) for socket in sockets])
print('Number of cores: %d' % num_cores)

# Check if core 89 is a valid core
result = cpuinfo.check_core(89)
print('Is 89 a valid core? %s' % ('Yes' if result else 'No'))

# Finalize PQoS library
pqos.fini()
Exemplo n.º 32
0
class PqosApi:
    """
    Wrapper for libpqos wrapper.
    """


    def __init__(self):
        self.pqos = Pqos()
        self.cap = None
        self.l3ca = None
        self.mba = None
        self.alloc = None
        self.cpuinfo = None


    def init(self):
        """
        Initializes libpqos

        Returns:
            0 on success
            -1 otherwise
        """

        try:
            self.pqos.init('MSR')
            self.cap = PqosCap()
            self.l3ca = PqosCatL3()
            self.mba = PqosMba()
            self.alloc = PqosAlloc()
            self.cpuinfo = PqosCpuInfo()
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0


    def fini(self):
        """
        De-initializes libpqos
        """
        self.pqos.fini()

        return 0


    def release(self, cores):
        """
        Release cores, assigns cores to CoS#0

        Parameters:
            cores: list of cores to be released

        Returns:
            0 on success
            -1 otherwise
        """
        if cores is None:
            return 0

        try:
            self.alloc.release(cores)
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0


    def alloc_assoc_set(self, cores, cos):
        """
        Assigns cores to CoS

        Parameters:
            cores: list of cores to be assigned to cos
            cos: Class of Service

        Returns:
            0 on success
            -1 otherwise
        """
        if not cores:
            return 0

        try:
            for core in cores:
                self.alloc.assoc_set(core, cos)
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0


    def l3ca_set(self, sockets, cos_id, ways_mask):
        """
        Configures L3 CAT for CoS

        Parameters:
            sockets: sockets list on which to configure L3 CAT
            cos_id: Class of Service
            ways_mask: L3 CAT CBM to set

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            cos = self.l3ca.COS(cos_id, ways_mask)
            for socket in sockets:
                self.l3ca.set(socket, [cos])
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0


    def mba_set(self, sockets, cos_id, mb_max):
        """
        Configures MBA for CoS

        Parameters:
            sockets: sockets list on which to configure L3 CAT
            cos_id: Class of Service
            mb_max: MBA to set

        Returns:
            0 on success
            -1 otherwise
        """
        try:
            cos = self.mba.COS(cos_id, mb_max)
            for socket in sockets:
                self.mba.set(socket, [cos])
        except Exception as ex:
            log.error(str(ex))
            return -1

        return 0


    def is_mba_supported(self):
        """
        Checks for MBA support

        Returns:
            1 if supported
            0 otherwise
        """
        try:
            return self.get_mba_num_cos() != 0
        except Exception as ex:
            log.error(str(ex))
            return 0


    def is_l3_cat_supported(self):
        """
        Checks for L3 CAT support

        Returns:
            1 if supported
            0 otherwise
        """
        try:
            return self.get_l3ca_num_cos() != 0
        except Exception as ex:
            log.error(str(ex))
            return 0


    def is_multicore(self):
        """
        Checks if system is multicore

        Returns:
            True if multicore
            False otherwise
        """
        return self.get_num_cores() > 1


    def get_num_cores(self):
        """
        Gets number of cores in system

        Returns:
            num of cores
            0 otherwise
        """
        try:
            sockets = self.cpuinfo.get_sockets()
            return sum([len(self.cpuinfo.get_cores(socket)) for socket in sockets])
        except Exception as ex:
            log.error(str(ex))
            return None


    def check_core(self, core):
        """
        Verifies if a specified core is a valid logical core ID.

        Parameters:
            core: core ID

        Returns:
            True/False a given core number is valid/invalid
            None otherwise
        """
        try:
            return self.cpuinfo.check_core(core)
        except Exception as ex:
            log.error(str(ex))
            return None


    def get_sockets(self):
        """
        Gets list of sockets

        Returns:
            sockets list,
            None otherwise
        """

        try:
            return self.cpuinfo.get_sockets()
        except Exception as ex:
            log.error(str(ex))
            return None


    def get_l3ca_num_cos(self):
        """
        Gets number of COS for L3 CAT

        Returns:
            num of COS for L3 CAT
            None otherwise
        """
        try:
            return self.cap.get_l3ca_cos_num()
        except Exception as ex:
            log.error(str(ex))
            return None


    def get_mba_num_cos(self):
        """
        Gets number of COS for MBA

        Returns:
            num of COS for MBA
            None otherwise
        """
        try:
            return self.cap.get_mba_cos_num()
        except Exception as ex:
            log.error(str(ex))
            return None


    def get_max_cos_id(self, alloc_type):
        """
        Gets max COS# (id) that can be used to configure set of allocation technologies

        Returns:
            Available COS# to be used
            None otherwise
        """
        max_cos_num = None
        max_cos_cat = self.get_l3ca_num_cos()
        max_cos_mba = self.get_mba_num_cos()

        if common.CAT_CAP not in alloc_type and common.MBA_CAP not in alloc_type:
            return None

        if common.CAT_CAP in alloc_type and not max_cos_cat:
            return None

        if common.MBA_CAP in alloc_type and not max_cos_mba:
            return None

        if common.CAT_CAP in alloc_type:
            max_cos_num = max_cos_cat

        if common.MBA_CAP in alloc_type:
            if max_cos_num is not None:
                max_cos_num = min(max_cos_mba, max_cos_num)
            else:
                max_cos_num = max_cos_mba

        return max_cos_num - 1


    def get_max_l3_cat_cbm(self):
        """
        Gets Max L3 CAT CBM

        Returns:
            Max L3 CAT CBM
            None otherwise
        """

        if not self.is_l3_cat_supported():
            return None

        try:
            l3ca_caps = self.cap.get_type("l3ca")
            return 2**l3ca_caps.num_ways - 1
        except Exception as ex:
            log.error(str(ex))
            return None