def test_get_pids(self, lib): "Tests get_pids() method." pids_uint = [ctypes.c_uint(pid) for pid in [1000, 1500, 3000, 5600]] pid_array = ctypes_build_array(pids_uint) def pqos_pid_get_pid_assoc_m(class_id, count_ref): "Mock pqos_pid_get_pid_assoc()." self.assertEqual(class_id, 7) ctypes_ref_set_uint(count_ref, len(pid_array)) return ctypes.cast(pid_array, ctypes.POINTER(ctypes.c_uint)) func_mock = MagicMock(side_effect=pqos_pid_get_pid_assoc_m) lib.pqos_pid_get_pid_assoc = func_mock alloc = PqosAlloc() with patch('pqos.allocation.free_memory'): pids = alloc.get_pids(7) lib.pqos_pid_get_pid_assoc.assert_called_once() self.assertEqual(len(pids), 4) self.assertEqual(pids[0], 1000) self.assertEqual(pids[1], 1500) self.assertEqual(pids[2], 3000) self.assertEqual(pids[3], 5600)
def test_assign_pid(self, lib): "Tests assign_pid() method." def pqos_alloc_assign_pid_m(mask, pid_array, pid_array_len, class_id_ref): "Mock pqos_alloc_assign_pid()." l2ca_mask = 1 << CPqosCapability.PQOS_CAP_TYPE_L2CA l3ca_mask = 1 << CPqosCapability.PQOS_CAP_TYPE_L3CA expected_mask = l2ca_mask | l3ca_mask self.assertEqual(mask, expected_mask) self.assertEqual(pid_array_len, 4) self.assertEqual(pid_array[0], 1000) self.assertEqual(pid_array[1], 1200) self.assertEqual(pid_array[2], 2300) self.assertEqual(pid_array[3], 5000) ctypes_ref_set_uint(class_id_ref, 3) return 0 func_mock = MagicMock(side_effect=pqos_alloc_assign_pid_m) lib.pqos_alloc_assign_pid = func_mock alloc = PqosAlloc() class_id = alloc.assign_pid(['l2ca', 'l3ca'], [1000, 1200, 2300, 5000]) lib.pqos_alloc_assign_pid.assert_called_once() self.assertEqual(class_id, 3)
def test_assign(self, lib): "Tests assign() method." def pqos_alloc_assign_m(mask, core_array, core_array_len, class_id_ref): "Mock pqos_alloc_assign()." mba_mask = 1 << CPqosCapability.PQOS_CAP_TYPE_MBA l3ca_mask = 1 << CPqosCapability.PQOS_CAP_TYPE_L3CA expected_mask = mba_mask | l3ca_mask self.assertEqual(mask, expected_mask) self.assertEqual(core_array_len, 4) self.assertEqual(core_array[0], 1) self.assertEqual(core_array[1], 2) self.assertEqual(core_array[2], 4) self.assertEqual(core_array[3], 7) ctypes_ref_set_uint(class_id_ref, 3) return 0 func_mock = MagicMock(side_effect=pqos_alloc_assign_m) lib.pqos_alloc_assign = func_mock alloc = PqosAlloc() class_id = alloc.assign(['mba', 'l3ca'], [1, 2, 4, 7]) lib.pqos_alloc_assign.assert_called_once() self.assertEqual(class_id, 3)
def test_assoc_set_pid(self, lib): "Tests assoc_set_pid() method." # pylint: disable=no-self-use lib.pqos_alloc_assoc_set_pid = MagicMock(return_value=0) alloc = PqosAlloc() alloc.assoc_set_pid(2, 1) lib.pqos_alloc_assoc_set_pid.assert_called_once_with(2, 1)
def reset_allocation(): """ Resets allocation configuration. """ alloc = PqosAlloc() try: alloc.reset('any', 'any', 'any') print("Allocation reset successful") except: print("Allocation reset failed!")
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 set_allocation_class(class_id, cores): """ Sets up allocation classes of service on selected CPUs Parameters: class_id: class of service ID cores: a list of cores """ alloc = PqosAlloc() for core in cores: try: alloc.assoc_set(core, class_id) except: print("Setting allocation class of service association failed!")
def test_assoc_get(self, lib): "Tests assoc_get() method." def pqos_allc_assoc_get_m(core, class_id_ref): "Mock pqos_alloc_assoc_get()." self.assertEqual(core, 2) ctypes_ref_set_uint(class_id_ref, 5) return 0 lib.pqos_alloc_assoc_get = MagicMock(side_effect=pqos_allc_assoc_get_m) alloc = PqosAlloc() class_id = alloc.assoc_get(2) lib.pqos_alloc_assoc_get.assert_called_once() self.assertEqual(class_id, 5)
def test_assoc_get_pid(self, lib): "Tests assoc_get_pid() method." def pqos_allc_assoc_get_pid_m(pid, class_id_ref): "Mock pqos_alloc_assoc_get_pid()." self.assertEqual(pid, 200) ctypes_ref_set_uint(class_id_ref, 1) return 0 func_mock = MagicMock(side_effect=pqos_allc_assoc_get_pid_m) lib.pqos_alloc_assoc_get_pid = func_mock alloc = PqosAlloc() class_id = alloc.assoc_get_pid(200) lib.pqos_alloc_assoc_get_pid.assert_called_once() self.assertEqual(class_id, 1)
def test_reset(self, lib): "Tests reset() method." def pqos_alloc_reset_m(l3_cdp_cfg, l2_cdp_cfg, mba_cfg): "Mock pqos_alloc_reset()." self.assertEqual(l3_cdp_cfg, 1) self.assertEqual(l2_cdp_cfg, 2) self.assertEqual(mba_cfg, 2) return 0 func_mock = MagicMock(side_effect=pqos_alloc_reset_m) lib.pqos_alloc_reset = func_mock alloc = PqosAlloc() alloc.reset('on', 'any', 'ctrl') lib.pqos_alloc_reset.assert_called_once()
def test_release(self, lib): "Tests release() method." def pqos_alloc_release_m(core_array, core_array_len): "Mock pqos_alloc_release()." self.assertEqual(core_array_len, 3) self.assertEqual(core_array[0], 2) self.assertEqual(core_array[1], 3) self.assertEqual(core_array[2], 5) return 0 func_mock = MagicMock(side_effect=pqos_alloc_release_m) lib.pqos_alloc_release = func_mock alloc = PqosAlloc() alloc.release([2, 3, 5]) lib.pqos_alloc_release.assert_called_once()
def test_release_pid(self, lib): "Tests release_pid() method." def pqos_alloc_release_pid_m(pid_array, pid_array_len): "Mock pqos_alloc_release_pid()." self.assertEqual(pid_array_len, 4) self.assertEqual(pid_array[0], 1234) self.assertEqual(pid_array[1], 5432) self.assertEqual(pid_array[2], 7568) self.assertEqual(pid_array[3], 4545) return 0 func_mock = MagicMock(side_effect=pqos_alloc_release_pid_m) lib.pqos_alloc_release_pid = func_mock alloc = PqosAlloc() alloc.release_pid([1234, 5432, 7568, 4545]) lib.pqos_alloc_release_pid.assert_called_once()
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 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
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
# 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. ################################################################################ import os from pqos import Pqos from pqos.allocation import PqosAlloc # Initialize PQoS library pqos = Pqos() pqos.init("OS") alloc = PqosAlloc() # Get PID of the current process pid = os.getpid() # Associate process with COS 1 print('Associating process %d with COS 1...' % pid) alloc.assoc_set_pid(pid, 1) # Get all processes associated with COS 1 print('Processes associated with COS 1:') print(alloc.get_pids(1)) # Release association print('Releasing association for process %d...' % pid) alloc.release_pid([pid])