1111 # Traitlets >= 4.3.3
1212 from traitlets import Callable
1313except ImportError :
14- from .callable import Callable
14+ from .utils import Callable
1515
16- from threading import Thread
17- from concurrent .futures import ThreadPoolExecutor , as_completed
16+ from concurrent .futures import ThreadPoolExecutor
1817from tornado .concurrent import run_on_executor
1918
2019class MetricsHandler (IPythonHandler ):
@@ -25,10 +24,6 @@ def initialize(self):
2524 # https://www.tornadoweb.org/en/stable/concurrent.html#tornado.concurrent.run_on_executor
2625 self .executor = ThreadPoolExecutor (max_workers = 10 )
2726
28- # A better approach would use cpu_affinity to account for the
29- # fact that the number of logical CPUs in the system is not
30- # necessarily the same as the number of CPUs the process
31- # can actually use. But cpu_affinity isn't available for OS X.
3227 self .cpu_count = psutil .cpu_count ()
3328
3429 @run_on_executor
@@ -51,7 +46,10 @@ async def get(self):
5146 """
5247 config = self .settings ['nbresuse_display_config' ]
5348 cur_process = psutil .Process ()
54- all_processes = [cur_process ] + cur_process .children (recursive = True )
49+ all_processes = [cur_process ] + cur_process .children (recursive = True )
50+ limits = {}
51+
52+ # Get memory information
5553 rss = sum ([p .memory_info ().rss for p in all_processes ])
5654
5755 if callable (config .mem_limit ):
@@ -68,28 +66,33 @@ async def get(self):
6866 if config .track_cpu_percent :
6967 self .cpu_percent = await self .update_cpu_percent (all_processes )
7068
71- limits = {}
72-
7369 if config .mem_limit != 0 :
7470 limits ['memory' ] = {
7571 'rss' : mem_limit
7672 }
7773 if config .mem_warning_threshold != 0 :
78- limits ['memory' ]['warn' ] = (config . mem_limit - rss ) < (config . mem_limit * config .mem_warning_threshold )
74+ limits ['memory' ]['warn' ] = (mem_limit - rss ) < (mem_limit * config .mem_warning_threshold )
7975
80- if config .cpu_limit != 0 :
81- limits ['cpu' ] = {
82- 'cpu' : config .cpu_limit
83- }
84- if config .cpu_warning_threshold != 0 :
85- limits ['cpu' ]['warn' ] = (config .cpu_limit - cpu_percent ) < (config .cpu_limit * config .cpu_warning_threshold )
76+ # Optionally get CPU information
77+ if config .track_cpu_percent :
78+ self .cpu_percent = await self .update_cpu_percent (all_processes )
79+
80+ if config .cpu_limit != 0 :
81+ limits ['cpu' ] = {
82+ 'cpu' : config .cpu_limit
83+ }
84+ if config .cpu_warning_threshold != 0 :
85+ limits ['cpu' ]['warn' ] = (config .cpu_limit - self .cpu_percent ) < (config .cpu_limit * config .cpu_warning_threshold )
8686
8787 metrics = {
8888 'rss' : rss ,
89- 'cpu_percent' : self .cpu_percent ,
90- 'cpu_count' : self .cpu_count ,
9189 'limits' : limits ,
9290 }
91+ if config .track_cpu_percent :
92+ metrics .update (cpu_percent = self .cpu_percent ,
93+ cpu_count = self .cpu_count )
94+
95+ self .log .debug ("NBResuse metrics: %s" , metrics )
9396 self .write (json .dumps (metrics ))
9497
9598
@@ -118,7 +121,7 @@ class ResourceUseDisplay(Configurable):
118121 """
119122
120123 mem_warning_threshold = Float (
121- 0.1 ,
124+ default_value = 0.1 ,
122125 help = """
123126 Warn user with flashing lights when memory usage is within this fraction
124127 memory limit.
@@ -132,7 +135,6 @@ class ResourceUseDisplay(Configurable):
132135
133136 mem_limit = Union (
134137 trait_types = [Int (), Callable ()],
135- 0 ,
136138 help = """
137139 Memory limit to display to the user, in bytes.
138140 Can also be a function which calculates the memory limit.
@@ -144,28 +146,32 @@ class ResourceUseDisplay(Configurable):
144146 """
145147 ).tag (config = True )
146148
149+ @default ('mem_limit' )
150+ def _mem_limit_default (self ):
151+ return int (os .environ .get ('MEM_LIMIT' , 0 ))
152+
147153 track_cpu_percent = Bool (
148- False ,
154+ default_value = False ,
149155 help = """
150156 Set to True in order to enable reporting of CPU usage statistics.
151157 """
152158 ).tag (config = True )
153159
154160 cpu_warning_threshold = Float (
155- 0.1 ,
161+ default_value = 0.1 ,
156162 help = """
157163 Warn user with flashing lights when CPU usage is within this fraction
158164 CPU usage limit.
159165
160- For example, if memory limit is 150%, `cpu_warning_threshold` is 0.1,
166+ For example, if CPU limit is 150%, `cpu_warning_threshold` is 0.1,
161167 we will start warning the user when they use (150 - (150 * 0.1)) %.
162168
163169 Set to 0 to disable warning.
164170 """
165171 ).tag (config = True )
166172
167173 cpu_limit = Float (
168- 0 ,
174+ default_value = 0 ,
169175 help = """
170176 CPU usage limit to display to the user.
171177
@@ -176,10 +182,6 @@ class ResourceUseDisplay(Configurable):
176182 """
177183 ).tag (config = True )
178184
179- @default ('mem_limit' )
180- def _mem_limit_default (self ):
181- return int (os .environ .get ('MEM_LIMIT' , 0 ))
182-
183185 @default ('cpu_limit' )
184186 def _cpu_limit_default (self ):
185187 return float (os .environ .get ('CPU_LIMIT' , 0 ))
0 commit comments