Describe the bug
The Hardware CPU Module and the RAM Module show inconsistent behavior how the handle child processes.
While RAM uses the Memory of all child process
|
def process_memory_GB(self): |
|
""" |
|
Property to compute the process's total memory usage in bytes. |
|
|
|
Returns: |
|
float: RAM usage (GB) |
|
""" |
|
children_memories = self._get_children_memories() if self._children else [] |
|
main_memory = psutil.Process(self._pid).memory_info().rss |
|
memories = children_memories + [main_memory] |
|
return sum([m for m in memories if m] + [0]) / B_TO_GB |
CPU Limits itself to the main process
|
elif self._tracking_mode == "process": |
|
|
|
cpu_load = self._process.cpu_percent(interval=0.5) / self._cpu_count |
|
power = self._tdp * cpu_load / 100 |
|
logger.debug( |
|
f"CPU load {self._tdp} W and {cpu_load * 100:.1f}% => estimation of {power} W for process {self._pid}." |
|
) |
Expected behavior
Similar behavior for booth, I would be in favor of having "process" and "process_tree" as option
Solution
Adding a "process_tree" option or exchanging the CPU Part with something similar to this:
elif self._tracking_mode == "process_tree":
# Sum CPU percent for process and its children
total_cpu_load = self._process.cpu_percent(interval=0.5)
for child in self._process.children(recursive=True):
try:
total_cpu_load += child.cpu_percent(interval=0.0)
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
cpu_load = total_cpu_load / self._cpu_count
power = self._tdp * cpu_load / 100
logger.debug(
f"CPU load {self._tdp} W and {cpu_load * 100:.1f}% => estimation of {power} W for process tree {self._pid}."
)
Cheers
Dominik
Describe the bug
The Hardware CPU Module and the RAM Module show inconsistent behavior how the handle child processes.
While RAM uses the Memory of all child process
codecarbon/codecarbon/external/ram.py
Lines 288 to 298 in 66cc372
CPU Limits itself to the main process
codecarbon/codecarbon/external/hardware.py
Lines 247 to 253 in 66cc372
Expected behavior
Similar behavior for booth, I would be in favor of having "process" and "process_tree" as option
Solution
Adding a "process_tree" option or exchanging the CPU Part with something similar to this:
Cheers
Dominik