|
| 1 | +# Copyright 2016-2021 Swiss National Supercomputing Centre (CSCS/ETH Zurich) |
| 2 | +# ReFrame Project Developers. See the top-level LICENSE file for details. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +# |
| 7 | +# SGE backend |
| 8 | +# |
| 9 | +# - Initial version submitted by Mosè Giordano, UCL (based on the PBS backend) |
| 10 | +# |
| 11 | + |
| 12 | +import functools |
| 13 | +import re |
| 14 | +import time |
| 15 | +import xml.etree.ElementTree as ET |
| 16 | + |
| 17 | +import reframe.core.runtime as rt |
| 18 | +import reframe.utility.osext as osext |
| 19 | +from reframe.core.backends import register_scheduler |
| 20 | +from reframe.core.exceptions import JobSchedulerError |
| 21 | +from reframe.core.schedulers.pbs import PbsJobScheduler |
| 22 | +from reframe.utility import seconds_to_hms |
| 23 | + |
| 24 | +_run_strict = functools.partial(osext.run_command, check=True) |
| 25 | + |
| 26 | + |
| 27 | +@register_scheduler('sge') |
| 28 | +class SgeJobScheduler(PbsJobScheduler): |
| 29 | + def __init__(self): |
| 30 | + self._prefix = '#$' |
| 31 | + self._submit_timeout = rt.runtime().get_option( |
| 32 | + f'schedulers/@{self.registered_name}/job_submit_timeout' |
| 33 | + ) |
| 34 | + |
| 35 | + def emit_preamble(self, job): |
| 36 | + preamble = [ |
| 37 | + self._format_option(f'-N "{job.name}"'), |
| 38 | + self._format_option(f'-o {job.stdout}'), |
| 39 | + self._format_option(f'-e {job.stderr}'), |
| 40 | + self._format_option(f'-wd {job.workdir}') |
| 41 | + ] |
| 42 | + |
| 43 | + if job.time_limit is not None: |
| 44 | + h, m, s = seconds_to_hms(job.time_limit) |
| 45 | + preamble.append( |
| 46 | + self._format_option(f'-l h_rt=%d:%d:%d' % (h, m, s)) |
| 47 | + ) |
| 48 | + |
| 49 | + # Emit the rest of the options |
| 50 | + options = job.options + job.cli_options |
| 51 | + for opt in options: |
| 52 | + if opt.startswith('#'): |
| 53 | + preamble.append(opt) |
| 54 | + else: |
| 55 | + preamble.append(self._format_option(opt)) |
| 56 | + |
| 57 | + return preamble |
| 58 | + |
| 59 | + def submit(self, job): |
| 60 | + # `-o` and `-e` options are only recognized in command line by the PBS, |
| 61 | + # SGE, and Slurm wrappers. |
| 62 | + cmd = f'qsub -o {job.stdout} -e {job.stderr} {job.script_filename}' |
| 63 | + completed = _run_strict(cmd, timeout=self._submit_timeout) |
| 64 | + jobid_match = re.search(r'^Your job (?P<jobid>\S+)', completed.stdout) |
| 65 | + if not jobid_match: |
| 66 | + raise JobSchedulerError('could not retrieve the job id ' |
| 67 | + 'of the submitted job') |
| 68 | + |
| 69 | + job._jobid = jobid_match.group('jobid') |
| 70 | + job._submit_time = time.time() |
| 71 | + |
| 72 | + def poll(self, *jobs): |
| 73 | + if jobs: |
| 74 | + # Filter out non-jobs |
| 75 | + jobs = [job for job in jobs if job is not None] |
| 76 | + |
| 77 | + if not jobs: |
| 78 | + return |
| 79 | + |
| 80 | + user = osext.osuser() |
| 81 | + completed = osext.run_command(f'qstat -xml -u {user}') |
| 82 | + if completed.returncode != 0: |
| 83 | + raise JobSchedulerError( |
| 84 | + f'qstat failed with exit code {completed.returncode} ' |
| 85 | + f'(standard error follows):\n{completed.stderr}' |
| 86 | + ) |
| 87 | + |
| 88 | + # Index the jobs to poll on their jobid |
| 89 | + jobs_to_poll = {job.jobid: job for job in jobs} |
| 90 | + |
| 91 | + # Parse the XML |
| 92 | + root = ET.fromstring(completed.stdout) |
| 93 | + |
| 94 | + # We are iterating over the returned XML and update the status of the |
| 95 | + # jobs relevant to ReFrame; the naming convention of variables matches |
| 96 | + # that of SGE's XML output |
| 97 | + |
| 98 | + known_jobs = set() # jobs known to the SGE scheduler |
| 99 | + for queue_info in root: |
| 100 | + # Reads the XML and prints jobs with status belonging to user. |
| 101 | + if queue_info is None: |
| 102 | + raise JobSchedulerError('could not retrieve queue information') |
| 103 | + |
| 104 | + for job_list in queue_info: |
| 105 | + if job_list.find("JB_owner").text != user: |
| 106 | + # Not a job of this user. |
| 107 | + continue |
| 108 | + |
| 109 | + jobid = job_list.find("JB_job_number").text |
| 110 | + if jobid not in jobs_to_poll: |
| 111 | + # Not a reframe job |
| 112 | + continue |
| 113 | + |
| 114 | + state = job_list.find("state").text |
| 115 | + job = jobs_to_poll[jobid] |
| 116 | + known_jobs.add(job) |
| 117 | + |
| 118 | + # For the list of known statuses see `man 5 sge_status` |
| 119 | + # (https://arc.liv.ac.uk/SGE/htmlman/htmlman5/sge_status.html) |
| 120 | + if state in ['r', 'hr', 't', 'Rr', 'Rt']: |
| 121 | + job._state = 'RUNNING' |
| 122 | + elif state in ['qw', 'Rq', 'hqw', 'hRwq']: |
| 123 | + job._state = 'PENDING' |
| 124 | + elif state in ['s', 'ts', 'S', 'tS', 'T', 'tT', 'Rs', |
| 125 | + 'Rts', 'RS', 'RtS', 'RT', 'RtT']: |
| 126 | + job._state = 'SUSPENDED' |
| 127 | + elif state in ['Eqw', 'Ehqw', 'EhRqw']: |
| 128 | + job._state = 'ERROR' |
| 129 | + elif state in ['dr', 'dt', 'dRr', 'dRt', 'ds', |
| 130 | + 'dS', 'dT', 'dRs', 'dRS', 'dRT']: |
| 131 | + job._state = 'DELETING' |
| 132 | + elif state == 'z': |
| 133 | + job._state = 'COMPLETED' |
| 134 | + |
| 135 | + # Mark any "unknown" job as completed |
| 136 | + unknown_jobs = set(jobs) - known_jobs |
| 137 | + for job in unknown_jobs: |
| 138 | + self.log(f'Job {job.jobid} not known to scheduler, ' |
| 139 | + f'assuming job completed') |
| 140 | + job._state = 'COMPLETED' |
| 141 | + |
| 142 | + def finished(self, job): |
| 143 | + if job.exception: |
| 144 | + raise job.exception |
| 145 | + |
| 146 | + return job.state == 'COMPLETED' |
0 commit comments