Example Code for Linux

This is an example of reading and outputting counter data on Linux platforms using PVRScope;

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

int main()
{
    struct SPVRScopeImplData *PVRScopeStatsData;
    enum EPVRScopeInitCode eInitRet = PVRScopeInitialise(&PVRScopeStatsData);
    const unsigned int nGroup = 0;

    printf("PVRScopeInitialise: %d\n", eInitRet);

    unsigned int numCounters = 0;
    struct SPVRScopeCounterDef *counterDefinitions = 0;
    struct SPVRScopeCounterReading counterReading;

    memset(&counterReading, 0, sizeof(counterReading));
    PVRScopeSetGroup(PVRScopeStatsData, nGroup);

    int iteration = 0;
    while (1)
    {
        int iCounter = 0, iResult = 0, iErrors = 0;

        usleep(1000 * 100);
        if (PVRScopeReadCounters(PVRScopeStatsData, &counterReading))
        {
            for (; iCounter < numCounters; iCounter++)
            {
                if (counterDefinitions[iCounter].nGroup == nGroup)
                {
                    // Ensure we have enough results
                    if (iResult < counterReading.nValueCnt)
                    {
                        int isPercentage = counterDefinitions[iCounter].nBoolPercentage;
                        const char * const counterName = counterDefinitions[iCounter].pszName;
                        float counterValue = counterReading.pfValueBuf[iResult++];

                        if (isPercentage)
                            printf(" %s: %f%%\n", counterName, counterValue);
                        else
                            printf(" %s: %f\n", counterName, counterValue);
                    }
                    else
                    {
                        ++iErrors;
                    }
                }
            }

            // If we have too many results, there may be new counters available
            if (iResult < counterReading.nValueCnt)
            {
                iErrors += counterReading.nValueCnt - iResult;
                PVRScopeGetCounters(PVRScopeStatsData, &numCounters, &counterDefinitions, &counterReading);
            }
        }
        else
        {
            ++iErrors;
        }

        printf("Iteration %d, errors: %d.\n", iteration++, iErrors);
    }

    PVRScopeDeInitialise(&PVRScopeStatsData, &counterDefinitions, &counterReading);

    return 0;