2222#error "No disk device defined, is your board supported?"
2323#endif
2424
25- /* Assume the largest sector we will encounter is 512 bytes */
26- #define SECTOR_SIZE 512
2725#if CONFIG_SRAM_SIZE >= 512
2826/* Cap buffer size at 128 KiB */
29- #define SEQ_BLOCK_COUNT 256
27+ #define MAX_TOTAL_BUF_SIZE 128
3028#elif CONFIG_SOC_POSIX
3129/* Posix does not define SRAM size */
32- #define SEQ_BLOCK_COUNT 256
30+ #define MAX_TOTAL_BUF_SIZE 128
3331#else
34- /* Two buffers with 512 byte blocks will use half of all SRAM */
35- #define SEQ_BLOCK_COUNT (CONFIG_SRAM_SIZE / 2)
32+ /* Use half of all SRAM */
33+ #define MAX_TOTAL_BUF_SIZE (CONFIG_SRAM_SIZE / 2)
3634#endif
37- #define BUF_SIZE (SECTOR_SIZE * SEQ_BLOCK_COUNT)
35+
36+ #define BUF_SIZE ((MAX_TOTAL_BUF_SIZE * 1024) / 2)
37+
3838/* Number of sequential reads to get an average speed */
3939#define SEQ_ITERATIONS 10
40- /* Number of random reads to get an IOPS calculation */
41- #define RANDOM_ITERATIONS SEQ_BLOCK_COUNT
40+ /* Maximum number of random reads to get an IOPS calculation */
41+ #define MAX_RANDOM_ITERATIONS 10
4242
43- static uint32_t chosen_sectors [RANDOM_ITERATIONS ];
43+ static uint32_t chosen_sectors [MAX_RANDOM_ITERATIONS ];
4444
4545
4646static const char * disk_pdrv = DISK_NAME ;
4747static uint32_t disk_sector_count ;
4848static uint32_t disk_sector_size ;
4949
50+ static uint32_t buf_sector_count ;
5051static uint8_t test_buf [BUF_SIZE ] __aligned (32 );
5152static uint8_t backup_buf [BUF_SIZE ] __aligned (32 );
5253
@@ -75,9 +76,11 @@ static void test_setup(void)
7576 TC_PRINT ("Disk reports sector size %u\n" , cmd_buf );
7677 disk_sector_size = cmd_buf ;
7778
78- /* Assume sector size is 512 bytes, it will speed up calculations later */
79- zassert_true (cmd_buf == SECTOR_SIZE ,
80- "Test will fail, SECTOR_SIZE definition must be changed" );
79+ buf_sector_count = BUF_SIZE / disk_sector_size ;
80+
81+ /* Verify that the buffer can hold at least one sector */
82+ zassert_true (buf_sector_count >= 1 ,
83+ "Test will fail, sector does not fit in buffer, buffer size must be increased" );
8184
8285 disk_init_done = true;
8386}
@@ -125,14 +128,14 @@ ZTEST(disk_performance, test_sequential_read)
125128 time_ns = read_helper (1 );
126129
127130 TC_PRINT ("Average read speed over one sector: %" PRIu64 " KiB/s\n" ,
128- ((SECTOR_SIZE * (NSEC_PER_SEC / time_ns ))) / 1024 );
131+ ((disk_sector_size * (NSEC_PER_SEC / time_ns ))) / 1024 );
129132
130133 /* Now time long sequential read */
131- time_ns = read_helper (SEQ_BLOCK_COUNT );
134+ time_ns = read_helper (buf_sector_count );
132135
133136 TC_PRINT ("Average read speed over %d sectors: %" PRIu64 " KiB/s\n" ,
134- SEQ_BLOCK_COUNT ,
135- ((BUF_SIZE ) * (NSEC_PER_SEC / time_ns )) / 1024 );
137+ buf_sector_count ,
138+ ((disk_sector_size * buf_sector_count ) * (NSEC_PER_SEC / time_ns )) / 1024 );
136139}
137140
138141/* Helper function to time multiple sequential writes. Returns average time. */
@@ -151,7 +154,7 @@ static uint64_t write_helper(uint32_t num_blocks)
151154 zassert_equal (rc , 0 , "disk read failed" );
152155
153156 /* Initialize write buffer with data */
154- sys_rand_get (test_buf , num_blocks * SECTOR_SIZE );
157+ sys_rand_get (test_buf , num_blocks * disk_sector_size );
155158
156159 total_ns = 0 ;
157160 for (int i = 0 ; i < SEQ_ITERATIONS ; i ++ ) {
@@ -188,29 +191,30 @@ ZTEST(disk_performance, test_sequential_write)
188191 time_ns = write_helper (1 );
189192
190193 TC_PRINT ("Average write speed over one sector: %" PRIu64 " KiB/s\n" ,
191- ((SECTOR_SIZE * (NSEC_PER_SEC / time_ns ))) / 1024 );
194+ ((disk_sector_size * (NSEC_PER_SEC / time_ns ))) / 1024 );
192195
193196 /* Now time long sequential write */
194- time_ns = write_helper (SEQ_BLOCK_COUNT );
197+ time_ns = write_helper (buf_sector_count );
195198
196199 TC_PRINT ("Average write speed over %d sectors: %" PRIu64 " KiB/s\n" ,
197- SEQ_BLOCK_COUNT ,
198- ((BUF_SIZE ) * (NSEC_PER_SEC / time_ns )) / 1024 );
200+ buf_sector_count ,
201+ ((disk_sector_size * buf_sector_count ) * (NSEC_PER_SEC / time_ns )) / 1024 );
199202}
200203
201204ZTEST (disk_performance , test_random_read )
202205{
203206 timing_t start_time , end_time ;
204207 uint64_t cycles , total_ns ;
205208 uint32_t sector ;
206- int rc ;
209+ int rc = 0 ;
210+ uint32_t random_iterations = MIN (MAX_RANDOM_ITERATIONS , buf_sector_count );
207211
208212 if (!disk_init_done ) {
209213 zassert_unreachable ("Disk is not initialized" );
210214 }
211215
212216 /* Build list of sectors to read from. */
213- for (int i = 0 ; i < RANDOM_ITERATIONS ; i ++ ) {
217+ for (int i = 0 ; i < random_iterations ; i ++ ) {
214218 /* Get random num until we select a value within sector count */
215219 sector = sys_rand32_get () / ((UINT32_MAX / disk_sector_count ) + 1 );
216220 chosen_sectors [i ] = sector ;
@@ -221,7 +225,7 @@ ZTEST(disk_performance, test_random_read)
221225 timing_start ();
222226
223227 start_time = timing_counter_get ();
224- for (int i = 0 ; i < RANDOM_ITERATIONS ; i ++ ) {
228+ for (int i = 0 ; i < random_iterations ; i ++ ) {
225229 /*
226230 * Note: we don't check return code here,
227231 * we want to do I/O as fast as possible
@@ -235,9 +239,10 @@ ZTEST(disk_performance, test_random_read)
235239 /* Stop timing system */
236240 timing_stop ();
237241
238- TC_PRINT ("512 Byte IOPS over %d random reads: %" PRIu64 " IOPS\n" ,
239- RANDOM_ITERATIONS ,
240- ((uint64_t )(((uint64_t )RANDOM_ITERATIONS )*
242+ TC_PRINT ("%d Byte IOPS over %d random reads: %" PRIu64 " IOPS\n" ,
243+ disk_sector_size ,
244+ random_iterations ,
245+ ((uint64_t )(((uint64_t )random_iterations )*
241246 ((uint64_t )NSEC_PER_SEC )))
242247 / total_ns );
243248}
@@ -247,19 +252,20 @@ ZTEST(disk_performance, test_random_write)
247252 timing_t start_time , end_time ;
248253 uint64_t cycles , total_ns ;
249254 uint32_t sector ;
250- int rc ;
255+ int rc = 0 ;
256+ uint32_t random_iterations = MIN (MAX_RANDOM_ITERATIONS , buf_sector_count );
251257
252258 if (!disk_init_done ) {
253259 zassert_unreachable ("Disk is not initialized" );
254260 }
255261
256262 /* Build list of sectors to read from. */
257- for (int i = 0 ; i < RANDOM_ITERATIONS ; i ++ ) {
263+ for (int i = 0 ; i < random_iterations ; i ++ ) {
258264 /* Get random num until we select a value within sector count */
259265 sector = sys_rand32_get () / ((UINT32_MAX / disk_sector_count ) + 1 );
260266 chosen_sectors [i ] = sector ;
261267 /* Backup this sector */
262- rc = disk_access_read (disk_pdrv , & backup_buf [i * SECTOR_SIZE ],
268+ rc = disk_access_read (disk_pdrv , & backup_buf [i * disk_sector_size ],
263269 sector , 1 );
264270 zassert_equal (rc , 0 , "disk read failed for random write backup" );
265271 }
@@ -272,12 +278,12 @@ ZTEST(disk_performance, test_random_write)
272278 timing_start ();
273279
274280 start_time = timing_counter_get ();
275- for (int i = 0 ; i < RANDOM_ITERATIONS ; i ++ ) {
281+ for (int i = 0 ; i < random_iterations ; i ++ ) {
276282 /*
277283 * Note: we don't check return code here,
278284 * we want to do I/O as fast as possible
279285 */
280- rc = disk_access_write (disk_pdrv , & test_buf [i * SECTOR_SIZE ],
286+ rc = disk_access_write (disk_pdrv , & test_buf [i * disk_sector_size ],
281287 chosen_sectors [i ], 1 );
282288 }
283289 end_time = timing_counter_get ();
@@ -287,14 +293,15 @@ ZTEST(disk_performance, test_random_write)
287293 /* Stop timing system */
288294 timing_stop ();
289295
290- TC_PRINT ("512 Byte IOPS over %d random writes: %" PRIu64 " IOPS\n" ,
291- RANDOM_ITERATIONS ,
292- ((uint64_t )(((uint64_t )RANDOM_ITERATIONS )*
296+ TC_PRINT ("%d Byte IOPS over %d random writes: %" PRIu64 " IOPS\n" ,
297+ disk_sector_size ,
298+ random_iterations ,
299+ ((uint64_t )(((uint64_t )random_iterations )*
293300 ((uint64_t )NSEC_PER_SEC )))
294301 / total_ns );
295302 /* Restore backed up sectors */
296- for (int i = 0 ; i < RANDOM_ITERATIONS ; i ++ ) {
297- disk_access_write (disk_pdrv , & backup_buf [i * SECTOR_SIZE ],
303+ for (int i = 0 ; i < random_iterations ; i ++ ) {
304+ disk_access_write (disk_pdrv , & backup_buf [i * disk_sector_size ],
298305 chosen_sectors [i ], 1 );
299306 zassert_equal (rc , 0 , "failed to write backup sector to disk" );
300307 }
0 commit comments