From 9ab6d12c2574f6bfe091d4fe013e08ed9caa76be Mon Sep 17 00:00:00 2001 From: Riccardo Murri Date: Mon, 14 Aug 2017 16:21:38 +0200 Subject: [PATCH] ShellcmdLrms: new public methods for reporting on core and memory usage. Now that we have `ShellcmdLrms.count_running_tasks()`, there's no reason not to have `.count_used_cores()` and `.count_used_memory()` (possibly a mismatch of verb and noun) as well. --- gc3libs/backends/shellcmd.py | 52 ++++++++++++++---------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/gc3libs/backends/shellcmd.py b/gc3libs/backends/shellcmd.py index 4a800c7c..68f686bc 100755 --- a/gc3libs/backends/shellcmd.py +++ b/gc3libs/backends/shellcmd.py @@ -859,44 +859,32 @@ def count_running_tasks(self): `ShellcmdLrms.get_resource_status()` has not been called for a while. """ - return sum(1 for info in self._job_infos.values() if not info['terminated']) + return sum(1 for info in self._job_infos.values() + if not info['terminated']) - def _compute_used_cores(self, job_infos): + def count_used_cores(self): """ - Accepts a dictionary of job informations and returns the - sum of the `requested_cores` attributes. - """ - return sum(map(self._filter_cores, job_infos.values())) + Return total nr. of cores used by running tasks. - @staticmethod - def _filter_cores(job): - if job['terminated']: - return 0 - else: - return job['requested_cores'] + Similar caveats as in `ShellcmdLrms.count_running_tasks`:meth: + apply here. + """ + return sum(info['requested_cores'] + for info in self._job_infos.values() + if not info['terminated']) - def _compute_used_memory(self, jobs): - """ - Accepts a dictionary of job informations and returns the - sum of the `requested_memory` attributes. + def count_used_memory(self): """ - used_memory = sum(map(self._filter_memory, jobs.values())) - # in case `jobs.values()` is the empty list, the `sum()` - # built-in returns (built-in) integer `0`, which is why we can - # use the `is` operator for this comparison ;-) - if used_memory is 0: - return 0 * MB - else: - return used_memory + Return total amount of memory used by running tasks. - @staticmethod - def _filter_memory(job): - if job['requested_memory'] is None or job['terminated']: - return 0 * MB - else: - return job['requested_memory'] + Similar caveats as in `ShellcmdLrms.count_running_tasks`:meth: + apply here. + """ + return sum((info['requested_memory'] + for info in self._job_infos.values() + if not info['terminated']), 0*MB) def _get_persisted_job_info(self): @@ -1148,9 +1136,9 @@ def _update_resource_usage_info(self): Helper method for (re)reading resource usage from disk. """ self._job_infos = self._get_persisted_job_info() - used_memory = self._compute_used_memory(self._job_infos) + used_memory = self.count_used_memory() self.available_memory = self.total_memory - used_memory - self.free_slots = self.max_cores - self._compute_used_cores(self._job_infos) + self.free_slots = self.max_cores - self.count_used_cores() self.user_run = self.count_running_tasks() log.debug("Recovered resource information from files in %s:" " total nr. of cores: %s, requested by jobs: %s;"