Skip to content

Commit 4d8c1ba

Browse files
furkanonderrostedt
authored andcommitted
tools/rtla: Improve exception handling in timerlat_load.py
The enhancements made to timerlat_load.py are intended to improve the script's exception handling. Summary of the changes: - Specific exceptions are now caught for CPU affinity and priority settings, with clearer error messages provided. - The timerlat file descriptor opening now includes handling for PermissionError and OSError, with informative messages. - In the infinite loop, generic exceptions have been replaced with specific types like KeyboardInterrupt and IOError, improving feedback. Before: $ sudo python timerlat_load.py 122 Error setting affinity After: $ sudo python timerlat_load.py 122 Error setting affinity: [Errno 22] Invalid argument Before: $ sudo python timerlat_load.py 1 -p 950 Error setting priority After: $ sudo python timerlat_load.py 1 -p 950 Error setting priority: [Errno 22] Invalid argument Before: $ python timerlat_load.py 1 Error opening timerlat fd, did you run timerlat -U? After: $ python timerlat_load.py 1 Permission denied. Please check your access rights. Cc: "lgoncalv@redhat.com" <lgoncalv@redhat.com> Cc: "jkacur@redhat.com" <jkacur@redhat.com> Link: https://lore.kernel.org/Q_k1s4hBtUy2px8ou0QKenjEK2_T_LoV8IxAE79aBakBogb-7uHp2fpET3oWtI1t3dy8uKjWeRzQOdKNzIzOOpyM4OjutJOriZ9TrGY6b-g=@protonmail.com Signed-off-by: Furkan Onder <furkanonder@protonmail.com> Reviewed-by: Tomas Glozar <tglozar@redhat.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent bd26818 commit 4d8c1ba

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

tools/tracing/rtla/sample/timerlat_load.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,48 @@
3131

3232
try:
3333
affinity_mask = {args.cpu}
34-
except:
35-
print("Invalid cpu: " + args.cpu)
36-
exit(1)
37-
38-
try:
3934
os.sched_setaffinity(0, affinity_mask)
40-
except:
41-
print("Error setting affinity")
42-
exit(1)
35+
except Exception as e:
36+
print(f"Error setting affinity: {e}")
37+
sys.exit(1)
4338

4439
if args.prio:
4540
try:
4641
param = os.sched_param(args.prio)
4742
os.sched_setscheduler(0, os.SCHED_FIFO, param)
48-
except:
49-
print("Error setting priority")
50-
exit(1)
43+
except Exception as e:
44+
print(f"Error setting priority: {e}")
45+
sys.exit(1)
5146

5247
try:
5348
timerlat_path = f"/sys/kernel/tracing/osnoise/per_cpu/cpu{args.cpu}/timerlat_fd"
5449
timerlat_fd = open(timerlat_path, 'r')
55-
except:
50+
except PermissionError:
51+
print("Permission denied. Please check your access rights.")
52+
sys.exit(1)
53+
except OSError:
5654
print("Error opening timerlat fd, did you run timerlat -U?")
57-
exit(1)
55+
sys.exit(1)
5856

5957
try:
6058
data_fd = open("/dev/full", 'r')
61-
except:
62-
print("Error opening data fd")
59+
except Exception as e:
60+
print(f"Error opening data fd: {e}")
61+
sys.exit(1)
6362

6463
while True:
6564
try:
6665
timerlat_fd.read(1)
6766
data_fd.read(20 * 1024 * 1024)
68-
except:
67+
except KeyboardInterrupt:
6968
print("Leaving")
7069
break
70+
except IOError as e:
71+
print(f"I/O error occurred: {e}")
72+
break
73+
except Exception as e:
74+
print(f"Unexpected error: {e}")
75+
break
7176

7277
timerlat_fd.close()
7378
data_fd.close()

0 commit comments

Comments
 (0)