@@ -22,7 +22,9 @@ const sqlWithReference = (ref) => {
22
22
23
23
let ctxIdx = 0
24
24
25
- const mergeStackTraces = async ( typeRegex , sel , fromTimeSec , toTimeSec , log ) => {
25
+ const newCtxIdx = ( ) => ++ ctxIdx
26
+
27
+ const importStackTraces = async ( typeRegex , sel , fromTimeSec , toTimeSec , log , _ctxIdx , save ) => {
26
28
const dist = clusterName ? '_dist' : ''
27
29
const v2 = checkVersion ( 'profiles_v2' , ( fromTimeSec - 3600 ) * 1000 )
28
30
const serviceNameSelector = serviceNameSelectorQuery ( sel )
@@ -127,30 +129,36 @@ const mergeStackTraces = async (typeRegex, sel, fromTimeSec, toTimeSec, log) =>
127
129
const binData = Uint8Array . from ( profiles . data )
128
130
log . debug ( `selectMergeStacktraces: profiles downloaded: ${ binData . length / 1025 } kB in ${ Date . now ( ) - start } ms` )
129
131
require ( './pprof-bin/pkg/pprof_bin' ) . init_panic_hook ( )
130
- const _ctxIdx = ++ ctxIdx
131
132
const [ legacyLen , shift ] = readULeb32 ( binData , 0 )
132
133
let ofs = shift
133
- try {
134
- let mergePprofLat = BigInt ( 0 )
135
- for ( let i = 0 ; i < legacyLen ; i ++ ) {
136
- const [ profLen , shift ] = readULeb32 ( binData , ofs )
137
- ofs += shift
138
- start = process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 )
139
- pprofBin . merge_prof ( _ctxIdx ,
140
- Uint8Array . from ( profiles . data . slice ( ofs , ofs + profLen ) ) ,
141
- `${ typeRegex . sampleType } :${ typeRegex . sampleUnit } ` )
142
- mergePprofLat += ( process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 ) ) - start
143
- ofs += profLen
144
- }
145
- start = process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 )
146
- pprofBin . merge_tree ( _ctxIdx , Uint8Array . from ( profiles . data . slice ( ofs ) ) ,
147
- typeRegex . sampleType + ':' + typeRegex . sampleUnit )
148
- const mergeTreeLat = ( process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 ) ) - start
134
+ let mergePprofLat = BigInt ( 0 )
135
+ for ( let i = 0 ; i < legacyLen ; i ++ ) {
136
+ const [ profLen , shift ] = readULeb32 ( binData , ofs )
137
+ ofs += shift
149
138
start = process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 )
139
+ pprofBin . merge_prof ( _ctxIdx ,
140
+ Uint8Array . from ( profiles . data . slice ( ofs , ofs + profLen ) ) ,
141
+ `${ typeRegex . sampleType } :${ typeRegex . sampleUnit } ` )
142
+ mergePprofLat += ( process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 ) ) - start
143
+ ofs += profLen
144
+ }
145
+ start = process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 )
146
+ save && require ( 'fs' ) . writeFileSync ( `/home/hromozeka/QXIP/qryn/data.${ Date . now ( ) } .bin` ,
147
+ Buffer . from ( Uint8Array . from ( profiles . data . slice ( ofs ) ) ) )
148
+ pprofBin . merge_tree ( _ctxIdx , Uint8Array . from ( profiles . data . slice ( ofs ) ) ,
149
+ typeRegex . sampleType + ':' + typeRegex . sampleUnit )
150
+ const mergeTreeLat = ( process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 ) ) - start
151
+ log . debug ( `merge_pprof: ${ mergePprofLat / BigInt ( 1000000 ) } ms` )
152
+ log . debug ( `merge_tree: ${ mergeTreeLat / BigInt ( 1000000 ) } ms` )
153
+ }
154
+
155
+ const mergeStackTraces = async ( typeRegex , sel , fromTimeSec , toTimeSec , log ) => {
156
+ const _ctxIdx = newCtxIdx ( )
157
+ try {
158
+ await importStackTraces ( typeRegex , sel , fromTimeSec , toTimeSec , log , _ctxIdx )
159
+ const start = process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 )
150
160
const resp = pprofBin . export_tree ( _ctxIdx , typeRegex . sampleType + ':' + typeRegex . sampleUnit )
151
161
const exportTreeLat = ( process . hrtime ?. bigint ? process . hrtime . bigint ( ) : BigInt ( 0 ) ) - start
152
- log . debug ( `merge_pprof: ${ mergePprofLat / BigInt ( 1000000 ) } ms` )
153
- log . debug ( `merge_tree: ${ mergeTreeLat / BigInt ( 1000000 ) } ms` )
154
162
log . debug ( `export_tree: ${ exportTreeLat / BigInt ( 1000000 ) } ms` )
155
163
return Buffer . from ( resp )
156
164
} finally {
@@ -159,5 +167,7 @@ const mergeStackTraces = async (typeRegex, sel, fromTimeSec, toTimeSec, log) =>
159
167
}
160
168
161
169
module . exports = {
162
- mergeStackTraces
163
- }
170
+ mergeStackTraces,
171
+ importStackTraces,
172
+ newCtxIdx
173
+ }
0 commit comments