Skip to content

Implement better error handling #18

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

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 6 additions & 7 deletions measureme/src/file_serialization_sink.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::serialization::{Addr, SerializationSink};
use std::error::Error;
use std::fs;
use std::io::{BufWriter, Write};
use std::path::Path;
Expand All @@ -9,16 +10,14 @@ pub struct FileSerializationSink {
}

impl SerializationSink for FileSerializationSink {
fn from_path(path: &Path) -> Self {
// TODO: This is very crude error handling.
// https://github.com/rust-lang/measureme/issues/9
fs::create_dir_all(path.parent().unwrap()).unwrap();
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
fs::create_dir_all(path.parent().unwrap())?;

let file = fs::File::create(path).expect("couldn't open file: {}");
let file = fs::File::create(path)?;

FileSerializationSink {
Ok(FileSerializationSink {
data: Mutex::new((BufWriter::new(file), 0)),
}
})
}

#[inline]
Expand Down
9 changes: 5 additions & 4 deletions measureme/src/mmap_serialization_sink.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::serialization::{Addr, SerializationSink};
use memmap::{MmapMut};
use std::error::Error;
use std::io::{Write, BufWriter};
use std::fs::{File};
use std::path::{Path, PathBuf};
Expand All @@ -12,17 +13,17 @@ pub struct MmapSerializationSink {
}

impl SerializationSink for MmapSerializationSink {
fn from_path(path: &Path) -> Self {
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>> {
// Lazily allocate 1 GB :O
let file_size = 1 << 30;

let mapped_file = MmapMut::map_anon(file_size).unwrap();
let mapped_file = MmapMut::map_anon(file_size)?;

MmapSerializationSink {
Ok(MmapSerializationSink {
mapped_file,
current_pos: AtomicUsize::new(0),
path: path.to_path_buf(),
}
})
}

#[inline]
Expand Down
13 changes: 7 additions & 6 deletions measureme/src/profiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::raw_event::{RawEvent, Timestamp, TimestampKind};
use crate::serialization::SerializationSink;
use crate::stringtable::{SerializableString, StringId, StringTableBuilder};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;
Expand Down Expand Up @@ -28,19 +29,19 @@ pub struct Profiler<S: SerializationSink> {
}

impl<S: SerializationSink> Profiler<S> {
pub fn new(path_stem: &Path) -> Profiler<S> {
pub fn new(path_stem: &Path) -> Result<Profiler<S>, Box<dyn Error>> {
let paths = ProfilerFiles::new(path_stem);
let event_sink = Arc::new(S::from_path(&paths.events_file));
let event_sink = Arc::new(S::from_path(&paths.events_file)?);
let string_table = StringTableBuilder::new(
Arc::new(S::from_path(&paths.string_data_file)),
Arc::new(S::from_path(&paths.string_index_file)),
Arc::new(S::from_path(&paths.string_data_file)?),
Arc::new(S::from_path(&paths.string_index_file)?),
);

Profiler {
Ok(Profiler {
event_sink,
string_table,
start_time: Instant::now(),
}
})
}

#[inline(always)]
Expand Down
7 changes: 4 additions & 3 deletions measureme/src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::error::Error;
use std::path::Path;

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
Expand All @@ -9,8 +10,8 @@ impl Addr {
}
}

pub trait SerializationSink {
fn from_path(path: &Path) -> Self;
pub trait SerializationSink: Sized {
fn from_path(path: &Path) -> Result<Self, Box<dyn Error>>;

fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
where
Expand Down Expand Up @@ -39,7 +40,7 @@ pub mod test {
}

impl SerializationSink for TestSink {
fn from_path(_path: &Path) -> Self {
fn from_path(_path: &Path) -> Result<Self, Box<dyn Error>> {
unimplemented!()
}

Expand Down
2 changes: 1 addition & 1 deletion measureme/src/testing_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn mk_filestem(file_name_stem: &str) -> PathBuf {

// Generate some profiling data. This is the part that would run in rustc.
fn generate_profiling_data<S: SerializationSink>(filestem: &Path) -> Vec<Event> {
let profiler = Arc::new(Profiler::<S>::new(Path::new(filestem)));
let profiler = Arc::new(Profiler::<S>::new(Path::new(filestem)).unwrap());

let event_id_reserved = StringId::reserved(42);

Expand Down