File: wait_for_suspend.py

package info (click to toggle)
slurm-wlm-contrib 24.11.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 50,596 kB
  • sloc: ansic: 529,598; exp: 64,795; python: 17,051; sh: 9,411; javascript: 6,528; makefile: 4,030; perl: 3,762; pascal: 131
file content (44 lines) | stat: -rw-r--r-- 1,363 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
############################################################################
# Copyright (C) SchedMD LLC.
############################################################################
import time
import sys

BILLION = 1_000_000_000


def main():
    """Print time every second and report detected process suspensions.

    Prints every second some information for 'start' number of seconds. Takes in
    an optional integer terminal argument for the value of 'start' -- defaults
    to 30 if no such argument is given.

    Additionally, this method attempts to determine if it has been suspended
    and, upon determination, prints 'JobSuspended' once the process is resumed.
    Because programs can't catch suspension signals like 'SIGSTOP', this method
    instead determines it was suspended if it hasn't updated the variable 'last'
    in the last 3 seconds. Note that all time measurements are performed in
    nanoseconds.
    """

    if len(sys.argv) > 1:
        start = int(sys.argv[1])
    else:
        start = 30

    last = time.monotonic_ns()
    for i in range(start, 0, -1):
        print(f"{i:02d} {last}")
        sys.stdout.flush()
        time.sleep(1)
        now = time.monotonic_ns()
        if now > (last + 3 * BILLION):
            print("JobSuspended")
        last = now

    print("AllDone")


if __name__ == "__main__":
    main()