-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: added utility function to retrieve CPU index from PID
- Loading branch information
1 parent
5005467
commit 82f6ab3
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
import builtins | ||
import os | ||
import pytest | ||
from unittest.mock import patch | ||
|
||
from zeus.device.cpu import get_current_cpu_index | ||
|
||
class MockFile: | ||
def __init__(self, file_path): | ||
self.file_path = file_path | ||
|
||
def read(self, *args, **kwargs): | ||
with open(self.file_path, "r") as file: | ||
return file.read() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
pass | ||
|
||
|
||
@pytest.fixture | ||
def mock_linux_files(): | ||
files_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../files")) | ||
mocked_stat_file_path = os.path.join(files_folder, "stat") | ||
mocked_phys_package_path = os.path.join(files_folder, "physical_package_id") | ||
mocked_stat_file = MockFile(mocked_stat_file_path) | ||
mocked_phys_package_file = MockFile(mocked_phys_package_path) | ||
|
||
real_open = builtins.open | ||
|
||
def mock_file_open(filepath, *args, **kwargs): | ||
if filepath == "/proc/515/stat": | ||
return mocked_stat_file | ||
elif filepath == "/sys/devices/system/cpu/cpu24/topology/physical_package_id": | ||
return mocked_phys_package_file | ||
else: | ||
return real_open(filepath, *args, **kwargs) | ||
|
||
patch_open = patch("builtins.open", side_effect=mock_file_open) | ||
|
||
patch_open.start() | ||
|
||
yield | ||
|
||
patch_open.stop() | ||
|
||
def test_get_current_cpu_index(mock_linux_files): | ||
assert(get_current_cpu_index(515) == 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6511 (cat) R 6501 6511 6501 34816 6511 4194304 95 0 0 0 0 0 0 0 20 0 1 0 7155 6053888 255 18446744073709551615 94288847015936 94288847031350 140727519907328 0 0 0 0 0 0 0 0 0 17 24 0 0 0 0 0 94288847043296 94288847044712 94288866566144 140727519913901 140727519913921 140727519913921 140727519916011 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters