5

I'm trying to get timestamp counter (TSC) of CPU. I've succeeded on my PC with Intel i7 CPU. Assembly code in this links helped me.

Now, I want to do it on my Raspberry Pi model B. The problem is ARmv6 has a different instruction set from Intel CPU. And user-mode prvents me from using some instructions. So I've searched some article to solve it.

But it didnt' work well as I expected. When I run the following two codes (with sudo privilege):

int main(void){
//      printf("try to enable user mode access to performance counter\n");
        asm("MCR p15, 0, %0, C9, C14, 0\n\t" :: "r"(1));
//      printf("disable counter overflow interrupts (just in case)\n");
        asm("MCR p15, 0, %0, C9, C14, 2\n\t" :: "r"(0x8000000f));

        return 0;
}

and

#include <stdio.h>

static inline unsigned int get_cyclecount(void) {
        unsigned int value;
        // Read CCNT Register
        asm volatile ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(value));
        return value;
}

int main(void){
        unsigned int t = get_cyclecount();
        printf("%d\n", t);
        return 0;
}

It prints nothing even there is printf

0

2 Answers 2

4

I do not believe you can successfully execute those instructions from Linux userland. I thought only Linux kernel code could successfully execute those instructions (sudo/root is not the same as kernel).

A kernel module to enable access to the CPU cycle timer is given at http://blog.regehr.org/archives/794

It uses the following (kernel) code to allow access to the CPU cycle timer from userland.

  asm volatile ("mcr p15,  0, %0, c15,  c9, 0\n" : : "r" (1));
1

There are two ways to solves.

A: You can write a kernel module to enable PMU on hardware. Then read counters from userland.

B: use syscall: syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); to access related PMU events.

Please check below link for detail.

http://zhiyisun.github.io/2016/03/02/How-to-Use-Performance-Monitor-Unit-(PMU)-of-64-bit-ARMv8-A-in-Linux.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.