|
2 | 2 |
|
3 | 3 | use std::borrow::Cow; |
4 | 4 | use std::collections::HashMap; |
| 5 | +use std::collections::HashSet; |
5 | 6 | use std::io::Write; |
6 | 7 | use std::time::Instant; |
7 | 8 |
|
@@ -100,7 +101,7 @@ pub struct RenderContext<'a> { |
100 | 101 | pub unit_data: Vec<UnitData>, |
101 | 102 | /// Concurrency-tracking information. This is periodically updated while |
102 | 103 | /// compilation progresses. |
103 | | - pub concurrency: &'a [Concurrency], |
| 104 | + pub concurrency: Vec<Concurrency>, |
104 | 105 | /// Recorded CPU states, stored as tuples. First element is when the |
105 | 106 | /// recording was taken and second element is percentage usage of the |
106 | 107 | /// system. |
@@ -391,6 +392,131 @@ pub(super) fn to_unit_data( |
391 | 392 | .collect() |
392 | 393 | } |
393 | 394 |
|
| 395 | +/// Derives concurrency information from unit timing data. |
| 396 | +pub(super) fn compute_concurrency(unit_data: &[UnitData]) -> Vec<Concurrency> { |
| 397 | + if unit_data.is_empty() { |
| 398 | + return Vec::new(); |
| 399 | + } |
| 400 | + |
| 401 | + let unit_by_index: HashMap<_, _> = unit_data.iter().map(|u| (u.i, u)).collect(); |
| 402 | + |
| 403 | + enum UnblockedBy { |
| 404 | + Rmeta(u64), |
| 405 | + Full(u64), |
| 406 | + } |
| 407 | + |
| 408 | + // unit_id -> unit that unblocks it. |
| 409 | + let mut unblocked_by: HashMap<_, _> = HashMap::new(); |
| 410 | + for unit in unit_data { |
| 411 | + for id in unit.unblocked_rmeta_units.iter() { |
| 412 | + assert!( |
| 413 | + unblocked_by |
| 414 | + .insert(*id, UnblockedBy::Rmeta(unit.i)) |
| 415 | + .is_none() |
| 416 | + ); |
| 417 | + } |
| 418 | + |
| 419 | + for id in unit.unblocked_units.iter() { |
| 420 | + assert!( |
| 421 | + unblocked_by |
| 422 | + .insert(*id, UnblockedBy::Full(unit.i)) |
| 423 | + .is_none() |
| 424 | + ); |
| 425 | + } |
| 426 | + } |
| 427 | + |
| 428 | + let ready_time = |unit: &UnitData| -> Option<f64> { |
| 429 | + let dep = unblocked_by.get(&unit.i)?; |
| 430 | + match dep { |
| 431 | + UnblockedBy::Rmeta(id) => { |
| 432 | + let dep = unit_by_index.get(id)?; |
| 433 | + let duration = dep.sections.iter().flatten().find_map(|(name, section)| { |
| 434 | + matches!(name, SectionName::Frontend).then_some(section.end) |
| 435 | + }); |
| 436 | + |
| 437 | + Some(dep.start + duration.unwrap_or(dep.duration)) |
| 438 | + } |
| 439 | + UnblockedBy::Full(id) => { |
| 440 | + let dep = unit_by_index.get(id)?; |
| 441 | + Some(dep.start + dep.duration) |
| 442 | + } |
| 443 | + } |
| 444 | + }; |
| 445 | + |
| 446 | + #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] |
| 447 | + enum State { |
| 448 | + Ready, |
| 449 | + Start, |
| 450 | + End, |
| 451 | + } |
| 452 | + |
| 453 | + let mut events: Vec<_> = unit_data |
| 454 | + .iter() |
| 455 | + .flat_map(|unit| { |
| 456 | + // Adding rounded numbers may cause ready > start, |
| 457 | + // so cap with unit.start here to be defensive. |
| 458 | + let ready = ready_time(unit).unwrap_or(unit.start).min(unit.start); |
| 459 | + |
| 460 | + [ |
| 461 | + (ready, State::Ready, unit.i), |
| 462 | + (unit.start, State::Start, unit.i), |
| 463 | + (unit.start + unit.duration, State::End, unit.i), |
| 464 | + ] |
| 465 | + }) |
| 466 | + .collect(); |
| 467 | + |
| 468 | + events.sort_by(|a, b| { |
| 469 | + a.0.partial_cmp(&b.0) |
| 470 | + .unwrap() |
| 471 | + .then_with(|| a.1.cmp(&b.1)) |
| 472 | + .then_with(|| a.2.cmp(&b.2)) |
| 473 | + }); |
| 474 | + |
| 475 | + let mut concurrency: Vec<Concurrency> = Vec::new(); |
| 476 | + let mut inactive: HashSet<u64> = unit_data.iter().map(|unit| unit.i).collect(); |
| 477 | + let mut waiting: HashSet<u64> = HashSet::new(); |
| 478 | + let mut active: HashSet<u64> = HashSet::new(); |
| 479 | + |
| 480 | + for (t, state, unit_id) in events { |
| 481 | + match state { |
| 482 | + State::Ready => { |
| 483 | + inactive.remove(&unit_id); |
| 484 | + waiting.insert(unit_id); |
| 485 | + active.remove(&unit_id); |
| 486 | + } |
| 487 | + State::Start => { |
| 488 | + inactive.remove(&unit_id); |
| 489 | + waiting.remove(&unit_id); |
| 490 | + active.insert(unit_id); |
| 491 | + } |
| 492 | + State::End => { |
| 493 | + inactive.remove(&unit_id); |
| 494 | + waiting.remove(&unit_id); |
| 495 | + active.remove(&unit_id); |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + let record = Concurrency { |
| 500 | + t, |
| 501 | + active: active.len(), |
| 502 | + waiting: waiting.len(), |
| 503 | + inactive: inactive.len(), |
| 504 | + }; |
| 505 | + |
| 506 | + if let Some(last) = concurrency.last_mut() |
| 507 | + && last.t == t |
| 508 | + { |
| 509 | + // We don't want to draw long vertical lines at the same timestamp, |
| 510 | + // so we keep only the latest state. |
| 511 | + *last = record; |
| 512 | + } else { |
| 513 | + concurrency.push(record); |
| 514 | + } |
| 515 | + } |
| 516 | + |
| 517 | + concurrency |
| 518 | +} |
| 519 | + |
394 | 520 | /// Aggregates section timing information from individual compilation sections. |
395 | 521 | /// |
396 | 522 | /// We can have a bunch of situations here. |
|
0 commit comments