Datasets:
File size: 3,131 Bytes
870b85a | 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """Bounded read-only live GPU inventory using previously recorded SSH endpoints."""
import argparse
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
import json
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
def scan(server, key, known_hosts):
argv = ['ssh', '-i', str(key), '-o', 'IdentitiesOnly=yes', '-o', 'BatchMode=yes',
'-o', 'ConnectTimeout=8', '-o', 'StrictHostKeyChecking=yes',
'-o', 'UserKnownHostsFile=' + str(known_hosts), '-p', str(server['port']),
'tutu@' + server['address'], 'timeout 15 nvidia-smi -q -x']
row = {k: server[k] for k in ('alias', 'address', 'port')}
row['observed_utc'] = datetime.now(timezone.utc).isoformat()
try:
result = subprocess.run(argv, capture_output=True, text=True, timeout=30)
row.update(exit_code=result.returncode, stderr=result.stderr)
if result.returncode == 0:
document = ET.fromstring(result.stdout)
row['gpus'] = []
for index, gpu in enumerate(document.findall('gpu')):
used = int(gpu.findtext('fb_memory_usage/used').split()[0])
utilization = int(gpu.findtext('utilization/gpu_util').split()[0])
processes = [{child.tag: child.text for child in process}
for process in gpu.findall('./processes/process_info')]
row['gpus'].append(dict(index=index, uuid=gpu.findtext('uuid'),
name=gpu.findtext('product_name'), used_mib=used,
utilization_percent=utilization, processes=processes,
idle=used < 100 and utilization == 0 and not processes))
except (subprocess.SubprocessError, OSError, ValueError, ET.ParseError) as error:
row['error'] = str(error)
return row
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--endpoints', type=Path, required=True)
parser.add_argument('--hosts', nargs='+', required=True)
parser.add_argument('--key', type=Path, required=True)
parser.add_argument('--known-hosts', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
servers = json.loads(args.endpoints.read_text())['servers']
selected = [server for server in servers if server['alias'] in args.hosts]
if {server['alias'] for server in selected} != set(args.hosts):
raise ValueError('An explicitly selected host has no recorded endpoint')
with ThreadPoolExecutor(max_workers=4) as executor:
rows = list(executor.map(lambda item: scan(item, args.key, args.known_hosts), selected))
payload = dict(snapshot_only_not_reservation=True,
idle_rule='memory<100MiB, utilization=0, no listed GPU processes', servers=rows)
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open('x') as stream:
json.dump(payload, stream, indent=2)
stream.write('\n')
print(json.dumps(payload, indent=2))
if __name__ == '__main__':
main()
|