-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan_tty_daemon.py
executable file
·79 lines (69 loc) · 2.46 KB
/
fan_tty_daemon.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
import subprocess
import serial
import daemon
import lockfile
import time
import logging
LOGFILE = "/var/log/fan_tty_daemon.log"
PORT_NAME = "/dev/ttyUSB0"
PORT_BAUD = 115200
def open_serial():
ser = None
while ser is None:
try:
ser = serial.Serial(PORT_NAME, PORT_BAUD)
except:
logging.warning("Unable to establish serial connection; retrying in 10 seconds")
time.sleep(10)
logging.info(f"Established serial connection {ser.name}")
return ser
def main():
logging.basicConfig(
filename=LOGFILE,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO)
logging.info("Daemon is starting")
ser = open_serial()
failsafe = False
while True:
try:
proc = subprocess.run("nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits", shell=True, check=True, stdout=subprocess.PIPE)
temperatures = [int(x) for x in proc.stdout.decode().split("\n")[:-1]]
logging.debug(f"Raw profiled GPU temperatures are {temperatures}")
high_temp = min(max(max(temperatures), 10), 100)
logging.debug(f"Will pass temperature value of {high_temp}")
if failsafe:
logging.info("Transient nvidia-smi issue seems to have recovered; exiting failsafe mode")
failsafe = False
except:
if not failsafe:
logging.warning("nvidia-smi returned an error or unexpected output; entering failsafe mode")
failsafe = True
high_temp = 100
packet = (str(high_temp) + "\n").encode()
try:
ser.write(packet)
except:
logging.warning("Failed to write to serial port; was the board unplugged?")
try:
ser.close()
except:
pass
success = False
while not success:
try:
ser = open_serial()
ser.write(packet)
logging.info("Successfully re-established serial communication")
success = True
except:
try:
ser.close()
except:
pass
time.sleep(1)
if __name__ == "__main__":
with daemon.DaemonContext(pidfile=lockfile.FileLock("/var/run/fan_tty_daemon.pid")):
main()