Salome HOME
64afc42aae5fac4b783f8bdef8c6631a397fc403
[modules/kernel.git] / src / KERNEL_PY / salome_psutil.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 '''
22 SALOME utilities for process management.
23 '''
24
25 import os
26 import psutil
27
28
29 def getNumberOfCPUCores(): # pragma pylint: disable=invalid-name
30     '''
31     Get number of CPU cores in the calculation node.
32     :return Number of cores
33     '''
34     return psutil.cpu_count(logical=True)
35
36
37 def loadOfCPUCores(script=None): # pragma pylint: disable=invalid-name
38     '''
39     Get a load of each CPU core in the calculation node.
40
41     A script to compute loads can be customized via `script` parameter.
42     In that case, the script must either set `cpu_loads` variable (which
43     should be of list type), or specify `getCPULoads()` function returning
44     list as result. In both cases, the list's size must be equal to the value
45     returning by method `getNumberOfCPUCores()`, and each value in this list
46     must be in range [0, 1], otherwise exception is raised.
47
48     If `script` is not specified, default implementation is used.
49
50     :param script Custom script to calculate loads
51     :return List that contains loads of each CPU core.
52     '''
53     if not script:
54         return [x/100 for x in psutil.cpu_percent(interval=None, percpu=True)]
55     cpu_loads, loc = None, {}
56     exec(script, globals(), loc) # pragma pylint: disable=exec-used
57     cpu_loads = loc['getCPULoads']() if 'getCPULoads' in loc else loc.get('cpu_loads')
58     if cpu_loads is None:
59         raise ValueError('Bad script. Specify `getCPULoads` function or `cpu_loads` variable')
60     if not isinstance(cpu_loads, (list, tuple)):
61         raise TypeError('Bad script. Result must be list or tuple.')
62     size = getNumberOfCPUCores()
63     if len(cpu_loads) != size:
64         raise ValueError('Bad script. Result is of incorrect length (must be {})'.format(size))
65     values = [i for i in cpu_loads if 0 <= i <= 1]
66     if len(values) != size:
67         raise ValueError('Bad script. Values are not in [0, 1] range')
68     return [i for i in cpu_loads]
69
70
71 def getTotalPhysicalMemory(): # pragma pylint: disable=invalid-name
72     '''
73     Get total physical memory of the calculation node.
74     :return Size of physical memory, in megabytes.
75     '''
76     return int(psutil.virtual_memory().total/1024/1024)
77
78
79 def getTotalPhysicalMemoryInUse(): # pragma pylint: disable=invalid-name
80     '''
81     Get used physical memory of the calculation node.
82     :return Size of used physical memory, in megabytes.
83     '''
84     return int(psutil.virtual_memory().used/1024/1024)
85
86
87 def getTotalPhysicalMemoryInUseByMe(): # pragma pylint: disable=invalid-name
88     '''
89     Get physical memory occupied by the current process.
90     :return Size of physical memory, used by current process, in megabytes.
91     '''
92     process = psutil.Process(os.getpid())
93     memory_in_mb = int(process.memory_info().rss/1024/1024)
94     return memory_in_mb