Skip to content

Commit

Permalink
#7 reformat CpuUsageHelper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Colucci committed May 4, 2020
1 parent 0a9c255 commit e5a04d3
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions Sources/macOS/CpuUsageHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//==============================================================================
/**
@file CpuUsageHelper.cpp
@file CpuUsageHelper.cpp
@brief Calculate the current CPU percentage
@copyright (c) 2018, Corsair Memory, Inc.
@copyright (c) 2018, Corsair Memory, Inc.
This source code is licensed under the MIT-style license found in the LICENSE file.
**/
Expand All @@ -15,11 +15,11 @@
CpuUsageHelper::CpuUsageHelper()
{
int mib[2U] = { CTL_HW, HW_NCPU };
size_t sizeOfNumCPUs = sizeof(mNumberOfCPUs);
int status = sysctl(mib, 2U, &mNumberOfCPUs, &sizeOfNumCPUs, NULL, 0U);
if(status)
{
mNumberOfCPUs = 1;
size_t sizeOfNumCPUs = sizeof(mNumberOfCPUs);
int status = sysctl(mib, 2U, &mNumberOfCPUs, &sizeOfNumCPUs, NULL, 0U);
if(status)
{
mNumberOfCPUs = 1;
}
}

Expand All @@ -31,43 +31,43 @@ int CpuUsageHelper::GetCurrentCPUValue()
double cpuPercentage = 0;

natural_t processorCount = 0;
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &processorCount, &mProcessorInfo, &mProcessorInfoCnt);
if(err == KERN_SUCCESS)
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &processorCount, &mProcessorInfo, &mProcessorInfoCnt);
if(err == KERN_SUCCESS)
{
for(unsigned int cpuIndex = 0 ; cpuIndex < mNumberOfCPUs; cpuIndex++)
{
double inUse = 0.0;
double total = 0.0;
double inUse = 0.0;
double total = 0.0;

if(mOldProcessorInfo != NULL)
{
if(mOldProcessorInfo != NULL)
{
inUse = (
(mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER])
+ (mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM])
+ (mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE])
);
(mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER])
+ (mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM])
+ (mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE])
);
total = inUse + (mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_IDLE] - mOldProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_IDLE]);
}
else
{
inUse = mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER] + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM] + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE];
total = inUse + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_IDLE];
}
}
else
{
inUse = mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_USER] + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_SYSTEM] + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_NICE];
total = inUse + mProcessorInfo[(CPU_STATE_MAX * cpuIndex) + CPU_STATE_IDLE];
}

cpuPercentage += inUse / total;
}
}

if(mOldProcessorInfo)
{
size_t prevCpuInfoSize = sizeof(integer_t) * mOldProcessorInfoCnt;
vm_deallocate(mach_task_self(), (vm_address_t)mOldProcessorInfo, prevCpuInfoSize);
}
if(mOldProcessorInfo)
{
size_t prevCpuInfoSize = sizeof(integer_t) * mOldProcessorInfoCnt;
vm_deallocate(mach_task_self(), (vm_address_t)mOldProcessorInfo, prevCpuInfoSize);
}

mOldProcessorInfo = mProcessorInfo;
mOldProcessorInfoCnt = mProcessorInfoCnt;
mOldProcessorInfo = mProcessorInfo;
mOldProcessorInfoCnt = mProcessorInfoCnt;

mProcessorInfo = NULL;
mProcessorInfoCnt = 0;
mProcessorInfo = NULL;
mProcessorInfoCnt = 0;
}

return (int)lround(100.0 * cpuPercentage / mNumberOfCPUs);
Expand Down

0 comments on commit e5a04d3

Please sign in to comment.