How can I control and read the CP0 performance counters from a C program?

Home Forums MIPS Insider MIPS FAQ How can I control and read the CP0 performance counters from a C program?

This topic contains 6 replies, has 3 voices, and was last updated by  wijdene 3 years ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #64628

    ChrisImgtec
    Moderator

    Here is a fragment of code that you can use in a C program
    to configure and read the performance counter registers.

    #include “mips/m32c0.h”

    /* set up performance counter registers to
    * count cycles and instructions
    */
    unsigned int perfevent;
    unsigned int perfcontrol;
    unsigned int cyclecount;
    unsigned int instrcount;

    /* Set up control register for perf counter 0 to count event 0 (cycles).
    * Count in User, Supervisor, and Kernel Mode. Count when EXL=0.
    */
    perfevent = 0; /* Event 0 is cycles */
    perfcontrol = ((perfevent < < PERFCNT_EVENTSHFT) |
    PERFCNT_U | PERFCNT_S | PERFCNT_K | PERFCNT_EXL);
    _m32c0_mtc0(C0_PERFCNT, 0, perfcontrol);

    /* Set up control register for perf counter 1 to count event 1
    (instructions).
    * Count in User, Supervisor, and Kernel Mode. Count when EXL=0.
    */
    perfevent = 1; /* Event 1 is instructions completed */
    perfcontrol = ((perfevent < < PERFCNT_EVENTSHFT) |
    PERFCNT_U | PERFCNT_S | PERFCNT_K | PERFCNT_EXL);
    _m32c0_mtc0(C0_PERFCNT, 2, perfcontrol);

    /* Clear performance counters */
    _m32c0_mtc0(C0_PERFCNT, 1, 0);
    _m32c0_mtc0(C0_PERFCNT, 3, 0);
    /* end of setup for performance counting */

    /* the code you want to measure goes here */

    /* read performance counter registers and print results */
    cyclecount = _m32c0_mfc0(C0_PERFCNT,1);
    instrcount = _m32c0_mfc0(C0_PERFCNT,3);
    printf (“Instructions = %dn”,instrcount);
    printf (“Cycles = %dn”,cyclecount);
    printf (“Instructions per Cycle = %fn”,
    (float) instrcount / (float) cyclecount);

    #64634

    wijdene
    Member

    Hello,
    Is the “mips/m32c0.h” included in gcc compiler?
    I received this error:test.c:2:24: error: mips/m32c0.h: No such file or directory, while compiling my test.c where I made the previous code.
    Best regards,

    #64633

    Gopi Krishna
    Participant

    Hi Chris where do we get more information about CP0 counters and the options to get cache misses and any other information?How about using CP) 9?Can we use these performance counters when running on linux? If so which mode I need to run?

    #64632

    ChrisImgtec
    Moderator

    The Software User’s Manual for the core or chip you are using should have the information on the performance counters. Note not all core support performance counters. The performance counters can only be accessed on Kernel mode.

    #64631

    Gopi Krishna
    Participant

    Thanks Chris,
    I could able to make use of it on bare metal…But on linux I am still looking at options to make use of it…

    #64630

    ChrisImgtec
    Moderator

    On Linux you would have to write a module and then call it from your program.

    #64629

    Gopi Krishna
    Participant

    Hi,
    Thanks for your response.

    Can you please let me know the details of the module that has to be written on linux to get both instruction count(MIPS) and cycles(for MHz)

    Regards,
    Gopi

Viewing 7 posts - 1 through 7 (of 7 total)

Forums are currently locked.