timerlat_load.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Copyright (C) 2024 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
  5. #
  6. # This is a sample code about how to use timerlat's timer by any workload
  7. # so rtla can measure and provide auto-analysis for the overall latency (IOW
  8. # the response time) for a task.
  9. #
  10. # Before running it, you need to dispatch timerlat with -U option in a terminal.
  11. # Then # run this script pinned to a CPU on another terminal. For example:
  12. #
  13. # timerlat_load.py 1 -p 95
  14. #
  15. # The "Timerlat IRQ" is the IRQ latency, The thread latency is the latency
  16. # for the python process to get the CPU. The Ret from user Timer Latency is
  17. # the overall latency. In other words, it is the response time for that
  18. # activation.
  19. #
  20. # This is just an example, the load is reading 20MB of data from /dev/full
  21. # It is in python because it is easy to read :-)
  22. import argparse
  23. import sys
  24. import os
  25. parser = argparse.ArgumentParser(description='user-space timerlat thread in Python')
  26. parser.add_argument("cpu", type=int, help='CPU to run timerlat thread')
  27. parser.add_argument("-p", "--prio", type=int, help='FIFO priority')
  28. args = parser.parse_args()
  29. try:
  30. affinity_mask = {args.cpu}
  31. os.sched_setaffinity(0, affinity_mask)
  32. except Exception as e:
  33. print(f"Error setting affinity: {e}")
  34. sys.exit(1)
  35. if args.prio:
  36. try:
  37. param = os.sched_param(args.prio)
  38. os.sched_setscheduler(0, os.SCHED_FIFO, param)
  39. except Exception as e:
  40. print(f"Error setting priority: {e}")
  41. sys.exit(1)
  42. try:
  43. timerlat_path = f"/sys/kernel/tracing/osnoise/per_cpu/cpu{args.cpu}/timerlat_fd"
  44. timerlat_fd = open(timerlat_path, 'r')
  45. except PermissionError:
  46. print("Permission denied. Please check your access rights.")
  47. sys.exit(1)
  48. except OSError:
  49. print("Error opening timerlat fd, did you run timerlat -U?")
  50. sys.exit(1)
  51. try:
  52. data_fd = open("/dev/full", 'r')
  53. except Exception as e:
  54. print(f"Error opening data fd: {e}")
  55. sys.exit(1)
  56. while True:
  57. try:
  58. timerlat_fd.read(1)
  59. data_fd.read(20 * 1024 * 1024)
  60. except KeyboardInterrupt:
  61. print("Leaving")
  62. break
  63. except IOError as e:
  64. print(f"I/O error occurred: {e}")
  65. break
  66. except Exception as e:
  67. print(f"Unexpected error: {e}")
  68. break
  69. timerlat_fd.close()
  70. data_fd.close()