Thread-bound CPU core-sched_setaffinity

Source: Internet
Author: User

CPU affinity refers to the ability to bind one or more processes to one or more processors on a Linux system. The CPU affinity mask for a process determines which CPU or CPUs the process will run on. In a multiprocessor system, a mask that sets the affinity of the CPU may achieve better performance.

A CPU affinity mask uses a cpu_set_t struct to represent a collection of CPUs, and several of the following macros operate on the mask set individually: • Cpu_zero () clears a collection ·   Cpu_set () and CPU_CLR () each add a given CPU number to a set or remove it from a collection. · Cpu_isset () checks if a CPU number is in this collection.

The following two functions are used to set the get thread CPU affinity State:
sched_setaffinity (pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)
This function sets the process to PID and lets it run on the CPU set by mask. If the PID has a value of 0, it indicates that the current process is specified. Causes the current process to run on those CPUs set by mask. The second parameter, Cpusetsize, is the length of the number specified by mask. Usually set to sizeof (cpu_set_t). If the process specified by the current PID is not running at this time on any of the CPUs specified by mask, the specified process will be migrated from the other CPU to the specified CPU on mask.
sched_getaffinity (pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)
The function obtains the CPU bit mask of the process indicated by the PID and returns the mask to the structure that the mask points to. That is, which CPUs the specified PID can currently run on. Similarly, if the PID has a value of 0. Also indicates that the current process

  1. definition of cpu_set_t
  2. # Define __cpu_setsize 1024x768
  3. # Define __ncpubits (8 * sizeof (__CPU_MASK))
  4. typedef unsigned long int __cpu_mask;
  5. # define __cpuelt (CPU) (CPU)/__ncpubits
  6. # define __cpumask (CPU) ((__cpu_mask) 1 << ((CPU)% __ncpubits))
  7. typedef struct
  8. {
  9. __cpu_mask __bits[__cpu_setsize/__ncpubits];
  10. } cpu_set_t;
  11. # define __cpu_zero (CPUSETP) \
  12. Do {\
  13. unsigned int __i; \
  14. cpu_set_t *__arr = (CPUSETP); \
  15. For (__i = 0; __i < sizeof (cpu_set_t)/sizeof (__cpu_mask); ++__i) \
  16. __arr->__bits[__i] = 0; \
  17. } while (0)
  18. # define __cpu_set (CPU, cpusetp) \
  19. ((CPUSETP)->__bits[__cpuelt (CPU)] |= __cpumask (CPU))
  20. # define __CPU_CLR (CPU, cpusetp) \
  21. ((CPUSETP)->__bits[__cpuelt (CPU)] &= ~__cpumask (CPU))
  22. # define __cpu_isset (CPU, cpusetp) \
  23. (((CPUSETP)->__bits[__cpuelt (CPU)] & __cpumask (CPU))! = 0)


Test code:

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<sys/types.h>
  4. #include<sys/sysinfo.h>
  5. #include<unistd.h>
  6. #define __USE_GNU
  7. #include<sched.h>
  8. #include<ctype.h>
  9. #include<string.h>
  10. #include<pthread.h>
  11. #Define Thread_max_num //1 CPUs up to the maximum number of processes
  12. int num=0; number of cores in//cpu
  13. void* threadfun(void* Arg) //arg pass thread designator (self-defined)
  14. {
  15. cpu_set_t Mask; the collection of//cpu nuclei
  16. cpu_set_t get; //Gets the CPU in the collection
  17. int *a = (int *) arg;
  18. printf ("The A is:%d\n", *a); //Display is the first few threads
  19. Cpu_zero (&mask); //Empty
  20. Cpu_set (*a,&mask); //Set affinity value
  21. if (sched_setaffinity (0, sizeof (mask), &mask) = = -1)//Set Thread CPU affinity
  22. {
  23. printf ("warning:could not set CPU affinity, continuing...\n");
  24. }
  25. While (1)
  26. {
  27. Cpu_zero (&get);
  28. if (sched_getaffinity (0, sizeof (GET), &get) = = -1)//Get thread CPU affinity
  29. {
  30. printf ("Warning:cound not get thread affinity, continuing...\n");
  31. }
  32. int i;
  33. For (i = 0; i < num; i++)
  34. {
  35. if (Cpu_isset (i, &get))//Determine which CPU the thread is affinity with
  36. {
  37. printf ("This thread%d is running processor:%d\n", i,i);
  38. }
  39. }
  40. }
  41. return NULL;
  42. }
  43. int main(int argc, char* argv[])
  44. {
  45. num = sysconf (_sc_nprocessors_conf); //Get the number of cores
  46. pthread_t Thread[thread_max_num];
  47. printf ("System has%i processor (s). \ n ", num);
  48. int tid[thread_max_num];
  49. int i;
  50. For (i=0;i<num;i++)
  51. {
  52. Tid[i] = i; //Each thread must have a tid[i]
  53. Pthread_create (&thread[0],null,threadfun, (void*) &tid[i]);
  54. }
  55. For (i=0; i< num; i++)
  56. {
  57. Pthread_join (Thread[i],NULL); Wait for all threads to end, thread is dead loop so Ctrl + C ends
  58. }
  59. return 0;
  60. }


Compile command: gcc bind.c-o bind-lpthread

Execution:./bind

Output result: slightly

Special attention:

#define __USE_GNU Don't write a # define _use_gnu

#include <pthread.h> must be written after the # define __USE_GNU, or the compilation will error

View your thread condition you can use top-h in another window to view the threads at execution time, and view the situation on each core using the top command and then the number "1".

Thread-bound CPU core-sched_setaffinity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.