def test_get_measurements(*mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) assert { 'memory_bandwidth': 2, 'llc_occupancy': 2 } == resgroup.get_measurements()
class Container: cgroup_path: str rdt_enabled: bool = True def __post_init__(self): self.cgroup = Cgroup(self.cgroup_path) self.perf_counters = PerfCounters(self.cgroup_path, event_names=DEFAULT_EVENTS) self.resgroup = ResGroup(self.cgroup_path) if self.rdt_enabled else None def sync(self): if self.rdt_enabled: self.resgroup.sync() def get_measurements(self) -> Measurements: try: return flatten_measurements([ self.cgroup.get_measurements(), self.resgroup.get_measurements() if self.rdt_enabled else {}, self.perf_counters.get_measurements(), ]) except FileNotFoundError: log.debug('Could not read measurements for container %s. ' 'Probably the mesos container has died during the current runner iteration.', self.cgroup_path) # Returning empty measurements. return {} def cleanup(self): if self.rdt_enabled: self.resgroup.cleanup() self.perf_counters.cleanup()
def test_sync(*args): resctrl_file_mock_simple_name = MagicMock() resctrl_file_mock_complex_name = MagicMock() open_mock = create_open_mock({ "/sys/fs/resctrl": "0", "/sys/fs/cgroup/cpu/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd/tasks": resctrl_file_mock_simple_name, "/sys/fs/cgroup/cpu/ddd/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd-ddd/tasks": resctrl_file_mock_complex_name, }) with patch('builtins.open', open_mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() resctrl_file_mock_simple_name.assert_called_once_with( '/sys/fs/resctrl/mon_groups/ddd/tasks', 'w') resctrl_file_mock_simple_name.assert_has_calls( [call().__enter__().write('123')]) with patch('builtins.open', open_mock): cgroup_path = "/ddd/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() resctrl_file_mock_complex_name.assert_called_once_with( '/sys/fs/resctrl/mon_groups/ddd-ddd/tasks', 'w') resctrl_file_mock_complex_name.assert_has_calls( [call().__enter__().write('123')])
def test_sync_flush_exception(SetEffectiveRootUid_mock, makedirs_mock, exists_mock, log_warning_mock): resctrl_file_mock = Mock( # open function return_value=Mock( # handler write=Mock( # write side_effect=ProcessLookupError ) ) ) open_mock = create_open_mock({ "/sys/fs/resctrl": "0", "/sys/fs/cgroup/cpu/ddd/tasks": "123", "/sys/fs/resctrl/mon_groups/ddd/tasks": resctrl_file_mock, }) with patch('builtins.open', open_mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() log_warning_mock.assert_any_call('sync: Unsuccessful synchronization attempts. Ignoring.')
def test_sync_resctrl_not_mounted(exists_mock, log_warning_mock): cgroup_path = "/ddd" resgroup = ResGroup(cgroup_path) resgroup.sync() log_warning_mock.assert_called_once_with('Resctrl not mounted, ignore sync!')
def test_sync_no_space_left_on_device(makedirs_mock, exists_mock, log_warning_mock): resgroup = ResGroup("/ddd") with pytest.raises(Exception, match='Limit of workloads reached'): resgroup.sync()
def __post_init__(self): self.cgroup = Cgroup(self.cgroup_path) self.perf_counters = PerfCounters(self.cgroup_path, event_names=DEFAULT_EVENTS) self.resgroup = ResGroup(self.cgroup_path) if self.rdt_enabled else None