Skip to content

Commit f30133a

Browse files
WIP feedback
1 parent 894d036 commit f30133a

3 files changed

Lines changed: 79 additions & 106 deletions

File tree

lock-analyzer/src/main.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
//! See `wgpu_core/src/lock/observing.rs` for a general explanation of
1414
//! this analysis.
1515
16+
#[path = "../../wgpu-core/src/lock/observing/action.rs"]
17+
mod action;
18+
1619
use std::sync::Arc;
1720
use std::{
1821
collections::{btree_map::Entry, BTreeMap, BTreeSet, HashMap},
1922
fmt,
2023
path::PathBuf,
2124
};
2225

26+
use action::Action;
2327
use anyhow::{Context, Result};
2428

2529
fn main() -> Result<()> {
@@ -56,7 +60,7 @@ fn main() -> Result<()> {
5660
if line.is_empty() {
5761
continue;
5862
}
59-
let action = ron::de::from_bytes::<Action>(line)
63+
let action = ron::de::from_bytes::<Action<LocationAddress>>(line)
6064
.with_context(|| format!("Error parsing action from {}", name.display()))?;
6165
match action {
6266
Action::Location {
@@ -67,7 +71,7 @@ fn main() -> Result<()> {
6771
} => {
6872
let file = match file.split_once("src/") {
6973
Some((_, after)) => after.to_string(),
70-
None => file,
74+
None => file.into_owned(),
7175
};
7276
assert!(locations
7377
.insert(address, Arc::new(Location { file, line, column }))
@@ -85,8 +89,8 @@ fn main() -> Result<()> {
8589
}
8690
Entry::Vacant(vacant) => {
8791
vacant.insert(Rank {
88-
member_name,
89-
const_name,
92+
member_name: member_name.into_owned(),
93+
const_name: const_name.into_owned(),
9094
acquisitions: BTreeMap::default(),
9195
});
9296
}
@@ -181,42 +185,6 @@ fn main() -> Result<()> {
181185
Ok(())
182186
}
183187

184-
#[derive(Debug, serde::Deserialize)]
185-
#[serde(deny_unknown_fields)]
186-
enum Action {
187-
/// A location that we will refer to in later actions.
188-
Location {
189-
address: LocationAddress,
190-
file: String,
191-
line: u32,
192-
column: u32,
193-
},
194-
195-
/// A lock rank that we will refer to in later actions.
196-
Rank {
197-
bit: u32,
198-
member_name: String,
199-
const_name: String,
200-
},
201-
202-
/// An attempt to acquire a lock while holding another lock.
203-
Acquisition {
204-
/// The number of the already acquired lock's rank.
205-
older_rank: u32,
206-
207-
/// The source position at which we acquired it. Specifically,
208-
/// its `Location`'s address, as an integer.
209-
older_location: LocationAddress,
210-
211-
/// The number of the rank of the lock we are acquiring.
212-
newer_rank: u32,
213-
214-
/// The source position at which we are acquiring it.
215-
/// Specifically, its `Location`'s address, as an integer.
216-
newer_location: LocationAddress,
217-
},
218-
}
219-
220188
/// The memory address at which the `Location` was stored in the
221189
/// observed process.
222190
///

wgpu-core/src/lock/observing.rs

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ use std::{
3939
vec::Vec,
4040
};
4141

42+
use self::action::Action;
4243
use super::rank::{LockRank, LockRankSet};
4344
use crate::FastHashSet;
4445

46+
mod action;
47+
4548
/// A `Mutex` instrumented for lock acquisition order observation.
4649
///
4750
/// This is just a wrapper around a [`parking_lot::Mutex`], along with
@@ -383,22 +386,22 @@ impl ObservationLog {
383386
if self.locations_seen.insert(location) {
384387
self.write_action(&Action::Location {
385388
address: addr(location),
386-
file: location.file(),
387-
line: location.line(),
388-
column: location.column(),
389+
file: location.file().into(),
390+
line: location.line().into(),
391+
column: location.column().into(),
389392
});
390393
}
391394
}
392395

393396
fn write_rank(&mut self, rank: LockRankSet) {
394397
self.write_action(&Action::Rank {
395398
bit: rank.number(),
396-
member_name: rank.member_name(),
397-
const_name: rank.const_name(),
399+
member_name: rank.member_name().into(),
400+
const_name: rank.const_name().into(),
398401
});
399402
}
400403

401-
fn write_action(&mut self, action: &Action) {
404+
fn write_action(&mut self, action: &Action<usize>) {
402405
use std::io::Write;
403406

404407
self.buffer.clear();
@@ -411,66 +414,6 @@ impl ObservationLog {
411414
}
412415
}
413416

414-
/// An action logged by a thread that is observing lock acquisition order.
415-
///
416-
/// Each thread's log file is a sequence of these enums, serialized
417-
/// using the [`ron`] crate, one action per line.
418-
///
419-
/// Lock observation cannot assume that there will be any convenient
420-
/// finalization point before the program exits, so in practice,
421-
/// actions must be written immediately when they occur. This means we
422-
/// can't, say, accumulate tables and write them out when they're
423-
/// complete. The `lock-analyzer` binary is then responsible for
424-
/// consolidating the data into a single table of observed transitions.
425-
#[derive(serde::Serialize)]
426-
enum Action {
427-
/// A location that we will refer to in later actions.
428-
///
429-
/// We write one of these events the first time we see a
430-
/// particular `Location`. Treating this as a separate action
431-
/// simply lets us avoid repeating the content over and over
432-
/// again in every [`Acquisition`] action.
433-
///
434-
/// [`Acquisition`]: Action::Acquisition
435-
Location {
436-
address: usize,
437-
file: &'static str,
438-
line: u32,
439-
column: u32,
440-
},
441-
442-
/// A lock rank that we will refer to in later actions.
443-
///
444-
/// We write out one these events for every lock rank at the
445-
/// beginning of each thread's log file. Treating this as a
446-
/// separate action simply lets us avoid repeating the names over
447-
/// and over again in every [`Acquisition`] action.
448-
///
449-
/// [`Acquisition`]: Action::Acquisition
450-
Rank {
451-
bit: u32,
452-
member_name: &'static str,
453-
const_name: &'static str,
454-
},
455-
456-
/// An attempt to acquire a lock while holding another lock.
457-
Acquisition {
458-
/// The number of the already acquired lock's rank.
459-
older_rank: u32,
460-
461-
/// The source position at which we acquired it. Specifically,
462-
/// its `Location`'s address, as an integer.
463-
older_location: usize,
464-
465-
/// The number of the rank of the lock we are acquiring.
466-
newer_rank: u32,
467-
468-
/// The source position at which we are acquiring it.
469-
/// Specifically, its `Location`'s address, as an integer.
470-
newer_location: usize,
471-
},
472-
}
473-
474417
impl LockRankSet {
475418
/// Return the number of this rank's first member.
476419
fn number(self) -> u32 {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::borrow::Cow;
2+
3+
/// An action logged by a thread that is observing lock acquisition order.
4+
///
5+
/// Each thread's log file is a sequence of these enums, serialized
6+
/// using the [`ron`] crate, one action per line.
7+
///
8+
/// Lock observation cannot assume that there will be any convenient
9+
/// finalization point before the program exits, so in practice,
10+
/// actions must be written immediately when they occur. This means we
11+
/// can't, say, accumulate tables and write them out when they're
12+
/// complete. The `lock-analyzer` binary is then responsible for
13+
/// consolidating the data into a single table of observed transitions.
14+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
15+
#[serde(deny_unknown_fields)]
16+
pub(super) enum Action<LocationAddress> {
17+
/// A location that we will refer to in later actions.
18+
///
19+
/// We write one of these events the first time we see a
20+
/// particular `Location`. Treating this as a separate action
21+
/// simply lets us avoid repeating the content over and over
22+
/// again in every [`Acquisition`] action.
23+
///
24+
/// [`Acquisition`]: Action::Acquisition
25+
Location {
26+
address: LocationAddress,
27+
file: Cow<'static, str>,
28+
line: u32,
29+
column: u32,
30+
},
31+
32+
/// A lock rank that we will refer to in later actions.
33+
///
34+
/// We write out one these events for every lock rank at the
35+
/// beginning of each thread's log file. Treating this as a
36+
/// separate action simply lets us avoid repeating the names over
37+
/// and over again in every [`Acquisition`] action.
38+
///
39+
/// [`Acquisition`]: Action::Acquisition
40+
Rank {
41+
bit: u32,
42+
member_name: Cow<'static, str>,
43+
const_name: Cow<'static, str>,
44+
},
45+
46+
/// An attempt to acquire a lock while holding another lock.
47+
Acquisition {
48+
/// The number of the already acquired lock's rank.
49+
older_rank: u32,
50+
51+
/// The source position at which we acquired it. Specifically,
52+
/// its `Location`'s address, as an integer.
53+
older_location: LocationAddress,
54+
55+
/// The number of the rank of the lock we are acquiring.
56+
newer_rank: u32,
57+
58+
/// The source position at which we are acquiring it.
59+
/// Specifically, its `Location`'s address, as an integer.
60+
newer_location: LocationAddress,
61+
},
62+
}

0 commit comments

Comments
 (0)