11from __future__ import annotations
22
33import os
4+ import sys
45from contextlib import suppress
56from datetime import datetime , timezone
67from logging import getLogger
7- from typing import Annotated , Any
8+ from typing import Annotated
89
910import psutil
1011from pydantic import BaseModel , ConfigDict , Field , PlainSerializer , PlainValidator
1314
1415logger = getLogger (__name__ )
1516
17+ if sys .platform == 'linux' :
18+ """Get the most suitable available used memory metric.
19+
20+ `Proportional Set Size (PSS)`, is the amount of own memory and memory shared with other processes, accounted in a
21+ way that the shared amount is divided evenly between the processes that share it. Available on Linux. Suitable for
22+ avoiding overestimation by counting the same shared memory used by children processes multiple times.
23+
24+ `Resident Set Size (RSS)` is the non-swapped physical memory a process has used; it includes shared memory. It
25+ should be available everywhere.
26+ """
27+
28+ def _get_used_memory (process : psutil .Process ) -> int :
29+ return int (process .memory_full_info ().pss )
30+ else :
31+
32+ def _get_used_memory (process : psutil .Process ) -> int :
33+ return int (process .memory_info ().rss )
34+
1635
1736class CpuInfo (BaseModel ):
1837 """Information about the CPU usage."""
@@ -88,14 +107,14 @@ def get_memory_info() -> MemoryInfo:
88107 current_process = psutil .Process (os .getpid ())
89108
90109 # Retrieve estimated memory usage of the current process.
91- current_size_bytes = int ( _get_used_memory (current_process . memory_full_info ()) )
110+ current_size_bytes = _get_used_memory (current_process )
92111
93112 # Sum memory usage by all children processes, try to exclude shared memory from the sum if allowed by OS.
94113 for child in current_process .children (recursive = True ):
95114 # Ignore any NoSuchProcess exception that might occur if a child process ends before we retrieve
96115 # its memory usage.
97116 with suppress (psutil .NoSuchProcess ):
98- current_size_bytes += _get_used_memory (child . memory_full_info () )
117+ current_size_bytes += _get_used_memory (child )
99118
100119 vm = psutil .virtual_memory ()
101120
@@ -104,20 +123,3 @@ def get_memory_info() -> MemoryInfo:
104123 current_size = ByteSize (current_size_bytes ),
105124 system_wide_used_size = ByteSize (vm .total - vm .available ),
106125 )
107-
108-
109- def _get_used_memory (memory_full_info : Any ) -> int :
110- """Get the most suitable available used memory metric.
111-
112- `Proportional Set Size (PSS)`, is the amount of own memory and memory shared with other processes, accounted in a
113- way that the shared amount is divided evenly between the processes that share it. Available on Linux. Suitable for
114- avoiding overestimation by counting the same shared memory used by children processes multiple times.
115-
116- `Resident Set Size (RSS)` is the non-swapped physical memory a process has used; it includes shared memory. It
117- should be available everywhere.
118- """
119- try :
120- # Linux
121- return int (memory_full_info .pss )
122- except AttributeError :
123- return int (memory_full_info .rss )
0 commit comments