Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. extern crate time;
  2.  
  3. use time::precise_time_ns;
  4.  
  5. use std::mem;
  6. use std::process::exit;
  7.  
  8. const PAGE_SIZE: usize = 4096;
  9.  
  10. fn main() {
  11. let args: Vec<String> = std::env::args().collect();
  12.  
  13. match args.len() {
  14. 0 | 1 => pow2_series(1_000_000, 16),
  15. 3 => {
  16. match parse_args(&args[1], &args[2]) {
  17. Ok(time) => println!("{} ns", time),
  18. Err(err) => println!("{}", err),
  19. }
  20. }
  21. _ => {
  22. println!("Wrong number of arguments");
  23. exit(1);
  24. }
  25. }
  26. }
  27.  
  28. fn parse_args(page_count: &str, trail_count: &str) -> Result<f64, &'static str> {
  29. let page_count = page_count
  30. .parse::<usize>()
  31. .map_err(|_| "Illegal page_count")?;
  32. let trail_count = trail_count
  33. .parse::<u32>()
  34. .map_err(|_| "Illegal trail_count")?;
  35.  
  36. Ok(tlb_probe(page_count, trail_count))
  37. }
  38.  
  39. /// Prints the results of multiple tlb_probe.
  40. /// The series has a given length and page_count gets incremented in powers of 2.
  41. /// The given trail_size is normalized for each run to get a consistent executing time.
  42. fn pow2_series(trail_count: u32, length: u32) {
  43. for page_count in (0..length).map(|x| 2u64.pow(x)) {
  44. let r = tlb_probe(page_count as usize, trail_count / page_count as u32);
  45. println!("{} = {:?}", page_count, r);
  46. }
  47. }
  48.  
  49. /// Probes the tlb using the two parameters.
  50. /// Returns the time taken per operation in ns.
  51. fn tlb_probe(page_count: usize, trail_count: u32) -> f64 {
  52. let jump = PAGE_SIZE / mem::size_of::<i32>();
  53.  
  54. let mut a = vec![0i32; page_count * jump];
  55.  
  56. // warm up
  57. for i in a.iter_mut() {
  58. *i += 1;
  59. }
  60.  
  61. let before = precise_time_ns();
  62.  
  63. // the hot loop
  64. for _ in 0..trail_count {
  65. let mut i = 0;
  66.  
  67. while i < a.len() {
  68. a[i] += 1;
  69.  
  70. i += jump;
  71. }
  72. }
  73.  
  74. let after = precise_time_ns();
  75.  
  76. // time calculations
  77. let d = after - before;
  78.  
  79. d as f64 / (trail_count * page_count as u32) as f64
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement