#!/usr/bin/env python """ ------------------------------------------------------------------------------- File: report_executable.x Author: Wayne Joubert (joubert@ornl.gov) Modified: Wayne Joubert National Center for Computational Sciences, Scientific Computing Group. Oak Ridge National Laboratory Copyright (C) 2016 Oak Ridge National Laboratory, UT-Battelle, LLC. ------------------------------------------------------------------------------- """ import os import argparse import re from subprocess import Popen, PIPE # NOTE: use here of harness code, somewhat against original intent for this # to be a standalone script. from libraries.status_file import StatusFile #------------------------------------------------------------------------------ def process_command_line_args(): """Get the command line arguments.""" command_description = ( 'Script used by app to report arbitrary user-defined results ' 'from a run back to the harness.') p_help = ( 'The absolute path to the results. This path must have the ' 'appropiate permissions to permit the user of the test to r, w, and x.') i_help = ('The test id string.') parser = argparse.ArgumentParser(description=command_description) parser.add_argument('-p', help=p_help, required=True) parser.add_argument('-i', help=i_help, required=True) args = parser.parse_args() return args #------------------------------------------------------------------------------ def main(): """Main program for report operation. Extracts information from the run and stores in files and sends to the logger. """ # Get the command line arguments. args = process_command_line_args() path_to_results = args.p test_id = args.i # Extract info from scheduler. scheduler = 'lsf' if 'LSF_BINDIR' in os.environ else 'pbs' if scheduler == 'pbs': # # C - Job is completed after having run/ # E - Job is exiting after having run. # H - Job is held. # Q - job is queued, eligible to run or routed. # R - job is running. # T - job is being moved to new location. # W - job is waiting for its execution time (-a option) to be reached. # S - (Unicos only) job is suspend. # running_jobs_tag = 'R' job_counts = {} job_counts.setdefault(running_jobs_tag, 0) total_job_count = 0 process = Popen(['qstat'], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = process.communicate() for line in output.decode("utf-8").split('\n'): if len(line) > 69 and re.search('^[0-9]', line): job_status = line[69] job_counts.setdefault(job_status, 0) job_counts[job_status] += 1 total_job_count += 1 if scheduler == 'lsf': running_jobs_tag = 'RUN' job_counts = {} job_counts.setdefault(running_jobs_tag, 0) total_job_count = 0 process = Popen(['bjobs', '-a'], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = process.communicate() for line in output.decode("utf-8").split('\n'): if len(line) > 0 and re.search('^[0-9]', line): tokens = re.split(r'\s+', line) job_status = tokens[2] job_counts.setdefault(job_status, 0) job_counts[job_status] += 1 total_job_count += 1 #---Write system logs with data. status_file_obj = StatusFile(test_id, StatusFile.MODE_OLD) running_job_count = job_counts[running_jobs_tag] status_file_obj.log_custom_event(event_type='system', event_subtype='running_job_count', event_value=str(running_job_count)) status_file_obj.log_custom_event(event_type='system', event_subtype='total_job_count', event_value=str(total_job_count)) #---Output results to file. file_path = os.path.join(path_to_results, 'out_system_job_counts.csv') file_ = open(file_path, 'w') file_.write('running_job_count' + '\t' + 'total_job_count' + '\n') file_.write(str(job_counts[running_jobs_tag]) + '\t' + str(total_job_count) + '\n') file_.close() #------------------------------------------------------------------------------ if __name__ == '__main__': main() #------------------------------------------------------------------------------