Previous page

Next page

Locate page in Contents

Obtaining Performance Statistics

The first step required to obtain performance statistics is to obtain a handle of type PHT_SYSTEM_STATISTICS:

  1. Call PrlSrv_GetStatistics or PrlVm_GetStatistics. This will return a job (PHT_JOB) reference.
  2. Get the job result (a reference to an object of type PHT_RESULT) using PrlJob_GetResult.
  3. Get the handle to the PHT_SYSTEM_STATISTICS object using PrlResult_GetParam (there will be only one parameter returned).

Functions that can be used to extract statistics data from a PHT_SYSTEM_STATISTICS handle can be found in the C API Reference under the following sections:

C API Reference Section

Description

PHT_SYSTEM_STATISTICS

Functions to drill deeper into specific system statistics. As an example, to use functions that return CPU statistics, a handle of type PHT_SYSTEM_STATISTICS_CPU will be required. This handle can be obtained using PrlStat_GetCpuStat.

Functions that return memory statistics are also grouped here.

PHT_SYSTEM_STATISTICS_CPU

Functions that provide CPU statistics data.

PHT_SYSTEM_STATISTICS_DISK

Functions that provide hard disk statistics data.

PHT_SYSTEM_STATISTICS_DISK_PARTITION

Functions that provide statistics data for a disk partition.

PHT_SYSTEM_STATISTICS_IFACE

Functions that provide statistics data for a network interface.

PHT_SYSTEM_STATISTICS_PROCESS

Functions that provide statistics data about processes that are running.

PHT_SYSTEM_STATISTICS_USER_SESSION

Functions that provide statistics data about a user session.

The following code example will display CPU usage, used RAM, free RAM, used disk space, and free disk space using the first method (PrlSrv_GetStatistics):

// Obtain host statistics (PHT_SYSTEM_STATISTICS handle), and wait for a

// maximum of 10 seconds for the asynchronous call PrlSrv_GetStatistics to complete.

// Note: PrlVm_GetStatistics(hVm) could be used instead of

// PrlSrv_GetStatistics(hServer) if statistics are required for a

// virtual machine that is running.

PRL_HANDLE hServerStatisticsJob = PrlSrv_GetStatistics(hServer);

PRL_RESULT nServerStatistics = PrlJob_Wait(hServerStatisticsJob, 10000);

if (PRL_FAILED(nServerStatistics))

{

    printf("PrlSrv_GetStatistics returned error: %s\n",

        prl_result_to_string(nServerStatistics));

    PrlHandle_Free(hServerStatisticsJob);

    PrlHandle_Free(hServer);

    PrlApi_Deinit();

    SdkWrap_Unload();

    return -1;

}

  

// Check that the job (call to PrlSrv_GetStatistics) was successful.

PrlJob_GetRetCode(hServerStatisticsJob, &nServerStatistics);

if (PRL_FAILED(nServerStatistics))

{

    printf("PrlSrv_GetStatistics returned error: %s\n",

        prl_result_to_string(nServerStatistics));

    PrlHandle_Free(hServerStatisticsJob);

    PrlHandle_Free(hServer);

    PrlApi_Deinit();

    SdkWrap_Unload();

    return -1;

}

  

// Extract the result (PHT_RESULT handle) for the job.

PRL_HANDLE hResult = PRL_INVALID_HANDLE;

nServerStatistics = PrlJob_GetResult(hServerStatisticsJob, &hResult);

if (PRL_FAILED(nServerStatistics))

{

    printf("PrlJob_GetResult returned error: %s\n",

        prl_result_to_string(nServerStatistics));

    PrlHandle_Free(hServerStatisticsJob);

    PrlHandle_Free(hServer);

    PrlApi_Deinit();

    SdkWrap_Unload();

    return -1;

}

  

// Get the result (PHT_SYSTEM_STATISTICS handle).

PRL_HANDLE hServerStatistics = PRL_INVALID_HANDLE;

PrlResult_GetParam(hResult, &hServerStatistics);

  

PRL_HANDLE hCpuStatistics = PRL_INVALID_HANDLE;

ret = PrlStat_GetCpuStat(hServerStatistics, 0, &hCpuStatistics);

if (PRL_FAILED(ret))

{

    printf("PrlStat_GetCpuStat returned error: %s\n",

        prl_result_to_string(ret));

    // Clean up and exit here.

}

  

// Get CPU usage data (% used).

PRL_UINT32 nCpuUsage = 0;

PrlStatCpu_GetCpuUsage(hCpuStatistics, &nCpuUsage);

printf("CPU usage: %d%%\n", nCpuUsage);

    

// Get memory statistics.

PRL_UINT64 nUsedRam, nFreeRam;

PrlStat_GetFreeRamSize(hServerStatistics, &nFreeRam);

PrlStat_GetUsageRamSize(hServerStatistics, &nUsedRam);

printf("Used RAM: %I64d MB\nFree RAM: %I64d MB\n",

    nUsedRam/1024/1024, nFreeRam/1024/1024);

    

// Get disk statistics.

PRL_UINT64 nFreeDiskSpace, nUsedDiskSpace;

PRL_HANDLE hDiskStatistics = PRL_INVALID_HANDLE;

PrlStat_GetDiskStat(hServerStatistics, 0, &hDiskStatistics);

PrlStatDisk_GetFreeDiskSpace(hDiskStatistics, &nFreeDiskSpace);

PrlStatDisk_GetUsageDiskSpace(hDiskStatistics, &nUsedDiskSpace);

printf("Used Disk Space: %I64d MB\nFree Disk Space: %I64d MB\n",

    nUsedDiskSpace/1024/1024, nFreeDiskSpace/1024/1024);

  

PrlHandle_Free(hDiskStatistics);

PrlHandle_Free(hCpuStatistics);

PrlHandle_Free(hResult);

PrlHandle_Free(hServerStatistics);

PrlHandle_Free(hServerStatisticsJob);