Skip to content

Commit

Permalink
Formatting code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gz committed May 2, 2020
1 parent 86a5a69 commit 5f060f5
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 268 deletions.
11 changes: 7 additions & 4 deletions examples/context_switches.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate perfcnt;
extern crate x86;

use perfcnt::{PerfCounter, AbstractPerfCounter};
use perfcnt::linux::SoftwareEventType as Software;
use perfcnt::linux::PerfCounterBuilderLinux as Builder;
use perfcnt::linux::SoftwareEventType as Software;
use perfcnt::{AbstractPerfCounter, PerfCounter};

pub fn main() {
let mut pc: PerfCounter = Builder::from_software_event(Software::ContextSwitches)
Expand All @@ -13,9 +13,12 @@ pub fn main() {
.expect("Could not create counter");

pc.start().expect("Can not start the counter");
std::thread::sleep(std::time::Duration::new(1,0));
std::thread::sleep(std::time::Duration::new(1, 0));
pc.stop().expect("Can not stop the counter");

println!("Context Switches/s: {:?}", pc.read().expect("Can not read counter"));
println!(
"Context Switches/s: {:?}",
pc.read().expect("Can not read counter")
);
pc.reset().expect("Can not reset the counter");
}
28 changes: 18 additions & 10 deletions examples/create_raw.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
extern crate perfcnt;
extern crate x86;

use perfcnt::{PerfCounter, AbstractPerfCounter};
use perfcnt::linux::{PerfCounterBuilderLinux};
use perfcnt::linux::PerfCounterBuilderLinux;
use perfcnt::{AbstractPerfCounter, PerfCounter};

pub fn main() {
let counter_description = x86::perfcnt::intel::events().unwrap().get("BR_INST_RETIRED.ALL_BRANCHES").unwrap();
let mut pc: PerfCounter = PerfCounterBuilderLinux::from_intel_event_description(counter_description)
.exclude_idle()
.exclude_kernel()
.finish()
.expect("Could not create counter");
let counter_description = x86::perfcnt::intel::events()
.unwrap()
.get("BR_INST_RETIRED.ALL_BRANCHES")
.unwrap();
let mut pc: PerfCounter =
PerfCounterBuilderLinux::from_intel_event_description(counter_description)
.exclude_idle()
.exclude_kernel()
.finish()
.expect("Could not create counter");

pc.start().expect("Can not start the counter");
println!("");
pc.stop().expect("Can not stop the counter");;
pc.stop().expect("Can not stop the counter");

println!("{}: {:?}", counter_description.brief_description, pc.read().expect("Can not read counter"));
println!(
"{}: {:?}",
counter_description.brief_description,
pc.read().expect("Can not read counter")
);
pc.reset().expect("Can not reset the counter");
}
6 changes: 3 additions & 3 deletions src/bin/parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate perfcnt;

use std::io::prelude::*;
use std::fs::File;
use std::env;
use std::fs::File;
use std::io::prelude::*;

use perfcnt::linux::perf_file::PerfFile;

Expand All @@ -12,7 +12,7 @@ fn main() {
println!("----------------------------------------------------------");

let mut file = File::open(argument).expect("File does not exist");
let mut buf: Vec<u8> = Vec::with_capacity(2*4096*4096);
let mut buf: Vec<u8> = Vec::with_capacity(2 * 4096 * 4096);
match file.read_to_end(&mut buf) {
Ok(_) => {
let pf = PerfFile::new(buf);
Expand Down
46 changes: 34 additions & 12 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub mod perf_format;

use self::perf_format::{EventAttrFlags, ReadFormatFlags, SampleFormatFlags};

use x86::perfcnt::intel::{EventDescription, Tuple};
use crate::AbstractPerfCounter;
use x86::perfcnt::intel::{EventDescription, Tuple};

const IOCTL: usize = 16;
const PERF_EVENT_OPEN: usize = 298;
Expand Down Expand Up @@ -301,20 +301,26 @@ impl PerfCounterBuilderLinux {

/// The counter starts out disabled.
pub fn disable<'a>(&'a mut self) -> &'a mut PerfCounterBuilderLinux {
self.attrs.settings.insert(EventAttrFlags::EVENT_ATTR_DISABLED);
self.attrs
.settings
.insert(EventAttrFlags::EVENT_ATTR_DISABLED);
self
}

/// This counter should count events of child tasks as well as the task specified.
pub fn inherit<'a>(&'a mut self) -> &'a mut PerfCounterBuilderLinux {
self.attrs.settings.insert(EventAttrFlags::EVENT_ATTR_INHERIT);
self.attrs
.settings
.insert(EventAttrFlags::EVENT_ATTR_INHERIT);
self
}

/// The pinned bit specifies that the counter should always be on the CPU if at all possible.
/// It applies only to hardware counters and only to group leaders.
pub fn pinned<'a>(&'a mut self) -> &'a mut PerfCounterBuilderLinux {
self.attrs.settings.insert(EventAttrFlags::EVENT_ATTR_PINNED);
self.attrs
.settings
.insert(EventAttrFlags::EVENT_ATTR_PINNED);
self
}

Expand Down Expand Up @@ -472,27 +478,37 @@ impl PerfCounterBuilderLinux {
}

pub fn enable_sampling_ip<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_IP);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_IP);
self
}

pub fn enable_sampling_tid<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_TID);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_TID);
self
}

pub fn enable_sampling_time<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_TIME);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_TIME);
self
}

pub fn enable_sampling_addr<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_ADDR);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_ADDR);
self
}

pub fn enable_sampling_read<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_READ);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_READ);
self
}

Expand All @@ -504,12 +520,16 @@ impl PerfCounterBuilderLinux {
}

pub fn enable_sampling_sample_id<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_ID);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_ID);
self
}

pub fn enable_sampling_cpu<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_CPU);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_CPU);
self
}

Expand All @@ -528,7 +548,9 @@ impl PerfCounterBuilderLinux {
}

pub fn enable_sampling_raw<'a>(&'a mut self) -> &'a PerfCounterBuilderLinux {
self.attrs.sample_type.insert(SampleFormatFlags::PERF_SAMPLE_RAW);
self.attrs
.sample_type
.insert(SampleFormatFlags::PERF_SAMPLE_RAW);
self
}

Expand Down
Loading

0 comments on commit 5f060f5

Please sign in to comment.