report_executable.x 4.47 KB
Newer Older
1
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
"""
-------------------------------------------------------------------------------
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.
-------------------------------------------------------------------------------
"""
12
13

import os
14
import argparse
15
16
17
import re
from subprocess import Popen, PIPE

18
19
20
# NOTE: use here of harness code, somewhat against original intent for this
# to be a standalone script.

21
22
from libraries.status_file import StatusFile

23
#------------------------------------------------------------------------------
24

25
26
27
28
29
30
31
32
33
34
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.')
35

36
    i_help = ('The test id string.')
37

38
39
40
    parser = argparse.ArgumentParser(description=command_description)
    parser.add_argument('-p', help=p_help, required=True)
    parser.add_argument('-i', help=i_help, required=True)
41

42
43
44
45
46
47
48
49
50
51
    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.
    """
52

53
    # Get the command line arguments.
54

55
56
57
    args = process_command_line_args()
    path_to_results = args.p
    test_id = args.i
58

59
    # Extract info from scheduler.
60

61
    scheduler = 'lsf' if 'LSF_BINDIR' in os.environ else 'pbs'
62

63
    if scheduler == 'pbs':
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        #
        # 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
78
79
        process = Popen(['qstat'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        output, err = process.communicate()
80
81
82
83
84
85
86
87

        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

88
    if scheduler == 'lsf':
89
90
91
92
        running_jobs_tag = 'RUN'
        job_counts = {}
        job_counts.setdefault(running_jobs_tag, 0)
        total_job_count = 0
93
94
        process = Popen(['bjobs', '-a'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        output, err = process.communicate()
95
96
97

        for line in output.decode("utf-8").split('\n'):
            if len(line) > 0 and re.search('^[0-9]', line):
98
                tokens = re.split(r'\s+', line)
99
100
101
102
103
104
105
                job_status = tokens[2]
                job_counts.setdefault(job_status, 0)
                job_counts[job_status] += 1
                total_job_count += 1

    #---Write system logs with data.

106
107
108
    status_file_obj = StatusFile(test_id, StatusFile.MODE_OLD)

    running_job_count = job_counts[running_jobs_tag]
109

110
111
112
113
114
115
116
    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))
117
118
119
120
121
122
123
124
125
126

    #---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()

127
#------------------------------------------------------------------------------
128

129
if __name__ == '__main__':
130
131
    main()

132
#------------------------------------------------------------------------------