Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010, 2011 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

"""Utilities for measuring execution time. 

""" 

__all__ = ["logInfo", "timeMethod"] 

 

import functools 

import resource 

import time 

import datetime 

 

from lsst.log import Log, log 

 

 

def logPairs(obj, pairs, logLevel=Log.DEBUG): 

"""Log ``(name, value)`` pairs to ``obj.metadata`` and ``obj.log`` 

 

Parameters 

---------- 

obj : `lsst.pipe.base.Task`-type 

A `~lsst.pipe.base.Task` or any other object with these two attributes: 

 

- ``metadata`` an instance of `lsst.daf.base.PropertyList`` (or other object with 

``add(name, value)`` method). 

- ``log`` an instance of `lsst.log.Log`. 

 

pairs : sequence 

A sequence of ``(name, value)`` pairs, with value typically numeric. 

logLevel : optional 

Log level (an `lsst.log` level constant, such as `lsst.log.Log.DEBUG`). 

""" 

strList = [] 

for name, value in pairs: 

try: 

# Use LongLong explicitly here in case an early value in the sequence is int-sized 

obj.metadata.addLongLong(name, value) 

except TypeError as e: 

obj.metadata.add(name, value) 

strList.append("%s=%s" % (name, value)) 

log(obj.log.getName(), logLevel, "; ".join(strList)) 

 

 

def logInfo(obj, prefix, logLevel=Log.DEBUG): 

"""Log timer information to ``obj.metadata`` and ``obj.log``. 

 

Parameters 

---------- 

obj : `lsst.pipe.base.Task`-type 

A `~lsst.pipe.base.Task` or any other object with these two attributes: 

 

- ``metadata`` an instance of `lsst.daf.base.PropertyList`` (or other object with 

``add(name, value)`` method). 

- ``log`` an instance of `lsst.log.Log`. 

 

prefix 

Name prefix, the resulting entries are ``CpuTime``, etc.. For example timeMethod uses 

``prefix = Start`` when the method begins and ``prefix = End`` when the method ends. 

logLevel : optional 

Log level (an `lsst.log` level constant, such as `lsst.log.Log.DEBUG`). 

 

Notes 

----- 

Logged items include: 

 

- ``Utc``: UTC date in ISO format (only in metadata since log entries have timestamps). 

- ``CpuTime``: CPU time (seconds). 

- ``MaxRss``: maximum resident set size. 

 

All logged resource information is only for the current process; child processes are excluded. 

""" 

cpuTime = time.clock() 

utcStr = datetime.datetime.utcnow().isoformat() 

res = resource.getrusage(resource.RUSAGE_SELF) 

obj.metadata.add(name=prefix + "Utc", value=utcStr) # log messages already have timestamps 

logPairs(obj=obj, 

pairs=[ 

(prefix + "CpuTime", cpuTime), 

(prefix + "UserTime", res.ru_utime), 

(prefix + "SystemTime", res.ru_stime), 

(prefix + "MaxResidentSetSize", int(res.ru_maxrss)), 

(prefix + "MinorPageFaults", int(res.ru_minflt)), 

(prefix + "MajorPageFaults", int(res.ru_majflt)), 

(prefix + "BlockInputs", int(res.ru_inblock)), 

(prefix + "BlockOutputs", int(res.ru_oublock)), 

(prefix + "VoluntaryContextSwitches", int(res.ru_nvcsw)), 

(prefix + "InvoluntaryContextSwitches", int(res.ru_nivcsw)), 

], 

logLevel=logLevel, 

) 

 

 

def timeMethod(func): 

"""Decorator to measure duration of a task method. 

 

Parameters 

---------- 

func 

The method to wrap. 

 

Notes 

----- 

Writes various measures of time and possibly memory usage to the task's metadata; all items are prefixed 

with the function name. 

 

.. warning:: 

 

This decorator only works with instance methods of Task, or any class with these attributes: 

 

- ``metadata``: an instance of `lsst.daf.base.PropertyList` (or other object with 

``add(name, value)`` method). 

- ``log``: an instance of `lsst.log.Log`. 

 

Examples 

-------- 

To use:: 

 

import lsst.pipe.base as pipeBase 

class FooTask(pipeBase.Task): 

pass 

 

@pipeBase.timeMethod 

def run(self, ...): # or any other instance method you want to time 

pass 

""" 

 

@functools.wraps(func) 

def wrapper(self, *args, **keyArgs): 

logInfo(obj=self, prefix=func.__name__ + "Start") 

try: 

res = func(self, *args, **keyArgs) 

finally: 

logInfo(obj=self, prefix=func.__name__ + "End") 

return res 

return wrapper