sohom004 commited on
Commit
8c06d2e
·
verified ·
1 Parent(s): 99feee8

Update app.py

Browse files

addition of get_container_memory_limit()

Files changed (1) hide show
  1. app.py +17 -1
app.py CHANGED
@@ -20,13 +20,29 @@ def get_container_cpu_count():
20
  period = int(f.read())
21
  if quota > 0 and period > 0:
22
  cpu_count = quota / period
 
 
23
  return int(cpu_count)
24
  except Exception:
25
  return os.cpu_count()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  @app.get("/")
28
  def greet_json():
29
- return {"Hello": "World!", "cpu_count": get_container_cpu_count(), "total_ram_in_MB": psutil.virtual_memory().total // (1024*1024)}
30
 
31
  @app.get("/health")
32
  def health():
 
20
  period = int(f.read())
21
  if quota > 0 and period > 0:
22
  cpu_count = quota / period
23
+ else:
24
+ print(f"{quota_path} or {period_path} doesn't exist")
25
  return int(cpu_count)
26
  except Exception:
27
  return os.cpu_count()
28
+
29
+ def get_container_memory_limit():
30
+ try:
31
+ mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
32
+ if os.path.exists(mem_limit_path):
33
+ with open(mem_limit_path) as f:
34
+ mem_bytes = int(f.read())
35
+ return mem_bytes // (1024 * 1024) # in MB
36
+ else:
37
+ print(f"{mem_limit_path} doesn't exist")
38
+ # fallback to total system memory
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():