|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use prost::Message; |
| 4 | + |
| 5 | +use crate::backend::types::{EncodedReport, Report}; |
| 6 | +use crate::encode::profiles::{Function, Label, Line, Location, Profile, Sample, ValueType}; |
| 7 | + |
| 8 | + |
| 9 | +struct PProfBuilder { |
| 10 | + profile: Profile, |
| 11 | + strings: HashMap<String, i64>, |
| 12 | + functions: HashMap<FunctionMirror, u64>, |
| 13 | + locations: HashMap<LocationMirror, u64>, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Hash, PartialEq, Eq, Clone)] |
| 17 | +pub struct LocationMirror { |
| 18 | + pub function_id: u64, |
| 19 | + pub line: i64, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Hash, PartialEq, Eq, Clone)] |
| 23 | +pub struct FunctionMirror { |
| 24 | + pub name: i64, |
| 25 | + pub filename: i64, |
| 26 | +} |
| 27 | + |
| 28 | +impl PProfBuilder { |
| 29 | + fn add_string(&mut self, s: &String) -> i64 { |
| 30 | + let v = self.strings.get(s); |
| 31 | + if v.is_some() { |
| 32 | + return *v.unwrap(); |
| 33 | + } |
| 34 | + assert!(self.strings.len() != self.profile.string_table.len() + 1); |
| 35 | + let id: i64 = self.strings.len() as i64; |
| 36 | + self.strings.insert(s.to_owned(), id); |
| 37 | + self.profile.string_table.push(s.to_owned()); |
| 38 | + id |
| 39 | + } |
| 40 | + |
| 41 | + fn add_function(&mut self, fm: FunctionMirror) -> u64 { |
| 42 | + let v = self.functions.get(&fm); |
| 43 | + if v.is_some() { |
| 44 | + return *v.unwrap(); |
| 45 | + } |
| 46 | + assert!(self.functions.len() != self.profile.function.len() + 1); |
| 47 | + let id: u64 = self.functions.len() as u64 + 1; |
| 48 | + let f = Function { |
| 49 | + id: id, |
| 50 | + name: fm.name, |
| 51 | + system_name: 0, |
| 52 | + filename: fm.filename, |
| 53 | + start_line: 0, |
| 54 | + }; |
| 55 | + self.functions.insert(fm, id); |
| 56 | + self.profile.function.push(f); |
| 57 | + id |
| 58 | + } |
| 59 | + |
| 60 | + fn add_location(&mut self, lm: LocationMirror) -> u64 { |
| 61 | + let v = self.locations.get(&lm); |
| 62 | + if v.is_some() { |
| 63 | + return *v.unwrap(); |
| 64 | + } |
| 65 | + assert!(self.locations.len() != self.profile.location.len() + 1); |
| 66 | + let id: u64 = self.locations.len() as u64 + 1; |
| 67 | + let l = Location { |
| 68 | + id, |
| 69 | + mapping_id: 0, |
| 70 | + address: 0, |
| 71 | + line: vec![Line { |
| 72 | + function_id: lm.function_id, |
| 73 | + line: lm.line, |
| 74 | + }], |
| 75 | + is_folded: false, |
| 76 | + }; |
| 77 | + self.locations.insert(lm, id); |
| 78 | + self.profile.location.push(l); |
| 79 | + id |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +pub fn encode(reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, duration_nanos: u64) -> Vec<EncodedReport> { |
| 84 | + let mut b = PProfBuilder { |
| 85 | + strings: HashMap::new(), |
| 86 | + functions: HashMap::new(), |
| 87 | + locations: HashMap::new(), |
| 88 | + profile: Profile { |
| 89 | + sample_type: vec![], |
| 90 | + sample: vec![], |
| 91 | + mapping: vec![], |
| 92 | + location: vec![], |
| 93 | + function: vec![], |
| 94 | + string_table: vec![], |
| 95 | + drop_frames: 0, |
| 96 | + keep_frames: 0, |
| 97 | + time_nanos: start_time_nanos as i64, |
| 98 | + duration_nanos: duration_nanos as i64, |
| 99 | + period_type: None, |
| 100 | + period: 0, |
| 101 | + comment: vec![], |
| 102 | + default_sample_type: 0, |
| 103 | + }, |
| 104 | + }; |
| 105 | + { |
| 106 | + let count = b.add_string(&"count".to_string()); |
| 107 | + let samples = b.add_string(&"samples".to_string()); |
| 108 | + let milliseconds = b.add_string(&"milliseconds".to_string()); |
| 109 | + b.profile.sample_type.push(ValueType { |
| 110 | + r#type: samples, |
| 111 | + unit: count, |
| 112 | + }); |
| 113 | + b.profile.period = 1_000 / sample_rate as i64; |
| 114 | + b.profile.period_type = Some(ValueType { |
| 115 | + r#type: 0, |
| 116 | + unit: milliseconds, |
| 117 | + }) |
| 118 | + } |
| 119 | + for report in reports { |
| 120 | + for (stacktrace, value) in &report.data { |
| 121 | + let mut sample = Sample { |
| 122 | + location_id: vec![], |
| 123 | + value: vec![*value as i64], |
| 124 | + label: vec![], |
| 125 | + }; |
| 126 | + for sf in &stacktrace.frames { |
| 127 | + let name = b.add_string(&sf.name.as_ref().unwrap_or(&"".to_string())); |
| 128 | + let filename = b.add_string(&sf.filename.as_ref().unwrap_or(&"".to_string())); |
| 129 | + let line = sf.line.unwrap_or(0) as i64; |
| 130 | + let function_id = b.add_function(FunctionMirror { |
| 131 | + name: name, |
| 132 | + filename: filename, |
| 133 | + }); |
| 134 | + let location_id = b.add_location(LocationMirror { |
| 135 | + function_id: function_id, |
| 136 | + line: line, |
| 137 | + }); |
| 138 | + sample.location_id.push(location_id as u64); |
| 139 | + } |
| 140 | + let mut labels = HashMap::new(); |
| 141 | + for l in &stacktrace.metadata.tags { |
| 142 | + let k = b.add_string(&l.key); |
| 143 | + let v = b.add_string(&l.value); |
| 144 | + labels.insert(k, v); |
| 145 | + } |
| 146 | + for l in &report.metadata.tags { |
| 147 | + let k = b.add_string(&l.key); |
| 148 | + let v = b.add_string(&l.value); |
| 149 | + labels.insert(k, v); |
| 150 | + } |
| 151 | + for (k, v) in &labels { |
| 152 | + sample.label.push(Label { |
| 153 | + key: *k, |
| 154 | + str: *v, |
| 155 | + num: 0, |
| 156 | + num_unit: 0, |
| 157 | + }) |
| 158 | + } |
| 159 | + b.profile.sample.push(sample); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + vec![EncodedReport { |
| 164 | + format: "pprof".to_string(), |
| 165 | + content_type: "binary/octet-stream".to_string(), |
| 166 | + content_encoding: "".to_string(), |
| 167 | + data: b.profile.encode_to_vec(), |
| 168 | + metadata: Default::default(), |
| 169 | + }] |
| 170 | +} |
0 commit comments