1165{
1166
1168 int size,line_size,associativity,sets;
1169 int write_policy,allocation_policy;
1170 DIR *dir;
1172 char filename[BUFSIZ],type_string[BUFSIZ];
1173 char *str_result;
1174 char write_policy_string[BUFSIZ],allocation_policy_string[BUFSIZ];
1175 struct dirent *d;
1176 int max_level=0;
1177 int level_count,level_index;
1178
1180
1181
1182
1183
1184
1185 dir=opendir("/sys/devices/system/cpu/cpu0/cache");
1186 if (dir==NULL) {
1187 goto unrecoverable_error;
1188 }
1189
1193 }
1194 }
1195
1196 while(1) {
1197 d = readdir(dir);
1198 if (d==NULL) break;
1199
1200 if (strncmp(d->d_name, "index", 5)) continue;
1201
1202 MEMDBG(
"Found %s\n",d->d_name);
1203
1204
1205
1206
1207 sprintf(filename,
1208 "/sys/devices/system/cpu/cpu0/cache/%s/level",
1209 d->d_name);
1210 fff=fopen(filename,
"r");
1212 MEMDBG(
"Cannot open level.\n");
1213 goto unrecoverable_error;
1214 }
1215
1219 MEMDBG(
"Could not read cache level\n");
1220 goto unrecoverable_error;
1221 }
1222
1223
1224 level_index=level-1;
1225
1226 level_count = 0;
1228 level_count++;
1230 break;
1231 }
1232 }
1233
1235 MEMDBG(
"Exceeded maximum levels %d\n",
1237 break;
1238 }
1239
1240
1241
1242
1243 sprintf(filename,
1244 "/sys/devices/system/cpu/cpu0/cache/%s/type",d->d_name);
1245 fff=fopen(filename,
"r");
1247 MEMDBG(
"Cannot open type\n");
1248 goto unrecoverable_error;
1249 }
1250 str_result=fgets(type_string, BUFSIZ,
fff);
1252 if (str_result==NULL) {
1253 MEMDBG(
"Could not read cache type\n");
1254 goto unrecoverable_error;
1255 }
1256 if (!strcmp(type_string,"Data")) {
1258 }
1259 if (!strcmp(type_string,"Instruction")) {
1261 }
1262 if (!strcmp(type_string,"Unified")) {
1264 }
1265
1266
1267
1268
1269 write_policy=0;
1270 sprintf(filename,
1271 "/sys/devices/system/cpu/cpu0/cache/%s/write_policy",d->d_name);
1272 fff=fopen(filename,
"r");
1274 MEMDBG(
"Cannot open write_policy\n");
1275 goto get_allocation_policy;
1276 }
1277 str_result=fgets(write_policy_string, BUFSIZ,
fff);
1279 if (str_result==NULL) {
1280 MEMDBG(
"Could not read cache write_policy\n");
1281 goto get_allocation_policy;
1282 }
1283 if (!strcmp(write_policy_string,"WriteThrough")) {
1285 }
1286 if (!strcmp(write_policy_string,"WriteBack")) {
1288 }
1289
1290get_allocation_policy:
1291
1292
1293
1294
1295 allocation_policy=0;
1296 sprintf(filename,
1297 "/sys/devices/system/cpu/cpu0/cache/%s/allocation_policy",d->d_name);
1298 fff=fopen(filename,
"r");
1300 MEMDBG(
"Cannot open allocation_policy\n");
1301 goto get_size;
1302 }
1303 str_result=fgets(allocation_policy_string, BUFSIZ,
fff);
1305 if (str_result==NULL) {
1306 MEMDBG(
"Could not read cache allocation_policy\n");
1307 goto get_size;
1308 }
1309 if (!strcmp(allocation_policy_string,"ReadAllocate")) {
1311 }
1312 if (!strcmp(allocation_policy_string,"WriteAllocate")) {
1314 }
1315 if (!strcmp(allocation_policy_string,"ReadWriteAllocate")) {
1317 }
1318
1319get_size:
1320 L[level_index].
cache[level_count].
type=
type | write_policy | allocation_policy;
1321
1322
1323
1324
1325 sprintf(filename,
1326 "/sys/devices/system/cpu/cpu0/cache/%s/size",d->d_name);
1327 fff=fopen(filename,
"r");
1329 MEMDBG(
"Cannot open size\n");
1330 goto unrecoverable_error;
1331 }
1335 MEMDBG(
"Could not read cache size\n");
1336 goto unrecoverable_error;
1337 }
1338
1339
1340 L[level_index].
cache[level_count].
size=size*1024;
1341
1342
1343
1344
1346
1347 sprintf(filename,
1348 "/sys/devices/system/cpu/cpu0/cache/%s/coherency_line_size",
1349 d->d_name);
1350 fff=fopen(filename,
"r");
1352 MEMDBG(
"Cannot open linesize\n");
1353 }
1354 else {
1358 MEMDBG(
"Could not read cache line-size\n");
1359 } else {
1361 }
1362 }
1363
1364
1365
1366
1367
1369
1370 sprintf(filename,
1371 "/sys/devices/system/cpu/cpu0/cache/%s/ways_of_associativity",
1372 d->d_name);
1373 fff=fopen(filename,
"r");
1375 MEMDBG(
"Cannot open associativity\n");
1376 }
1377 else {
1381 MEMDBG(
"Could not read cache associativity\n");
1382 }
1383 else {
1385 }
1386 }
1387
1388
1389
1390
1392
1393 sprintf(filename,
1394 "/sys/devices/system/cpu/cpu0/cache/%s/number_of_sets",
1395 d->d_name);
1396 fff=fopen(filename,
"r");
1398 MEMDBG(
"Cannot open sets\n");
1399 }
1400 else {
1403
1405 MEMDBG(
"Could not read cache sets\n");
1406 }
1407 else {
1409 }
1410 }
1411
1412
1413 if ((line_size!=0) && (associativity!=0)) {
1414 if (((size*1024)/line_size/associativity)!=sets) {
1415 MEMDBG(
"Warning! sets %d != expected %d\n",
1416 sets,((size*1024)/line_size/associativity));
1417 }
1418 }
1419
1420 MEMDBG(
"\tL%d %s cache\n",level,type_string);
1421 MEMDBG(
"\t%d kilobytes\n",size);
1422 MEMDBG(
"\t%d byte linesize\n",line_size);
1423 MEMDBG(
"\t%d-way associative\n",associativity);
1424 MEMDBG(
"\t%d lines\n",sets);
1425 MEMDBG(
"\tUnknown inclusivity\n");
1426 MEMDBG(
"\tUnknown replacement algorithm\n");
1427 MEMDBG(
"\tUnknown if victim cache\n");
1428
1429 if (level>max_level) max_level=level;
1430
1432 MEMDBG(
"Exceeded maximum cache level %d\n",
1434 break;
1435 }
1436 }
1437
1438 closedir(dir);
1440
1441 return 0;
1442
1443unrecoverable_error:
1444
1445
1447
1448 closedir(dir);
1449 return 0;
1450}
static const PAPI_hw_info_t * hw_info
#define PAPI_MAX_MEM_HIERARCHY_LEVELS
#define PAPI_MH_TYPE_DATA
#define PAPI_MH_TYPE_RW_ALLOC
#define PAPI_MH_MAX_LEVELS
#define PAPI_MH_TYPE_WR_ALLOC
#define PAPI_MH_TYPE_RD_ALLOC
#define PAPI_MH_TYPE_INST
#define PAPI_MH_TYPE_EMPTY
#define PAPI_MH_TYPE_UNIFIED
#define MEMDBG(format, args...)
PAPI_mh_info_t mem_hierarchy
PAPI_mh_level_t level[PAPI_MAX_MEM_HIERARCHY_LEVELS]
PAPI_mh_cache_info_t cache[PAPI_MH_MAX_LEVELS]