timerlat_load.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. except:
  32. print("Invalid cpu: " + args.cpu)
  33. exit(1)
  34. try:
  35. os.sched_setaffinity(0, affinity_mask);
  36. except:
  37. print("Error setting affinity")
  38. exit(1)
  39. if (args.prio):
  40. try:
  41. param = os.sched_param(args.prio)
  42. os.sched_setscheduler(0, os.SCHED_FIFO, param)
  43. except:
  44. print("Error setting priority")
  45. exit(1)
  46. try:
  47. timerlat_path = "/sys/kernel/tracing/osnoise/per_cpu/cpu" + args.cpu + "/timerlat_fd"
  48. timerlat_fd = open(timerlat_path, 'r')
  49. except:
  50. print("Error opening timerlat fd, did you run timerlat -U?")
  51. exit(1)
  52. try:
  53. data_fd = open("/dev/full", 'r');
  54. except:
  55. print("Error opening data fd")
  56. while True:
  57. try:
  58. timerlat_fd.read(1)
  59. data_fd.read(20*1024*1024)
  60. except:
  61. print("Leaving")
  62. break
  63. timerlat_fd.close()
  64. data_fd.close()