get_cpu_nums()) { printf("%s:%d nums must le_linux 命令 设置线程亲">
当前位置:   article > 正文

linux线程设置cpu亲和性与调度方式及优先级_linux 命令 设置线程亲核

linux 命令 设置线程亲核

cpus_set.c


#include "cpus_set.h"

int get_cpu_nums()
{
    return sysconf(_SC_NPROCESSORS_CONF);
}
int set_cpus_mask(int* cpus_arr, int nums, cpu_set_t *mask)
{
    CPU_ZERO(mask);
    if (nums > get_cpu_nums())
    {
        printf("%s:%d nums must less then cpu nums total\n", __FUNCTION__, __LINE__);
        return -1;
    }

    for (int i = 0; i < nums; i++)
    {
        if (cpus_arr[i] > (get_cpu_nums() - 1) || cpus_arr[i] < 0)
        {
            printf("cpus_arr[%d]: %d\n",i,cpus_arr[i]);
            printf("%s:%d cpus_arr error\n", __FUNCTION__, __LINE__);
            return -1;
        }
        CPU_SET(cpus_arr[i], mask); //将CPU号加入到mask集合
    }
    //printf("set_cpus_mask success\n");
    return 0;
}
int del_cpu_mask(int cpu_num, cpu_set_t *mask)
{
    if (!CPU_ISSET(cpu_num, mask))
    {
        printf("%s:%d cpu_num does not exist in mask\n", __FUNCTION__, __LINE__);
        return -1;
    }
    CPU_CLR(cpu_num, mask);
    return 0;
}
int bind_pthread_cpu_cores(cpu_set_t *mask, pthread_t pid)
{
    int ret = pthread_setaffinity_np(pid, sizeof(cpu_set_t), mask); /* 设置cpu 亲和性(affinity)*/
    if (ret < 0)
    {
        printf("%s:%d:", __FUNCTION__, __LINE__);
        perror("faild sched_setaffinity");
        return -1;
    }
    //printf("bind_cpu_cores success\n");
    return 0;
}
int bind_fork_cpu_cores(cpu_set_t *mask)
{

    int ret = sched_setaffinity(0, sizeof(cpu_set_t), mask);
    if (ret < 0)
    {
        printf("%s:%d:", __FUNCTION__, __LINE__);
        perror("faild sched_setaffinity");
        return -1;
    }
    //printf("bind_cpu_cores success\n");
    return 0;
}
void cpu_get(char* thread_name)
{
     usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("%s [%ld] is running in processor:",thread_name, pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
}
int create_pthread(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void *), void *arg, int* cpus_arr,int nums)
{
    int ret;
    cpu_set_t mask;
    ret = set_cpus_mask(cpus_arr, nums, &mask);
    if (ret < 0)
    {
        printf("set_cpus_mask failed\n");
        return -1;
    }
    ret = pthread_create(thread, NULL, start_routine, arg);
    if (ret < 0)
    {
        printf("pthread_create failed\n");
        return -1;
    }
    ret = bind_pthread_cpu_cores(&mask, *thread);
    if (ret < 0)
    {
        printf("bind_pthread_cpu_cores failed\n");
        return -1;
    }
    
    usleep(100000);
}

int pthread_attribute_init(pthread_attr_t *attr){
    return pthread_attr_init(attr);
}
int pthread_attribute_destory(pthread_attr_t *attr){
    return pthread_attr_destroy(&attr);
}
int pthread_set_inheritsched(pthread_attr_t *attr,int __inherit){
    return pthread_attr_setinheritsched(attr, __inherit);
}
int pthread_set_schedpolicy(pthread_attr_t *attr,int policy){
    pthread_attr_setschedpolicy(attr, policy);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126

cpus_set.h


#ifndef __CPUS_SET_H__
#define __CPUS_SET_H__

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

/*
    return : Number of CPU cores
*/
extern int get_cpu_num();
/*
    
*/
extern int set_cpus_mask(int *cpus_arr, int nums,cpu_set_t* mask);

extern int del_cpu_mask(int cpu_num,cpu_set_t* mask);

extern int bind_cpu_cores(cpu_set_t* mask,pthread_t pid);

extern int create_pthread(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void *), void *arg, int *cpus_arr,int nunms);

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

main

#define _GNU_SOURCE

#include "cpus_set.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>
void printf_str(char *str)
{
    printf("%s", str);
}
void *thread_run1()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run1[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run1 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run1 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run2()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run2[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run2 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run2 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run3()
{
   
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run3 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run3 is running!\n");
    }
    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    pthread_t th1, th2, th3;

    pthread_attr_t attr;
    struct sched_param sched;
    int ret;
    int cpu_arr1[1] = {0};
    cpu_set_t mask1;
    sched.__sched_priority = 99;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th1, &attr, thread_run1, NULL, cpu_arr1,sizeof(cpu_arr1)/sizeof(cpu_arr1[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread1 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr2[1] = {0};
    cpu_set_t mask2;
    sched.__sched_priority = 50;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th2, &attr, thread_run2, NULL, cpu_arr2,sizeof(cpu_arr2)/sizeof(cpu_arr2[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread2 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr3[1] = {0};
    cpu_set_t mask3;
    sched.__sched_priority = 1;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th3, &attr, thread_run3, NULL, cpu_arr3,sizeof(cpu_arr3)/sizeof(cpu_arr3[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread3 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    pthread_join(th1, NULL);
    pthread_join(th2, NULL);
    pthread_join(th3, NULL);

    return 0;
}
#if 0
#define _GNU_SOURCE

#include "cpus_set.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>
void printf_str(char *str)
{
    printf("%s", str);
}
void *thread_run1()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run1[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run1 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run1 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run2()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run2[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run2 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run2 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run3()
{
   
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run3 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run3 is running!\n");
    }
    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    pthread_t th1, th2, th3;

    pthread_attr_t attr;
    struct sched_param sched;
    int ret;
    int cpu_arr1[1] = {0};
    cpu_set_t mask1;
    sched.__sched_priority = 99;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th1, &attr, thread_run1, NULL, cpu_arr1,sizeof(cpu_arr1)/sizeof(cpu_arr1[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread1 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr2[1] = {0};
    cpu_set_t mask2;
    sched.__sched_priority = 50;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th2, &attr, thread_run2, NULL, cpu_arr2,sizeof(cpu_arr2)/sizeof(cpu_arr2[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread2 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr3[1] = {0};
    cpu_set_t mask3;
    sched.__sched_priority = 1;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th3, &attr, thread_run3, NULL, cpu_arr3,sizeof(cpu_arr3)/sizeof(cpu_arr3[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread3 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    pthread_join(th1, NULL);
    pthread_join(th2, NULL);
    pthread_join(th3, NULL);

    return 0;
}

#endif 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/151120
推荐阅读
相关标签
  

闽ICP备14008679号