@@ -48,6 +48,12 @@ type resourceSpec struct {
48
48
resourceCheckPeriod time.Duration
49
49
}
50
50
51
+ // isSpecified returns true if any part of resourceSpec is specified,
52
+ // i.e. has non-zero value.
53
+ func (rs * resourceSpec ) isSpecified () bool {
54
+ return rs != nil && (rs .expectedMaxCPU != 0 || rs .expectedMaxRAM != 0 )
55
+ }
56
+
51
57
// childProcess is a child process that can be monitored and the output
52
58
// of which will be written to a log file.
53
59
type childProcess struct {
@@ -101,10 +107,11 @@ type childProcess struct {
101
107
}
102
108
103
109
type startParams struct {
104
- name string
105
- logFilePath string
106
- cmd string
107
- cmdArgs []string
110
+ name string
111
+ logFilePath string
112
+ cmd string
113
+ cmdArgs []string
114
+ resourceSpec * resourceSpec
108
115
}
109
116
110
117
type ResourceConsumption struct {
@@ -126,6 +133,7 @@ func (cp *childProcess) start(params startParams) error {
126
133
127
134
cp .name = params .name
128
135
cp .doneSignal = make (chan struct {})
136
+ cp .resourceSpec = params .resourceSpec
129
137
130
138
log .Printf ("Starting %s (%s)" , cp .name , params .cmd )
131
139
@@ -236,8 +244,11 @@ func (cp *childProcess) stop() {
236
244
})
237
245
}
238
246
239
- func (cp * childProcess ) watchResourceConsumption (spec * resourceSpec ) error {
240
- cp .resourceSpec = spec
247
+ func (cp * childProcess ) watchResourceConsumption () error {
248
+ if ! cp .resourceSpec .isSpecified () {
249
+ // Resource monitoring is not enabled.
250
+ return nil
251
+ }
241
252
242
253
var err error
243
254
cp .processMon , err = process .NewProcess (int32 (cp .cmd .Process .Pid ))
@@ -257,7 +268,7 @@ func (cp *childProcess) watchResourceConsumption(spec *resourceSpec) error {
257
268
}
258
269
259
270
// Measure every resourceCheckPeriod.
260
- ticker := time .NewTicker (spec .resourceCheckPeriod )
271
+ ticker := time .NewTicker (cp . resourceSpec .resourceCheckPeriod )
261
272
defer ticker .Stop ()
262
273
263
274
for {
@@ -351,7 +362,7 @@ func (cp *childProcess) isAllowedResourceUsage() bool {
351
362
352
363
// GetResourceConsumption returns resource consumption as a string
353
364
func (cp * childProcess ) GetResourceConsumption () string {
354
- if cp .resourceSpec == nil {
365
+ if ! cp .resourceSpec . isSpecified () {
355
366
// Monitoring is not enabled.
356
367
return ""
357
368
}
0 commit comments