手机版
你好,游客 登录 注册
背景:
阅读新闻

硬实时Linux(RT-Preempt Patch)在PC上的编译、使用和测试

[日期:2012-10-04] 来源:Linux社区  作者:宋宝华Barry [字体:大 中 小]

RT-Preempt Patch试用
运行同样的测试cyclictest benchmark工具,结果迥异:

barry@barry-VirtualBox:~$ sudo cyclictest -p 80 -t5 -n
WARNING: Most functions require kernel 2.6
policy: fifo: loadavg: 0.71 0.42 0.17 1/289 1926         

T: 0 ( 1921) P:80 I:1000 C:  7294 Min:      7 Act:  89 Avg:  197 Max:    3177
T: 1 ( 1922) P:79 I:1500 C:  4863 Min:    10 Act:  85 Avg:  186 Max:    2681
T: 2 ( 1923) P:78 I:2000 C:  3647 Min:    15 Act:  93 Avg:  160 Max:    2504
T: 3 ( 1924) P:77 I:2500 C:  2918 Min:    23 Act:  67 Avg:  171 Max:    2114
T: 4 ( 1925) P:76 I:3000 C:  2432 Min:    19 Act:  134 Avg:  339 Max:    3129我们还是运行这个测试,但是在运行这个测试的过程中引入更多干扰,如mount /dev/sdb1 ~/development,则结果变为:

barry@barry-VirtualBox:~$ sudo cyclictest -p 80 -t5 -n
# /dev/cpu_dma_latency set to 0us
policy: fifo: loadavg: 0.11 0.12 0.13 1/263 2860         

T: 0 ( 2843) P:80 I:1000 C:  28135 Min:      5 Act:  198 Avg:  200 Max:    7387
T: 1 ( 2844) P:80 I:1500 C:  18756 Min:    22 Act:  169 Avg:  188 Max:    6875
T: 2 ( 2845) P:80 I:2000 C:  14067 Min:      7 Act:  91 Avg:  149 Max:    7288
T: 3 ( 2846) P:80 I:2500 C:  11254 Min:    19 Act:  131 Avg:  155 Max:    6287
T: 4 ( 2847) P:80 I:3000 C:  9378 Min:    25 Act:  58 Avg:  172 Max:    6121时间在可预期的范围内,没有出现标准kernel里面jitter达到331482的情况。

我们在这个kernel里面运行ps aux命令,可以看出线程化了的irq:

USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root        1  0.8  0.1  2880  1788 ?        Ss  18:39  0:03 init
root        2  0.0  0.0      0    0 ?        S    18:39  0:00 kthreadd
...

root        45  0.0  0.0      0    0 ?        S    18:39  0:00 irq/14-ata_piix
root        46  0.0  0.0      0    0 ?        S    18:39  0:00 irq/15-ata_piix
root        50  0.0  0.0      0    0 ?        S    18:39  0:00 irq/19-ehci_hcd
root        51  0.0  0.0      0    0 ?        S    18:39  0:00 irq/22-ohci_hcd
root        55  0.0  0.0      0    0 ?        S    18:39  0:00 irq/12-i8042
root        56  0.0  0.0      0    0 ?        S    18:39  0:00 irq/1-i8042
root        57  0.0  0.0      0    0 ?        S    18:39  0:00 irq/8-rtc0
root      863  0.0  0.0      0    0 ?        S    18:39  0:00 irq/19-eth0
root      864  0.0  0.0      0    0 ?        S    18:39  0:00 irq/16-eth1
root      1002  0.5  0.0      0    0 ?        S    18:39  0:01 irq/21-snd_inte
...在其中编写一个RT 线程的应用程序,通常需要如下步骤:

Setting a real time scheduling policy and priority.
Locking memory so that page faults caused by virtual memory will not undermine deterministic behavior
Pre-faulting the stack, so that a future stack fault will not undermine deterministic behavior
例 子test_rt.c,其中的mlockall是为了防止进程的虚拟地址空间对应的物理页面被swap出去,而stack_prefault()则故意提 前导致stack往下增长8KB,因此其后的函数调用和局部变量的使用将不再导致栈增长(依赖于page fault和内存申请):

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sched.h>
#include <sys/mman.h>
#include <string.h>

#define MY_PRIORITY (49) /* we use 49 as the PRREMPT_RT use 50
                            as the priority of kernel tasklets
                            and interrupt handler by default */

#define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
                                  guaranteed safe to access without
                                  faulting */

#define NSEC_PER_SEC    (1000000000) /* The number of nsecs per sec. */

void stack_prefault(void) {

        unsigned char dummy[MAX_SAFE_STACK];

        memset(dummy, 0, MAX_SAFE_STACK);
        return;
}

int main(int argc, char* argv[])
{
        struct timespec t;
        struct sched_param param;
        int interval = 50000; /* 50us*/

        /* Declare ourself as a real time task */

        param.sched_priority = MY_PRIORITY;
        if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
                perror("sched_setscheduler failed");
                exit(-1);
        }

        /* Lock memory */

        if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
                perror("mlockall failed");
                exit(-2);
        }

        /* Pre-fault our stack */

        stack_prefault();

        clock_gettime(CLOCK_MONOTONIC ,&t);
        /* start after one second */
        t.tv_sec++;

        while(1) {
                /* wait until next shot */
                clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);

                /* do the stuff */

                /* calculate next shot */
                t.tv_nsec += interval;

                while (t.tv_nsec >= NSEC_PER_SEC) {
                      t.tv_nsec -= NSEC_PER_SEC;
                        t.tv_sec++;
                }
  }
}

编译之:gcc -o test_rt test_rt.c -lrt。本节就到这里,后续我们会有一系列博文来描述RT-Preempt Patch对kernel的主要改动,以及其工作原理。

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款