0

I have to make a specific core busy wait.

For example, there are 4 cores(core_1, core_2, core_3, core_4) in the cpu, and I need to make the core_2 busy wait for t nanosecond, meanwhile the other cores still process their tasks without busy wait.

So, is there some way to achieve this?

The cpu's model name is Intel(R) Xeon(R) CPU E5-2650 v3 @ 2.30GHz

RuDevel
  • 694
  • 3
  • 14
Aries_Liu
  • 95
  • 1
  • 10
  • Depending on your OS you should be able to use [SetAffinity()](http://stackoverflow.com/questions/16034448/obtaining-thread-core-affinity-in-c-11-through-pthreads). – RuDevel Mar 01 '17 at 14:28
  • I can't figure out the SetAffinity(). So can u give me more details? And i'm using linux and the kernel is 3.10.0-327.el7.x86_64. – Aries_Liu Mar 02 '17 at 03:58
  • Something like if (this_cpu_id == 2) ndelay(t); ? – 0andriy Mar 02 '17 at 06:42
  • Why do you need one core to busy-wait? – user253751 Mar 02 '17 at 23:24
  • Because I need to do some simulation, and in some case I have to make the core busy-wait to **extend the execution time** of all processes on this core – Aries_Liu Mar 03 '17 at 07:04
  • yeah, it is just something like udelay(). – Aries_Liu Mar 03 '17 at 07:20
  • Sounds like the task is to put a brake on one core and thus influence all processes running that core. Exactly what the os/scheduler will try to avoid by e.g. merging the affected processes to some other core. IMO you are targeting the scheduler directly(?) – RuDevel Mar 03 '17 at 13:00

1 Answers1

1

This code will bind your current thread to the specific core. This is until the thread ends or you call something similar.

This is only if your OS allows that

It works here using 4.9.11-1-ARCH with a Intel(R) Core(TM) i5-3320M CPU

See also pthread_setaffinity_np

#include <pthread.h>
#include <iostream>

#define handle_error(msg) \
               do { std::cerr << msg << std::endl; exit(-1); } while (0)

void bindToSingleCore(int core)
{
  pthread_t thread = pthread_self();
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
  CPU_SET(core, &cpuset);

  //bind this thread to one core
  int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  if (s != 0)
  {
    handle_error("Cannot write cpuset");
  }
}

int main()
{
  bindToSingleCore(1); //replace with your core 
                       //(0==first core, 1==second, ...)

  unsigned int j;
  for(unsigned int i=0; i<-1; i++)
  {
    j+=i;
    //usleep(100); //when uncommented core is not at 100% anymore..
  }
}

Compile and run with something like this:

g++ prog.cpp -pthread && ./a.out

Open your favorite process- monitor and watch your core. It should be at 100% load.

RuDevel
  • 694
  • 3
  • 14
  • In my eyes, **pthread_setaffinity_np** is used to bind a thread to "specific cores", but what I need is to make these cores busy-wait rather than just sleep a thread or a process. – Aries_Liu Mar 03 '17 at 07:25
  • Your question suggests you want to busy wait with one thread, on one specific core. The above code provides everything for that, except the implementation of your - not further disclosed - *busy waiting*. Usually your [busy waiting](https://en.wikipedia.org/wiki/Busy_waiting) would be a loop checking for a value change - or a specific time to elapse. If this is **not what you need** I strongly recommend more explanations on what you are trying to achieve *within your question* (not as a comment). – RuDevel Mar 03 '17 at 12:42
  • I just want to make these processes slower, but, in fact, it's the same thing. And THX very much for what U have done. – Aries_Liu Mar 04 '17 at 13:11