PageRenderTime 34ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/lib/python/indra/util/simperf_proc_interface.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 191 lines | 181 code | 0 blank | 10 comment | 0 complexity | d1a1dc8660b173973faf609779de96a1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env python
  2. """\
  3. @file simperf_proc_interface.py
  4. @brief Utility to extract log messages from *.<pid>.llsd files containing performance statistics.
  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. # ----------------------------------------------------
  25. # Utility to extract log messages from *.<pid>.llsd
  26. # files that contain performance statistics.
  27. # ----------------------------------------------------
  28. import sys, os
  29. if os.path.exists("setup-path.py"):
  30. execfile("setup-path.py")
  31. from indra.base import llsd
  32. DEFAULT_PATH="/dev/shm/simperf/"
  33. # ----------------------------------------------------
  34. # Pull out the stats and return a single document
  35. def parse_logfile(filename, target_column=None, verbose=False):
  36. full_doc = []
  37. # Open source temp log file. Let exceptions percolate up.
  38. sourcefile = open( filename,'r')
  39. if verbose:
  40. print "Reading " + filename
  41. # Parse and output all lines from the temp file
  42. for line in sourcefile.xreadlines():
  43. partial_doc = llsd.parse(line)
  44. if partial_doc is not None:
  45. if target_column is None:
  46. full_doc.append(partial_doc)
  47. else:
  48. trim_doc = { target_column: partial_doc[target_column] }
  49. if target_column != "fps":
  50. trim_doc[ 'fps' ] = partial_doc[ 'fps' ]
  51. trim_doc[ '/total_time' ] = partial_doc[ '/total_time' ]
  52. trim_doc[ 'utc_time' ] = partial_doc[ 'utc_time' ]
  53. full_doc.append(trim_doc)
  54. sourcefile.close()
  55. return full_doc
  56. # Extract just the meta info line, and the timestamp of the first/last frame entry.
  57. def parse_logfile_info(filename, verbose=False):
  58. # Open source temp log file. Let exceptions percolate up.
  59. sourcefile = open(filename, 'rU') # U is to open with Universal newline support
  60. if verbose:
  61. print "Reading " + filename
  62. # The first line is the meta info line.
  63. info_line = sourcefile.readline()
  64. if not info_line:
  65. sourcefile.close()
  66. return None
  67. # The rest of the lines are frames. Read the first and last to get the time range.
  68. info = llsd.parse( info_line )
  69. info['start_time'] = None
  70. info['end_time'] = None
  71. first_frame = sourcefile.readline()
  72. if first_frame:
  73. try:
  74. info['start_time'] = int(llsd.parse(first_frame)['timestamp'])
  75. except:
  76. pass
  77. # Read the file backwards to find the last two lines.
  78. sourcefile.seek(0, 2)
  79. file_size = sourcefile.tell()
  80. offset = 1024
  81. num_attempts = 0
  82. end_time = None
  83. if file_size < offset:
  84. offset = file_size
  85. while 1:
  86. sourcefile.seek(-1*offset, 2)
  87. read_str = sourcefile.read(offset)
  88. # Remove newline at the end
  89. if read_str[offset - 1] == '\n':
  90. read_str = read_str[0:-1]
  91. lines = read_str.split('\n')
  92. full_line = None
  93. if len(lines) > 2: # Got two line
  94. try:
  95. end_time = llsd.parse(lines[-1])['timestamp']
  96. except:
  97. # We couldn't parse this line. Try once more.
  98. try:
  99. end_time = llsd.parse(lines[-2])['timestamp']
  100. except:
  101. # Nope. Just move on.
  102. pass
  103. break
  104. if len(read_str) == file_size: # Reached the beginning
  105. break
  106. offset += 1024
  107. info['end_time'] = int(end_time)
  108. sourcefile.close()
  109. return info
  110. def parse_proc_filename(filename):
  111. try:
  112. name_as_list = filename.split(".")
  113. cur_stat_type = name_as_list[0].split("_")[0]
  114. cur_pid = name_as_list[1]
  115. except IndexError, ValueError:
  116. return (None, None)
  117. return (cur_pid, cur_stat_type)
  118. # ----------------------------------------------------
  119. def get_simstats_list(path=None):
  120. """ Return stats (pid, type) listed in <type>_proc.<pid>.llsd """
  121. if path is None:
  122. path = DEFAULT_PATH
  123. simstats_list = []
  124. for file_name in os.listdir(path):
  125. if file_name.endswith(".llsd") and file_name != "simperf_proc_config.llsd":
  126. simstats_info = parse_logfile_info(path + file_name)
  127. if simstats_info is not None:
  128. simstats_list.append(simstats_info)
  129. return simstats_list
  130. def get_log_info_list(pid=None, stat_type=None, path=None, target_column=None, verbose=False):
  131. """ Return data from all llsd files matching the pid and stat type """
  132. if path is None:
  133. path = DEFAULT_PATH
  134. log_info_list = {}
  135. for file_name in os.listdir ( path ):
  136. if file_name.endswith(".llsd") and file_name != "simperf_proc_config.llsd":
  137. (cur_pid, cur_stat_type) = parse_proc_filename(file_name)
  138. if cur_pid is None:
  139. continue
  140. if pid is not None and pid != cur_pid:
  141. continue
  142. if stat_type is not None and stat_type != cur_stat_type:
  143. continue
  144. log_info_list[cur_pid] = parse_logfile(path + file_name, target_column, verbose)
  145. return log_info_list
  146. def delete_simstats_files(pid=None, stat_type=None, path=None):
  147. """ Delete *.<pid>.llsd files """
  148. if path is None:
  149. path = DEFAULT_PATH
  150. del_list = []
  151. for file_name in os.listdir(path):
  152. if file_name.endswith(".llsd") and file_name != "simperf_proc_config.llsd":
  153. (cur_pid, cur_stat_type) = parse_proc_filename(file_name)
  154. if cur_pid is None:
  155. continue
  156. if pid is not None and pid != cur_pid:
  157. continue
  158. if stat_type is not None and stat_type != cur_stat_type:
  159. continue
  160. del_list.append(cur_pid)
  161. # Allow delete related exceptions to percolate up if this fails.
  162. os.unlink(os.path.join(DEFAULT_PATH, file_name))
  163. return del_list