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

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

# 

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

# 

 

"""Code for measuring metrics that apply to any Task. 

""" 

 

__all__ = ["TimingMetricConfig", "TimingMetricTask", 

"MemoryMetricConfig", "MemoryMetricTask", 

] 

 

import resource 

import sys 

 

import astropy.units as u 

 

import lsst.pex.config as pexConfig 

 

from lsst.verify import Measurement, Name, Datum 

from lsst.verify.gen2tasks.metricRegistry import registerMultiple 

from lsst.verify.tasks import MetricComputationError, MetadataMetricTask 

 

 

class TimeMethodMetricConfig(MetadataMetricTask.ConfigClass): 

"""Common config fields for metrics based on `~lsst.pipe.base.timeMethod`. 

 

These fields let metrics distinguish between different methods that have 

been decorated with `~lsst.pipe.base.timeMethod`. 

""" 

target = pexConfig.Field( 

dtype=str, 

doc="The method to profile, optionally prefixed by one or more tasks " 

"in the format of `lsst.pipe.base.Task.getFullMetadata()`.") 

metric = pexConfig.Field( 

dtype=str, 

doc="The fully qualified name of the metric to store the " 

"profiling information.") 

 

 

# Expose TimingMetricConfig name because config-writers expect it 

TimingMetricConfig = TimeMethodMetricConfig 

 

 

@registerMultiple("timing") 

class TimingMetricTask(MetadataMetricTask): 

"""A Task that computes a wall-clock time using metadata produced by the 

`lsst.pipe.base.timeMethod` decorator. 

 

Parameters 

---------- 

args 

kwargs 

Constructor parameters are the same as for 

`lsst.verify.tasks.MetricTask`. 

""" 

 

ConfigClass = TimingMetricConfig 

_DefaultName = "timingMetric" 

 

@classmethod 

def getInputMetadataKeys(cls, config): 

"""Get search strings for the metadata. 

 

Parameters 

---------- 

config : ``cls.ConfigClass`` 

Configuration for this task. 

 

Returns 

------- 

keys : `dict` 

A dictionary of keys, optionally prefixed by one or more tasks in 

the format of `lsst.pipe.base.Task.getFullMetadata()`. 

 

``"StartTime"`` 

The key for when the target method started (`str`). 

``"EndTime"`` 

The key for when the target method ended (`str`). 

""" 

keyBase = config.target 

return {"StartTime": keyBase + "StartCpuTime", 

"EndTime": keyBase + "EndCpuTime", 

"StartTimestamp": keyBase + "StartUtc", 

"EndTimestamp": keyBase + "EndUtc", 

} 

 

def makeMeasurement(self, timings): 

"""Compute a wall-clock measurement from metadata provided by 

`lsst.pipe.base.timeMethod`. 

 

Parameters 

---------- 

timings : `dict` [`str`, any] 

A representation of the metadata passed to `run`. The `dict` has 

the following keys: 

 

``"StartTime"`` 

The time the target method started (`float` or `None`). 

``"EndTime"`` 

The time the target method ended (`float` or `None`). 

``"StartTimestamp"``, ``"EndTimestamp"`` 

The start and end timestamps, in an ISO 8601-compliant format 

(`str` or `None`). 

 

Returns 

------- 

measurement : `lsst.verify.Measurement` or `None` 

The running time of the target method. 

 

Raises 

------ 

MetricComputationError 

Raised if the timing metadata are invalid. 

""" 

if timings["StartTime"] is not None or timings["EndTime"] is not None: 

try: 

totalTime = timings["EndTime"] - timings["StartTime"] 

except TypeError: 

raise MetricComputationError("Invalid metadata") 

else: 

meas = Measurement(self.getOutputMetricName(self.config), 

totalTime * u.second) 

meas.notes["estimator"] = "pipe.base.timeMethod" 

if timings["StartTimestamp"]: 

meas.extras["start"] = Datum(timings["StartTimestamp"]) 

if timings["EndTimestamp"]: 

meas.extras["end"] = Datum(timings["EndTimestamp"]) 

return meas 

else: 

self.log.info("Nothing to do: no timing information for %s found.", 

self.config.target) 

return None 

 

@classmethod 

def getOutputMetricName(cls, config): 

return Name(config.metric) 

 

 

# Expose MemoryMetricConfig name because config-writers expect it 

MemoryMetricConfig = TimeMethodMetricConfig 

 

 

@registerMultiple("memory") 

class MemoryMetricTask(MetadataMetricTask): 

"""A Task that computes the maximum resident set size using metadata 

produced by the `lsst.pipe.base.timeMethod` decorator. 

 

Parameters 

---------- 

args 

kwargs 

Constructor parameters are the same as for 

`lsst.verify.tasks.MetricTask`. 

""" 

 

ConfigClass = MemoryMetricConfig 

_DefaultName = "memoryMetric" 

 

@classmethod 

def getInputMetadataKeys(cls, config): 

"""Get search strings for the metadata. 

 

Parameters 

---------- 

config : ``cls.ConfigClass`` 

Configuration for this task. 

 

Returns 

------- 

keys : `dict` 

A dictionary of keys, optionally prefixed by one or more tasks in 

the format of `lsst.pipe.base.Task.getFullMetadata()`. 

 

``"EndMemory"`` 

The key for the memory usage at the end of the method (`str`). 

""" 

keyBase = config.target 

return {"EndMemory": keyBase + "EndMaxResidentSetSize"} 

 

def makeMeasurement(self, memory): 

"""Compute a maximum resident set size measurement from metadata 

provided by `lsst.pipe.base.timeMethod`. 

 

Parameters 

---------- 

memory : `dict` [`str`, any] 

A representation of the metadata passed to `run`. Each `dict` has 

the following keys: 

 

``"EndMemory"`` 

The memory usage at the end of the method (`int` or `None`). 

 

Returns 

------- 

measurement : `lsst.verify.Measurement` or `None` 

The maximum memory usage of the target method. 

 

Raises 

------ 

MetricComputationError 

Raised if the memory metadata are invalid. 

""" 

if memory["EndMemory"] is not None: 

try: 

maxMemory = int(memory["EndMemory"]) 

except (ValueError, TypeError) as e: 

raise MetricComputationError("Invalid metadata") from e 

else: 

meas = Measurement(self.getOutputMetricName(self.config), 

self._addUnits(maxMemory)) 

meas.notes['estimator'] = 'pipe.base.timeMethod' 

return meas 

else: 

self.log.info("Nothing to do: no memory information for %s found.", 

self.config.target) 

return None 

 

def _addUnits(self, memory): 

"""Represent memory usage in correct units. 

 

Parameters 

---------- 

memory : `int` 

The memory usage as returned by `resource.getrusage`, in 

platform-dependent units. 

 

Returns 

------- 

memory : `astropy.units.Quantity` 

The memory usage in absolute units. 

""" 

if sys.platform.startswith('darwin'): 

# MacOS uses bytes 

return memory * u.byte 

elif sys.platform.startswith('sunos') \ 

or sys.platform.startswith('solaris'): 

# Solaris and SunOS use pages 

return memory * resource.getpagesize() * u.byte 

else: 

# Assume Linux, which uses kibibytes 

return memory * u.kibibyte 

 

@classmethod 

def getOutputMetricName(cls, config): 

return Name(config.metric)