play a bit with the ARM PMU

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-11-18 00:00:00 +00:00
parent 95430c7c0c
commit d4a27987d6
7 changed files with 142 additions and 22 deletions

View File

@@ -28,6 +28,11 @@ class MyMap {
auto pair = *it;
return std::make_pair(2*pair.first, 3*pair.second);
}
// TODO. How to return that new object by address?
//value_type& operator->() {
// auto pair = *it;
// return std::make_pair(2*pair.first, 3*pair.second);
//}
};
iterator begin() { return iterator(map.begin()); }
iterator end() { return iterator(map.end()); }
@@ -44,7 +49,14 @@ int main() {
assert((*it++ == std::pair<const int, int>(2, 33)));
assert((*it++ == std::pair<const int, int>(4, 36)));
// TODO operator->()
it = map.begin();
//assert((it->first == 0));
auto stl_it = map.map.begin();
assert((stl_it->first == 0));
for (const auto& v : map) {
std::cout << v.first << " " << v.second << std::endl;
}
}

View File

@@ -2,18 +2,7 @@
* https://cirosantilli.com/linux-kernel-module-cheat#c-busy-loop
* https://cirosantilli.com/linux-kernel-module-cheat#benchmark-emulators-on-userland-executables */
#include <stdlib.h>
void __attribute__ ((noinline)) busy_loop(
unsigned long long max,
unsigned long long max2
) {
for (unsigned long long i = 0; i < max2; i++) {
for (unsigned long long j = 0; j < max; j++) {
__asm__ __volatile__ ("" : "+g" (i), "+g" (j) : :);
}
}
}
#include <lkmc.h>
int main(int argc, char **argv) {
unsigned long long max, max2;
@@ -27,5 +16,5 @@ int main(int argc, char **argv) {
} else {
max2 = 1;
}
busy_loop(max, max2);
lkmc_busy_loop(max, max2);
}

View File

@@ -0,0 +1,67 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#perf-event-open
*
* Malloc n bytes as given from the command line.
*/
#include <asm/unistd.h>
#include <linux/perf_event.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <lkmc.h>
static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
int
main(int argc, char **argv)
{
struct perf_event_attr pe;
long long count;
int fd;
uint64_t n;
if (argc > 1) {
n = strtoll(argv[1], NULL, 0);
} else {
n = 100;
}
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled = 1;
pe.exclude_kernel = 1;
// Don't count hypervisor events.
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0);
if (fd == -1) {
fprintf(stderr, "Error opening leader %llx\n", pe.config);
exit(EXIT_FAILURE);
}
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
lkmc_busy_loop(n, 1);
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
close(fd);
}