sched-deadline调度器设置deadline/period小于100us报错原因咨询
问题分析与解决
你遇到的EINVAL错误主要来自两个关键原因:
1. SCHED_DEADLINE核心约束被违反
SCHED_DEADLINE调度器要求参数必须满足严格的大小关系:*sched_runtime ≤ sched_deadline ≤ sched_period*
你的代码中:
attr.sched_deadline = 99*1000 = 99000nsattr.sched_runtime = execution_time + 1024
如果你的execution_time是99000ns(对应注释里的99us),那么attr.sched_runtime会等于99000+1024=100024ns,明显大于deadline的99000ns,直接违反runtime ≤ deadline的约束,内核必然返回EINVAL。
2. 系统时钟粒度限制
即使调整runtime使其小于等于deadline,还要注意系统的调度时钟粒度(由内核配置或硬件决定)。很多默认配置的Linux系统,调度时钟最小粒度是100us(100000ns),当你设置小于这个值的deadline/period时,内核会自动将参数向上对齐到最近的时钟粒度值。如果对齐后的参数破坏了runtime ≤ deadline ≤ period的关系,或者部分内核版本对SCHED_DEADLINE的最小周期有更严格限制,也会触发错误。
修复建议
- 首先确保
execution_time + 1024 ≤ 99000,即execution_time ≤ 97976ns,保证runtime不超过deadline。 - 若需要99us的deadline,先检查系统时钟粒度:执行
cat /proc/timer_list查看系统时钟分辨率,确认是否支持小于100us的调度周期。如果时钟粒度确实是100us,建议将deadline/period设置为100000ns(100us)。 - 初始化
sched_attr结构体时,先用memset(&attr, 0, sizeof(attr))清零,避免未初始化字段导致意外错误。
修改后的示例代码片段:
/* 确保runtime ≤ deadline ≤ period */ struct sched_attr attr; memset(&attr, 0, sizeof(attr)); // 初始化结构体 attr.sched_policy = SCHED_DEADLINE; attr.sched_runtime = 90000; // 90us,小于99us的deadline attr.sched_period = attr.sched_deadline = 99*1000; // 99000ns ret = sched_setattr(0, &attr, 0); printf("sched_setattr ret value: %d\n", ret); if (ret < 0) { perror("sched_setattr"); exit(-1); }
内容的提问来源于stack exchange,提问作者Ehsan
相关产品推荐
相关产品推荐

