@@ -269,29 +269,51 @@ module.exports = { Config }
269269function getProfilers ( {
270270 DD_PROFILING_HEAP_ENABLED , DD_PROFILING_WALLTIME_ENABLED , DD_PROFILING_PROFILERS
271271} ) {
272- // First consider "legacy" DD_PROFILING_PROFILERS env variable, defaulting to wall + space
272+ // First consider "legacy" DD_PROFILING_PROFILERS env variable, defaulting to space + wall
273273 // Use a Set to avoid duplicates
274- const profilers = new Set ( ( DD_PROFILING_PROFILERS ?? 'wall,space' ) . split ( ',' ) )
274+ // NOTE: space profiler is very deliberately in the first position. This way
275+ // when profilers are stopped sequentially one after the other to create
276+ // snapshots the space profile won't include memory taken by profiles created
277+ // before it in the sequence. That memory is ultimately transient and will be
278+ // released when all profiles are subsequently encoded.
279+ const profilers = new Set ( ( DD_PROFILING_PROFILERS ?? 'space,wall' ) . split ( ',' ) )
280+
281+ let spaceExplicitlyEnabled = false
282+ // Add/remove space depending on the value of DD_PROFILING_HEAP_ENABLED
283+ if ( DD_PROFILING_HEAP_ENABLED != null ) {
284+ if ( isTrue ( DD_PROFILING_HEAP_ENABLED ) ) {
285+ if ( ! profilers . has ( 'space' ) ) {
286+ profilers . add ( 'space' )
287+ spaceExplicitlyEnabled = true
288+ }
289+ } else if ( isFalse ( DD_PROFILING_HEAP_ENABLED ) ) {
290+ profilers . delete ( 'space' )
291+ }
292+ }
275293
276294 // Add/remove wall depending on the value of DD_PROFILING_WALLTIME_ENABLED
277295 if ( DD_PROFILING_WALLTIME_ENABLED != null ) {
278296 if ( isTrue ( DD_PROFILING_WALLTIME_ENABLED ) ) {
279297 profilers . add ( 'wall' )
280298 } else if ( isFalse ( DD_PROFILING_WALLTIME_ENABLED ) ) {
281299 profilers . delete ( 'wall' )
300+ profilers . delete ( 'cpu' ) // remove alias too
282301 }
283302 }
284303
285- // Add/remove wall depending on the value of DD_PROFILING_HEAP_ENABLED
286- if ( DD_PROFILING_HEAP_ENABLED != null ) {
287- if ( isTrue ( DD_PROFILING_HEAP_ENABLED ) ) {
288- profilers . add ( 'space' )
289- } else if ( isFalse ( DD_PROFILING_HEAP_ENABLED ) ) {
290- profilers . delete ( 'space' )
304+ const profilersArray = [ ...profilers ]
305+ // If space was added through DD_PROFILING_HEAP_ENABLED, ensure it is in the
306+ // first position. Basically, the only way for it not to be in the first
307+ // position is if it was explicitly specified in a different position in
308+ // DD_PROFILING_PROFILERS.
309+ if ( spaceExplicitlyEnabled ) {
310+ const spaceIdx = profilersArray . indexOf ( 'space' )
311+ if ( spaceIdx > 0 ) {
312+ profilersArray . splice ( spaceIdx , 1 )
313+ profilersArray . unshift ( 'space' )
291314 }
292315 }
293-
294- return [ ...profilers ]
316+ return profilersArray
295317}
296318
297319function getExportStrategy ( name , options ) {
0 commit comments