PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/lib/python/indra/util/llperformance.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 182 lines | 151 code | 4 blank | 27 comment | 0 complexity | 7ceabf702b8dee08883a49817bcd2a67 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env python
  2. """\
  3. @file llperformance.py
  4. $LicenseInfo:firstyear=2010&license=viewerlgpl$
  5. Second Life Viewer Source Code
  6. Copyright (C) 2010-2011, Linden Research, Inc.
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation;
  10. version 2.1 of the License only.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  19. $/LicenseInfo$
  20. """
  21. # ------------------------------------------------
  22. # Sim metrics utility functions.
  23. import glob, os, time, sys, stat, exceptions
  24. from indra.base import llsd
  25. gBlockMap = {} #Map of performance metric data with function hierarchy information.
  26. gCurrentStatPath = ""
  27. gIsLoggingEnabled=False
  28. class LLPerfStat:
  29. def __init__(self,key):
  30. self.mTotalTime = 0
  31. self.mNumRuns = 0
  32. self.mName=key
  33. self.mTimeStamp = int(time.time()*1000)
  34. self.mUTCTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  35. def __str__(self):
  36. return "%f" % self.mTotalTime
  37. def start(self):
  38. self.mStartTime = int(time.time() * 1000000)
  39. self.mNumRuns += 1
  40. def stop(self):
  41. execution_time = int(time.time() * 1000000) - self.mStartTime
  42. self.mTotalTime += execution_time
  43. def get_map(self):
  44. results={}
  45. results['name']=self.mName
  46. results['utc_time']=self.mUTCTime
  47. results['timestamp']=self.mTimeStamp
  48. results['us']=self.mTotalTime
  49. results['count']=self.mNumRuns
  50. return results
  51. class PerfError(exceptions.Exception):
  52. def __init__(self):
  53. return
  54. def __Str__(self):
  55. print "","Unfinished LLPerfBlock"
  56. class LLPerfBlock:
  57. def __init__( self, key ):
  58. global gBlockMap
  59. global gCurrentStatPath
  60. global gIsLoggingEnabled
  61. #Check to see if we're running metrics right now.
  62. if gIsLoggingEnabled:
  63. self.mRunning = True #Mark myself as running.
  64. self.mPreviousStatPath = gCurrentStatPath
  65. gCurrentStatPath += "/" + key
  66. if gCurrentStatPath not in gBlockMap:
  67. gBlockMap[gCurrentStatPath] = LLPerfStat(key)
  68. self.mStat = gBlockMap[gCurrentStatPath]
  69. self.mStat.start()
  70. def finish( self ):
  71. global gBlockMap
  72. global gIsLoggingEnabled
  73. if gIsLoggingEnabled:
  74. self.mStat.stop()
  75. self.mRunning = False
  76. gCurrentStatPath = self.mPreviousStatPath
  77. # def __del__( self ):
  78. # if self.mRunning:
  79. # #SPATTERS FIXME
  80. # raise PerfError
  81. class LLPerformance:
  82. #--------------------------------------------------
  83. # Determine whether or not we want to log statistics
  84. def __init__( self, process_name = "python" ):
  85. self.process_name = process_name
  86. self.init_testing()
  87. self.mTimeStamp = int(time.time()*1000)
  88. self.mUTCTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  89. def init_testing( self ):
  90. global gIsLoggingEnabled
  91. host_performance_file = "/dev/shm/simperf/simperf_proc_config.llsd"
  92. #If file exists, open
  93. if os.path.exists(host_performance_file):
  94. file = open (host_performance_file,'r')
  95. #Read serialized LLSD from file.
  96. body = llsd.parse(file.read())
  97. #Calculate time since file last modified.
  98. stats = os.stat(host_performance_file)
  99. now = time.time()
  100. mod = stats[stat.ST_MTIME]
  101. age = now - mod
  102. if age < ( body['duration'] ):
  103. gIsLoggingEnabled = True
  104. def get ( self ):
  105. global gIsLoggingEnabled
  106. return gIsLoggingEnabled
  107. #def output(self,ptr,path):
  108. # if 'stats' in ptr:
  109. # stats = ptr['stats']
  110. # self.mOutputPtr[path] = stats.get_map()
  111. # if 'children' in ptr:
  112. # children=ptr['children']
  113. # curptr = self.mOutputPtr
  114. # curchildren={}
  115. # curptr['children'] = curchildren
  116. # for key in children:
  117. # curchildren[key]={}
  118. # self.mOutputPtr = curchildren[key]
  119. # self.output(children[key],path + '/' + key)
  120. def done(self):
  121. global gBlockMap
  122. if not self.get():
  123. return
  124. output_name = "/dev/shm/simperf/%s_proc.%d.llsd" % (self.process_name, os.getpid())
  125. output_file = open(output_name, 'w')
  126. process_info = {
  127. "name" : self.process_name,
  128. "pid" : os.getpid(),
  129. "ppid" : os.getppid(),
  130. "timestamp" : self.mTimeStamp,
  131. "utc_time" : self.mUTCTime,
  132. }
  133. output_file.write(llsd.format_notation(process_info))
  134. output_file.write('\n')
  135. for key in gBlockMap.keys():
  136. gBlockMap[key] = gBlockMap[key].get_map()
  137. output_file.write(llsd.format_notation(gBlockMap))
  138. output_file.write('\n')
  139. output_file.close()