Skip to content
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

Add feature flag for reducing randomness in memalloc test #622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion examples/mem-alloc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ num_cpus = "1.14.0"
[package.metadata.fortanix-sgx]
# heap size (in bytes), the default heap size is 0x2000000.
heap-size=0x4000000
debug=false
debug=false

[features]
reduce_randomness = []
43 changes: 39 additions & 4 deletions examples/mem-alloc-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* https://fortanix.atlassian.net/browse/RTE-36 (under the heading
* "Instructions on how to run the test:" )
*/
/*
The performance results produced by this test were quite random to get a clear picture of
improvements between dlmalloc and snmalloc allocator. In order to reduce the randomness
of the tests, use feature flag "reduce_randomness" - RTE-85
*/

use rand::Rng;
use std::alloc::{alloc, dealloc, Layout};
Expand Down Expand Up @@ -157,14 +162,30 @@ fn wakeup_all_child_threads(pair_clone: Arc<(Mutex<bool>, Condvar)>) {
}

fn traverse_buffer(buf: *mut u8, size: usize, _scan_interval: usize) {
let num_indices_checks = get_random_num(1, MAX_INDEX_CHECKS_PER_BUFFER);
let num_indices_checks: usize;
#[cfg(feature = "reduce_randomness")]
{
num_indices_checks = 10;
}
#[cfg(not(feature = "reduce_randomness"))]
{
num_indices_checks= get_random_num(1, MAX_INDEX_CHECKS_PER_BUFFER);
}
for _i in 1..=num_indices_checks {
/* Check for random indices and number of such indices is num_indices_checks
* Please note that depending on the number random number generator, we
* can check for the same index multiple times. We could have checked
* for all the indices but that would be too time consuming
*/
let index = get_random_num(0, size - 1);
let index: usize;
#[cfg(feature = "reduce_randomness")]
{
index = _i * 10 % size;
}
#[cfg(not(feature = "reduce_randomness"))]
{
index = get_random_num(0, size - 1);
}
unsafe {
ptr::write(buf.offset(index as isize), 1);
}
Expand All @@ -189,7 +210,7 @@ fn worker_thread(
*/
loop {
/* Create a random size depending on the memory type */
let (scan_interval, size, limit) = match memsize {
let (scan_interval, mut size, limit) = match memsize {
MemSize::Large => {
(
SCAN_INTERVAL_LARGE_THREAD,
Expand Down Expand Up @@ -218,7 +239,21 @@ fn worker_thread(
/* Create an array of x GB where x is a random number between 1 to 4 */

// Create a layout based on the size and alignment
let align = get_random_num(2, PAGE_SIZE).next_power_of_two();
let align: usize;
#[cfg(feature = "reduce_randomness")]
{
align = PAGE_SIZE;
size = match memsize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Can we have the size conditionally defined in the match case before this? Since both the size and the layout is being conditionally defined, I was thinking if we can have the size in the condition block this

/* Create a random size depending on the memory type */
        let (scan_interval, mut size, limit) = match memsize {
            MemSize::Large => {
                (
                    SCAN_INTERVAL_LARGE_THREAD,
                    #[cfg(feature = "reduce_randomness")] {crate::TO_GB * 1} 
                    #[cfg(not(feature = "reduce_randomness"))] {TO_GB * get_random_num(LARGE_THREAD_MEM_START, LARGE_THREAD_MEM_END)},
                    LIMIT_LARGE_THREAD,
                )
            }
            MemSize::Medium => {
                (
                    SCAN_INTERVAL_MEDIUM_THREAD,
                    // similarly for medium and small threads
                    TO_MB * get_random_num(MEDIUM_THREAD_MEM_START, MEDIUM_THREAD_MEM_END),
                    LIMIT_MEDIUM_THREAD,
                )
            }
            MemSize::Small => {
                (
                    SCAN_INTERVAL_SMALL_THREAD,
                    TO_KB * get_random_num(SMALL_THREAD_MEM_START, SMALL_THREAD_MEM_END),
                    LIMIT_SMALL_THREAD,
                )
            }
        };

MemSize::Large => crate::TO_GB * 1,
MemSize::Medium => crate::TO_MB * 1,
MemSize::Small => crate::TO_KB * 1
}
}
#[cfg(not(feature = "reduce_randomness"))]
{
align = get_random_num(2, PAGE_SIZE).next_power_of_two();

}
let layout = Layout::from_size_align(size, align).unwrap();

// Allocate memory using the global allocator
Expand Down
Loading