code: def virtual_memory() -> _virt_memory_stat:
      global _root_pid
  
      sys_mem = psutil.virtual_memory()
      if _mem_use_cgroup_stat:
          max_mem = min(_mem_total or sys_mem.total, sys_mem.total)
          if _is_cgroup_v2:
              # see Memory section in https://www.kernel.org/doc/Documentation/cgroup-v2.txt
              with open(CGROUP_V2_MEM_MAX_FILE, "r") as mem_max_file:
                  max_str = mem_max_file.read().strip()
                  total = max_mem if max_str == "max" else int(max_str)
              with open(
                  CGROUP_V2_MEM_CURRENT_FILE, "r"
              ) as mem_current_file:
                  used = int(mem_current_file.read().strip())
          else:
              # see section 5.5 in https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
              cgroup_mem_info = _read_cgroup_stat_file(
                  CGROUP_V1_MEM_STAT_FILE
              )
              total = cgroup_mem_info["hierarchical_memory_limit"]
              total = min(max_mem, total)
              used = cgroup_mem_info["rss"] + cgroup_mem_info.get(
                  "swap", 0
              )
  
          if _shm_path:
              shm_stats = psutil.disk_usage(_shm_path)
              used += shm_stats.used
          available = free = total - used
          percent = 100.0 * (total - available) / total
          return _virt_memory_stat(
              total, available, percent, used, free
          )
      elif not _mem_use_process_stat:
          total = min(_mem_total or sys_mem.total, sys_mem.total)
          used = sys_mem.used + getattr(sys_mem, "shared", 0)
          available = sys_mem.available
          free = sys_mem.free
          percent = 100.0 * (total - available) / total
          return _virt_memory_stat(
              total, available, percent, used, free
          )
      else:
          used = 0
          if _root_pid is None:
              cur_proc = psutil.Process()
              while True:
                  par_proc = cur_proc.parent()
                  if par_proc is None:
                      break
                  try:
                      cmd = par_proc.cmdline()
                      if "python" not in " ".join(cmd).lower():
                          break
                      cur_proc = par_proc
                  except:  # noqa: E722  # nosec  # pylint: disable=bare-except  # pragma: no cover
                      break
              _root_pid = cur_proc.pid
  
          root_proc = psutil.Process(_root_pid)
          for p in root_proc.children(True):
              try:
                  used += p.memory_info().rss
              except (psutil.NoSuchProcess, psutil.AccessDenied):
                  pass
  
          if _shm_path:
              shm_stats = psutil.disk_usage(_shm_path)
              used += shm_stats.used
  
          total = min(_mem_total or sys_mem.total, sys_mem.total)
          # TODO sys_mem.available does not work in container
          # available = min(sys_mem.available, total - used)
          available = total - used
          free = min(sys_mem.free, total - used)
          percent = 100.0 * (total - available) / total
          return _virt_memory_stat(
              total, available, percent, used, free
          )
  
imports: import logging
  import math
  import os
  import subprocess
  import sys
  import time
  from collections import namedtuple
  from typing import List, Optional
  import psutil
  from .lib import nvutils
  from ._resource import Resource, ZeroResource
  from .utils import get_bool_environ
label_desc: Use image node_id 2 for calling a global function with example usage: virtual_memory() and returns: _virt_memory_stat, _virt_memory_stat, _virt_memory_stat