Obtaining Performance StatisticsThe first step required to obtain performance statistics is to obtain a handle of type
Functions that can be used to extract statistics data from a
The following code example will display CPU usage, used RAM, free RAM, used disk space, and free disk space using the first method ( // 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); |
||||||||||||||||||||
|