6

I working on a project that measure the effect of some code pattern on the CPU. To do this I need to run my process on the CPU and stop all other processes on the CPU to see the real effect of my process.

Also I need to run my process on 1 core of the CPU. Can anyone help how to do this in C++?

nbro
  • 15,395
  • 32
  • 113
  • 196
Naif
  • 61
  • 1
  • 3
  • 2
    You can test your program on a system without multithreading (like DOS). – Paweł Stawarz Nov 29 '13 at 15:44
  • possible duplicate of [how to set CPU affinity of a particular pthread?](http://stackoverflow.com/questions/1407786/how-to-set-cpu-affinity-of-a-particular-pthread) – noob Nov 29 '13 at 15:47
  • Can you not just derive your results statistically? I don't see much point in testing the effect of code patterns in unrepresentative situations. – Martin James Nov 29 '13 at 16:02
  • Process / thread / task scheduling is an Operating System concept, you will not find anything this low-level in C++ itself. You may have access to the necessary platform API to influence scheduling from C++ , but this will be platform specific rather than language specific. – Andon M. Coleman Nov 29 '13 at 20:30
  • Ok , so how to minimize the effect of other processes on my procces – Naif Nov 30 '13 at 01:20

4 Answers4

1

You can set processor affinity for the process. When you do that your process will only run in that CPU. So you can measure the performance of your process. This is how i have done for a service in VC++. Hope this is helpful.

SYSTEM_INFO SystemInfo; 
    GetSystemInfo(&SystemInfo);
    HANDLE hProcess = GetCurrentProcess();
    if(SystemInfo.dwNumberOfProcessors >1)
    {
        //DWORD dwProcessAffinityMask, dwSystemAffinityMask;
        //GetProcessAffinityMask( hProcess, &dwProcessAffinityMask, &dwSystemAffinityMask );
        //SetProcessAffinityMask( hProcess, 1L );// use CPU 0 only
        //SetProcessAffinityMask( hProcess, 2L );// use CPU 1 only
        //SetProcessAffinityMask( hProcess, 3L );// allow running on both CPUs

        SetProcessAffinityMask( hProcess, 2L );// use CPU 1 only

    }
ckv
  • 10,539
  • 20
  • 100
  • 144
1

The C++ standard does not allow you to set a thread's affinity. You will have to use boost or your system's API for that.

NOTE: While explicit affinity management can result in great rewards, it also bears risk. It might result in disabling of some optimizations implemented by the system's own scheduler, which is especially bad when the processes you control with this method are not the heaviest in your system. Also, affinity management can quickly go wrong: Imagine you accidentally load 90% of your load onto just one or two cores on your (today's common) octa-core system. Affinity management will eventually need an AI to work well in the ever-more complex systems we are facing, but simple rule-based management can work well if you can accurately predict the process work load.

EDIT: In order to prevent your OS from scheduling anything else on that core, you will need another OS function. As mentioned in the comments, isolcpus lets you achieve that on Linux.

Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
1

ckv's answer has a winapi example. Here's a linux example:

#include <sched.h>

cpu_set_t  mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
CPU_SET(2, &mask);
result = sched_setaffinity(0, sizeof(mask), &mask);
Community
  • 1
  • 1
0

To really stop all other code running on the CPU, you would probably have to write your code as an operating system driver, or run with no OS at all. If the occasional minor interruption is okay, you can set your process priority to a very high priority. This will minimize the amount of time other processes will "steal" from your code.

Which operating system are you using?

avl_sweden
  • 613
  • 4
  • 14