Example #1
0
def test_cpu_freq_procfs(tmp_path: pathlib.Path) -> None:
    populate_directory(
        str(tmp_path),
        {
            "sysfs": {
                "devices": {
                    "system": {
                        "cpu": {}
                    }
                }
            },
            "procfs": {
                "cpuinfo":
                """
core id: 0
physical id: 0
cpu MHz: 2000

core id: 1
physical id: 0
cpu MHz: 2500
"""
            },
        },
    )

    with replace_info_directories(sysfs=str(tmp_path / "sysfs"),
                                  procfs=str(tmp_path / "procfs")):
        assert pypsutil.percpu_freq() == [  # type: ignore
            pypsutil.CPUFrequencies(current=2000, min=0, max=0),
            pypsutil.CPUFrequencies(current=2500, min=0, max=0),
        ]

        assert pypsutil.cpu_freq() == (  # type: ignore
            pypsutil.CPUFrequencies(current=2250, min=0, max=0))
Example #2
0
    def test_percpu_freq_range() -> None:
        # pylint: disable=no-member
        all_freqs = pypsutil.percpu_freq()  # type: ignore[attr-defined]
        if not all_freqs:
            pytest.skip("Unable to determine CPU frequencies")

        for freqs in all_freqs:
            assert freqs.current > 0
            assert freqs.min == 0.0 or freqs.current >= freqs.min
            assert freqs.max == 0.0 or freqs.current <= freqs.max
Example #3
0
def test_percpu_info() -> None:
    # pylint: disable=no-member

    ncpus = os.cpu_count()
    if not ncpus:
        pytest.skip("Unable to detect number of CPUs")

    if hasattr(pypsutil, "percpu_times"):
        assert len(
            pypsutil.percpu_times()) == ncpus  # type: ignore[attr-defined]

    if hasattr(pypsutil, "percpu_freq"):
        assert len(pypsutil.percpu_freq()) in (0, ncpus
                                               )  # type: ignore[attr-defined]
Example #4
0
def test_cpu_freq_none(tmp_path: pathlib.Path) -> None:
    populate_directory(
        str(tmp_path),
        {
            "sysfs": {},
            "procfs": {}
        },
    )

    with replace_info_directories(sysfs=str(tmp_path / "sysfs"),
                                  procfs=str(tmp_path / "procfs")):
        assert pypsutil.percpu_freq() == []  # type: ignore

        assert pypsutil.cpu_freq() is None  # type: ignore
Example #5
0
def test_cpu_freq_sysfs(tmp_path: pathlib.Path) -> None:
    populate_directory(
        str(tmp_path),
        {
            "devices": {
                "system": {
                    "cpu": {
                        "cpu0": {
                            "cpufreq": {
                                "scaling_min_freq": "1000000",
                                "scaling_cur_freq": "2000000",
                                "scaling_max_freq": "3000000",
                            },
                        },
                        "cpu1": {
                            "cpufreq": {
                                "scaling_min_freq": "1500000",
                                "scaling_cur_freq": "2500000",
                                "scaling_max_freq": "3500000",
                            },
                        },
                        "online": "0-4",
                    }
                }
            }
        },
    )

    with replace_info_directories(sysfs=str(tmp_path)):
        assert pypsutil.percpu_freq() == [  # type: ignore
            pypsutil.CPUFrequencies(current=2000, min=1000, max=3000),
            pypsutil.CPUFrequencies(current=2500, min=1500, max=3500),
        ]

        assert pypsutil.cpu_freq() == (  # type: ignore
            pypsutil.CPUFrequencies(current=2250, min=1250, max=3250))
Example #6
0
 def cpu_freq(
     percpu: bool = False,
 ) -> Union[pypsutil.CPUFrequencies, List[pypsutil.CPUFrequencies]]:
     return pypsutil.percpu_freq() if percpu else pypsutil.cpu_freq()  # type: ignore