We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Running Mars under docker can use psutil.virtual_memory() directly
**Is your feature request related to a problem? Please describe.**
When running in Docker containers with ``MARS_USE_PROCESS_STAT`` on, both cpu and memory usage are calculated by summing results of all processes. This can be effective for cpu. However, this method may produce larger memory usages than actual.

**Describe the solution you'd like**
We can specify different methods to measure cpu and memory usage to obtain more accurate memory usage for most of the cases.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mars/resource.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Copyright 1999-2018 Alibaba Group Holding Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 import subprocess  # nosec
18 import sys
19 import time
20 from collections import namedtuple
21 
22 import psutil
23 
24 from .lib import nvutils
25 
26 _proc = psutil.Process()
27 _timer = getattr(time, 'monotonic', time.time)
28 
29 if 'MARS_USE_PROCESS_STAT' in os.environ:
30     _use_process_stat = bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('"')))
31 else:
32     _use_process_stat = False
33 
34 if 'MARS_CPU_TOTAL' in os.environ:
35     _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('"'))
36 else:
37     _cpu_total = psutil.cpu_count(logical=True)
38 
39 if 'MARS_MEMORY_TOTAL' in os.environ:
40     _mem_total = int(os.environ['MARS_MEMORY_TOTAL'].strip('"'))
41 else:
42     _mem_total = None
43 
44 _virt_memory_stat = namedtuple('virtual_memory', 'total available percent used free')
45 
46 _shm_path = [pt.mountpoint for pt in psutil.disk_partitions(all=True)
47              if pt.mountpoint in ('/tmp', '/dev/shm') and pt.fstype == 'tmpfs']
48 if not _shm_path:
49     _shm_path = None
50 else:
51     _shm_path = _shm_path[0]
52 
53 
54 def virtual_memory():
55     sys_mem = psutil.virtual_memory()
56     if not _use_process_stat:
57         total = sys_mem.total
58         used = sys_mem.used + getattr(sys_mem, 'shared', 0)
59         available = sys_mem.available
60         free = sys_mem.free
61         percent = 100.0 * (total - available) / total
62         return _virt_memory_stat(total, available, percent, used, free)
63     else:
64         used = 0
65         for p in psutil.process_iter():
66             try:
67                 used += p.memory_info().rss
68             except (psutil.NoSuchProcess, psutil.AccessDenied):
69                 pass
70 
71         if _shm_path:
72             shm_stats = psutil.disk_usage(_shm_path)
73             used += shm_stats.used
74 
75         total = min(_mem_total or sys_mem.total, sys_mem.total)
76         # TODO sys_mem.available does not work in container
77         # available = min(sys_mem.available, total - used)
78         available = total - used
79         free = min(sys_mem.free, total - used)
80         percent = 100.0 * (total - available) / total
81         return _virt_memory_stat(total, available, percent, used, free)
82 
83 
84 def cpu_count():
85     return _cpu_total
86 
87 
88 _last_cpu_measure = None
89 
90 
91 def _take_process_cpu_snapshot():
92     num_cpus = cpu_count() or 1
93 
94     def timer():
95         return _timer() * num_cpus
96 
97     processes = [p for p in psutil.process_iter() if p.pid != _proc.pid]
98 
99     pts = dict()
100     sts = dict()
101     for p in processes:
102         try:
103             pts[p.pid] = p.cpu_times()
104             sts[p.pid] = timer()
105         except (psutil.NoSuchProcess, psutil.AccessDenied):
106             pass
107 
108     pts[_proc.pid] = _proc.cpu_times()
109     sts[_proc.pid] = timer()
110     return pts, sts
111 
112 
113 def cpu_percent():
114     global _last_cpu_measure
115     if not _use_process_stat:
116         return sum(psutil.cpu_percent(percpu=True))
117 
118     num_cpus = cpu_count() or 1
119     pts, sts = _take_process_cpu_snapshot()
120 
121     if _last_cpu_measure is None:
122         _last_cpu_measure = (pts, sts)
123         return None
124 
125     old_pts, old_sts = _last_cpu_measure
126 
127     percents = []
128     for pid in pts:
129         if pid not in old_pts:
130             continue
131         pt1 = old_pts[pid]
132         pt2 = pts[pid]
133         delta_proc = (pt2.user - pt1.user) + (pt2.system - pt1.system)
134         delta_time = sts[pid] - old_sts[pid]
135 
136         try:
137             overall_cpus_percent = (delta_proc / delta_time) * 100
138         except ZeroDivisionError:
139             percents.append(0.0)
140         else:
141             single_cpu_percent = overall_cpus_percent * num_cpus
142             percents.append(single_cpu_percent)
143     _last_cpu_measure = (pts, sts)
144     return round(sum(percents), 1)
145 
146 
147 def disk_usage(d):
148     return psutil.disk_usage(d)
149 
150 
151 def iowait():
152     cpu_percent = psutil.cpu_times_percent()
153     try:
154         return cpu_percent.iowait
155     except AttributeError:
156         return None
157 
158 
159 _last_disk_io_meta = None
160 _win_diskperf_called = False
161 
162 
163 def disk_io_usage():
164     global _last_disk_io_meta, _win_diskperf_called
165 
166     # Needed by psutil.disk_io_counters() under newer version of Windows.
167     # diskperf -y need to be called or no disk information can be found.
168     if sys.platform == 'win32' and not _win_diskperf_called:  # pragma: no cover
169         CREATE_NO_WINDOW = 0x08000000
170         try:
171             proc = subprocess.Popen(['diskperf', '-y'], shell=False,
172                                     creationflags=CREATE_NO_WINDOW)  # nosec
173             proc.wait()
174         except (subprocess.CalledProcessError, OSError):
175             pass
176         _win_diskperf_called = True
177 
178     disk_counters = psutil.disk_io_counters()
179     tst = time.time()
180 
181     read_bytes = disk_counters.read_bytes
182     write_bytes = disk_counters.write_bytes
183     if _last_disk_io_meta is None:
184         _last_disk_io_meta = (read_bytes, write_bytes, tst)
185         return None
186 
187     last_read_bytes, last_write_bytes, last_time = _last_disk_io_meta
188     delta_time = tst - last_time
189     read_speed = (read_bytes - last_read_bytes) / delta_time
190     write_speed = (write_bytes - last_write_bytes) / delta_time
191 
192     _last_disk_io_meta = (read_bytes, write_bytes, tst)
193     return read_speed, write_speed
194 
195 
196 _last_net_io_meta = None
197 
198 
199 def net_io_usage():
200     global _last_net_io_meta
201 
202     net_counters = psutil.net_io_counters()
203     tst = time.time()
204 
205     send_bytes = net_counters.bytes_sent
206     recv_bytes = net_counters.bytes_recv
207     if _last_net_io_meta is None:
208         _last_net_io_meta = (send_bytes, recv_bytes, tst)
209         return None
210 
211     last_send_bytes, last_recv_bytes, last_time = _last_net_io_meta
212     delta_time = tst - last_time
213     recv_speed = (recv_bytes - last_recv_bytes) / delta_time
214     send_speed = (send_bytes - last_send_bytes) / delta_time
215 
216     _last_net_io_meta = (send_bytes, recv_bytes, tst)
217     return recv_speed, send_speed
218 
219 
220 _cuda_info = namedtuple('cuda_info', 'driver_version cuda_version products gpu_count')
221 _cuda_card_stat = namedtuple('cuda_card_stat', 'product_name gpu_usage temperature fb_mem_info')
222 
223 
224 def cuda_info():  # pragma: no cover
225     driver_info = nvutils.get_driver_info()
226     if not driver_info:
227         return
228     gpu_count = nvutils.get_device_count()
229     return _cuda_info(
230         driver_version=driver_info.driver_version,
231         cuda_version=driver_info.cuda_version,
232         products=[nvutils.get_device_info(idx).name for idx in range(gpu_count)],
233         gpu_count=gpu_count,
234     )
235 
236 
237 def cuda_card_stats():  # pragma: no cover
238     infos = []
239     device_count = nvutils.get_device_count()
240     if not device_count:
241         return
242     for device_idx in range(device_count):
243         device_info = nvutils.get_device_info(device_idx)
244         device_status = nvutils.get_device_status(device_idx)
245 
246         infos.append(_cuda_card_stat(
247             product_name=device_info.name,
248             gpu_usage=device_status.gpu_util,
249             temperature=device_status.temperature,
250             fb_mem_info=_virt_memory_stat(
251                 total=device_status.fb_total_mem, used=device_status.fb_used_mem,
252                 free=device_status.fb_free_mem, available=device_status.fb_free_mem,
253                 percent=device_status.mem_util,
254             )
255         ))
256     return infos
257
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mars/resource.py b/mars/resource.py
--- a/mars/resource.py
+++ b/mars/resource.py
@@ -26,10 +26,12 @@
 _proc = psutil.Process()
 _timer = getattr(time, 'monotonic', time.time)
 
+_cpu_use_process_stat = bool(int(os.environ.get('MARS_CPU_USE_PROCESS_STAT', '0').strip('"')))
+_mem_use_process_stat = bool(int(os.environ.get('MARS_MEM_USE_PROCESS_STAT', '0').strip('"')))
+
 if 'MARS_USE_PROCESS_STAT' in os.environ:
-    _use_process_stat = bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('"')))
-else:
-    _use_process_stat = False
+    _cpu_use_process_stat = _mem_use_process_stat = \
+        bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('"')))
 
 if 'MARS_CPU_TOTAL' in os.environ:
     _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('"'))
@@ -53,7 +55,7 @@
 
 def virtual_memory():
     sys_mem = psutil.virtual_memory()
-    if not _use_process_stat:
+    if not _mem_use_process_stat:
         total = sys_mem.total
         used = sys_mem.used + getattr(sys_mem, 'shared', 0)
         available = sys_mem.available
@@ -112,7 +114,7 @@
 
 def cpu_percent():
     global _last_cpu_measure
-    if not _use_process_stat:
+    if not _cpu_use_process_stat:
         return sum(psutil.cpu_percent(percpu=True))
 
     num_cpus = cpu_count() or 1

{"golden_diff": "diff --git a/mars/resource.py b/mars/resource.py\n--- a/mars/resource.py\n+++ b/mars/resource.py\n@@ -26,10 +26,12 @@\n _proc = psutil.Process()\n _timer = getattr(time, 'monotonic', time.time)\n \n+_cpu_use_process_stat = bool(int(os.environ.get('MARS_CPU_USE_PROCESS_STAT', '0').strip('\"')))\n+_mem_use_process_stat = bool(int(os.environ.get('MARS_MEM_USE_PROCESS_STAT', '0').strip('\"')))\n+\n if 'MARS_USE_PROCESS_STAT' in os.environ:\n-    _use_process_stat = bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('\"')))\n-else:\n-    _use_process_stat = False\n+    _cpu_use_process_stat = _mem_use_process_stat = \\\n+        bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('\"')))\n \n if 'MARS_CPU_TOTAL' in os.environ:\n     _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('\"'))\n@@ -53,7 +55,7 @@\n \n def virtual_memory():\n     sys_mem = psutil.virtual_memory()\n-    if not _use_process_stat:\n+    if not _mem_use_process_stat:\n         total = sys_mem.total\n         used = sys_mem.used + getattr(sys_mem, 'shared', 0)\n         available = sys_mem.available\n@@ -112,7 +114,7 @@\n \n def cpu_percent():\n     global _last_cpu_measure\n-    if not _use_process_stat:\n+    if not _cpu_use_process_stat:\n         return sum(psutil.cpu_percent(percpu=True))\n \n     num_cpus = cpu_count() or 1\n", "issue": "Running Mars under docker can use psutil.virtual_memory() directly\n**Is your feature request related to a problem? Please describe.**\r\nWhen running in Docker containers with ``MARS_USE_PROCESS_STAT`` on, both cpu and memory usage are calculated by summing results of all processes. This can be effective for cpu. However, this method may produce larger memory usages than actual.\r\n\r\n**Describe the solution you'd like**\r\nWe can specify different methods to measure cpu and memory usage to obtain more accurate memory usage for most of the cases.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright 1999-2018 Alibaba Group Holding Ltd.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport os\nimport subprocess  # nosec\nimport sys\nimport time\nfrom collections import namedtuple\n\nimport psutil\n\nfrom .lib import nvutils\n\n_proc = psutil.Process()\n_timer = getattr(time, 'monotonic', time.time)\n\nif 'MARS_USE_PROCESS_STAT' in os.environ:\n    _use_process_stat = bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('\"')))\nelse:\n    _use_process_stat = False\n\nif 'MARS_CPU_TOTAL' in os.environ:\n    _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('\"'))\nelse:\n    _cpu_total = psutil.cpu_count(logical=True)\n\nif 'MARS_MEMORY_TOTAL' in os.environ:\n    _mem_total = int(os.environ['MARS_MEMORY_TOTAL'].strip('\"'))\nelse:\n    _mem_total = None\n\n_virt_memory_stat = namedtuple('virtual_memory', 'total available percent used free')\n\n_shm_path = [pt.mountpoint for pt in psutil.disk_partitions(all=True)\n             if pt.mountpoint in ('/tmp', '/dev/shm') and pt.fstype == 'tmpfs']\nif not _shm_path:\n    _shm_path = None\nelse:\n    _shm_path = _shm_path[0]\n\n\ndef virtual_memory():\n    sys_mem = psutil.virtual_memory()\n    if not _use_process_stat:\n        total = sys_mem.total\n        used = sys_mem.used + getattr(sys_mem, 'shared', 0)\n        available = sys_mem.available\n        free = sys_mem.free\n        percent = 100.0 * (total - available) / total\n        return _virt_memory_stat(total, available, percent, used, free)\n    else:\n        used = 0\n        for p in psutil.process_iter():\n            try:\n                used += p.memory_info().rss\n            except (psutil.NoSuchProcess, psutil.AccessDenied):\n                pass\n\n        if _shm_path:\n            shm_stats = psutil.disk_usage(_shm_path)\n            used += shm_stats.used\n\n        total = min(_mem_total or sys_mem.total, sys_mem.total)\n        # TODO sys_mem.available does not work in container\n        # available = min(sys_mem.available, total - used)\n        available = total - used\n        free = min(sys_mem.free, total - used)\n        percent = 100.0 * (total - available) / total\n        return _virt_memory_stat(total, available, percent, used, free)\n\n\ndef cpu_count():\n    return _cpu_total\n\n\n_last_cpu_measure = None\n\n\ndef _take_process_cpu_snapshot():\n    num_cpus = cpu_count() or 1\n\n    def timer():\n        return _timer() * num_cpus\n\n    processes = [p for p in psutil.process_iter() if p.pid != _proc.pid]\n\n    pts = dict()\n    sts = dict()\n    for p in processes:\n        try:\n            pts[p.pid] = p.cpu_times()\n            sts[p.pid] = timer()\n        except (psutil.NoSuchProcess, psutil.AccessDenied):\n            pass\n\n    pts[_proc.pid] = _proc.cpu_times()\n    sts[_proc.pid] = timer()\n    return pts, sts\n\n\ndef cpu_percent():\n    global _last_cpu_measure\n    if not _use_process_stat:\n        return sum(psutil.cpu_percent(percpu=True))\n\n    num_cpus = cpu_count() or 1\n    pts, sts = _take_process_cpu_snapshot()\n\n    if _last_cpu_measure is None:\n        _last_cpu_measure = (pts, sts)\n        return None\n\n    old_pts, old_sts = _last_cpu_measure\n\n    percents = []\n    for pid in pts:\n        if pid not in old_pts:\n            continue\n        pt1 = old_pts[pid]\n        pt2 = pts[pid]\n        delta_proc = (pt2.user - pt1.user) + (pt2.system - pt1.system)\n        delta_time = sts[pid] - old_sts[pid]\n\n        try:\n            overall_cpus_percent = (delta_proc / delta_time) * 100\n        except ZeroDivisionError:\n            percents.append(0.0)\n        else:\n            single_cpu_percent = overall_cpus_percent * num_cpus\n            percents.append(single_cpu_percent)\n    _last_cpu_measure = (pts, sts)\n    return round(sum(percents), 1)\n\n\ndef disk_usage(d):\n    return psutil.disk_usage(d)\n\n\ndef iowait():\n    cpu_percent = psutil.cpu_times_percent()\n    try:\n        return cpu_percent.iowait\n    except AttributeError:\n        return None\n\n\n_last_disk_io_meta = None\n_win_diskperf_called = False\n\n\ndef disk_io_usage():\n    global _last_disk_io_meta, _win_diskperf_called\n\n    # Needed by psutil.disk_io_counters() under newer version of Windows.\n    # diskperf -y need to be called or no disk information can be found.\n    if sys.platform == 'win32' and not _win_diskperf_called:  # pragma: no cover\n        CREATE_NO_WINDOW = 0x08000000\n        try:\n            proc = subprocess.Popen(['diskperf', '-y'], shell=False,\n                                    creationflags=CREATE_NO_WINDOW)  # nosec\n            proc.wait()\n        except (subprocess.CalledProcessError, OSError):\n            pass\n        _win_diskperf_called = True\n\n    disk_counters = psutil.disk_io_counters()\n    tst = time.time()\n\n    read_bytes = disk_counters.read_bytes\n    write_bytes = disk_counters.write_bytes\n    if _last_disk_io_meta is None:\n        _last_disk_io_meta = (read_bytes, write_bytes, tst)\n        return None\n\n    last_read_bytes, last_write_bytes, last_time = _last_disk_io_meta\n    delta_time = tst - last_time\n    read_speed = (read_bytes - last_read_bytes) / delta_time\n    write_speed = (write_bytes - last_write_bytes) / delta_time\n\n    _last_disk_io_meta = (read_bytes, write_bytes, tst)\n    return read_speed, write_speed\n\n\n_last_net_io_meta = None\n\n\ndef net_io_usage():\n    global _last_net_io_meta\n\n    net_counters = psutil.net_io_counters()\n    tst = time.time()\n\n    send_bytes = net_counters.bytes_sent\n    recv_bytes = net_counters.bytes_recv\n    if _last_net_io_meta is None:\n        _last_net_io_meta = (send_bytes, recv_bytes, tst)\n        return None\n\n    last_send_bytes, last_recv_bytes, last_time = _last_net_io_meta\n    delta_time = tst - last_time\n    recv_speed = (recv_bytes - last_recv_bytes) / delta_time\n    send_speed = (send_bytes - last_send_bytes) / delta_time\n\n    _last_net_io_meta = (send_bytes, recv_bytes, tst)\n    return recv_speed, send_speed\n\n\n_cuda_info = namedtuple('cuda_info', 'driver_version cuda_version products gpu_count')\n_cuda_card_stat = namedtuple('cuda_card_stat', 'product_name gpu_usage temperature fb_mem_info')\n\n\ndef cuda_info():  # pragma: no cover\n    driver_info = nvutils.get_driver_info()\n    if not driver_info:\n        return\n    gpu_count = nvutils.get_device_count()\n    return _cuda_info(\n        driver_version=driver_info.driver_version,\n        cuda_version=driver_info.cuda_version,\n        products=[nvutils.get_device_info(idx).name for idx in range(gpu_count)],\n        gpu_count=gpu_count,\n    )\n\n\ndef cuda_card_stats():  # pragma: no cover\n    infos = []\n    device_count = nvutils.get_device_count()\n    if not device_count:\n        return\n    for device_idx in range(device_count):\n        device_info = nvutils.get_device_info(device_idx)\n        device_status = nvutils.get_device_status(device_idx)\n\n        infos.append(_cuda_card_stat(\n            product_name=device_info.name,\n            gpu_usage=device_status.gpu_util,\n            temperature=device_status.temperature,\n            fb_mem_info=_virt_memory_stat(\n                total=device_status.fb_total_mem, used=device_status.fb_used_mem,\n                free=device_status.fb_free_mem, available=device_status.fb_free_mem,\n                percent=device_status.mem_util,\n            )\n        ))\n    return infos\n", "path": "mars/resource.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright 1999-2018 Alibaba Group Holding Ltd.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport os\nimport subprocess  # nosec\nimport sys\nimport time\nfrom collections import namedtuple\n\nimport psutil\n\nfrom .lib import nvutils\n\n_proc = psutil.Process()\n_timer = getattr(time, 'monotonic', time.time)\n\n_cpu_use_process_stat = bool(int(os.environ.get('MARS_CPU_USE_PROCESS_STAT', '0').strip('\"')))\n_mem_use_process_stat = bool(int(os.environ.get('MARS_MEM_USE_PROCESS_STAT', '0').strip('\"')))\n\nif 'MARS_USE_PROCESS_STAT' in os.environ:\n    _cpu_use_process_stat = _mem_use_process_stat = \\\n        bool(int(os.environ['MARS_USE_PROCESS_STAT'].strip('\"')))\n\nif 'MARS_CPU_TOTAL' in os.environ:\n    _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('\"'))\nelse:\n    _cpu_total = psutil.cpu_count(logical=True)\n\nif 'MARS_MEMORY_TOTAL' in os.environ:\n    _mem_total = int(os.environ['MARS_MEMORY_TOTAL'].strip('\"'))\nelse:\n    _mem_total = None\n\n_virt_memory_stat = namedtuple('virtual_memory', 'total available percent used free')\n\n_shm_path = [pt.mountpoint for pt in psutil.disk_partitions(all=True)\n             if pt.mountpoint in ('/tmp', '/dev/shm') and pt.fstype == 'tmpfs']\nif not _shm_path:\n    _shm_path = None\nelse:\n    _shm_path = _shm_path[0]\n\n\ndef virtual_memory():\n    sys_mem = psutil.virtual_memory()\n    if not _mem_use_process_stat:\n        total = sys_mem.total\n        used = sys_mem.used + getattr(sys_mem, 'shared', 0)\n        available = sys_mem.available\n        free = sys_mem.free\n        percent = 100.0 * (total - available) / total\n        return _virt_memory_stat(total, available, percent, used, free)\n    else:\n        used = 0\n        for p in psutil.process_iter():\n            try:\n                used += p.memory_info().rss\n            except (psutil.NoSuchProcess, psutil.AccessDenied):\n                pass\n\n        if _shm_path:\n            shm_stats = psutil.disk_usage(_shm_path)\n            used += shm_stats.used\n\n        total = min(_mem_total or sys_mem.total, sys_mem.total)\n        # TODO sys_mem.available does not work in container\n        # available = min(sys_mem.available, total - used)\n        available = total - used\n        free = min(sys_mem.free, total - used)\n        percent = 100.0 * (total - available) / total\n        return _virt_memory_stat(total, available, percent, used, free)\n\n\ndef cpu_count():\n    return _cpu_total\n\n\n_last_cpu_measure = None\n\n\ndef _take_process_cpu_snapshot():\n    num_cpus = cpu_count() or 1\n\n    def timer():\n        return _timer() * num_cpus\n\n    processes = [p for p in psutil.process_iter() if p.pid != _proc.pid]\n\n    pts = dict()\n    sts = dict()\n    for p in processes:\n        try:\n            pts[p.pid] = p.cpu_times()\n            sts[p.pid] = timer()\n        except (psutil.NoSuchProcess, psutil.AccessDenied):\n            pass\n\n    pts[_proc.pid] = _proc.cpu_times()\n    sts[_proc.pid] = timer()\n    return pts, sts\n\n\ndef cpu_percent():\n    global _last_cpu_measure\n    if not _cpu_use_process_stat:\n        return sum(psutil.cpu_percent(percpu=True))\n\n    num_cpus = cpu_count() or 1\n    pts, sts = _take_process_cpu_snapshot()\n\n    if _last_cpu_measure is None:\n        _last_cpu_measure = (pts, sts)\n        return None\n\n    old_pts, old_sts = _last_cpu_measure\n\n    percents = []\n    for pid in pts:\n        if pid not in old_pts:\n            continue\n        pt1 = old_pts[pid]\n        pt2 = pts[pid]\n        delta_proc = (pt2.user - pt1.user) + (pt2.system - pt1.system)\n        delta_time = sts[pid] - old_sts[pid]\n\n        try:\n            overall_cpus_percent = (delta_proc / delta_time) * 100\n        except ZeroDivisionError:\n            percents.append(0.0)\n        else:\n            single_cpu_percent = overall_cpus_percent * num_cpus\n            percents.append(single_cpu_percent)\n    _last_cpu_measure = (pts, sts)\n    return round(sum(percents), 1)\n\n\ndef disk_usage(d):\n    return psutil.disk_usage(d)\n\n\ndef iowait():\n    cpu_percent = psutil.cpu_times_percent()\n    try:\n        return cpu_percent.iowait\n    except AttributeError:\n        return None\n\n\n_last_disk_io_meta = None\n_win_diskperf_called = False\n\n\ndef disk_io_usage():\n    global _last_disk_io_meta, _win_diskperf_called\n\n    # Needed by psutil.disk_io_counters() under newer version of Windows.\n    # diskperf -y need to be called or no disk information can be found.\n    if sys.platform == 'win32' and not _win_diskperf_called:  # pragma: no cover\n        CREATE_NO_WINDOW = 0x08000000\n        try:\n            proc = subprocess.Popen(['diskperf', '-y'], shell=False,\n                                    creationflags=CREATE_NO_WINDOW)  # nosec\n            proc.wait()\n        except (subprocess.CalledProcessError, OSError):\n            pass\n        _win_diskperf_called = True\n\n    disk_counters = psutil.disk_io_counters()\n    tst = time.time()\n\n    read_bytes = disk_counters.read_bytes\n    write_bytes = disk_counters.write_bytes\n    if _last_disk_io_meta is None:\n        _last_disk_io_meta = (read_bytes, write_bytes, tst)\n        return None\n\n    last_read_bytes, last_write_bytes, last_time = _last_disk_io_meta\n    delta_time = tst - last_time\n    read_speed = (read_bytes - last_read_bytes) / delta_time\n    write_speed = (write_bytes - last_write_bytes) / delta_time\n\n    _last_disk_io_meta = (read_bytes, write_bytes, tst)\n    return read_speed, write_speed\n\n\n_last_net_io_meta = None\n\n\ndef net_io_usage():\n    global _last_net_io_meta\n\n    net_counters = psutil.net_io_counters()\n    tst = time.time()\n\n    send_bytes = net_counters.bytes_sent\n    recv_bytes = net_counters.bytes_recv\n    if _last_net_io_meta is None:\n        _last_net_io_meta = (send_bytes, recv_bytes, tst)\n        return None\n\n    last_send_bytes, last_recv_bytes, last_time = _last_net_io_meta\n    delta_time = tst - last_time\n    recv_speed = (recv_bytes - last_recv_bytes) / delta_time\n    send_speed = (send_bytes - last_send_bytes) / delta_time\n\n    _last_net_io_meta = (send_bytes, recv_bytes, tst)\n    return recv_speed, send_speed\n\n\n_cuda_info = namedtuple('cuda_info', 'driver_version cuda_version products gpu_count')\n_cuda_card_stat = namedtuple('cuda_card_stat', 'product_name gpu_usage temperature fb_mem_info')\n\n\ndef cuda_info():  # pragma: no cover\n    driver_info = nvutils.get_driver_info()\n    if not driver_info:\n        return\n    gpu_count = nvutils.get_device_count()\n    return _cuda_info(\n        driver_version=driver_info.driver_version,\n        cuda_version=driver_info.cuda_version,\n        products=[nvutils.get_device_info(idx).name for idx in range(gpu_count)],\n        gpu_count=gpu_count,\n    )\n\n\ndef cuda_card_stats():  # pragma: no cover\n    infos = []\n    device_count = nvutils.get_device_count()\n    if not device_count:\n        return\n    for device_idx in range(device_count):\n        device_info = nvutils.get_device_info(device_idx)\n        device_status = nvutils.get_device_status(device_idx)\n\n        infos.append(_cuda_card_stat(\n            product_name=device_info.name,\n            gpu_usage=device_status.gpu_util,\n            temperature=device_status.temperature,\n            fb_mem_info=_virt_memory_stat(\n                total=device_status.fb_total_mem, used=device_status.fb_used_mem,\n                free=device_status.fb_free_mem, available=device_status.fb_free_mem,\n                percent=device_status.mem_util,\n            )\n        ))\n    return infos\n", "path": "mars/resource.py"}]}