sohom004 commited on
Commit
94a9eb3
·
verified ·
1 Parent(s): 7c5f24b

Update app.py

Browse files

addition of get_cgroupv2_cpu_limit() & get_cgroupv2_memory_limit()

Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -39,10 +39,36 @@ def get_container_memory_limit():
39
  return int(psutil.virtual_memory().total // (1024 * 1024))
40
  except Exception:
41
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  @app.get("/")
44
  def greet_json():
45
- return {"Hello": "World!", "cpu_count": get_container_cpu_count(), "total_ram_in_MB": get_container_memory_limit()}
46
 
47
  @app.get("/health")
48
  def health():
 
39
  return int(psutil.virtual_memory().total // (1024 * 1024))
40
  except Exception:
41
  return None
42
+
43
+ # = for cgroup v2
44
+ def get_cgroupv2_cpu_limit():
45
+ try:
46
+ with open("/sys/fs/cgroup/cpu.max") as f:
47
+ data = f.read().strip()
48
+ quota, period = data.split()
49
+ if quota == "max":
50
+ print("no CPU limit")
51
+ return os.cpu_count() # no limit
52
+ return int(int(quota) / int(period))
53
+ except Exception as ex:
54
+ print(repr(ex))
55
+ return os.cpu_count()
56
+
57
+ def get_cgroupv2_memory_limit():
58
+ try:
59
+ with open("/sys/fs/cgroup/memory.max") as f:
60
+ mem_bytes = f.read().strip()
61
+ if mem_bytes == "max":
62
+ print("no MEMORY limit")
63
+ return int(psutil.virtual_memory().total // (1024*1024)) # no limit
64
+ return int(mem_bytes) // (1024*1024) # in MB
65
+ except Exception as ex:
66
+ print(repr(ex))
67
+ return int(psutil.virtual_memory().total // (1024*1024))
68
 
69
  @app.get("/")
70
  def greet_json():
71
+ return {"Hello": "World!", "cpu_count": get_cgroupv2_cpu_limit(), "total_ram_in_MB": get_cgroupv2_memory_limit()}
72
 
73
  @app.get("/health")
74
  def health():