mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
play a bit with the ARM PMU
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
67
userland/linux/perf_event_open.c
Normal file
67
userland/linux/perf_event_open.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user