Skip to content

Commit 37ad59c

Browse files
committed
Avoid restarting profiling for last collect
Upon process exit, profiler collects profiles a last time and was restarting all profilers before stopping them. Avoid this useless restart by changing the profiler API and make `profile()` methode take a restart argument.
1 parent 17fca21 commit 37ad59c

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

packages/dd-trace/src/profiling/profiler.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Profiler extends EventEmitter {
6969
mapper,
7070
nearOOMCallback: this._nearOOMExport.bind(this)
7171
})
72-
this._logger.debug(`Started ${profiler.type} profiler in thread ${threadId}`)
72+
this._logger.debug(`Started ${profiler.type} profiler in ${threadNamePrefix} thread`)
7373
}
7474

7575
this._capture(this._timeoutInterval, start)
@@ -83,7 +83,7 @@ class Profiler extends EventEmitter {
8383

8484
_nearOOMExport (profileType, encodedProfile) {
8585
const start = this._lastStart
86-
const end = new Date()
86+
const end = Date.now()
8787
this._submit({
8888
[profileType]: encodedProfile
8989
}, start, end, snapshotKinds.ON_OUT_OF_MEMORY)
@@ -98,7 +98,7 @@ class Profiler extends EventEmitter {
9898

9999
// collect and export current profiles
100100
// once collect returns, profilers can be safely stopped
101-
this._collect(snapshotKinds.ON_SHUTDOWN)
101+
this._collect(snapshotKinds.ON_SHUTDOWN, false)
102102
this._stop()
103103
}
104104

@@ -109,13 +109,11 @@ class Profiler extends EventEmitter {
109109

110110
for (const profiler of this._config.profilers) {
111111
profiler.stop()
112-
this._logger.debug(`Stopped ${profiler.type} profiler`)
112+
this._logger.debug(`Stopped ${profiler.type} profiler in ${threadNamePrefix} thread`)
113113
}
114114

115115
clearTimeout(this._timer)
116116
this._timer = undefined
117-
118-
return this
119117
}
120118

121119
_capture (timeout, start) {
@@ -129,18 +127,21 @@ class Profiler extends EventEmitter {
129127
}
130128
}
131129

132-
async _collect (snapshotKind) {
130+
async _collect (snapshotKind, restart = true) {
133131
if (!this._enabled) return
134132

135-
const start = this._lastStart
136-
const end = new Date()
133+
const startDate = this._lastStart
134+
const endDate = new Date()
137135
const profiles = []
138136
const encodedProfiles = {}
139137

140138
try {
141139
// collect profiles synchronously so that profilers can be safely stopped asynchronously
142140
for (const profiler of this._config.profilers) {
143-
const profile = profiler.profile(start, end)
141+
const profile = profiler.profile({ startDate, endDate, restart })
142+
if (!restart) {
143+
this._logger.debug(`Stopped ${profiler.type} profiler in ${threadNamePrefix} thread`)
144+
}
144145
if (!profile) continue
145146
profiles.push({ profiler, profile })
146147
}
@@ -156,8 +157,10 @@ class Profiler extends EventEmitter {
156157
})
157158
}
158159

159-
this._capture(this._timeoutInterval, end)
160-
await this._submit(encodedProfiles, start, end, snapshotKind)
160+
if (restart) {
161+
this._capture(this._timeoutInterval, endDate)
162+
}
163+
await this._submit(encodedProfiles, startDate, endDate, snapshotKind)
161164
this._logger.debug('Submitted profiles')
162165
} catch (err) {
163166
this._logger.error(err)
@@ -197,10 +200,10 @@ class ServerlessProfiler extends Profiler {
197200
this._flushAfterIntervals = this._config.flushInterval / 1000
198201
}
199202

200-
async _collect (snapshotKind) {
201-
if (this._profiledIntervals >= this._flushAfterIntervals) {
203+
async _collect (snapshotKind, restart = true) {
204+
if (this._profiledIntervals >= this._flushAfterIntervals || !restart) {
202205
this._profiledIntervals = 0
203-
await super._collect(snapshotKind)
206+
await super._collect(snapshotKind, restart)
204207
} else {
205208
this._profiledIntervals += 1
206209
this._capture(this._timeoutInterval, new Date())

packages/dd-trace/src/profiling/profilers/events.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ class EventsProfiler {
171171
stop () {
172172
if (this._observer) {
173173
this._observer.disconnect()
174+
this._observer = undefined
174175
}
175176
}
176177

177-
profile (startDate, endDate) {
178+
profile ({ startDate, endDate, restart = true }) {
178179
if (this.entries.length === 0) {
179180
// No events in the period; don't produce a profile
180181
return null
@@ -243,6 +244,10 @@ class EventsProfiler {
243244
unit: stringTable.dedup(pprofValueUnit)
244245
})
245246

247+
if (!restart) {
248+
this.stop()
249+
}
250+
246251
return new Profile({
247252
sampleType: [timeValueType],
248253
timeNanos: endDate.getTime() * MS_TO_NS,

packages/dd-trace/src/profiling/profilers/space.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ class NativeSpaceProfiler {
3333
}
3434
}
3535

36-
profile () {
37-
return this._pprof.heap.profile(undefined, this._mapper, getThreadLabels)
36+
profile ({ restart = true } = {}) {
37+
const profile = this._pprof.heap.profile(undefined, this._mapper, getThreadLabels)
38+
if (!restart) {
39+
this.stop()
40+
}
41+
return profile
3842
}
3943

4044
encode (profile) {

packages/dd-trace/src/profiling/profilers/wall.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,32 @@ class NativeWallProfiler {
223223

224224
_stop (restart) {
225225
if (!this._started) return
226+
226227
if (this._captureSpanData) {
227228
// update last sample context if needed
228229
this._enter()
229230
this._lastSampleCount = 0
230231
}
231232
const profile = this._pprof.time.stop(restart, this._generateLabels)
233+
232234
if (restart) {
233235
const v8BugDetected = this._pprof.time.v8ProfilerStuckEventLoopDetected()
234236
if (v8BugDetected !== 0) {
235237
this._reportV8bug(v8BugDetected === 1)
236238
}
239+
} else {
240+
if (this._captureSpanData) {
241+
beforeCh.unsubscribe(this._enter)
242+
enterCh.unsubscribe(this._enter)
243+
spanFinishCh.unsubscribe(this._spanFinished)
244+
this._profilerState = undefined
245+
this._lastSpan = undefined
246+
this._lastStartedSpans = undefined
247+
this._lastWebTags = undefined
248+
}
249+
this._started = false
237250
}
251+
238252
return profile
239253
}
240254

@@ -276,30 +290,16 @@ class NativeWallProfiler {
276290
return labels
277291
}
278292

279-
profile () {
280-
return this._stop(true)
293+
profile ({ restart = true } = {}) {
294+
return this._stop(restart)
281295
}
282296

283297
encode (profile) {
284298
return this._pprof.encode(profile)
285299
}
286300

287301
stop () {
288-
if (!this._started) return
289-
290-
const profile = this._stop(false)
291-
if (this._captureSpanData) {
292-
beforeCh.unsubscribe(this._enter)
293-
enterCh.unsubscribe(this._enter)
294-
spanFinishCh.unsubscribe(this._spanFinished)
295-
this._profilerState = undefined
296-
this._lastSpan = undefined
297-
this._lastStartedSpans = undefined
298-
this._lastWebTags = undefined
299-
}
300-
301-
this._started = false
302-
return profile
302+
this._stop(false)
303303
}
304304
}
305305

0 commit comments

Comments
 (0)