Linux Specifies the cpu__linux of the process to run

Source: Internet
Author: User
Tags redis

If you think you know more about your process than the kernel's process scheduler, and you don't want to consume too much CPU0 and a higher cache hit, you can set up a process to run on one or some CPUs.

Redis is a single process model, in order to take full advantage of multi-core server performance, you can specify different Redis instances to run on different CPUs, which can also reduce process context switching.

There are two kinds of methods:

First, use the command taskset

In Redhat Linux, you can sudo yum privodes taskset find Taskset in which package, as follows:

UTIL-LINUX-2.23.2-26.EL7.X86_64:A collection of basic system utilities
Repo: @anaconda
Matched from:
Filename:/usr/bin/taskset
Can know is in util-linux-2.23.2-26.el7.x86_64, then sudo yum install util-linux can install.

A. Show which CPUs the process is running on

[Zhujiang@localhost ~]$ ps aux | grep Redis
Root 2884 0.1 0.2 37260 5372? SSL Mar10 3:53/usr/local/bin/redis-server *:6379
Zhujiang 17266 0.0 0.0 112648 956 pts/0 s+ 05:57 0:00 grep--color=auto Redis

[Zhujiang@localhost ~]$ taskset-p 2884
PID 2884 ' s current affinity mask:f
Display the result f indicates binary 1111, each 1 represents a CPU, the process is running on 4 CPUs, I This virtual machine is four CPU.

B. Specifies that the process is running on a CPU

[Zhujiang@localhost ~]$ sudo taskset-pc 2 2884
PID 2884 ' s current affinity list:0-3
PID 2884 ' s new affinity List:2
[Zhujiang@localhost ~]$
Note: 2 indicates that the process will only run on the 3rd CPU (counting from 0)

C. Specify CPU at process startup

[Zhujiang@localhost ~]$ taskset-c 0/usr/local/bin/redis-server


Ii. Use of sched_setaffinity

#include <stdio.h> #include <time.h> #include <stdlib.h> #define __USE_GNU #include <sched.h> in
    T main () {int cpu_id = 1;
    cpu_set_t Mask;
    Cpu_zero (&mask);

    Cpu_set (cpu_id, &mask);
        if (sched_setaffinity (0, sizeof (mask), &mask) < 0) {perror ("set affinity");
    return-1;
    } struct TIMESPEC ts;
    Clock_gettime (Clock_monotonic, &ts);
    int now = TS.TV_SEC; int end = Now + 20;
        Busy computing 10 seconds while (now < end) {Clock_gettime (clock_monotonic, &ts);
    now = ts.tv_sec;
    } cpu_zero (&mask);
    cpu_id = 3;
    
    Cpu_set (cpu_id, &mask);
        if (sched_setaffinity (0, sizeof (mask), &mask) < 0) {perror ("set affinity");
    return-1;
    } clock_gettime (Clock_monotonic, &ts);
    now = ts.tv_sec;
    End = Now + 10;
        while (now < end) {Clock_gettime (clock_monotonic, &ts);
    now = ts.tv_sec;

   } return 0;
 }



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.