-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathandroid.rs
253 lines (218 loc) · 8 KB
/
android.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use std::collections::{HashMap, HashSet};
use android_trace_log::chrono::{DateTime, Utc};
use android_trace_log::{AndroidTraceLog, Clock, Time, Vm};
use serde::{Deserialize, Serialize};
use relay_general::protocol::EventId;
use crate::measurements::Measurement;
use crate::transaction_metadata::TransactionMetadata;
use crate::utils::{deserialize_number_from_string, is_zero};
use crate::ProfileError;
#[derive(Debug, Serialize, Deserialize, Clone)]
struct AndroidProfile {
android_api_level: u16,
#[serde(default, skip_serializing_if = "String::is_empty")]
build_id: String,
device_cpu_frequencies: Vec<u32>,
device_is_emulator: bool,
device_locale: String,
device_manufacturer: String,
device_model: String,
device_os_name: String,
device_os_version: String,
#[serde(deserialize_with = "deserialize_number_from_string")]
device_physical_memory_bytes: u64,
#[serde(default, skip_serializing_if = "String::is_empty")]
environment: String,
platform: String,
profile_id: EventId,
version_code: String,
version_name: String,
#[serde(default, skip_serializing_if = "EventId::is_nil")]
transaction_id: EventId,
#[serde(default, skip_serializing_if = "String::is_empty")]
transaction_name: String,
#[serde(default, skip_serializing_if = "EventId::is_nil")]
trace_id: EventId,
#[serde(
default,
deserialize_with = "deserialize_number_from_string",
skip_serializing_if = "is_zero"
)]
duration_ns: u64,
#[serde(
default,
deserialize_with = "deserialize_number_from_string",
skip_serializing_if = "is_zero"
)]
active_thread_id: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
transactions: Vec<TransactionMetadata>,
#[serde(skip_serializing_if = "Option::is_none")]
transaction: Option<TransactionMetadata>,
#[serde(default, skip_serializing)]
sampled_profile: String,
#[serde(default = "AndroidProfile::default")]
profile: AndroidTraceLog,
#[serde(default, skip_serializing_if = "Option::is_none")]
measurements: Option<HashMap<String, Measurement>>,
}
impl AndroidProfile {
fn default() -> AndroidTraceLog {
AndroidTraceLog {
data_file_overflow: Default::default(),
clock: Clock::Global,
elapsed_time: Default::default(),
total_method_calls: Default::default(),
clock_call_overhead: Default::default(),
vm: Vm::Dalvik,
start_time: Utc::now(),
pid: Default::default(),
gc_trace: Default::default(),
threads: Default::default(),
methods: Default::default(),
events: Default::default(),
}
}
fn parse(&mut self) -> Result<(), ProfileError> {
let profile_bytes = match base64::decode(&self.sampled_profile) {
Ok(profile) => profile,
Err(_) => return Err(ProfileError::InvalidBase64Value),
};
self.profile = match android_trace_log::parse(&profile_bytes) {
Ok(profile) => profile,
Err(_) => return Err(ProfileError::InvalidSampledProfile),
};
Ok(())
}
fn has_transaction_metadata(&self) -> bool {
!self.transaction_name.is_empty() && self.duration_ns > 0
}
/// Removes an event with a duration of 0
fn remove_events_with_no_duration(&mut self) {
let android_trace = &mut self.profile;
let events = &mut android_trace.events;
let clock = android_trace.clock;
let start_time = android_trace.start_time;
let mut timestamps_per_thread_id: HashMap<u16, HashSet<u64>> = HashMap::new();
for event in events.iter() {
let event_time = get_timestamp(clock, start_time, event.time);
timestamps_per_thread_id
.entry(event.thread_id)
.or_default()
.insert(event_time);
}
timestamps_per_thread_id.retain(|_, timestamps| timestamps.len() > 1);
events.retain(|event| timestamps_per_thread_id.contains_key(&event.thread_id));
}
}
fn parse_profile(payload: &[u8]) -> Result<AndroidProfile, ProfileError> {
let mut profile: AndroidProfile =
serde_json::from_slice(payload).map_err(ProfileError::InvalidJson)?;
let transaction_opt = profile.transactions.drain(..).next();
if let Some(transaction) = transaction_opt {
if !transaction.valid() {
return Err(ProfileError::InvalidTransactionMetadata);
}
// this is for compatibility
profile.active_thread_id = transaction.active_thread_id;
profile.duration_ns = transaction.duration_ns();
profile.trace_id = transaction.trace_id;
profile.transaction_id = transaction.id;
profile.transaction_name = transaction.name.clone();
profile.transaction = Some(transaction);
} else if !profile.has_transaction_metadata() {
return Err(ProfileError::NoTransactionAssociated);
}
if !profile.sampled_profile.is_empty() {
profile.parse()?;
profile.remove_events_with_no_duration();
}
if profile.profile.events.is_empty() {
return Err(ProfileError::NotEnoughSamples);
}
Ok(profile)
}
pub fn parse_android_profile(payload: &[u8]) -> Result<Vec<u8>, ProfileError> {
let profile = parse_profile(payload)?;
serde_json::to_vec(&profile).map_err(|_| ProfileError::CannotSerializePayload)
}
fn get_timestamp(clock: Clock, start_time: DateTime<Utc>, event_time: Time) -> u64 {
match (clock, event_time) {
(Clock::Global, Time::Global(time)) => {
let time_ns = time.as_nanos() as u64;
let start_time_ns = start_time.timestamp_nanos() as u64;
if time_ns >= start_time_ns {
time_ns - start_time_ns
} else {
0
}
}
(
Clock::Cpu,
Time::Monotonic {
cpu: Some(cpu),
wall: None,
},
) => cpu.as_nanos() as u64,
(
Clock::Wall,
Time::Monotonic {
cpu: None,
wall: Some(wall),
},
) => wall.as_nanos() as u64,
(
Clock::Dual,
Time::Monotonic {
cpu: Some(_),
wall: Some(wall),
},
) => wall.as_nanos() as u64,
_ => unimplemented!(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_roundtrip_android() {
let payload = include_bytes!("../tests/fixtures/profiles/android/roundtrip.json");
let profile = parse_profile(payload);
assert!(profile.is_ok());
let data = serde_json::to_vec(&profile.unwrap());
assert!(parse_android_profile(&(data.unwrap())[..]).is_ok());
}
#[test]
fn test_no_transaction() {
let payload = include_bytes!("../tests/fixtures/profiles/android/no_transaction.json");
let data = parse_android_profile(payload);
assert!(data.is_err());
}
#[test]
fn test_remove_invalid_events() {
let payload =
include_bytes!("../tests/fixtures/profiles/android/remove_invalid_events.json");
let data = parse_android_profile(payload);
assert!(data.is_err());
}
#[test]
fn test_transactions_to_top_level() {
let payload =
include_bytes!("../tests/fixtures/profiles/android/multiple_transactions.json");
let profile = match parse_profile(payload) {
Err(err) => panic!("cannot parse profile: {:?}", err),
Ok(profile) => profile,
};
assert_eq!(
profile.transaction_id,
"7d6db784355e4d7dbf98671c69cbcc77".parse().unwrap()
);
assert_eq!(
profile.trace_id,
"7d6db784355e4d7dbf98671c69cbcc77".parse().unwrap()
);
assert_eq!(profile.active_thread_id, 12345);
assert_eq!(profile.transaction_name, "transaction1");
assert_eq!(profile.duration_ns, 1000000000);
}
}