Skip to content

Commit 362bc71

Browse files
committed
bugfix: flags never passed
* flags PERF_FLAG_PID_CGROUP and PERF_FLAG_FD_OUTPUT had values of 0 * flags were never passed to perf_event_open() * add new context switches example * run the context switches example in travis to improve test coverage
1 parent 88e654e commit 362bc71

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

.travis.yml

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
language: rust
2-
rust: nightly
2+
sudo: required
3+
env: TYPE=default RUST_BACKTRACE=1
4+
cache:
5+
directories:
6+
- $HOME/.cargo
7+
- $TRAVIS_BUILD_DIR/target/debug
8+
- $TRAVIS_BUILD_DIR/target/release
9+
script:
10+
- cargo build --verbose
11+
- cargo test --verbose
12+
- cargo build --release --verbose
13+
- cargo test --release --verbose
14+
matrix:
15+
include:
16+
- os: linux
17+
rust: nightly
18+
- os: linux
19+
rust: nightly
20+
env: TYPE=example RUST_BACKTRACE=1
21+
script:
22+
- cargo build --release --example context_switches
23+
- sudo target/release/examples/context_switches

examples/context_switches.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extern crate perfcnt;
2+
extern crate x86;
3+
4+
use perfcnt::{PerfCounter, AbstractPerfCounter};
5+
use perfcnt::linux::SoftwareEventType as Software;
6+
use perfcnt::linux::PerfCounterBuilderLinux as Builder;
7+
8+
pub fn main() {
9+
let mut pc: PerfCounter = Builder::from_software_event(Software::ContextSwitches)
10+
.on_cpu(0)
11+
.for_all_pids()
12+
.finish()
13+
.expect("Could not create counter");
14+
15+
pc.start().expect("Can not start the counter");
16+
std::thread::sleep(std::time::Duration::new(1,0));
17+
pc.stop().expect("Can not stop the counter");;
18+
19+
println!("Context Switches/s: {:?}", pc.read().expect("Can not read counter"));
20+
pc.reset().expect("Can not reset the counter");
21+
}

src/linux/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub struct PerfCounterBuilderLinux {
5656
group: isize,
5757
pid: pid_t,
5858
cpu: isize,
59-
flags: usize,
59+
flags: i32,
6060
attrs: perf_format::EventAttr,
6161
}
6262

@@ -265,7 +265,7 @@ impl PerfCounterBuilderLinux {
265265
///
266266
/// This flag re-routes the output from an event to the group leader.
267267
pub fn set_flag_fd_output<'a>(&'a mut self) -> &'a mut PerfCounterBuilderLinux {
268-
self.flags |= 0x0; //PERF_FLAG_FD_OUTPUT;
268+
self.flags |= 0x02; //PERF_FLAG_FD_OUTPUT;
269269
self
270270
}
271271

@@ -277,7 +277,7 @@ impl PerfCounterBuilderLinux {
277277
/// event is measured only if the thread running on the monitored
278278
/// CPU belongs to the designated container (cgroup).
279279
pub fn set_flag_pid_cgroup<'a>(&'a mut self) -> &'a mut PerfCounterBuilderLinux {
280-
self.flags |= 0x0; //PERF_FLAG_PID_CGROUP;
280+
self.flags |= 0x04; //PERF_FLAG_PID_CGROUP;
281281
self
282282
}
283283

@@ -604,13 +604,12 @@ impl PerfCounterBuilderLinux {
604604
}
605605

606606
pub fn finish_sampling_counter(&self) -> Result<PerfCounter, io::Error> {
607-
let flags = 0;
608607
let fd = perf_event_open(
609608
&self.attrs,
610609
self.pid,
611610
self.cpu as i32,
612611
self.group as i32,
613-
flags,
612+
self.flags,
614613
) as ::libc::c_int;
615614
if fd < 0 {
616615
return Err(Error::from_raw_os_error(-fd));
@@ -625,13 +624,12 @@ impl PerfCounterBuilderLinux {
625624

626625
/// Instantiate the performance counter.
627626
pub fn finish(&self) -> Result<PerfCounter, io::Error> {
628-
let flags = 0;
629627
let fd = perf_event_open(
630628
&self.attrs,
631629
self.pid,
632630
self.cpu as i32,
633631
self.group as i32,
634-
flags,
632+
self.flags,
635633
) as ::libc::c_int;
636634
if fd < 0 {
637635
return Err(Error::from_raw_os_error(-fd));

0 commit comments

Comments
 (0)