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

游戏引擎

开发平台:

C++ Builder

  1. #!/usr/bin/python
  2. """
  3. @file llperformance.py
  4. @brief 
  5. $LicenseInfo:firstyear=2007&license=mit$
  6. Copyright (c) 2007-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. # ------------------------------------------------
  25. # Sim metrics utility functions.
  26. import glob, os, time, sys, stat, exceptions
  27. from indra.base import llsd
  28. gBlockMap = {}              #Map of performance metric data with function hierarchy information.
  29. gCurrentStatPath = ""
  30. gIsLoggingEnabled=False
  31. class LLPerfStat:
  32.     def __init__(self,key):
  33.         self.mTotalTime = 0
  34.         self.mNumRuns = 0
  35.         self.mName=key
  36.         self.mTimeStamp = int(time.time()*1000)
  37.         self.mUTCTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  38.     def __str__(self):
  39.         return "%f" %  self.mTotalTime
  40.     def start(self):
  41.         self.mStartTime = int(time.time() * 1000000)
  42.         self.mNumRuns += 1
  43.     def stop(self):
  44.         execution_time = int(time.time() * 1000000) - self.mStartTime
  45.         self.mTotalTime += execution_time
  46.     def get_map(self):
  47.         results={}
  48.         results['name']=self.mName
  49.         results['utc_time']=self.mUTCTime
  50.         results['timestamp']=self.mTimeStamp
  51.         results['us']=self.mTotalTime
  52.         results['count']=self.mNumRuns
  53.         return results
  54. class PerfError(exceptions.Exception):
  55.     def __init__(self):
  56.         return
  57.     def __Str__(self):
  58.         print "","Unfinished LLPerfBlock"
  59. class LLPerfBlock:
  60.     def __init__( self, key ):
  61.         global gBlockMap
  62.         global gCurrentStatPath
  63.         global gIsLoggingEnabled
  64.         #Check to see if we're running metrics right now.
  65.         if gIsLoggingEnabled:
  66.             self.mRunning = True        #Mark myself as running.
  67.     
  68.             self.mPreviousStatPath = gCurrentStatPath
  69.             gCurrentStatPath += "/" + key
  70.             if gCurrentStatPath not in gBlockMap:
  71.                 gBlockMap[gCurrentStatPath] = LLPerfStat(key)
  72.             self.mStat = gBlockMap[gCurrentStatPath]
  73.             self.mStat.start()
  74.     
  75.     def finish( self ):
  76.         global gBlockMap
  77.         global gIsLoggingEnabled
  78.         if gIsLoggingEnabled:
  79.             self.mStat.stop()
  80.             self.mRunning = False
  81.             gCurrentStatPath = self.mPreviousStatPath
  82. #    def __del__( self ):
  83. #        if self.mRunning:
  84. #            #SPATTERS FIXME
  85. #            raise PerfError
  86. class LLPerformance:
  87.     #--------------------------------------------------
  88.     # Determine whether or not we want to log statistics
  89.     def __init__( self, process_name = "python" ):
  90.         self.process_name = process_name
  91.         self.init_testing()
  92.         self.mTimeStamp = int(time.time()*1000)
  93.         self.mUTCTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  94.     def init_testing( self ):
  95.         global gIsLoggingEnabled
  96.         host_performance_file = "/dev/shm/simperf/simperf_proc_config.llsd"
  97.     
  98.         #If file exists, open
  99.         if os.path.exists(host_performance_file):
  100.             file = open (host_performance_file,'r')
  101.     
  102.             #Read serialized LLSD from file.
  103.             body = llsd.parse(file.read())
  104.     
  105.             #Calculate time since file last modified.
  106.             stats = os.stat(host_performance_file)
  107.             now = time.time()
  108.             mod = stats[stat.ST_MTIME]
  109.             age = now - mod
  110.     
  111.             if age < ( body['duration'] ):
  112.                 gIsLoggingEnabled = True
  113.     
  114.     def get ( self ):
  115.         global gIsLoggingEnabled
  116.         return gIsLoggingEnabled
  117.     #def output(self,ptr,path):
  118.     #    if 'stats' in ptr:
  119.     #        stats = ptr['stats']
  120.     #        self.mOutputPtr[path] = stats.get_map()
  121.     #    if 'children' in ptr:
  122.     #        children=ptr['children']
  123.     #        curptr = self.mOutputPtr
  124.     #        curchildren={}
  125.     #        curptr['children'] = curchildren
  126.     #        for key in children:
  127.     #            curchildren[key]={}
  128.     #            self.mOutputPtr = curchildren[key]
  129.     #            self.output(children[key],path + '/' + key)
  130.     
  131.     def done(self):
  132.         global gBlockMap
  133.         if not self.get():
  134.             return
  135.         output_name = "/dev/shm/simperf/%s_proc.%d.llsd" % (self.process_name, os.getpid())
  136.         output_file = open(output_name, 'w')
  137.         process_info = {
  138.             "name"  :   self.process_name,
  139.             "pid"   :   os.getpid(),
  140.             "ppid"  :   os.getppid(),
  141.             "timestamp" :   self.mTimeStamp,
  142.             "utc_time"  :   self.mUTCTime,
  143.             }
  144.         output_file.write(llsd.format_notation(process_info))
  145.         output_file.write('n')
  146.         for key in gBlockMap.keys():
  147.             gBlockMap[key] = gBlockMap[key].get_map()
  148.         output_file.write(llsd.format_notation(gBlockMap))
  149.         output_file.write('n')
  150.         output_file.close()