@@ -5032,11 +5032,18 @@ look_sysfscpu(struct hwloc_topology *topology,
5032
5032
5033
5033
/* look at the caches */
5034
5034
if (topology -> want_some_cpu_caches ) {
5035
- for (j = 0 ; j < 10 ; j ++ ) {
5035
+ DIR * cachesdir ;
5036
+ struct dirent * dirent ;
5037
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/" , i );
5038
+ cachesdir = hwloc_opendir (str , data -> root_fd );
5039
+ if (cachesdir ) {
5040
+ while ((dirent = readdir (cachesdir )) != NULL ) {
5036
5041
char str2 [20 ]; /* enough for a level number (one digit) or a type (Data/Instruction/Unified) */
5037
5042
hwloc_bitmap_t cacheset ;
5038
5043
5039
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map" , i , j );
5044
+ if (strncmp (dirent -> d_name , "index" , 5 ))
5045
+ continue ;
5046
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s/shared_cpu_map" , i , dirent -> d_name );
5040
5047
cacheset = hwloc__alloc_read_path_as_cpumask (str , data -> root_fd );
5041
5048
if (cacheset ) {
5042
5049
if (hwloc_bitmap_iszero (cacheset )) {
@@ -5066,14 +5073,14 @@ look_sysfscpu(struct hwloc_topology *topology,
5066
5073
struct hwloc_obj * cache ;
5067
5074
5068
5075
/* get the cache level depth */
5069
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /level" , i , j ); /* contains %u at least up to 4.19 */
5076
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /level" , i , dirent -> d_name ); /* contains %u at least up to 4.19 */
5070
5077
if (hwloc_read_path_as_uint (str , & depth , data -> root_fd ) < 0 ) {
5071
5078
hwloc_bitmap_free (cacheset );
5072
5079
continue ;
5073
5080
}
5074
5081
5075
5082
/* cache type */
5076
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /type" , i , j );
5083
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /type" , i , dirent -> d_name );
5077
5084
if (hwloc_read_path_by_length (str , str2 , sizeof (str2 ), data -> root_fd ) > 0 ) {
5078
5085
if (!strncmp (str2 , "Data" , 4 ))
5079
5086
ctype = HWLOC_OBJ_CACHE_DATA ;
@@ -5084,7 +5091,7 @@ look_sysfscpu(struct hwloc_topology *topology,
5084
5091
}
5085
5092
5086
5093
/* cache id */
5087
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /id" , i , j );
5094
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /id" , i , dirent -> d_name );
5088
5095
hwloc_read_path_as_uint (str , & id , data -> root_fd );
5089
5096
5090
5097
otype = hwloc_cache_type_by_depth_type (depth , ctype );
@@ -5099,7 +5106,7 @@ look_sysfscpu(struct hwloc_topology *topology,
5099
5106
5100
5107
/* get the cache size */
5101
5108
kB = 0 ;
5102
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /size" , i , j ); /* contains %uK at least up to 4.19 */
5109
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /size" , i , dirent -> d_name ); /* contains %uK at least up to 4.19 */
5103
5110
hwloc_read_path_as_uint (str , & kB , data -> root_fd );
5104
5111
/* KNL reports L3 with size=0 and full cpuset in cpuid.
5105
5112
* Let hwloc_linux_try_add_knl_mcdram_cache() detect it better.
@@ -5111,19 +5118,19 @@ look_sysfscpu(struct hwloc_topology *topology,
5111
5118
5112
5119
/* get the line size */
5113
5120
linesize = 0 ;
5114
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /coherency_line_size" , i , j ); /* contains %u at least up to 4.19 */
5121
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /coherency_line_size" , i , dirent -> d_name ); /* contains %u at least up to 4.19 */
5115
5122
hwloc_read_path_as_uint (str , & linesize , data -> root_fd );
5116
5123
5117
5124
/* get the number of sets and lines per tag.
5118
5125
* don't take the associativity directly in "ways_of_associativity" because
5119
5126
* some archs (ia64, ppc) put 0 there when fully-associative, while others (x86) put something like -1 there.
5120
5127
*/
5121
5128
sets = 0 ;
5122
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /number_of_sets" , i , j ); /* contains %u at least up to 4.19 */
5129
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /number_of_sets" , i , dirent -> d_name ); /* contains %u at least up to 4.19 */
5123
5130
hwloc_read_path_as_uint (str , & sets , data -> root_fd );
5124
5131
5125
5132
lines_per_tag = 1 ;
5126
- sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/index%d /physical_line_partition" , i , j ); /* contains %u at least up to 4.19 */
5133
+ sprintf (str , "/sys/devices/system/cpu/cpu%d/cache/%s /physical_line_partition" , i , dirent -> d_name ); /* contains %u at least up to 4.19 */
5127
5134
hwloc_read_path_as_uint (str , & lines_per_tag , data -> root_fd );
5128
5135
5129
5136
/* first cpu in this cache, add the cache */
@@ -5147,6 +5154,8 @@ look_sysfscpu(struct hwloc_topology *topology,
5147
5154
}
5148
5155
hwloc_bitmap_free (cacheset );
5149
5156
}
5157
+ closedir (cachesdir );
5158
+ }
5150
5159
}
5151
5160
5152
5161
} hwloc_bitmap_foreach_end ();
0 commit comments