Benchmarking Security in the HiFive1

@bruce For part of my project, I need to benchmark the difference between running in machine mode versus user mode. It seems that there should be a performance hit from PMP checking while in u mode. My first thought was to run a sorting algorithm once in m mode and once in u mode and compare the instruction counts using the mhpmcounter and hpmcounter registers. Does this seem like a correct and simple way to compare the two?

Sorting could work, but it might be hard to find a reasonable algorithm that takes long enough to run with maximum 16 KB of data.

My own counting primes benchmark takes quite a but of time without using a huge amount of RAM: http://hoult.org/primes.txt

I’m pretty sure I know what your results will be, but I won’t spoil your fun :slight_smile: It would certainly be interesting if I’m wrong.

1 Like

Thanks for link, I will port this concept into rust and give it a shot.

I haven’t tested this on the HiFive1B yet, but it should do what I need. (I won’t be printing the values out)

fn main() {

    let mut primes: [usize;1000] = [0;1000];

    for i in 2..primes.len()-1 {
        primes[i] = i;
    }

    for i in 0..primes.len() {
        let factor = primes[i];
        if factor != 0 {
            sieve(&mut primes, factor);
        }
    }

    for i in 0..primes.len() {
        if primes[i] != 0 {
            println!("{}", primes[i])
        }
    }
}

fn sieve(primes: &mut [usize], factor: usize) {
    for i in 0..primes.len() {
        let value = primes[i];
        if value != 0 && value != factor {
            if value % factor == 0 {
                primes[i] = 0;
            }
        }
    }
}```