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