Skip to content

Commit 908d5d3

Browse files
authored
Refactor ProfilerState to a dataclass (#833)
1 parent 715cbdf commit 908d5d3

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

gprofiler/profiler_state.py

+15-47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dataclasses import dataclass
12
from threading import Event
23
from typing import List, Optional
34

@@ -7,59 +8,26 @@
78
from gprofiler.utils import TemporaryDirectoryWithMode
89

910

11+
@dataclass
1012
class ProfilerState:
1113
# Class for storing generic state parameters. These parameters are the same for each profiler.
1214
# Thanks to that class adding new state parameters to profilers won't result changing code in every profiler.
13-
def __init__(
14-
self,
15-
*,
16-
stop_event: Event,
17-
storage_dir: str,
18-
profile_spawned_processes: bool,
19-
insert_dso_name: bool,
20-
profiling_mode: str,
21-
container_names_client: Optional[ContainerNamesClient],
22-
processes_to_profile: Optional[List[Process]],
23-
) -> None:
24-
self._stop_event = stop_event
25-
self._profile_spawned_processes = profile_spawned_processes
26-
self._insert_dso_name = insert_dso_name
27-
self._profiling_mode = profiling_mode
28-
self._temporary_dir = TemporaryDirectoryWithMode(dir=storage_dir, mode=0o755)
29-
self._storage_dir = self._temporary_dir.name
30-
self._container_names_client = container_names_client
31-
self._processes_to_profile = processes_to_profile
15+
# TODO: as soon as Python3.10 is adopted, turn this dataclass into kw_only
3216

33-
@property
34-
def stop_event(self) -> Event:
35-
return self._stop_event
17+
stop_event: Event
18+
storage_dir: str
19+
profile_spawned_processes: bool
20+
insert_dso_name: bool
21+
profiling_mode: str
22+
container_names_client: Optional[ContainerNamesClient]
23+
processes_to_profile: Optional[List[Process]]
3624

37-
@property
38-
def storage_dir(self) -> str:
39-
return str(self._storage_dir)
40-
41-
@property
42-
def profile_spawned_processes(self) -> bool:
43-
return self._profile_spawned_processes
44-
45-
@property
46-
def insert_dso_name(self) -> bool:
47-
return self._insert_dso_name
48-
49-
@property
50-
def profiling_mode(self) -> str:
51-
return self._profiling_mode
52-
53-
@property
54-
def container_names_client(self) -> Optional[ContainerNamesClient]:
55-
return self._container_names_client
56-
57-
@property
58-
def processes_to_profile(self) -> Optional[List[Process]]:
59-
return self._processes_to_profile
25+
def __post_init__(self) -> None:
26+
self._temporary_dir = TemporaryDirectoryWithMode(dir=self.storage_dir, mode=0o755)
27+
self.storage_dir = self._temporary_dir.name
6028

6129
def get_container_name(self, pid: int) -> str:
62-
if self._container_names_client is not None:
63-
return self._container_names_client.get_container_name(pid)
30+
if self.container_names_client is not None:
31+
return self.container_names_client.get_container_name(pid)
6432
else:
6533
return ""

0 commit comments

Comments
 (0)