CPU


C/C++: Set Affinity to process thread – Example Code 3

The following code sets the affinity of the process thread to a specific CPU core.
In this example, we define the CPU core id using the variable core_id.

Full source code available here [download id=”2363″]


#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <errno.h>
#include <unistd.h>

// The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong.
#define print_error_then_terminate(en, msg) \
  do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)


int main(int argc, char *argv[]) {

  // We want to camp on the 2nd CPU. The ID of that core is #1.
  const int core_id = 1;
  const pid_t pid = getpid();

  // cpu_set_t: This data set is a bitset where each bit represents a CPU.
  cpu_set_t cpuset;
  // CPU_ZERO: This macro initializes the CPU set set to be the empty set.
  CPU_ZERO(&cpuset);
  // CPU_SET: This macro adds cpu to the CPU set set.
  CPU_SET(core_id, &cpuset);

  // sched_setaffinity: This function installs the cpusetsize bytes long affinity mask pointed to by cpuset for the process or thread with the ID pid. If successful the function returns zero and the scheduler will in future take the affinity information into account. 
  const int set_result = sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset);
  if (set_result != 0) {

    print_error_then_terminate(set_result, "sched_setaffinity");
  }

  // Check what is the actual affinity mask that was assigned to the thread.
  // sched_getaffinity: This functions stores the CPU affinity mask for the process or thread with the ID pid in the cpusetsize bytes long bitmap pointed to by cpuset. If successful, the function always initializes all bits in the cpu_set_t object and returns zero.
  const int get_affinity = sched_getaffinity(pid, sizeof(cpu_set_t), &cpuset);
  if (get_affinity != 0) {

    print_error_then_terminate(get_affinity, "sched_getaffinity");
  }

  // CPU_ISSET: This macro returns a nonzero value (true) if cpu is a member of the CPU set set, and zero (false) otherwise. 
  if (CPU_ISSET(core_id, &cpuset)) {

    fprintf(stdout, "Successfully set thread %d to affinity to CPU %d\n", pid, core_id);
  } else {

    fprintf(stderr, "Failed to set thread %d to affinity to CPU %d\n", pid, core_id);
  }

  return 0;
}

To compile we used the following command

gcc -Wall affinity.c -o affinity;

Full source code available here [download id=”2363″]

For a full pthread example please visit this link.


C/C++: Set Affinity to threads Example Code 1

The following code sets the affinity of each pthread to a different and specific CPU core.

The selection is made with the variable speid (that is user defined) and contains a number from 0 to (CPU NUMBER – 1).

int s, j;
cpu_set_t cpuset;
pthread_t thread;

thread = pthread_self();

/* Set affinity mask to include CPUs 0 to 7 */

CPU_ZERO(&cpuset);
CPU_SET(speid, &cpuset);

s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) {
    handle_error_en(s, "pthread_setaffinity_np");
}

/* Check the actual affinity mask assigned to the thread */
s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) {
    handle_error_en(s, "pthread_getaffinity_np");
}

printf("Set returned by pthread_getaffinity_np() contained:\n");
for (j = 0; j < CPU_SETSIZE; j++) {
    if (CPU_ISSET(j, &cpuset)) {
        fprintf(stderr,"%d CPU %d\n",speid, j);
    }
}

This code also verifies that the affinity set was successful.

Please note that you can use CPU_SET(core_id, &cpuset); multiple times, with different values for the variable core_id. This way you instruct the OS that it can move your thread to any of those available cores for execution and not limit it to just one.

Full example

Below you will find a full working example. This code will create 4 pthreads, assign each of them to a different CPU core, test that the affiliation was successful and then wait for all the threads to terminate and return their output in the form of a string.

Full source code available here [download id=”2370″]

#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

// The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong.
#define print_error_then_terminate(en, msg) \
  do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define print_perror_then_terminate(msg) \
  do { perror(msg); exit(EXIT_FAILURE); } while (0)

  struct thread_info {

    pthread_t thread_id; // ID returned by pthread_create()
    int core_id; // Core ID we want this pthread to set its affinity to
  };

#define SUCCESS_MSG "Successfully set thread %lu to affinity to CPU %d\n"
#define FAILURE_MSG "Failed to set thread %lu to affinity to CPU %d\n"


void * thread_camper(void *arg) {

  struct thread_info *thread_info = arg;

  const pthread_t pid = pthread_self();
  const int core_id = thread_info->core_id;

  // cpu_set_t: This data set is a bitset where each bit represents a CPU.
  cpu_set_t cpuset;
  // CPU_ZERO: This macro initializes the CPU set set to be the empty set.
  CPU_ZERO(&cpuset);
  // CPU_SET: This macro adds cpu to the CPU set set.
  CPU_SET(core_id, &cpuset);

  // pthread_setaffinity_np: The pthread_setaffinity_np() function sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. If the call is successful, and the thread is not currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.
  const int set_result = pthread_setaffinity_np(pid, sizeof(cpu_set_t), &cpuset);
  if (set_result != 0) {

    print_error_then_terminate(set_result, "pthread_setaffinity_np");
  }

  // Check what is the actual affinity mask that was assigned to the thread.
  // pthread_getaffinity_np: The pthread_getaffinity_np() function returns the CPU affinity mask of the thread thread in the buffer pointed to by cpuset.
  const int get_affinity = pthread_getaffinity_np(pid, sizeof(cpu_set_t), &cpuset);
  if (get_affinity != 0) {

    print_error_then_terminate(get_affinity, "pthread_getaffinity_np");
  }

  char *buffer;
  // CPU_ISSET: This macro returns a nonzero value (true) if cpu is a member of the CPU set set, and zero (false) otherwise. 
  if (CPU_ISSET(core_id, &cpuset)) {

    const size_t needed = snprintf(NULL, 0, SUCCESS_MSG, pid, core_id);
    buffer = malloc(needed);
    snprintf(buffer, needed, SUCCESS_MSG, pid, core_id);
  } else {

    const size_t needed = snprintf(NULL, 0, FAILURE_MSG, pid, core_id);
    buffer = malloc(needed);
    snprintf(buffer, needed, FAILURE_MSG, pid, core_id);
  }

  return buffer;
}

int main(int argc, char *argv[]) {

  // Initialize thread creation attributes
  pthread_attr_t attr;
  const int attr_init_result = pthread_attr_init(&attr);
  if (attr_init_result != 0) {
      print_error_then_terminate(attr_init_result, "pthread_attr_init");
  }

  // We will set the stack size limit to is 1 MB (0x100000 bytes)
  const int stack_size = 0x100000;
  const int setstacksize_result = pthread_attr_setstacksize(&attr, stack_size);
  if (setstacksize_result != 0) {
    print_error_then_terminate(setstacksize_result, "pthread_attr_setstacksize");
  }

  const int num_threads = 4;
  // Allocate memory for pthread_create() arguments
  struct thread_info *thread_info = calloc(num_threads, sizeof(struct thread_info));
  if (thread_info == NULL) {
      print_perror_then_terminate("calloc");
  }


  // Create the threads and initialize the core_id argument, which will be used to set the thread to the specific CPU core.
  // For example, we want the first pthread to camp on the first CPU core which has the ID 0. So we pass the value 0 to its core_id.
  int tnum;
  for (tnum = 0; tnum < num_threads; tnum++) {

    thread_info[tnum].core_id = tnum;
    // The pthread_create() call stores the thread ID into corresponding element of thread_info[]
    const int create_result = pthread_create(&thread_info[tnum].thread_id, &attr, &thread_camper, &thread_info[tnum]);
    if (create_result != 0) {
      print_error_then_terminate(create_result, "pthread_create");
    }
  }

  // Destroy the thread attributes object, since it is no longer needed
  const int destroy_result = pthread_attr_destroy(&attr);
  if (destroy_result != 0) {
   
   print_error_then_terminate(destroy_result, "pthread_attr_destroy");
  }

  // Now join with each thread, and display its returned value
  for (tnum = 0; tnum < num_threads; tnum++) {
    void *res;
    const int join_result = pthread_join(thread_info[tnum].thread_id, &res);
    if (join_result != 0) {
      print_error_then_terminate(join_result, "pthread_join");
    }

    printf("Joined with thread %d; returned value was %s\n", thread_info[tnum].core_id, (char *) res);
    free(res); // Free memory allocated by thread
  }

  free(thread_info);
  return 0;
}

To compile we used the following command


gcc -Wall -pthread  affinity_pthread.c -o  affinity_pthread;

Full source code available here [download id=”2370″]

For a full example that sets affinity for single threaded applications please visit this link.


Ubuntu Linux: Enable CPU Frequency Scaling / Change CPU Frequency 1

First: Install gnome-applets in order to get cpufreq-selector application.

sudo apt-get install gnome-applets

*NOTES: gnome-applets installs a LOT of stuff and will take more than 300MB of free space.

 

Second: Check if your CPU supports frequency scaling

sudo cpufreq-selector

*NOTES: It will take a few seconds or more to finish. If it doesn’t return any output proceed to the next step. If you get this message “No cpufreq support” then do not follow the next steps as you will not find neither the files mentioned below nor work out this way for you.

 

Three: Find out the available frequencies that your system can scale to and the available governors (modes of operation for frequency scaling).

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

*NOTES: You will get output that resembles these: 3000000 2300000 1800000 800000 and conservative ondemand userspace powersave performance.

 

Four and final step: Configure your system one core at a time.

sudo cpufreq-selector -c CORE_NUMBER -f FREQUENCY_IN_MHz -g GOVERNOR_MODE

*NOTES: CORE_NUMBER: An integer value that defines the core you want to edit. FREQUENCY_IN_MHz: A frequency from the scaling_available_frequencies file. GOVERNOR_MODE: A mode name from the scaling_available_governors file.