1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 SALOME utilities for process management.
29 def getNumberOfCPUCores(): # pragma pylint: disable=invalid-name
31 Get number of CPU cores in the calculation node.
32 :return Number of cores
34 return psutil.cpu_count(logical=True)
37 def loadOfCPUCores(script=None): # pragma pylint: disable=invalid-name
39 Get a load of each CPU core in the calculation node.
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.
48 If `script` is not specified, default implementation is used.
50 :param script Custom script to calculate loads
51 :return List that contains loads of each CPU core.
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')
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]
71 def getTotalPhysicalMemory(): # pragma pylint: disable=invalid-name
73 Get total physical memory of the calculation node.
74 :return Size of physical memory, in megabytes.
76 return int(psutil.virtual_memory().total/1024/1024)
79 def getTotalPhysicalMemoryInUse(): # pragma pylint: disable=invalid-name
81 Get used physical memory of the calculation node.
82 :return Size of used physical memory, in megabytes.
84 return int(psutil.virtual_memory().used/1024/1024)
87 def getTotalPhysicalMemoryInUseByMe(): # pragma pylint: disable=invalid-name
89 Get physical memory occupied by the current process.
90 :return Size of physical memory, used by current process, in megabytes.
92 process = psutil.Process(os.getpid())
93 memory_in_mb = int(process.memory_info().rss/1024/1024)