Skip to content

Commit 7afb175

Browse files
Fix counter blocks in MaliCounter (#7)
* Get l2_slices and save them in MaliCounter * Fix the indexing for counter blocks considering L2 slices * Define counter names in a single place * Reset PMU counters on stop() for consistency with Mali counters * Fix typo and better formatting
1 parent 3d74620 commit 7afb175

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

mali_counter.cpp

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ MaliHWInfo get_mali_hw_info(const char *path)
191191
hw_info.p_value = props.minor_revision;
192192
for (uint32_t i = 0; i < props.num_core_groups; i++)
193193
hw_info.core_mask |= props.core_mask[i];
194-
hw_info.mp_count = __builtin_popcountll(hw_info.core_mask);
195-
//hw_info.l2_slices = props.l2_slices;
194+
hw_info.mp_count = __builtin_popcountll(hw_info.core_mask);
195+
hw_info.l2_slices = props.l2_slices;
196196

197197
close(fd);
198198
}
@@ -204,20 +204,15 @@ MaliHWInfo get_mali_hw_info(const char *path)
204204

205205
MaliCounter::MaliCounter()
206206
{
207-
_counters =
208-
{
209-
{"GPU_ACTIVE", Measurement(0, "cycles")},
210-
{"JS0_JOBS", Measurement(0, "jobs")},
211-
{"JS1_JOBS", Measurement(0, "jobs")},
212-
{"L2_READ_LOOKUP", Measurement(0, "cache lookups")},
213-
{"L2_EXT_READ", Measurement(0, "transactions")},
214-
{"L2_EXT_AR_STALL", Measurement(0, "stall cycles")},
215-
{"L2_WRITE_LOOKUP", Measurement(0, "cache lookups")},
216-
{"L2_EXT_WRITE", Measurement(0, "transactions")},
217-
{"L2_EXT_W_STALL", Measurement(0, "stall cycles")},
218-
{"L2_EXT_READ_BEATS", Measurement(0, "bus cycles")},
219-
{"L2_EXT_WRITE_BEATS", Measurement(0, "bus cycles")}
220-
};
207+
for (const auto &jm_counter : _jm_counter_names)
208+
{
209+
_counters.emplace(std::make_pair(jm_counter.first, Measurement{0, jm_counter.second}));
210+
}
211+
212+
for (const auto &mmu_counter : _mmu_counter_names)
213+
{
214+
_counters.emplace(std::make_pair(mmu_counter.first, Measurement{0, mmu_counter.second}));
215+
}
221216

222217
init();
223218
}
@@ -233,7 +228,8 @@ void MaliCounter::init()
233228

234229
MaliHWInfo hw_info = get_mali_hw_info(_device);
235230

236-
_num_cores = hw_info.mp_count;
231+
_num_cores = hw_info.mp_count;
232+
_num_l2_slices = hw_info.l2_slices;
237233

238234
_fd = open(_device, O_RDWR | O_CLOEXEC | O_NONBLOCK); // NOLINT
239235

@@ -443,23 +439,30 @@ const uint32_t *MaliCounter::get_counters() const
443439
return _raw_counter_buffer.data();
444440
}
445441

446-
const uint32_t *MaliCounter::get_counters(mali_userspace::MaliCounterBlockName block, int core) const
442+
const uint32_t *MaliCounter::get_counters(mali_userspace::MaliCounterBlockName block, int index) const
447443
{
448444
switch (block)
449445
{
450446
case mali_userspace::MALI_NAME_BLOCK_JM:
451447
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * 0;
452448
case mali_userspace::MALI_NAME_BLOCK_MMU:
453-
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * 2;
449+
if (index < 0 || index >= _num_l2_slices)
450+
{
451+
throw std::runtime_error("Invalid slice number.");
452+
}
453+
454+
// If an MMU counter is selected, index refers to the MMU slice
455+
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * (2 + index);
454456
case mali_userspace::MALI_NAME_BLOCK_TILER:
455457
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * 1;
456458
default:
457-
if (core < 0)
459+
if (index < 0 || index >= _num_cores)
458460
{
459461
throw std::runtime_error("Invalid core number.");
460462
}
461463

462-
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * (3 + _core_index_remap[core]);
464+
// If a shader core counter is selected, index refers to the core index
465+
return _raw_counter_buffer.data() + mali_userspace::MALI_NAME_BLOCK_SIZE * (2 + _num_l2_slices + _core_index_remap[index]);
463466
}
464467
}
465468

@@ -491,19 +494,29 @@ void MaliCounter::stop()
491494
wait_next_event();
492495

493496
const uint32_t *jm_counter = get_counters(mali_userspace::MALI_NAME_BLOCK_JM);
494-
_counters.at("GPU_ACTIVE") = Measurement(jm_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_JM, "GPU_ACTIVE")], _counters.at("GPU_ACTIVE").unit());
495-
_counters.at("JS0_JOBS") = Measurement(jm_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_JM, "JS0_JOBS")], _counters.at("JS0_JOBS").unit());
496-
_counters.at("JS1_JOBS") = Measurement(jm_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_JM, "JS1_JOBS")], _counters.at("JS1_JOBS").unit());
497-
498-
const uint32_t *mmu_counter = get_counters(mali_userspace::MALI_NAME_BLOCK_MMU);
499-
_counters.at("L2_READ_LOOKUP") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_READ_LOOKUP")], _counters.at("L2_READ_LOOKUP").unit());
500-
_counters.at("L2_EXT_READ") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_READ")], _counters.at("L2_EXT_READ").unit());
501-
_counters.at("L2_EXT_AR_STALL") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_AR_STALL")], _counters.at("L2_EXT_AR_STALL").unit());
502-
_counters.at("L2_WRITE_LOOKUP") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_WRITE_LOOKUP")], _counters.at("L2_WRITE_LOOKUP").unit());
503-
_counters.at("L2_EXT_WRITE") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_WRITE")], _counters.at("L2_EXT_WRITE").unit());
504-
_counters.at("L2_EXT_W_STALL") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_W_STALL")], _counters.at("L2_EXT_W_STALL").unit());
505-
_counters.at("L2_EXT_READ_BEATS") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_READ_BEATS")], _counters.at("L2_EXT_READ_BEATS").unit());
506-
_counters.at("L2_EXT_WRITE_BEATS") = Measurement(mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, "L2_EXT_WRITE_BEATS")], _counters.at("L2_EXT_WRITE_BEATS").unit());
497+
498+
for (const auto &jm_counter_name : _jm_counter_names)
499+
{
500+
_counters.at(jm_counter_name.first) = Measurement(jm_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_JM, jm_counter_name.first)], _counters.at(jm_counter_name.first).unit());
501+
}
502+
503+
// We have one MMU counter per L2 cache slice
504+
std::vector<const uint32_t *> mmu_counters;
505+
for (int i = 0; i < _num_l2_slices; i++)
506+
{
507+
mmu_counters.push_back(get_counters(mali_userspace::MALI_NAME_BLOCK_MMU, i));
508+
}
509+
510+
// We iterate over counter names and accumulate data from all L2 cache slices
511+
for (const auto &mmu_counter_name : _mmu_counter_names)
512+
{
513+
uint32_t mmu_counter_value = 0;
514+
for (const auto &mmu_counter : mmu_counters)
515+
{
516+
mmu_counter_value += mmu_counter[find_counter_index_by_name(mali_userspace::MALI_NAME_BLOCK_MMU, mmu_counter_name.first)];
517+
}
518+
_counters.at(mmu_counter_name.first) = Measurement(mmu_counter_value, _counters.at(mmu_counter_name.first).unit());
519+
}
507520

508521
_stop_time = _timestamp;
509522
}

mali_counter.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,30 @@ class MaliCounter : public Instrument
5858
void sample_counters();
5959
void wait_next_event();
6060
const uint32_t *get_counters() const;
61-
const uint32_t *get_counters(mali_userspace::MaliCounterBlockName block, int core = -1) const;
61+
const uint32_t *get_counters(mali_userspace::MaliCounterBlockName block, int index = -1) const;
6262
int find_counter_index_by_name(mali_userspace::MaliCounterBlockName block, const char *name);
6363

64+
const std::vector<const std::pair<const char *, const char *>> _jm_counter_names{
65+
{"GPU_ACTIVE", "cycles"},
66+
{"JS0_JOBS", "jobs"},
67+
{"JS1_JOBS", "jobs"}};
68+
const std::vector<const std::pair<const char *, const char *>> _mmu_counter_names{
69+
{"L2_READ_LOOKUP", "cache lookups"},
70+
{"L2_EXT_READ", "transactions"},
71+
{"L2_EXT_AR_STALL", "stall cycles"},
72+
{"L2_WRITE_LOOKUP", "cache lookups"},
73+
{"L2_EXT_WRITE", "transactions"},
74+
{"L2_EXT_W_STALL", "stall cycles"},
75+
{"L2_EXT_READ_BEATS", "bus cycles"},
76+
{"L2_EXT_WRITE_BEATS", "bus cycles"}};
6477
std::map<std::string, Measurement> _counters{};
6578

6679
uint64_t _start_time{0};
6780
uint64_t _stop_time{0};
6881

6982
const char *const _device{"/dev/mali0"};
7083
int _num_cores{0};
84+
int _num_l2_slices{0};
7185
uint32_t _hw_ver{0};
7286
int _buffer_count{16};
7387
size_t _buffer_size{0};

pmu_counter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void PMUCounter::stop()
4343
try
4444
{
4545
_cycles = _pmu_cycles.get_value<long long>();
46+
_pmu_cycles.reset();
4647
}
4748
catch (const std::runtime_error &)
4849
{
@@ -52,6 +53,7 @@ void PMUCounter::stop()
5253
try
5354
{
5455
_instructions = _pmu_instructions.get_value<long long>();
56+
_pmu_instructions.reset();
5557
}
5658
catch (const std::runtime_error &)
5759
{
@@ -61,6 +63,7 @@ void PMUCounter::stop()
6163
try
6264
{
6365
_cache_references = _pmu_cache_references.get_value<long long>();
66+
_pmu_cache_references.reset();
6467
}
6568
catch (const std::runtime_error &)
6669
{
@@ -70,6 +73,7 @@ void PMUCounter::stop()
7073
try
7174
{
7275
_cache_misses = _pmu_cache_misses.get_value<long long>();
76+
_pmu_cache_misses.reset();
7377
}
7478
catch (const std::runtime_error &)
7579
{
@@ -79,6 +83,7 @@ void PMUCounter::stop()
7983
try
8084
{
8185
_branch_instructions = _pmu_branch_instructions.get_value<long long>();
86+
_pmu_branch_instructions.reset();
8287
}
8388
catch (const std::runtime_error &)
8489
{
@@ -88,6 +93,7 @@ void PMUCounter::stop()
8893
try
8994
{
9095
_branch_misses = _pmu_branch_misses.get_value<long long>();
96+
_pmu_branch_misses.reset();
9197
}
9298
catch (const std::runtime_error &)
9399
{

0 commit comments

Comments
 (0)