设置cpu亲和力demo

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#define __USE_GNU
#include <sched.h>
#include <ctype.h>
#include <pthread.h>
#define MAX_N1  3
#define MAX_N2  1
#define MAX_N3  0
#define MAX_N4  0
#define MAX_N5  0

#define LOOP_NUM 1000*1000*1000
#define gettid() syscall(__NR_gettid)

#define MAXEPOLLSIZE 100
int gaiefd[MAX_N1];


typedef struct thdparam {
    unsigned int uiNum;
    unsigned long ulLoop;
    unsigned int tid;
	int imalloclen;
    unsigned char szInfo[32];    
} thdparam;

long long ustime(void) {
    struct timeval tv;
    long long ust;

    gettimeofday(&tv, NULL);
    ust = ((long)tv.tv_sec)*1000000;
    ust += tv.tv_usec;
    return ust;
}




int *thread_fun1(void *arg)
{
    long long t1,t2,diff;
    struct thdparam *parg = arg;

    unsigned int uiNum = 500;//(parg->uiNum + 3)*100;
    unsigned long aulMem[10240];
	int imalloclen = parg->imalloclen;
    unsigned int uiLoop;
    unsigned long ulCount = 0;
    int i;
	void *p;
    t1 = ustime();
	
    while(1)
    {
        for (i = 0; i < 1000; i++)
		{
            ulCount ++;
            p = malloc(imalloclen);
			aulMem[i] = p;
			
			if ((ulCount&0xFFFF)==0)
            {
				
                t2 = ustime();
                diff = t2-t1;
                t1 = ustime();
                printf("tid %d malloclen %d %lu,diff %llu\n", parg->tid, imalloclen, ulCount, diff);
				ulCount = 1;
				return NULL;
            }
		}
		
		for (i = 0; i < 1000; i++)
		{
			free(aulMem[i]);
		}
		

    }
    
    return NULL;
}



void setparam(pthread_attr_t * pattr, unsigned int cpuindex)
{
    cpu_set_t cpu_info;
    pthread_attr_init(pattr);
        
    CPU_ZERO(&cpu_info);
    CPU_SET(cpuindex, &cpu_info);
    if (0!=pthread_attr_setaffinity_np(pattr, sizeof(cpu_set_t), &cpu_info))
    {
        printf("set affinity failed");
        return;
    }
}


int main(int argc, char ** argv)
{
    unsigned int i = 0;
    unsigned int cpuindex = 0;
	int imalloclen = atoi(argv[1]);
    cpu_set_t cpu_info;
    int cpu_num = (int)sysconf(_SC_NPROCESSORS_ONLN);
    printf("The number of cpu is %d\n", cpu_num);
   

    struct thdparam astharg[MAX_N1];
    pthread_t thid1[MAX_N1];
    pthread_attr_t attr1[MAX_N1];
    for (i = 0; i < MAX_N1 ; i++)
    {
        cpuindex++;
        setparam(attr1+i, cpuindex);

        astharg[i].uiNum = i;
        astharg[i].ulLoop = LOOP_NUM;
		astharg[i].tid = i;
		astharg[i].imalloclen = imalloclen;
        //sprintf(astharg[i].szInfo, "f1V1T%dL%lu\n",i,astharg[i].ulLoop/(1000*1000));
        
        (void)pthread_create(&thid1[i], attr1+i,(void *)thread_fun1,astharg + i);
    }
    

    

    while(1)
    {
        //printf("diff_main %lu %lu\n",*pulValue1, *pulValue2);
        sleep(50);
    }
    
	return 0;
}

### FreeRTOS 任务优先级设置与调度机制 #### 任务优先级的概念 在 FreeRTOS 中,任务优先级用于决定多个可运行状态的任务之间的执行顺序。较高优先级的任务总是先于较低优先级的任务获得 CPU 资源并被执行[^1]。 #### 设置任务优先级的方法 FreeRTOS 提供了灵活的任务创建接口 `xTaskCreate` 和 `xTaskCreateStatic` 来设定新任务的初始优先级。这些函数允许开发者指定一个整数值作为参数之一传递给它们,该值即代表所要创建的新线程应具有的相对重要程度或紧迫感级别。通常情况下,0 表示最低级别的活动单元而 configMAX_PRIORITIES-1 则对应着最高的那个等级[^2]。 ```c // 创建具有特定优先级的任务实例 BaseType_t xReturned; void vTaskCode( void * pvParameters ); const char *pcName = "Demo Task"; uint16_t usStackDepth = 128; // 单位为字节的数量取决于架构 static StackType_t xStack[ 128 ]; static StaticTask_t xTaskBuffer; xReturned = xTaskCreate( vTaskCode, /* 执行此任务的功能 */ pcName, /* 名称赋予给这个新的工作单位 */ usStackDepth, /* 栈大小 (以 words 计算) */ NULL, /* 参数传入到任务中去 */ tskIDLE_PRIORITY,// 此处定义了任务的重要度/紧急性水平 NULL /* 不需要返回句柄 */ ); ``` #### 同优先级下的时间片轮转策略 对于相同优先级的任务,默认采用的是时间片轮转算法(Round Robin),这意味着即使两个以上的进程共享同一层次结构,在一定条件下也会轮流得到处理器使用权从而得以继续推进各自的工作流程。 #### 多重核心特性支持下跨核迁移考量 值得注意的是,在某些版本和支持多内核硬件平台上的移植版里,还存在有关亲和力(Affinity Mask) 的选项可供调整——这使得用户能够控制哪些逻辑处理单元(CPU Cores) 上可以安排某个具体的应用层实体运行。不过这种高级特性的应用需谨慎评估其必要性和潜在影响。 #### 解决优先级反转问题 为了防止由于资源竞争引发的所谓“优先级倒置”现象(即本来应该更早完成的关键操作反而因为等待其他不那么紧要的事情结束而延迟),FreeRTOS 实现了一个叫做优先级继承(Priority Inheritance Protocol) 的解决方案。当高优先进程试图获取已被低优先生命周期占用的同步对象时,后者会被暂时提升至前者相同的权重量级直至释放锁为止,以此确保整个系统的实时响应性能不受损害[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值