simperf_oprof_interface.py
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:6k
源码类别:

游戏引擎

开发平台:

C++ Builder

  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-2010, 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.     
  55. class SimPerfOProfileInterface:
  56.     def __init__(self):
  57.         self.isBrief = True             # public
  58.         self.isValid = False            # public
  59.         self.result = []                # public
  60.     def parse(self, input):
  61.         in_samples = False
  62.         for line in input:
  63.             if in_samples:
  64.                 if line[0:6] == "------":
  65.                     self.isBrief = False
  66.                     self._parseFull(input)
  67.                 else:
  68.                     self._parseBrief(input, line)
  69.                 self.isValid = True
  70.                 return
  71.             try:
  72.                 hd1, remain = line.split(None, 1)
  73.                 if hd1 == "samples":
  74.                     in_samples = True
  75.             except ValueError:
  76.                 pass
  77.     def _parseBrief(self, input, line1):
  78.         try:
  79.             fld1, fld2, fld3, fld4 = line1.split(None, 3)
  80.             self.result.append({"samples" : fld1,
  81.                                 "percentage" : fld2,
  82.                                 "file" : fld3,
  83.                                 "symbol" : fld4.strip("n")})
  84.         except ValueError:
  85.             pass
  86.         for line in input:
  87.             try:
  88.                 fld1, fld2, fld3, fld4 = line.split(None, 3)
  89.                 self.result.append({"samples" : fld1,
  90.                                     "percentage" : fld2,
  91.                                     "file" : fld3,
  92.                                     "symbol" : fld4.strip("n")})
  93.             except ValueError:
  94.                 pass
  95.     def _parseFull(self, input):
  96.         state = 0       # In 'called_by' section
  97.         calls = []
  98.         called_by = []
  99.         current = {}
  100.         for line in input:
  101.             if line[0:6] == "------":
  102.                 if len(current):
  103.                     current["calls"] = calls
  104.                     current["called_by"] = called_by
  105.                     self.result.append(current)
  106.                 state = 0
  107.                 calls = []
  108.                 called_by = []
  109.                 current = {}
  110.             else:
  111.                 try:
  112.                     fld1, fld2, fld3, fld4 = line.split(None, 3)
  113.                     tmp = {"samples" : fld1,
  114.                            "percentage" : fld2,
  115.                            "file" : fld3,
  116.                            "symbol" : fld4.strip("n")}
  117.                 except ValueError:
  118.                     continue
  119.                 if line[0] != " ":
  120.                     current = tmp
  121.                     state = 1       # In 'calls' section
  122.                 elif state == 0:
  123.                     called_by.append(tmp)
  124.                 else:
  125.                     calls.append(tmp)
  126.         if len(current):
  127.             current["calls"] = calls
  128.             current["called_by"] = called_by
  129.             self.result.append(current)
  130. def main(argv=None):
  131.     opts, args = getopt.getopt(sys.argv[1:], "i:o:h", ["in=", "out=", "help"])
  132.     input_file = sys.stdin
  133.     output_file = sys.stdout
  134.     for o, a in opts:
  135.         if o in ("-i", "--in"):
  136.             input_file = open(a, 'r')
  137.         if o in ("-o", "--out"):
  138.             output_file = open(a, 'w')
  139.         if o in ("-h", "--help"):
  140.             usage()
  141.             sys.exit(0)
  142.     oprof = SimPerfOProfileInterface()
  143.     oprof.parse(input_file)
  144.     if input_file != sys.stdin:
  145.         input_file.close()
  146.     # Create JSONable dict with interesting data and format/print it
  147.     print >>output_file, simplejson.dumps(oprof.result)
  148.     return 0
  149. if __name__ == "__main__":
  150.     sys.exit(main())