rt-tester.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env python
  2. #
  3. # rt-mutex tester
  4. #
  5. # (C) 2006 Thomas Gleixner <tglx@linutronix.de>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. import os
  12. import sys
  13. import getopt
  14. import shutil
  15. import string
  16. # Globals
  17. quiet = 0
  18. test = 0
  19. comments = 0
  20. sysfsprefix = "/sys/devices/system/rttest/rttest"
  21. statusfile = "/status"
  22. commandfile = "/command"
  23. # Command opcodes
  24. cmd_opcodes = {
  25. "schedother" : "1",
  26. "schedfifo" : "2",
  27. "lock" : "3",
  28. "locknowait" : "4",
  29. "lockint" : "5",
  30. "lockintnowait" : "6",
  31. "lockcont" : "7",
  32. "unlock" : "8",
  33. "lockbkl" : "9",
  34. "unlockbkl" : "10",
  35. "signal" : "11",
  36. "resetevent" : "98",
  37. "reset" : "99",
  38. }
  39. test_opcodes = {
  40. "prioeq" : ["P" , "eq" , None],
  41. "priolt" : ["P" , "lt" , None],
  42. "priogt" : ["P" , "gt" , None],
  43. "nprioeq" : ["N" , "eq" , None],
  44. "npriolt" : ["N" , "lt" , None],
  45. "npriogt" : ["N" , "gt" , None],
  46. "unlocked" : ["M" , "eq" , 0],
  47. "trylock" : ["M" , "eq" , 1],
  48. "blocked" : ["M" , "eq" , 2],
  49. "blockedwake" : ["M" , "eq" , 3],
  50. "locked" : ["M" , "eq" , 4],
  51. "opcodeeq" : ["O" , "eq" , None],
  52. "opcodelt" : ["O" , "lt" , None],
  53. "opcodegt" : ["O" , "gt" , None],
  54. "eventeq" : ["E" , "eq" , None],
  55. "eventlt" : ["E" , "lt" , None],
  56. "eventgt" : ["E" , "gt" , None],
  57. }
  58. # Print usage information
  59. def usage():
  60. print "rt-tester.py <-c -h -q -t> <testfile>"
  61. print " -c display comments after first command"
  62. print " -h help"
  63. print " -q quiet mode"
  64. print " -t test mode (syntax check)"
  65. print " testfile: read test specification from testfile"
  66. print " otherwise from stdin"
  67. return
  68. # Print progress when not in quiet mode
  69. def progress(str):
  70. if not quiet:
  71. print str
  72. # Analyse a status value
  73. def analyse(val, top, arg):
  74. intval = int(val)
  75. if top[0] == "M":
  76. intval = intval / (10 ** int(arg))
  77. intval = intval % 10
  78. argval = top[2]
  79. elif top[0] == "O":
  80. argval = int(cmd_opcodes.get(arg, arg))
  81. else:
  82. argval = int(arg)
  83. # progress("%d %s %d" %(intval, top[1], argval))
  84. if top[1] == "eq" and intval == argval:
  85. return 1
  86. if top[1] == "lt" and intval < argval:
  87. return 1
  88. if top[1] == "gt" and intval > argval:
  89. return 1
  90. return 0
  91. # Parse the commandline
  92. try:
  93. (options, arguments) = getopt.getopt(sys.argv[1:],'chqt')
  94. except getopt.GetoptError, ex:
  95. usage()
  96. sys.exit(1)
  97. # Parse commandline options
  98. for option, value in options:
  99. if option == "-c":
  100. comments = 1
  101. elif option == "-q":
  102. quiet = 1
  103. elif option == "-t":
  104. test = 1
  105. elif option == '-h':
  106. usage()
  107. sys.exit(0)
  108. # Select the input source
  109. if arguments:
  110. try:
  111. fd = open(arguments[0])
  112. except Exception,ex:
  113. sys.stderr.write("File not found %s\n" %(arguments[0]))
  114. sys.exit(1)
  115. else:
  116. fd = sys.stdin
  117. linenr = 0
  118. # Read the test patterns
  119. while 1:
  120. linenr = linenr + 1
  121. line = fd.readline()
  122. if not len(line):
  123. break
  124. line = line.strip()
  125. parts = line.split(":")
  126. if not parts or len(parts) < 1:
  127. continue
  128. if len(parts[0]) == 0:
  129. continue
  130. if parts[0].startswith("#"):
  131. if comments > 1:
  132. progress(line)
  133. continue
  134. if comments == 1:
  135. comments = 2
  136. progress(line)
  137. cmd = parts[0].strip().lower()
  138. opc = parts[1].strip().lower()
  139. tid = parts[2].strip()
  140. dat = parts[3].strip()
  141. try:
  142. # Test or wait for a status value
  143. if cmd == "t" or cmd == "w":
  144. testop = test_opcodes[opc]
  145. fname = "%s%s%s" %(sysfsprefix, tid, statusfile)
  146. if test:
  147. print fname
  148. continue
  149. while 1:
  150. query = 1
  151. fsta = open(fname, 'r')
  152. status = fsta.readline().strip()
  153. fsta.close()
  154. stat = status.split(",")
  155. for s in stat:
  156. s = s.strip()
  157. if s.startswith(testop[0]):
  158. # Seperate status value
  159. val = s[2:].strip()
  160. query = analyse(val, testop, dat)
  161. break
  162. if query or cmd == "t":
  163. break
  164. progress(" " + status)
  165. if not query:
  166. sys.stderr.write("Test failed in line %d\n" %(linenr))
  167. sys.exit(1)
  168. # Issue a command to the tester
  169. elif cmd == "c":
  170. cmdnr = cmd_opcodes[opc]
  171. # Build command string and sys filename
  172. cmdstr = "%s:%s" %(cmdnr, dat)
  173. fname = "%s%s%s" %(sysfsprefix, tid, commandfile)
  174. if test:
  175. print fname
  176. continue
  177. fcmd = open(fname, 'w')
  178. fcmd.write(cmdstr)
  179. fcmd.close()
  180. except Exception,ex:
  181. sys.stderr.write(str(ex))
  182. sys.stderr.write("\nSyntax error in line %d\n" %(linenr))
  183. if not test:
  184. fd.close()
  185. sys.exit(1)
  186. # Normal exit pass
  187. print "Pass"
  188. sys.exit(0)