Skip to content

Commit 9180775

Browse files
authored
Merge pull request #23 from krinsman/dynamicMem
Made memory limit possibly dynamic via passing a callable in the config.
2 parents e7b723b + 3756379 commit 9180775

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

nbresuse/__init__.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import os
22
import json
33
import psutil
4-
from traitlets import Float, Int, default
4+
from traitlets import Float, Int, Union, default
55
from traitlets.config import Configurable
66
from notebook.utils import url_path_join
77
from notebook.base.handlers import IPythonHandler
88
from tornado import web
9+
try:
10+
# Traitlets >= 4.3.3
11+
from traitlets import Callable
12+
except ImportError:
13+
from .callable import Callable
914

1015

1116
class MetricsHandler(IPythonHandler):
@@ -19,11 +24,16 @@ def get(self):
1924
all_processes = [cur_process] + cur_process.children(recursive=True)
2025
rss = sum([p.memory_info().rss for p in all_processes])
2126

27+
if callable(config.mem_limit):
28+
mem_limit = config.mem_limit(rss=rss)
29+
else: # mem_limit is an Int
30+
mem_limit = config.mem_limit
31+
2232
limits = {}
2333

2434
if config.mem_limit != 0:
2535
limits['memory'] = {
26-
'rss': config.mem_limit
36+
'rss': mem_limit
2737
}
2838
if config.mem_warning_threshold != 0:
2939
limits['memory']['warn'] = (config.mem_limit - rss) < (config.mem_limit * config.mem_warning_threshold)
@@ -68,22 +78,22 @@ class ResourceUseDisplay(Configurable):
6878
we will start warning the user when they use (128 - (128 * 0.1)) MB.
6979
7080
Set to 0 to disable warning.
71-
""",
72-
config=True
73-
)
81+
"""
82+
).tag(config=True)
7483

75-
mem_limit = Int(
84+
mem_limit = Union(
85+
trait_types=[Int(), Callable()],
7686
0,
77-
config=True,
7887
help="""
7988
Memory limit to display to the user, in bytes.
89+
Can also be a function which calculates the memory limit.
8090
8191
Note that this does not actually limit the user's memory usage!
8292
8393
Defaults to reading from the `MEM_LIMIT` environment variable. If
8494
set to 0, no memory limit is displayed.
8595
"""
86-
)
96+
).tag(config=True)
8797

8898
@default('mem_limit')
8999
def _mem_limit_default(self):

nbresuse/callable.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from traitlets import TraitType
2+
import six
3+
4+
# copy-pasted from the master of Traitlets source
5+
class Callable(TraitType):
6+
"""A trait which is callable.
7+
Notes
8+
-----
9+
Classes are callable, as are instances
10+
with a __call__() method."""
11+
12+
info_text = 'a callable'
13+
14+
def validate(self, obj, value):
15+
if six.callable(value):
16+
return value
17+
else:
18+
self.error(obj, value)

nbresuse/static/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ define(['jquery', 'base/js/utils'], function ($, utils) {
3333
var limits = data['limits'];
3434
if ('memory' in limits) {
3535
if ('rss' in limits['memory']) {
36-
display += " / " + (limits['memory']['rss'] / (1024 * 1024));
36+
display += " / " + Math.round(limits['memory']['rss'] / (1024 * 1024));
3737
}
3838
if (limits['memory']['warn']) {
3939
$('#nbresuse-display').addClass('nbresuse-warn');

0 commit comments

Comments
 (0)