Skip to content

Commit b6f0262

Browse files
Merge pull request #606 from fortanix/nr/mem-alloc-test-documentation
Adding some documentation for the parameters and test description memory allocator tests
2 parents a2f10f6 + 1f5f200 commit b6f0262

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

examples/mem-alloc-test/src/main.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/* Test description:
2+
* This test tries to record the performance numbers with the memory allocator
3+
* used in EDP. The steps are as follows:
4+
* Create n threads. Each threads will continuosly allocate a randomly sized
5+
* buffer, traverse random locations in the buffer and then free it.
6+
*
7+
* There are 3 types of threads, small, medium and large. Depending on the type
8+
* the small threads will allocate and deallocate small sized buffers, and the
9+
* large threads will allocate and deallocate large sized buffers. The buffer
10+
* sizes and number of checks are all controlled by some parameters/constants
11+
* which are there a couple of lines below.
12+
*
13+
* The todos before running this test is there in the description of
14+
* https://fortanix.atlassian.net/browse/RTE-36 (under the heading
15+
* "Instructions on how to run the test:" )
16+
*/
17+
118
use rand::Rng;
219
use std::alloc::{alloc, dealloc, Layout};
320
use std::ptr;
@@ -30,25 +47,63 @@ const TO_MB: usize = TO_KB * 1024;
3047
const TO_GB: usize = TO_MB * 1024;
3148

3249
/* Set of configurable parameters. These will adjusted as necessary while
33-
* recording the performance numbers
50+
* recording the performance numbers.
51+
* TODO: Replace the hard coded parameters with command line arguments.
52+
*/
53+
54+
/* This denotes the number of cpus in the system in which the test is being run.
55+
* This test will create as many number of threads as there are number of CPUs.
3456
*/
3557
const NUM_CPUS: usize = 2;
3658

59+
/* This thread creates 3 types of threads. Small, medium and large. Each type of
60+
* thread will run the loop of allocation and deallocation different number of
61+
* times. LIMIT_SMALL_THREAD, LIMIT_MEDIUM_THREAD, LIMIT_LARGE_THREAD denotes
62+
* the number of times small threads, medium threads and large threads run the
63+
* loop respectively.
64+
*/
3765
const LIMIT_SMALL_THREAD: i32 = 2;
3866
const LIMIT_MEDIUM_THREAD: i32 = 2;
3967
const LIMIT_LARGE_THREAD: i32 = 2;
4068

69+
/* Each type of thread namely, small, medium and large allocates different sizes
70+
* of memory and hence they have different scan intervals. This scan interval is
71+
* used in the traverse_buffer function. Large threads have large buffers and
72+
* their intervals are larger compared to small threads which allocate smaller
73+
* buffer. SCAN_INTERVAL_SMALL_THREAD, SCAN_INTERVAL_MEDIUM_THREAD, and
74+
* SCAN_INTERVAL_LARGE_THREAD denote the scan intervals for small threads,
75+
* medium threads and large threads respectively.
76+
*/
4177
const SCAN_INTERVAL_SMALL_THREAD: usize = 1 * TO_KB;
4278
const SCAN_INTERVAL_MEDIUM_THREAD: usize = 1 * TO_MB;
4379
const SCAN_INTERVAL_LARGE_THREAD: usize = 1 * TO_MB;
4480

45-
const SMALL_THREAD_MEM_START: usize = 1;
46-
const SMALL_THREAD_MEM_END: usize = 512;
47-
const MEDIUM_THREAD_MEM_START: usize = 1;
48-
const MEDIUM_THREAD_MEM_END: usize = 2;
49-
const LARGE_THREAD_MEM_START: usize = 1;
50-
const LARGE_THREAD_MEM_END: usize = 2;
5181

82+
/* Each thread allocates a random sized buffer. The range of the random sizes
83+
* depend on the thread type namely small, medium and large.
84+
* SMALL_THREAD_MEM_START and SMALL_THREAD_MEM_END denote the minium and maximum
85+
* buffer size of small threads in KB respectively.
86+
*
87+
* MEDIUM_THREAD_MEM_START and MEDIUM_THREAD_MEM_END denote the minium and maximum
88+
* buffer size of medium threads in MB respectively.
89+
* LARGE_THREAD_MEM_START and LARGE_THREAD_MEM_END denote the minium and maximum
90+
* buffer size of large threads in GB respectively.
91+
*/
92+
const SMALL_THREAD_MEM_START: usize = 1; // in KB
93+
const SMALL_THREAD_MEM_END: usize = 512; // in KB
94+
const MEDIUM_THREAD_MEM_START: usize = 1; // in MB
95+
const MEDIUM_THREAD_MEM_END: usize = 2; // in MB
96+
const LARGE_THREAD_MEM_START: usize = 1; // in GB
97+
const LARGE_THREAD_MEM_END: usize = 2; // in GB
98+
99+
/* In traverse_buffer function, we randomly pick up random number of indices in
100+
* in the buffer and access them. MAX_INDEX_CHECKS_PER_BUFFER denotes the maximum
101+
* number of checks per buffer. We don't traverse the entire buffer as it will
102+
* slow down each thread and the threads won't be able to exihibit concurrency
103+
* during thread allocation and de allocation. Higher the value of
104+
* MAX_INDEX_CHECKS_PER_BUFFER, slower will be the threads and lesser will be
105+
* the concurrency.
106+
*/
52107
const MAX_INDEX_CHECKS_PER_BUFFER: usize = 32;
53108

54109
fn calculate_and_print_stat(
@@ -136,23 +191,20 @@ fn worker_thread(
136191
/* Create a random size depending on the memory type */
137192
let (scan_interval, size, limit) = match memsize {
138193
MemSize::Large => {
139-
/* buffer size will be from 1GB to 4GB */
140194
(
141195
SCAN_INTERVAL_LARGE_THREAD,
142196
TO_GB * get_random_num(LARGE_THREAD_MEM_START, LARGE_THREAD_MEM_END),
143197
LIMIT_LARGE_THREAD,
144198
)
145199
}
146200
MemSize::Medium => {
147-
/* buffer size will be from 8MB to 128 */
148201
(
149202
SCAN_INTERVAL_MEDIUM_THREAD,
150203
TO_MB * get_random_num(MEDIUM_THREAD_MEM_START, MEDIUM_THREAD_MEM_END),
151204
LIMIT_MEDIUM_THREAD,
152205
)
153206
}
154207
MemSize::Small => {
155-
/* buffer size will be from 1KB to 512KB */
156208
(
157209
SCAN_INTERVAL_SMALL_THREAD,
158210
TO_KB * get_random_num(SMALL_THREAD_MEM_START, SMALL_THREAD_MEM_END),

examples/mem-correctness-test/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
}
1414
}
1515
}
16-
* So this basically runs for a long time and should never crash
16+
* So this basically runs for a long time and should never crash.
17+
* The todos before running this test is there in the description of
18+
* https://fortanix.atlassian.net/browse/RTE-39 (under the heading
19+
* "Instructions on how to run the test:" )
1720
*/
1821

1922
use core::arch::asm;

0 commit comments

Comments
 (0)