1

I would like to programmatically isolate my process to run on a core different from everything else (including the OS), to reduce context switching. I found this similar question:

Whole one core dedicated to single process

And the accepted answer dated 2011:

https://stackoverflow.com/a/13585364/997112

mentions:

The alternative method is to use cpusets which is way more elegant and dynamic but suffers from some weaknesses at this point in time (no migration of timers for example) which makes me recommend the old, crude but effective isolcpus parameter.

Note that work is currently being done by the Linux community to address all these issues and more to give even better isolation.

Has there been any update? Is it possible to emulate isolcpus from a Linux C API function?

My C++ environment is CentOS 7 and GCC 5.2

Cœur
  • 37,241
  • 25
  • 195
  • 267
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 3
    This is something that's implementation specific. There's nothing in the C++ standard for this. Therefore, at the very least, you need to specify which C++ implementation you are using. – Sam Varshavchik Jan 21 '19 at 16:57
  • @SamVarshavchik Not sure what you mean by which C++ implementation? I assumed there would be Linux C API calls. Did you mean which version of Linux? – user997112 Jan 21 '19 at 17:01
  • 1
    @user997112 Sam's comment is about how the language C++ makes no mention of this so there is no generic portable answer that will be true for all C++ developers. A C++ implementation is a specific implementation of the language, a set of tools (compiler, linker, standard library implementation, etc.) and an environment that allows a developer to convert C++ code into an executable form. You need to specify what implementation you are using for a reasonable answer (ex. gcc version x.y on Linux distro such-and-such). In this case, the platform you are using is very important. – François Andrieux Jan 21 '19 at 17:02
  • I have just added my C++ environment info to the question. Is there anything else you need to know? – user997112 Jan 21 '19 at 17:05
  • For Linux, the appropriate system call [appears to be sched_setaffinity(2)](http://manpages.courier-mta.org/htmlman2/sched_setaffinity.2.html). I don't have specific knowledge of it, to write a complete answer, but that's where you want to look further. – Sam Varshavchik Jan 21 '19 at 17:11
  • @SamVarshavchik thanks but I think that only solves half the problem (the easier half). The part which is trickier is scheduling all other processes to not run on your core (what isolcpus does). – user997112 Jan 21 '19 at 17:19
  • 1
    Well, then you have the answer: use usolcpus boot option, and use the reserved CPUs for your own threads. As the man page explains, in the "NOTES" section, there is no other option: either use isolcpus, or manually isolate every process on the system. – Sam Varshavchik Jan 21 '19 at 17:25

2 Answers2

3

What you're trying to achieve is known as CPU affinity. This is usually a task done at sysadmin stage and not at programming, in an enterprise environment a programme will deliver binaries and the sysadmin group may or may not decide to give certain binaries a certain affinity.

Usually this is done with the taskset command, see Realtime Reference Guide: Affinity.

This command will run my-c-app on core 4 exclusively

taskset -c 4 /bin/my-c-app

If you want to do it inside the code of your C program regardless of what I mentioned, see Section 6.2. Using the sched_getaffinity() system call to set processor affinity and also this answer to the thread How to use sched_getaffinity and sched_setaffinity in Linux from C?.

A very short example:

cpu_set_t my_set;        /* Define your cpu_set bit mask. */
CPU_ZERO(&my_set);       /* Initialize it all to 0, i.e. no CPUs selected. */
CPU_SET(7, &my_set);     /* set the bit that represents core 7. */
sched_setaffinity(0, sizeof(cpu_set_t), &my_set); /* Set affinity of tihs process to */
                                                  /* the defined mask, i.e. only 7. */
brunorey
  • 2,135
  • 1
  • 18
  • 26
  • Hi, the question is more about preventing other processes running on the core I have reserved. – user997112 Jan 22 '19 at 13:14
  • 1
    @user997112 There's not really any nice option for doing so except iterate through all existing processes and change their affinity using sched_setaffinity() to be anything except the core you want to run on. (This applies to user space proceses only, there is no way to prevent the kernel from utilizing a core) – nos Jan 29 '20 at 15:42
0

Using the linux API sched_setaffinity() to set the calling processes's processor affinity on a SMP system would partially achieve your requirement.

To not allow any other processes from being scheduled on the reserved CPU, you need to achieve it programmatically by using lock-less and no-sleep implementations. This totally blocks any other process/thread from being context-switched on the reserved CPU.