| Conditions | 20 |
| Total Lines | 135 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like glances.processes.GlancesProcesses.update() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 244 | def update(self): |
||
| 245 | """Update the processes stats.""" |
||
| 246 | # Reset the stats |
||
| 247 | self.processlist = [] |
||
| 248 | self.reset_processcount() |
||
| 249 | |||
| 250 | # Do not process if disable tag is set |
||
| 251 | if self.disable_tag: |
||
| 252 | return |
||
| 253 | |||
| 254 | # Time since last update (for disk_io rate computation) |
||
| 255 | time_since_update = getTimeSinceLastUpdate('process_disk') |
||
| 256 | |||
| 257 | # Grab standard stats |
||
| 258 | ##################### |
||
| 259 | standard_attrs = ['cmdline', 'cpu_percent', 'cpu_times', 'memory_info', |
||
| 260 | 'memory_percent', 'name', 'nice', 'pid', 'ppid', |
||
| 261 | 'status', 'username', 'status', 'num_threads'] |
||
| 262 | if not self.disable_io_counters: |
||
| 263 | standard_attrs += ['io_counters'] |
||
| 264 | if not self.disable_gids: |
||
| 265 | standard_attrs += ['gids'] |
||
| 266 | |||
| 267 | # and build the processes stats list (psutil>=5.3.0) |
||
| 268 | self.processlist = [p.info for p in psutil.process_iter(attrs=standard_attrs, |
||
| 269 | ad_value=None) |
||
| 270 | # OS-related processes filter |
||
| 271 | if not (BSD and p.info['name'] == 'idle') and |
||
| 272 | not (WINDOWS and p.info['name'] == 'System Idle Process') and |
||
| 273 | not (MACOS and p.info['name'] == 'kernel_task') and |
||
| 274 | # Kernel threads filter |
||
| 275 | not (self.no_kernel_threads and LINUX and p.info['gids'].real == 0) and |
||
| 276 | # User filter |
||
| 277 | not (self._filter.is_filtered(p.info))] |
||
| 278 | |||
| 279 | # Sort the processes list by the current sort_key |
||
| 280 | self.processlist = sort_stats(self.processlist, |
||
| 281 | sortedby=self.sort_key, |
||
| 282 | reverse=True) |
||
| 283 | |||
| 284 | # Update the processcount |
||
| 285 | self.update_processcount(self.processlist) |
||
| 286 | |||
| 287 | # Loop over processes and add metadata |
||
| 288 | first = True |
||
| 289 | for proc in self.processlist: |
||
| 290 | # Get extended stats, only for top processes (see issue #403). |
||
| 291 | if first and not self.disable_extended_tag: |
||
| 292 | # - cpu_affinity (Linux, Windows, FreeBSD) |
||
| 293 | # - ionice (Linux and Windows > Vista) |
||
| 294 | # - num_ctx_switches (not available on Illumos/Solaris) |
||
| 295 | # - num_fds (Unix-like) |
||
| 296 | # - num_handles (Windows) |
||
| 297 | # - memory_maps (only swap, Linux) |
||
| 298 | # https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/ |
||
| 299 | # - connections (TCP and UDP) |
||
| 300 | extended = {} |
||
| 301 | try: |
||
| 302 | top_process = psutil.Process(proc['pid']) |
||
| 303 | extended_stats = ['cpu_affinity', 'ionice', |
||
| 304 | 'num_ctx_switches'] |
||
| 305 | if LINUX: |
||
| 306 | # num_fds only avalable on Unix system (see issue #1351) |
||
| 307 | extended_stats += ['num_fds'] |
||
| 308 | if WINDOWS: |
||
| 309 | extended_stats += ['num_handles'] |
||
| 310 | |||
| 311 | # Get the extended stats |
||
| 312 | extended = top_process.as_dict(attrs=extended_stats, |
||
| 313 | ad_value=None) |
||
| 314 | |||
| 315 | if LINUX: |
||
| 316 | try: |
||
| 317 | extended['memory_swap'] = sum([v.swap for v in top_process.memory_maps()]) |
||
| 318 | except (psutil.NoSuchProcess, KeyError): |
||
| 319 | # KeyError catch for issue #1551) |
||
| 320 | pass |
||
| 321 | except (psutil.AccessDenied, NotImplementedError): |
||
| 322 | # NotImplementedError: /proc/${PID}/smaps file doesn't exist |
||
| 323 | # on kernel < 2.6.14 or CONFIG_MMU kernel configuration option |
||
| 324 | # is not enabled (see psutil #533/glances #413). |
||
| 325 | extended['memory_swap'] = None |
||
| 326 | try: |
||
| 327 | extended['tcp'] = len(top_process.connections(kind="tcp")) |
||
| 328 | extended['udp'] = len(top_process.connections(kind="udp")) |
||
| 329 | except (psutil.AccessDenied, psutil.NoSuchProcess): |
||
| 330 | # Manage issue1283 (psutil.AccessDenied) |
||
| 331 | extended['tcp'] = None |
||
| 332 | extended['udp'] = None |
||
| 333 | except (psutil.NoSuchProcess, ValueError, AttributeError) as e: |
||
| 334 | logger.error('Can not grab extended stats ({})'.format(e)) |
||
| 335 | extended['extended_stats'] = False |
||
| 336 | else: |
||
| 337 | logger.debug('Grab extended stats for process {}'.format(proc['pid'])) |
||
| 338 | extended['extended_stats'] = True |
||
| 339 | proc.update(extended) |
||
| 340 | first = False |
||
| 341 | # /End of extended stats |
||
| 342 | |||
| 343 | # Time since last update (for disk_io rate computation) |
||
| 344 | proc['time_since_update'] = time_since_update |
||
| 345 | |||
| 346 | # Process status (only keep the first char) |
||
| 347 | proc['status'] = str(proc['status'])[:1].upper() |
||
| 348 | |||
| 349 | # Process IO |
||
| 350 | # procstat['io_counters'] is a list: |
||
| 351 | # [read_bytes, write_bytes, read_bytes_old, write_bytes_old, io_tag] |
||
| 352 | # If io_tag = 0 > Access denied or first time (display "?") |
||
| 353 | # If io_tag = 1 > No access denied (display the IO rate) |
||
| 354 | if 'io_counters' in proc and proc['io_counters'] is not None: |
||
| 355 | io_new = [proc['io_counters'].read_bytes, |
||
| 356 | proc['io_counters'].write_bytes] |
||
| 357 | # For IO rate computation |
||
| 358 | # Append saved IO r/w bytes |
||
| 359 | try: |
||
| 360 | proc['io_counters'] = io_new + self.io_old[proc['pid']] |
||
| 361 | io_tag = 1 |
||
| 362 | except KeyError: |
||
| 363 | proc['io_counters'] = io_new + [0, 0] |
||
| 364 | io_tag = 0 |
||
| 365 | # then save the IO r/w bytes |
||
| 366 | self.io_old[proc['pid']] = io_new |
||
| 367 | else: |
||
| 368 | proc['io_counters'] = [0, 0] + [0, 0] |
||
| 369 | io_tag = 0 |
||
| 370 | # Append the IO tag (for display) |
||
| 371 | proc['io_counters'] += [io_tag] |
||
| 372 | |||
| 373 | # Compute the maximum value for keys in self._max_values_list: CPU, MEM |
||
| 374 | # Usefull to highlight the processes with maximum values |
||
| 375 | for k in self._max_values_list: |
||
| 376 | values_list = [i[k] for i in self.processlist if i[k] is not None] |
||
| 377 | if values_list != []: |
||
| 378 | self.set_max_values(k, max(values_list)) |
||
| 379 | |||
| 478 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.