rt-tester.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/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. "signal" : "11",
  34. "resetevent" : "98",
  35. "reset" : "99",
  36. }
  37. test_opcodes = {
  38. "prioeq" : ["P" , "eq" , None],
  39. "priolt" : ["P" , "lt" , None],
  40. "priogt" : ["P" , "gt" , None],
  41. "nprioeq" : ["N" , "eq" , None],
  42. "npriolt" : ["N" , "lt" , None],
  43. "npriogt" : ["N" , "gt" , None],
  44. "unlocked" : ["M" , "eq" , 0],
  45. "trylock" : ["M" , "eq" , 1],
  46. "blocked" : ["M" , "eq" , 2],
  47. "blockedwake" : ["M" , "eq" , 3],
  48. "locked" : ["M" , "eq" , 4],
  49. "opcodeeq" : ["O" , "eq" , None],
  50. "opcodelt" : ["O" , "lt" , None],
  51. "opcodegt" : ["O" , "gt" , None],
  52. "eventeq" : ["E" , "eq" , None],
  53. "eventlt" : ["E" , "lt" , None],
  54. "eventgt" : ["E" , "gt" , None],
  55. }
  56. # Print usage information
  57. def usage():
  58. print "rt-tester.py <-c -h -q -t> <testfile>"
  59. print " -c display comments after first command"
  60. print " -h help"
  61. print " -q quiet mode"
  62. print " -t test mode (syntax check)"
  63. print " testfile: read test specification from testfile"
  64. print " otherwise from stdin"
  65. return
  66. # Print progress when not in quiet mode
  67. def progress(str):
  68. if not quiet:
  69. print str
  70. # Analyse a status value
  71. def analyse(val, top, arg):
  72. intval = int(val)
  73. if top[0] == "M":
  74. intval = intval / (10 ** int(arg))
  75. intval = intval % 10
  76. argval = top[2]
  77. elif top[0] == "O":
  78. argval = int(cmd_opcodes.get(arg, arg))
  79. else:
  80. argval = int(arg)
  81. # progress("%d %s %d" %(intval, top[1], argval))
  82. if top[1] == "eq" and intval == argval:
  83. return 1
  84. if top[1] == "lt" and intval < argval:
  85. return 1
  86. if top[1] == "gt" and intval > argval:
  87. return 1
  88. return 0
  89. # Parse the commandline
  90. try:
  91. (options, arguments) = getopt.getopt(sys.argv[1:],'chqt')
  92. except getopt.GetoptError, ex:
  93. usage()
  94. sys.exit(1)
  95. # Parse commandline options
  96. for option, value in options:
  97. if option == "-c":
  98. comments = 1
  99. elif option == "-q":
  100. quiet = 1
  101. elif option == "-t":
  102. test = 1
  103. elif option == '-h':
  104. usage()
  105. sys.exit(0)
  106. # Select the input source
  107. if arguments:
  108. try:
  109. fd = open(arguments[0])
  110. except Exception,ex:
  111. sys.stderr.write("File not found %s\n" %(arguments[0]))
  112. sys.exit(1)
  113. else:
  114. fd = sys.stdin
  115. linenr = 0
  116. # Read the test patterns
  117. while 1:
  118. linenr = linenr + 1
  119. line = fd.readline()
  120. if not len(line):
  121. break
  122. line = line.strip()
  123. parts = line.split(":")
  124. if not parts or len(parts) < 1:
  125. continue
  126. if len(parts[0]) == 0:
  127. continue
  128. if parts[0].startswith("#"):
  129. if comments > 1:
  130. progress(line)
  131. continue
  132. if comments == 1:
  133. comments = 2
  134. progress(line)
  135. cmd = parts[0].strip().lower()
  136. opc = parts[1].strip().lower()
  137. tid = parts[2].strip()
  138. dat = parts[3].strip()
  139. try:
  140. # Test or wait for a status value
  141. if cmd == "t" or cmd == "w":
  142. testop = test_opcodes[opc]
  143. fname = "%s%s%s" %(sysfsprefix, tid, statusfile)
  144. if test:
  145. print fname
  146. continue
  147. while 1:
  148. query = 1
  149. fsta = open(fname, 'r')
  150. status = fsta.readline().strip()
  151. fsta.close()
  152. stat = status.split(",")
  153. for s in stat:
  154. s = s.strip()
  155. if s.startswith(testop[0]):
  156. # Separate status value
  157. val = s[2:].strip()
  158. query = analyse(val, testop, dat)
  159. break
  160. if query or cmd == "t":
  161. break
  162. progress(" " + status)
  163. if not query:
  164. sys.stderr.write("Test failed in line %d\n" %(linenr))
  165. sys.exit(1)
  166. # Issue a command to the tester
  167. elif cmd == "c":
  168. cmdnr = cmd_opcodes[opc]
  169. # Build command string and sys filename
  170. cmdstr = "%s:%s" %(cmdnr, dat)
  171. fname = "%s%s%s" %(sysfsprefix, tid, commandfile)
  172. if test:
  173. print fname
  174. continue
  175. fcmd = open(fname, 'w')
  176. fcmd.write(cmdstr)
  177. fcmd.close()
  178. except Exception,ex:
  179. sys.stderr.write(str(ex))
  180. sys.stderr.write("\nSyntax error in line %d\n" %(linenr))
  181. if not test:
  182. fd.close()
  183. sys.exit(1)
  184. # Normal exit pass
  185. print "Pass"
  186. sys.exit(0)