Example Code for Android

The code below shows a simple example of reading and outputting counters using PVRScope on Android platforms.

/*********************************************************************************************
*
* This example outputs the values of the hardware counters found in Group 0
* to Android Logcat once a second for 60 seconds, and consists of five steps:
*
* 1. Define a function to initialise PVRScopeStats
* 2. Initialise PVRScopeStats
* 3. Set the active group to 0
* 4. Read and output the counter information for group 0 to Logcat
* 5. Shutdown PVRScopeStats
*
*********************************************************************************************/

#include <android/log.h>

// Few macro definitions here 
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "PVRScope", __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "PVRScope", __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO   , "PVRScope", __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN   , "PVRScope", __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "PVRScope", __VA_ARGS__)
#define NUMBER_OF_LOOPS 60


#include <unistd.h>
#include <string.h>
#include <jni.h>
#include "PVRScopeStats.h"

// Step 1. Define a function to initialise PVRScopeStats
bool PSInit(SPVRScopeImplData **ppsPVRScopeData, SPVRScopeCounterDef **ppsCounters, SPVRScopeCounterReading* const psReading, unsigned int* const pnCount)
{
    //Initialise PVRScope
    const EPVRScopeInitCode eInitCode = PVRScopeInitialise(ppsPVRScopeData);

    if (ePVRScopeInitCodeOk == eInitCode)
    {
        LOGI("Initialised services connection.\n");
    }
    else
    {
        LOGE("Error: failed to initialise services connection.\n");
        *ppsPVRScopeData = NULL;
        return false;
    }

    //Initialise the counter data structures.
    if (PVRScopeGetCounters(*ppsPVRScopeData, pnCount, ppsCounters, psReading))
    {
        LOGI("Total counters enabled: %d.", *pnCount);
    }

    return true;
}

int main()
{
    //Internal control data
    SPVRScopeImplData *psData;

    //Counter information (set at uint time)
    SPVRScopeCounterDef     *psCounters = nullptr;
    unsigned int            uCounterNum;

    //Counter reading data
    unsigned int            uActiveGroupSelect;
    bool                bActiveGroupChanged;
    SPVRScopeCounterReading sReading;


    // Step 2. Initialise PVRScopeStats
    if (PSInit(&psData, &psCounters, &sReading, &uCounterNum))
        LOGI("PVRScope up and running.");
    else
        LOGE("Error initializing PVRScope.");

    //Print each and every counter (and its group)
    LOGI("Find below the list of counters:");
    for (int i = 0; i < uCounterNum; ++i)
    {
        LOGI("    Group %d %s", psCounters[i].nGroup, psCounters[i].pszName);
    }

    // Step 3. Set the active group to 0
    bActiveGroupChanged = true;
    uActiveGroupSelect = 0;

    unsigned int sampleRate = 100;
    unsigned int index = 0;

    unsigned int i = 0;
    while (i < NUMBER_OF_LOOPS)
    {
        // Ask for the active group 0 only on the first run. 
        if (bActiveGroupChanged)
        {
            PVRScopeSetGroup(psData, uActiveGroupSelect);
            bActiveGroupChanged = false;
        }

        ++index;
        if (index < sampleRate)
        {
            // Sample the counters every 10ms. Don't read or output it.
            PVRScopeReadCounters(psData, NULL);
        }
        else
        {
            ++i;
            index = 0;

            // Step 4. Read and output the counter information for group 0 to Logcat
            if (PVRScopeReadCounters(psData, &sReading))
            {
                // Check for all the counters in the system if the counter has a value on the given active group and ouptut it.
                for (int i = 0; i < uCounterNum; ++i)
                {
                    if (i < sReading.nValueCnt)
                    {
                        //Print the 3D Load
                        if (strcmp(psCounters[i].pszName, "GPU task load: 3D core") == 0)
                        {
                            LOGI("%s : %f\%", psCounters[i].pszName, sReading.pfValueBuf[i]);
                        }
                        //Print the TA Load
                        else if (strcmp(psCounters[i].pszName, "GPU task load: TA core") == 0)
                        {
                            LOGI("%s : %f\%", psCounters[i].pszName, sReading.pfValueBuf[i]);
                        }
                    }
                }
            }
        }

        //Poll for the counters once a second
        usleep(10 * 1000);
    }

    // Step 5. Shutdown PVRScopeStats
    PVRScopeDeInitialise(&psData, &psCounters, &sReading);
}