PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/lib/python/indra/util/simperf_oprof_interface.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 167 lines | 157 code | 0 blank | 10 comment | 0 complexity | 437fdedf0a1cc9c9921f35cf0b098892 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env python
  2. """\
  3. @file simperf_oprof_interface.py
  4. @brief Manage OProfile data collection on a host
  5. $LicenseInfo:firstyear=2008&license=mit$
  6. Copyright (c) 2008-2009, Linden Research, Inc.
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. $/LicenseInfo$
  23. """
  24. import sys, os, getopt
  25. import simplejson
  26. def usage():
  27. print "Usage:"
  28. print sys.argv[0] + " [options]"
  29. print " Digest the OProfile report forms that come out of the"
  30. print " simperf_oprof_ctl program's -r/--report command. The result"
  31. print " is an array of dictionaires with the following keys:"
  32. print
  33. print " symbol Name of sampled, calling, or called procedure"
  34. print " file Executable or library where symbol resides"
  35. print " percentage Percentage contribution to profile, calls or called"
  36. print " samples Sample count"
  37. print " calls Methods called by the method in question (full only)"
  38. print " called_by Methods calling the method (full only)"
  39. print
  40. print " For 'full' reports the two keys 'calls' and 'called_by' are"
  41. print " themselves arrays of dictionaries based on the first four keys."
  42. print
  43. print "Return Codes:"
  44. print " None. Aggressively digests everything. Will likely mung results"
  45. print " if a program or library has whitespace in its name."
  46. print
  47. print "Options:"
  48. print " -i, --in Input settings filename. (Default: stdin)"
  49. print " -o, --out Output settings filename. (Default: stdout)"
  50. print " -h, --help Print this message and exit."
  51. print
  52. print "Interfaces:"
  53. print " class SimPerfOProfileInterface()"
  54. class SimPerfOProfileInterface:
  55. def __init__(self):
  56. self.isBrief = True # public
  57. self.isValid = False # public
  58. self.result = [] # public
  59. def parse(self, input):
  60. in_samples = False
  61. for line in input:
  62. if in_samples:
  63. if line[0:6] == "------":
  64. self.isBrief = False
  65. self._parseFull(input)
  66. else:
  67. self._parseBrief(input, line)
  68. self.isValid = True
  69. return
  70. try:
  71. hd1, remain = line.split(None, 1)
  72. if hd1 == "samples":
  73. in_samples = True
  74. except ValueError:
  75. pass
  76. def _parseBrief(self, input, line1):
  77. try:
  78. fld1, fld2, fld3, fld4 = line1.split(None, 3)
  79. self.result.append({"samples" : fld1,
  80. "percentage" : fld2,
  81. "file" : fld3,
  82. "symbol" : fld4.strip("\n")})
  83. except ValueError:
  84. pass
  85. for line in input:
  86. try:
  87. fld1, fld2, fld3, fld4 = line.split(None, 3)
  88. self.result.append({"samples" : fld1,
  89. "percentage" : fld2,
  90. "file" : fld3,
  91. "symbol" : fld4.strip("\n")})
  92. except ValueError:
  93. pass
  94. def _parseFull(self, input):
  95. state = 0 # In 'called_by' section
  96. calls = []
  97. called_by = []
  98. current = {}
  99. for line in input:
  100. if line[0:6] == "------":
  101. if len(current):
  102. current["calls"] = calls
  103. current["called_by"] = called_by
  104. self.result.append(current)
  105. state = 0
  106. calls = []
  107. called_by = []
  108. current = {}
  109. else:
  110. try:
  111. fld1, fld2, fld3, fld4 = line.split(None, 3)
  112. tmp = {"samples" : fld1,
  113. "percentage" : fld2,
  114. "file" : fld3,
  115. "symbol" : fld4.strip("\n")}
  116. except ValueError:
  117. continue
  118. if line[0] != " ":
  119. current = tmp
  120. state = 1 # In 'calls' section
  121. elif state == 0:
  122. called_by.append(tmp)
  123. else:
  124. calls.append(tmp)
  125. if len(current):
  126. current["calls"] = calls
  127. current["called_by"] = called_by
  128. self.result.append(current)
  129. def main(argv=None):
  130. opts, args = getopt.getopt(sys.argv[1:], "i:o:h", ["in=", "out=", "help"])
  131. input_file = sys.stdin
  132. output_file = sys.stdout
  133. for o, a in opts:
  134. if o in ("-i", "--in"):
  135. input_file = open(a, 'r')
  136. if o in ("-o", "--out"):
  137. output_file = open(a, 'w')
  138. if o in ("-h", "--help"):
  139. usage()
  140. sys.exit(0)
  141. oprof = SimPerfOProfileInterface()
  142. oprof.parse(input_file)
  143. if input_file != sys.stdin:
  144. input_file.close()
  145. # Create JSONable dict with interesting data and format/print it
  146. print >>output_file, simplejson.dumps(oprof.result)
  147. return 0
  148. if __name__ == "__main__":
  149. sys.exit(main())