@@ -23,16 +23,16 @@ pub(crate) enum CacheInfoError {
23
23
MissingOptionalAttr ( String , CacheEntry ) ,
24
24
}
25
25
26
- struct CacheEngine {
27
- store : Box < dyn CacheStore > ,
26
+ struct CacheEngine < T : CacheStore = HostCacheStore > {
27
+ store : T ,
28
28
}
29
29
30
30
trait CacheStore : std:: fmt:: Debug {
31
31
fn get_by_key ( & self , index : u8 , file_name : & str ) -> Result < String , CacheInfoError > ;
32
32
}
33
33
34
34
#[ derive( Debug ) ]
35
- pub ( crate ) struct CacheEntry {
35
+ pub struct CacheEntry {
36
36
// Cache Level: 1, 2, 3..
37
37
pub level : u8 ,
38
38
// Type of cache: Unified, Data, Instruction.
@@ -45,17 +45,16 @@ pub(crate) struct CacheEntry {
45
45
}
46
46
47
47
#[ derive( Debug ) ]
48
- struct HostCacheStore {
48
+ pub struct HostCacheStore {
49
49
cache_dir : PathBuf ,
50
50
}
51
51
52
- #[ cfg( not( test) ) ]
53
- impl Default for CacheEngine {
52
+ impl Default for CacheEngine < HostCacheStore > {
54
53
fn default ( ) -> Self {
55
54
CacheEngine {
56
- store : Box :: new ( HostCacheStore {
55
+ store : HostCacheStore {
57
56
cache_dir : PathBuf :: from ( "/sys/devices/system/cpu/cpu0/cache" ) ,
58
- } ) ,
57
+ } ,
59
58
}
60
59
}
61
60
}
@@ -72,7 +71,7 @@ impl CacheStore for HostCacheStore {
72
71
}
73
72
74
73
impl CacheEntry {
75
- fn from_index ( index : u8 , store : & dyn CacheStore ) -> Result < CacheEntry , CacheInfoError > {
74
+ fn from_index ( index : u8 , store : & impl CacheStore ) -> Result < CacheEntry , CacheInfoError > {
76
75
let mut err_str = String :: new ( ) ;
77
76
let mut cache: CacheEntry = CacheEntry :: default ( ) ;
78
77
@@ -287,10 +286,10 @@ pub(crate) fn read_cache_config(
287
286
// Also without this mechanism we would be logging the warnings for each level which pollutes
288
287
// a lot the logs.
289
288
let mut logged_missing_attr = false ;
290
- let engine = CacheEngine :: default ( ) ;
289
+ let engine = CacheEngine :: < HostCacheStore > :: default ( ) ;
291
290
292
291
for index in 0 ..=MAX_CACHE_LEVEL {
293
- match CacheEntry :: from_index ( index, engine. store . as_ref ( ) ) {
292
+ match CacheEntry :: from_index ( index, & engine. store ) {
294
293
Ok ( cache) => {
295
294
append_cache_level ( cache_l1, cache_non_l1, cache) ;
296
295
}
@@ -326,22 +325,22 @@ mod tests {
326
325
dummy_fs : HashMap < String , String > ,
327
326
}
328
327
329
- impl Default for CacheEngine {
328
+ impl Default for CacheEngine < MockCacheStore > {
330
329
fn default ( ) -> Self {
331
330
CacheEngine {
332
- store : Box :: new ( MockCacheStore {
331
+ store : MockCacheStore {
333
332
dummy_fs : create_default_store ( ) ,
334
- } ) ,
333
+ } ,
335
334
}
336
335
}
337
336
}
338
337
339
- impl CacheEngine {
338
+ impl CacheEngine < MockCacheStore > {
340
339
fn new ( map : & HashMap < String , String > ) -> Self {
341
340
CacheEngine {
342
- store : Box :: new ( MockCacheStore {
341
+ store : MockCacheStore {
343
342
dummy_fs : map. clone ( ) ,
344
- } ) ,
343
+ } ,
345
344
}
346
345
}
347
346
}
@@ -425,12 +424,12 @@ mod tests {
425
424
let mut map1 = default_map. clone ( ) ;
426
425
map1. remove ( "index0/type" ) ;
427
426
let engine = CacheEngine :: new ( & map1) ;
428
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
427
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
429
428
// We did create the level file but we still do not have the type file.
430
429
assert ! ( matches!( res. unwrap_err( ) , CacheInfoError :: MissingCacheType ) ) ;
431
430
432
431
let engine = CacheEngine :: new ( & default_map) ;
433
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
432
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
434
433
assert_eq ! (
435
434
format!( "{}" , res. unwrap_err( ) ) ,
436
435
"shared cpu map, coherency line size, size, number of sets" ,
@@ -440,15 +439,15 @@ mod tests {
440
439
let mut map2 = default_map. clone ( ) ;
441
440
map2. insert ( "index0/level" . to_string ( ) , "d" . to_string ( ) ) ;
442
441
let engine = CacheEngine :: new ( & map2) ;
443
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
442
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
444
443
assert_eq ! (
445
444
format!( "{}" , res. unwrap_err( ) ) ,
446
445
"Invalid cache configuration found for level: invalid digit found in string"
447
446
) ;
448
447
449
448
default_map. insert ( "index0/type" . to_string ( ) , "Instructionn" . to_string ( ) ) ;
450
449
let engine = CacheEngine :: new ( & default_map) ;
451
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
450
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
452
451
assert_eq ! (
453
452
format!( "{}" , res. unwrap_err( ) ) ,
454
453
"Invalid cache configuration found for type: Instructionn"
@@ -464,7 +463,7 @@ mod tests {
464
463
"00000000,00000001" . to_string ( ) ,
465
464
) ;
466
465
let engine = CacheEngine :: new ( & default_map) ;
467
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
466
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
468
467
assert_eq ! (
469
468
format!( "{}" , res. unwrap_err( ) ) ,
470
469
"coherency line size, size, number of sets"
@@ -475,15 +474,15 @@ mod tests {
475
474
"00000000,0000000G" . to_string ( ) ,
476
475
) ;
477
476
let engine = CacheEngine :: new ( & default_map) ;
478
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
477
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
479
478
assert_eq ! (
480
479
format!( "{}" , res. unwrap_err( ) ) ,
481
480
"Invalid cache configuration found for shared_cpu_map: invalid digit found in string"
482
481
) ;
483
482
484
483
default_map. insert ( "index0/shared_cpu_map" . to_string ( ) , "00000000" . to_string ( ) ) ;
485
484
let engine = CacheEngine :: new ( & default_map) ;
486
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
485
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
487
486
assert_eq ! (
488
487
format!( "{}" , res. unwrap_err( ) ) ,
489
488
"Invalid cache configuration found for shared_cpu_map: 00000000"
@@ -496,7 +495,7 @@ mod tests {
496
495
497
496
default_map. insert ( "index0/coherency_line_size" . to_string ( ) , "64" . to_string ( ) ) ;
498
497
let engine = CacheEngine :: new ( & default_map) ;
499
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
498
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
500
499
assert_eq ! (
501
500
"shared cpu map, size, number of sets" ,
502
501
format!( "{}" , res. unwrap_err( ) )
@@ -507,7 +506,7 @@ mod tests {
507
506
"Instruction" . to_string ( ) ,
508
507
) ;
509
508
let engine = CacheEngine :: new ( & default_map) ;
510
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
509
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
511
510
assert_eq ! (
512
511
format!( "{}" , res. unwrap_err( ) ) ,
513
512
"Invalid cache configuration found for coherency_line_size: invalid digit found in \
@@ -521,23 +520,23 @@ mod tests {
521
520
522
521
default_map. insert ( "index0/size" . to_string ( ) , "64K" . to_string ( ) ) ;
523
522
let engine = CacheEngine :: new ( & default_map) ;
524
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
523
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
525
524
assert_eq ! (
526
525
format!( "{}" , res. unwrap_err( ) ) ,
527
526
"shared cpu map, coherency line size, number of sets" ,
528
527
) ;
529
528
530
529
default_map. insert ( "index0/size" . to_string ( ) , "64" . to_string ( ) ) ;
531
530
let engine = CacheEngine :: new ( & default_map) ;
532
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
531
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
533
532
assert_eq ! (
534
533
format!( "{}" , res. unwrap_err( ) ) ,
535
534
"Invalid cache configuration found for size: 64"
536
535
) ;
537
536
538
537
default_map. insert ( "index0/size" . to_string ( ) , "64Z" . to_string ( ) ) ;
539
538
let engine = CacheEngine :: new ( & default_map) ;
540
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
539
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
541
540
assert_eq ! (
542
541
format!( "{}" , res. unwrap_err( ) ) ,
543
542
"Invalid cache configuration found for size: 64Z"
@@ -550,15 +549,15 @@ mod tests {
550
549
551
550
default_map. insert ( "index0/number_of_sets" . to_string ( ) , "64" . to_string ( ) ) ;
552
551
let engine = CacheEngine :: new ( & default_map) ;
553
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
552
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
554
553
assert_eq ! (
555
554
"shared cpu map, coherency line size, size" ,
556
555
format!( "{}" , res. unwrap_err( ) )
557
556
) ;
558
557
559
558
default_map. insert ( "index0/number_of_sets" . to_string ( ) , "64K" . to_string ( ) ) ;
560
559
let engine = CacheEngine :: new ( & default_map) ;
561
- let res = CacheEntry :: from_index ( 0 , engine. store . as_ref ( ) ) ;
560
+ let res = CacheEntry :: from_index ( 0 , & engine. store ) ;
562
561
assert_eq ! (
563
562
format!( "{}" , res. unwrap_err( ) ) ,
564
563
"Invalid cache configuration found for number_of_sets: invalid digit found in string"
0 commit comments