Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert(profiling): Remove concurrent profiling #1697

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Internal**:

- Remove concurrent profiling. ([#1697](https://github.com/getsentry/relay/pull/1697))

## 22.12.0

**Features**:
Expand Down
223 changes: 45 additions & 178 deletions relay-profiling/src/android.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::time::Duration;

use android_trace_log::chrono::{DateTime, Utc};
use android_trace_log::{AndroidTraceLog, Clock, Time, Vm};
Expand Down Expand Up @@ -50,9 +49,17 @@ struct AndroidProfile {
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,
Expand Down Expand Up @@ -94,13 +101,6 @@ impl AndroidProfile {
Ok(())
}

fn set_transaction(&mut self, transaction: &TransactionMetadata) {
self.transaction_name = transaction.name.clone();
self.transaction_id = transaction.id;
self.trace_id = transaction.trace_id;
self.duration_ns = transaction.duration_ns();
}

fn has_transaction_metadata(&self) -> bool {
!self.transaction_name.is_empty() && self.duration_ns > 0
}
Expand All @@ -126,98 +126,43 @@ impl AndroidProfile {
}
}

pub fn expand_android_profile(payload: &[u8]) -> Result<Vec<Vec<u8>>, ProfileError> {
let profile = parse_android_profile(payload)?;
let mut items: Vec<Vec<u8>> = Vec::new();
fn parse_profile(payload: &[u8]) -> Result<AndroidProfile, ProfileError> {
let mut profile: AndroidProfile =
serde_json::from_slice(payload).map_err(ProfileError::InvalidJson)?;

if profile.transactions.is_empty() && profile.has_transaction_metadata() {
match serde_json::to_vec(&profile) {
Ok(payload) => items.push(payload),
Err(_) => {
return Err(ProfileError::CannotSerializePayload);
}
};
let transaction_opt = profile.transactions.drain(..).next();
if let Some(transaction) = transaction_opt {
if !transaction.valid() {
return Err(ProfileError::InvalidTransactionMetadata);
}

return Ok(items);
}
// 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();

for transaction in &profile.transactions {
let mut new_profile = profile.clone();

new_profile.profile_id = EventId::new();
new_profile.set_transaction(transaction);
new_profile.transactions.clear();

let clock = new_profile.profile.clock;
let start_time = new_profile.profile.start_time;

new_profile.profile.events.retain_mut(|event| {
let event_timestamp = get_timestamp(clock, start_time, event.time);
if transaction.relative_start_ns <= event_timestamp
&& event_timestamp <= transaction.relative_end_ns
{
let relative_start = match clock {
Clock::Cpu => Duration::from_millis(transaction.relative_cpu_start_ms),
Clock::Wall | Clock::Dual | Clock::Global => {
Duration::from_nanos(transaction.relative_start_ns)
}
};
event.time = substract_to_timestamp(event.time, relative_start);
true
} else {
false
}
});
profile.transaction = Some(transaction);
} else if !profile.has_transaction_metadata() {
return Err(ProfileError::NoTransactionAssociated);
}

if new_profile.profile.events.is_empty() {
continue;
}
if !profile.sampled_profile.is_empty() {
profile.parse()?;
profile.remove_events_with_no_duration();
}

match serde_json::to_vec(&new_profile) {
Ok(payload) => items.push(payload),
Err(_) => {
return Err(ProfileError::CannotSerializePayload);
}
};
if profile.profile.events.is_empty() {
return Err(ProfileError::NotEnoughSamples);
}

Ok(items)
Ok(profile)
}

fn substract_to_timestamp(time: Time, duration: Duration) -> Time {
match time {
Time::Global(time) => Time::Global(time - duration),
Time::Monotonic {
cpu: Some(time),
wall: None,
} => Time::Monotonic {
cpu: time
.checked_sub(duration)
.or_else(|| Some(Duration::default())),
wall: None,
},
Time::Monotonic {
wall: Some(time),
cpu: None,
} => Time::Monotonic {
wall: time
.checked_sub(duration)
.or_else(|| Some(Duration::default())),
cpu: None,
},
Time::Monotonic {
cpu: Some(cpu),
wall: Some(wall),
} => Time::Monotonic {
cpu: cpu
.checked_sub(duration)
.or_else(|| Some(Duration::default())),
wall: wall
.checked_sub(duration)
.or_else(|| Some(Duration::default())),
},
_ => unimplemented!(),
}
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 {
Expand Down Expand Up @@ -256,53 +201,19 @@ fn get_timestamp(clock: Clock, start_time: DateTime<Utc>, event_time: Time) -> u
}
}

fn parse_android_profile(payload: &[u8]) -> Result<AndroidProfile, ProfileError> {
let mut profile: AndroidProfile =
serde_json::from_slice(payload).map_err(ProfileError::InvalidJson)?;

if profile.transactions.is_empty() && !profile.has_transaction_metadata() {
return Err(ProfileError::NoTransactionAssociated);
}

for transaction in &profile.transactions {
if !transaction.valid() {
return Err(ProfileError::InvalidTransactionMetadata);
}
}

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)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_roundtrip_android() {
let payload = include_bytes!("../tests/fixtures/profiles/android/roundtrip.json");
let profile = parse_android_profile(payload);
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_multiple_transactions() {
let payload =
include_bytes!("../tests/fixtures/profiles/android/multiple_transactions.json");
let data = parse_android_profile(payload);
assert!(data.is_ok());
}

#[test]
fn test_no_transaction() {
let payload = include_bytes!("../tests/fixtures/profiles/android/no_transaction.json");
Expand All @@ -319,68 +230,24 @@ mod tests {
}

#[test]
fn test_expand_multiple_transactions() {
fn test_transactions_to_top_level() {
let payload =
include_bytes!("../tests/fixtures/profiles/android/multiple_transactions.json");
let data = expand_android_profile(payload);
assert!(data.is_ok());
assert_eq!(data.as_ref().unwrap().len(), 3);

let profile = match parse_android_profile(&data.as_ref().unwrap()[0][..]) {
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);
assert_eq!(profile.profile.events.len(), 8453);

for event in &profile.profile.events {
assert!(
get_timestamp(
profile.profile.clock,
profile.profile.start_time,
event.time
) < profile.duration_ns
);
}
}

#[test]
fn test_substract_to_timestamp() {
let now = Utc::now();
let timestamps: Vec<(Clock, Time)> = vec![
(
Clock::Global,
Time::Global(Duration::from_nanos(now.timestamp_nanos() as u64 + 100)),
),
(
Clock::Cpu,
Time::Monotonic {
cpu: Some(Duration::from_nanos(100)),
wall: None,
},
),
(
Clock::Wall,
Time::Monotonic {
wall: Some(Duration::from_nanos(100)),
cpu: None,
},
),
(
Clock::Dual,
Time::Monotonic {
cpu: Some(Duration::from_nanos(20)),
wall: Some(Duration::from_nanos(100)),
},
),
];
for (clock, timestamp) in timestamps {
let result = substract_to_timestamp(timestamp, Duration::from_nanos(50));
assert_eq!(get_timestamp(clock, now, result), 50);
}
}
}
Loading