Skip to content

Add host available memory #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jupyter_resource_usage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ async def get(self):
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
pass

available = psutil.virtual_memory().available

if callable(config.mem_limit):
mem_limit = config.mem_limit(rss=rss, pss=pss)
else: # mem_limit is an Int
Expand All @@ -58,6 +60,7 @@ async def get(self):
)

metrics = {"rss": rss, "limits": limits}
metrics["mem_avail"] = available
if pss is not None:
metrics["pss"] = pss

Expand Down
5 changes: 4 additions & 1 deletion jupyter_resource_usage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class ResourceUseDisplay(Configurable):

system_memory_metrics = List(
trait=PSUtilMetric(),
default_value=[{"name": "virtual_memory", "attribute": "total"}],
default_value=[
{"name": "virtual_memory", "attribute": "total"},
{"name": "virtual_memory", "attribute": "available"}
],
)

process_cpu_metrics = List(
Expand Down
21 changes: 19 additions & 2 deletions packages/labextension/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,25 @@ export namespace ResourceUsage {
this._memoryAvailable = numBytes !== undefined;
this._currentMemory = currentMemory;
this._memUnits = memUnits;
this._memoryLimit = memoryLimit
? memoryLimit / MEMORY_UNIT_LIMITS[memUnits]

// Use mem_avail if the host exposes it.
const hostAvailBytes = value.mem_avail ?? null ;
let minMemAvailable = null;
let hostMaxBytes = hostAvailBytes ? ( hostAvailBytes + numBytes ) : null;

if ( memoryLimit && hostMaxBytes ) { minMemAvailable = Math.min(hostMaxBytes,memoryLimit); }
else if ( hostMaxBytes ) { minMemAvailable = hostMaxBytes }
else if ( memoryLimit ) { minMemAvailable = memoryLimit }
else if ( hostAvailBytes ) { minMemAvailable = hostAvailBytes + numBytes; }

this._memoryLimit = minMemAvailable
? minMemAvailable / MEMORY_UNIT_LIMITS[memUnits]
: null;

// this._memoryLimit = memoryLimit
// ? memoryLimit / MEMORY_UNIT_LIMITS[memUnits]
// : null;

const memoryPercent = this.memoryLimit
? Math.min(this._currentMemory / this.memoryLimit, 1)
: 0;
Expand Down Expand Up @@ -373,6 +389,7 @@ namespace Private {
export interface IMetricRequestResult {
rss: number;
pss?: number;
mem_avail?: number;
cpu_percent?: number;
cpu_count?: number;
disk_total?: number;
Expand Down
Loading