def test_collect_topology_information(filename, expected_cpus, expected_cores,
                                      expected_sockets):
    with patch('builtins.open',
               new=create_open_mock(
                   {"/proc/cpuinfo": open(relative_module_path(__file__, filename)).read()})
               ):
        cpuinfo = _parse_cpuinfo()
        got_cpus, got_cores, got_sockets, got_topology = collect_topology_information(cpuinfo)
        assert got_cpus == expected_cpus
        assert got_cores == expected_cores
        assert got_sockets == expected_sockets
Esempio n. 2
0
    def _initialize(self) -> Optional[int]:
        """Check privileges, RDT availability and prepare internal state.
        Can return error code that should stop Runner.
        """
        if not security.are_privileges_sufficient():
            log.error(
                "Insufficient privileges! "
                "Impossible to use perf_event_open/resctrl subsystems. "
                "For unprivileged user it is needed to: "
                "adjust /proc/sys/kernel/perf_event_paranoid (set to -1), "
                "has CAP_DAC_OVERRIDE and CAP_SETUID capabilities and"
                "SECBIT_NO_SETUID_FIXUP secure bit set.")
            return 1

        # Initialization (auto discovery Intel RDT features).

        rdt_available = resctrl.check_resctrl()
        if self._rdt_enabled is None:
            self._rdt_enabled = rdt_available
            log.info('RDT enabled (auto configuration): %s', self._rdt_enabled)
        elif self._rdt_enabled is True and not rdt_available:
            log.error('RDT explicitly enabled but not available - exiting!')
            return 1

        if self._rdt_enabled:
            # Resctrl is enabled and available, call a placeholder to allow further initialization.
            rdt_initialization_ok = self._initialize_rdt()
            if not rdt_initialization_ok:
                return 1

        # Postpone the container manager initialization after rdt checks were performed.
        platform_cpus, _, platform_sockets = platforms.collect_topology_information(
        )

        platform, _, _ = platforms.collect_platform_information(
            self._rdt_enabled)
        rdt_information = platform.rdt_information

        self._event_names = _filter_out_event_names_for_cpu(
            self._event_names, platform.cpu_codename)

        # We currently do not support RDT without monitoring.
        if self._rdt_enabled and not rdt_information.is_monitoring_enabled():
            log.error('RDT monitoring is required - please enable CAT '
                      'or MBM with kernel parameters!')
            return 1

        self._containers_manager = ContainerManager(
            platform=platform,
            allocation_configuration=self._allocation_configuration,
            event_names=self._event_names,
            enable_derived_metrics=self._enable_derived_metrics,
        )
        return None