Skip to content

Commit

Permalink
Merge pull request #454 from metrico/fix/451_452_prof_bugs
Browse files Browse the repository at this point in the history
fix: #451, #452
  • Loading branch information
akvlad authored Feb 2, 2024
2 parents deff5ae + 06fd760 commit 240d403
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pyroscope/pprof-bin/pkg/pprof_bin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
export function merge_tree(id: number, bytes: Uint8Array, sample_type: string): void;
/**
* @param {number} id
* @param {string} sample_type
* @returns {Uint8Array}
*/
export function export_tree(id: number): Uint8Array;
export function export_tree(id: number, sample_type: string): Uint8Array;
/**
* @param {number} id
*/
Expand Down
11 changes: 7 additions & 4 deletions pyroscope/pprof-bin/pkg/pprof_bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,20 @@ function getArrayU8FromWasm0(ptr, len) {
}
/**
* @param {number} id
* @param {string} sample_type
* @returns {Uint8Array}
*/
module.exports.export_tree = function(id) {
module.exports.export_tree = function(id, sample_type) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.export_tree(retptr, id);
const ptr0 = passStringToWasm0(sample_type, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.export_tree(retptr, id, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1, 1);
return v1;
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
Expand Down
Binary file modified pyroscope/pprof-bin/pkg/pprof_bin_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion pyroscope/pprof-bin/pkg/pprof_bin_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function merge_tree(a: number, b: number, c: number, d: number, e: number): void;
export function export_tree(a: number, b: number): void;
export function export_tree(a: number, b: number, c: number, d: number): void;
export function drop_tree(a: number): void;
export function init_panic_hook(): void;
export function __wbindgen_malloc(a: number, b: number): number;
Expand Down
22 changes: 18 additions & 4 deletions pyroscope/pprof-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@ pub unsafe fn merge_tree(id: u32, bytes: &[u8], sample_type: String) {
}

#[wasm_bindgen]
pub unsafe fn export_tree(id: u32) -> Vec<u8> {
pub unsafe fn export_tree(id: u32, sample_type: String) -> Vec<u8> {
let mut ctx = CTX.lock().unwrap();
let mut res = SelectMergeStacktracesResponse::default();
if !ctx.contains_key(&id) {
return res.encode_to_vec();
let mut tree = &mut Tree {
names: Vec::new(),
names_map: HashMap::new(),
root: TreeNode {
name_idx: 0,
_self: 0,
children: vec![],
prepend: 0,
total: 0,
},
sample_type,
max_self: 0,
};
tree.names.push("total".to_string());
tree.names_map.insert("total".to_string(), 0);
if ctx.contains_key(&id) {
tree = (*ctx).get_mut(&id).unwrap();
}
let tree = (*ctx).get_mut(&id).unwrap();
let mut fg = FlameGraph::default();
fg.names = tree.names.clone();
let mut levels: Vec<&[i64]> = Vec::new();
Expand Down
7 changes: 4 additions & 3 deletions pyroscope/pyroscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ const labelSelectorQuery = (query, labelSelector) => {
labelsConds.push(labelSubCond)
}
query.where(Sql.Or(...labelsConds))
query.groupBy(new Sql.Raw('fingerprint'))
query.having(Sql.Eq(
new Sql.Raw(labelsConds.map((cond, i) => {
new Sql.Raw(`groupBitOr(${labelsConds.map((cond, i) => {
return `bitShiftLeft(toUInt64(${cond}), ${i})`
}).join('+')),
}).join('+')})`),
new Sql.Raw(`bitShiftLeft(toUInt64(1), ${labelsConds.length})-1`)
))
}
Expand Down Expand Up @@ -207,7 +208,7 @@ const selectMergeStacktraces = async (req, res) => {
let sResp = null
try {
await Promise.all(promises)
sResp = pprofBin.export_tree(_ctxIdx)
sResp = pprofBin.export_tree(_ctxIdx, `${typeRe[2]}:${typeRe[3]}`)
} finally {
try { pprofBin.drop_tree(_ctxIdx) } catch (e) { req.log.error(e) }
}
Expand Down

0 comments on commit 240d403

Please sign in to comment.