Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically check the number of cores application is using

Tags:

c++

multicore

Is there a way to programmatically check how many cores a C++ application is using?

I am looking for Windows/Linux solution but of course platform independent solution would be preferable, I guess its asking for too much.

like image 476
makc Avatar asked May 28 '13 12:05

makc


People also ask

How many cores is a process using?

As a general rule, 1 process only uses 1 core. Actually, 1 thread can only be executed by 1 core. If you have a dual core processor, it is literally 2 CPUs stuck together in the same pc. These are called physical processors.

How can I tell what cores a process is running on?

You can use ps command to find out which process is currently assigned to which CPU core. Lookout for the PSR field in ps command output. The above command output indicates that the process with PID 24868 (crond) is assigned to CPU core 2.

How many cores is my Python using?

We can get the count of the number of logical CPU cores in the current system using the multiprocessing. cpu_count() function. It returns a positive integer.


1 Answers

There is no way to know how many cores a application uses. But you can guess it by the number of threads it has.

For windows:

You're going to want to use the Tool Help Library as microsoft calls it. More specifically you're going to want to take a look at the Traversing the Thread List example which can get you the number of threads a application has.

Microsoft really loves to make their examples as ugly as one could possibly make them, so heres a prettified version I came up with, you feed it a PID and it lists all the threads associated with it:

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <cstdio>

bool list(unsigned int PID);

int main(void)
{
    list(5532);
    list(GetCurrentProcessId());

    return 0;
}

bool list(unsigned int PID)
{
    HANDLE thread_snap = INVALID_HANDLE_VALUE;
    THREADENTRY32 te32;

    // Take a snapshot of all running threads
    thread_snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (thread_snap == INVALID_HANDLE_VALUE) return false;

    // Fill in the size of the structure before using it.
    te32.dwSize = (DWORD)sizeof(THREADENTRY32);

    // Retrieve information about the first thread, and exit if unsuccessful
    if (!Thread32First(thread_snap, &te32))
    {
        CloseHandle(thread_snap);
        return false;
    }

    // Now walk the thread list of the system, and display information about each thread associated with the specified process
    printf("Printing threads for PID %u\n", PID); 
    do
    {
        if (te32.th32OwnerProcessID == PID)
        {
            printf( "THREAD ID = 0x%08X with base priority %u and delta priority %u\n", (unsigned int)te32.th32ThreadID, (unsigned int)te32.tpBasePri, (unsigned int)te32.tpDeltaPri);
        }
    }
    while (Thread32Next(thread_snap, &te32));
    printf("Done printing threads for PID %u\n\n", PID);

    //  Don't forget to clean up the snapshot object.
    CloseHandle(thread_snap);

    return true;
}

Input:

5532 (steam's service process ID for me), GetCurrentProcessId()

Output:

Printing threads for PID 5532
THREAD ID = 0x00000BCC with base priority 8 and delta priority 0
THREAD ID = 0x0000041C with base priority 8 and delta priority 0
THREAD ID = 0x00001924 with base priority 8 and delta priority 0
THREAD ID = 0x00000C9C with base priority 8 and delta priority 0
Done printing threads for PID 5532

Printing threads for PID 9836
THREAD ID = 0x000000FC with base priority 8 and delta priority 0
Done printing threads for PID 9836

You can assume that if a application uses more threads than the number of cores the cpu has it probably uses all of them and if it uses less, it probably uses x number of cores, where x is the number of threads.

If you want to go even further, you can get the CPU usage for each thread to make a better approximation of how many cores it uses.


Another approach that I'm not entirely sure would work is to take the CPU usage of all the threads of a application and add them up (in percentage), take the number of cores the system has, raise that number to the power of -1 and multiply it with 100 (x^-1*100) where x is the number of cores, and then divide the percentage of CPU usage of all threads to the percentage of how much a core can handle to approximate how many cores it uses.

For example:

Given 4 cores and a application with 4 threads, 2 of each are at 25% CPU usage and other 2 at 11% each.

You could assume that it uses:

(25+25+11+11)/((4^-1)*100) = 2.88 cores

The problem:

Its possible that not all cores are clocked at same speed. In which case its not going to work as intended.


If you're using c++11 you can find out the number of cores the system has with std::thread::hardware_concurrency().

Alternatively you can also traverse the process list and get the number of threads a process has from there, but it doesn't have advanced info about each thread like traversing the threads has.

like image 80
Edward A Avatar answered Sep 28 '22 09:09

Edward A