From 830a691be6f6d1eb2635726696a466bac212654b Mon Sep 17 00:00:00 2001 From: Keigh Rim Date: Fri, 5 Jul 2024 20:59:34 -0400 Subject: [PATCH] added runningtime and hwinfo recorder (as universal params) --- clams/app/__init__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/clams/app/__init__.py b/clams/app/__init__.py index dc031d2..33f7b25 100644 --- a/clams/app/__init__.py +++ b/clams/app/__init__.py @@ -5,6 +5,7 @@ import warnings from abc import ABC, abstractmethod from contextlib import contextmanager +from datetime import datetime from urllib import parse as urlparser __all__ = ['ClamsApp'] @@ -44,6 +45,14 @@ class ClamsApp(ABC): 'name': 'pretty', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False, 'description': 'The JSON body of the HTTP response will be re-formatted with 2-space indentation', }, + { + 'name': 'runningTime', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False, + 'description': 'The running time of the app will be recorded in the view metadata', + }, + { + 'name': 'hwFetch', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False, + 'description': 'The hardware information (architecture, GPU and vRAM) will be recorded in the view metadata', + }, ] # this key is used to store users' raw input params in the parameter dict @@ -136,6 +145,7 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) -> refined = self._refine_params(**runtime_params) self.logger.debug(f"Refined parameters: {refined}") pretty = refined.get('pretty', False) + t = datetime.now() with warnings.catch_warnings(record=True) as ws: annotated = self._annotate(mmif, **refined) if ws: @@ -144,6 +154,26 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) -> warnings_view = annotated.new_view() self.sign_view(warnings_view, refined) warnings_view.metadata.warnings = issued_warnings + td = datetime.now() - t + runningTime = refined.get('runningTime', False) + hwFetch = refined.get('hwFetch', False) + runtime_recs = {} + if runningTime: + runtime_recs['runningTime'] = str(td) + if hwFetch: + import platform, shutil, subprocess + runtime_recs['architecture'] = platform.machine() + # runtime_recs['processor'] = platform.processor() # this only works on Windows + runtime_recs['cuda'] = [] + if shutil.which('nvidia-smi'): + for gpu in subprocess.run(['nvidia-smi', '--query-gpu=name,memory.total', '--format=csv,noheader'], + stdout=subprocess.PIPE).stdout.decode('utf-8').strip().split('\n'): + name, mem = gpu.split(', ') + runtime_recs['cuda'].append(f'{name} ({mem})') + if len(runtime_recs) > 0: + for annotated_view in annotated.views: + if annotated_view.metadata.app == self.metadata.identifier: + annotated_view.metadata.set_additional_property('runtime', runtime_recs) return annotated.serialize(pretty=pretty, sanitize=True) @abstractmethod