关于cbs调度算法,我们上面知道了其默认基于edf,以截止时间作为优先级,每个任务都有预算,当预算完成时任务也完成了,那么更新预算,重新周期,如果任务的unblock后的剩余占比大于设置预算设置占比,则将任务推迟运行。
本文基于rtems的测试用例,简单演示一下cbs调度算法
为了支持测试,我们需要创建四个任务,这个比较常规,使用 rtems_task_create和rtems_task_start即可
在cbs中,我们需要额外调用rtems_cbs_initialize即可。那么代码如下
for ( index = 1 ; index <= NUM_TASKS ; index++ ) { status = rtems_task_create( Task_name[ index ], Priorities[ index ], RTEMS_MINIMUM_STACK_SIZE * 4, RTEMS_DEFAULT_MODES, RTEMS_DEFAULT_ATTRIBUTES, &Task_id[ index ] ); directive_failed( status, "rtems_task_create loop" ); } rtems_cbs_initialize(); for ( index = 1 ; index <= NUM_PERIODIC_TASKS ; index++ ) { status = rtems_task_start( Task_id[ index ], Tasks_Periodic, index ); directive_failed( status, "rtems_task_start loop" ); }
根据上面,任务开始时会默认调用Tasks_Periodic,我们在这里设置任务
首先,我们需要按照edf的规范,设置周期,主要调用如下
rtems_rate_monotonic_create rtems_rate_monotonic_ident rtems_rate_monotonic_period
对于cbs而言,我们需要预设deadline和budget,并且如果任务超限,也就是cpu利用率大于1时,我们可以设置一个函数回调,如下
params.deadline = Periods[ argument ]; params.budget = Execution[ argument ]+1; rtems_cbs_create_server( ¶ms, &overrun_handler_task_4, &server_id )
如果我们需要在任务运行的时候,重设cbs的预算和截止时间,可以使用如下函数
rtems_cbs_set_parameters
至此,我们的测试程序如下
rtems_task Tasks_Periodic( rtems_task_argument argument ) { rtems_id rmid; rtems_id test_rmid; rtems_status_code status; bool scenario_done = 0; int start, stop, now; rtems_cbs_server_id server_id, tsid; rtems_cbs_parameters params, tparams; params.deadline = Periods[ argument ]; params.budget = Execution[ argument ]+1; if ( argument == 4 ) { if ( rtems_cbs_create_server( ¶ms, &overrun_handler_task_4, &server_id )) printf( "ERROR: CREATE SERVER FAILED\n" ); } else { if ( rtems_cbs_create_server( ¶ms, NULL, &server_id ) ) printf( "ERROR: CREATE SERVER FAILED\n" ); } if ( rtems_cbs_attach_thread( server_id, Task_id[ argument ] ) ) printf( "ERROR: ATTACH THREAD FAILED\n" ); if ( rtems_cbs_get_server_id( Task_id[ argument ], &tsid ) ) printf( "ERROR: GET SERVER ID FAILED\n" ); if ( tsid != server_id ) printf( "ERROR: SERVER ID MISMATCH\n" ); if ( rtems_cbs_get_parameters( server_id, &tparams ) ) printf( "ERROR: GET PARAMETERS FAILED\n" ); if ( params.deadline != tparams.deadline || params.budget != tparams.budget ) printf( "ERROR: PARAMETERS MISMATCH\n" ); status = rtems_rate_monotonic_create( argument, &rmid ); directive_failed( status, "rtems_rate_monotonic_create" ); put_name( Task_name[ argument ], FALSE ); printf( "- rtems_rate_monotonic_create id = 0x%08" PRIxrtems_id "\n", rmid ); status = rtems_rate_monotonic_ident( argument, &test_rmid ); directive_failed( status, "rtems_rate_monotonic_ident" ); put_name( Task_name[ argument ], FALSE ); printf( "- rtems_rate_monotonic_ident id = 0x%08" PRIxrtems_id "\n", test_rmid ); if ( rmid != test_rmid ) { printf( "RMID's DO NOT MATCH (0x%" PRIxrtems_id " and 0x%" PRIxrtems_id ")\n", rmid, test_rmid ); rtems_test_exit( 0 ); } put_name( Task_name[ argument ], FALSE ); printf( "- (0x%08" PRIxrtems_id ") period %" PRIu32 "\n", rmid, Periods[ argument ] ); status = rtems_task_wake_after( 2 + Phases[argument] ); directive_failed( status, "rtems_task_wake_after" ); while (FOREVER) { if (rtems_rate_monotonic_period(rmid, Periods[argument])==RTEMS_TIMEOUT) printf("P%" PRIdPTR " - Deadline miss\n", argument); start = rtems_clock_get_ticks_since_boot(); printf("P%" PRIdPTR "-S ticks:%d\n", argument, start); if ( start >= 2*HP_LENGTH ) break; /* stop */ /* Specific scenario for task 4: tries to exceed announced budget, the task priority has to be pulled down to background. */ now = rtems_clock_get_ticks_since_boot(); if ( !scenario_done && argument == 4 && now >= 200 ) { Violating_task[ argument ] = 1; scenario_done = 1; } /* Specific scenario for task 3: changes scheduling parameters. */ if ( !scenario_done && argument == 3 && now >= 250 ) { Periods[ argument ] = Periods[ argument ] * 2; Execution[ argument ] = Execution[ argument ] * 2; params.deadline = Periods[ argument ]; params.budget = Execution[ argument ]+1; if ( rtems_cbs_set_parameters( server_id, ¶ms) ) printf( "ERROR: SET PARAMETERS FAILED\n" ); if ( rtems_cbs_get_parameters( server_id, &tparams ) ) printf( "ERROR: GET PARAMETERS FAILED\n" ); if ( params.deadline != tparams.deadline || params.budget != tparams.budget ) printf( "ERROR: PARAMETERS MISMATCH\n" ); scenario_done = 1; } /* Specific scenario for task 2: late unblock after being blocked by itself, the task priority has to be pulled down to background. */ if ( !scenario_done && argument == 2 && now >= 500 ) { Violating_task[ argument ] = 1; scenario_done = 1; } if (argument == 2 && Violating_task[ argument ]) rtems_task_wake_after( 10 ); /* active computing */ while(FOREVER) { now = rtems_clock_get_ticks_since_boot(); if ( argument == 4 && !Violating_task[ argument ] && (now >= start + Execution[argument])) break; if ( argument != 4 && (now >= start + Execution[argument]) ) break; } stop = rtems_clock_get_ticks_since_boot(); printf("P%" PRIdPTR "-F ticks:%d\n", argument, stop); } /* delete period and SELF */ status = rtems_rate_monotonic_delete( rmid ); if ( status != RTEMS_SUCCESSFUL ) { printf("rtems_rate_monotonic_delete failed with status of %d.\n",status); rtems_test_exit( 0 ); } if ( rtems_cbs_cleanup() ) printf( "ERROR: CBS CLEANUP\n" ); fflush(stdout); TEST_END(); rtems_test_exit( 0 ); }
代码比较容易理解,下面运行查看日志
*** TEST CBS SCHEDULER 3 *** PT1 - rtems_rate_monotonic_create id = 0x42010001 PT1 - rtems_rate_monotonic_ident id = 0x42010001 PT1 - (0x42010001) period 30 PT2 - rtems_rate_monotonic_create id = 0x42010002 PT2 - rtems_rate_monotonic_ident id = 0x42010002 PT2 - (0x42010002) period 40 PT3 - rtems_rate_monotonic_create id = 0x42010003 PT3 - rtems_rate_monotonic_ident id = 0x42010003 PT3 - (0x42010003) period 50 PT4 - rtems_rate_monotonic_create id = 0x42010004 PT4 - rtems_rate_monotonic_ident id = 0x42010004 PT4 - (0x42010004) period 70 AT5 AT6 P1-S ticks:2 P1-F ticks:12 P2-S ticks:12 P2-F ticks:22 P3-S ticks:22 P1-S ticks:32 P1-F ticks:42 P3-F ticks:42 P4-S ticks:42 P2-S ticks:52 P2-F ticks:62 P1-S ticks:62 P1-F ticks:72 P4-F ticks:72 P3-S ticks:72 P3-F ticks:82 AT6-S ticks:82 P6-F ticks:87 Killing task 6 AT5-S ticks:87 P1-S ticks:92 P1-F ticks:102 P2-S ticks:102 P2-F ticks:112 P4-S ticks:112 P1-S ticks:122 P1-F ticks:132 P3-S ticks:132 P3-F ticks:142 P2-S ticks:142 P2-F ticks:152 P4-F ticks:152 P1-S ticks:152 P1-F ticks:162 P2-S ticks:172 P2-F ticks:182 P1-S ticks:182 P1-F ticks:192 P3-S ticks:192 P3-F ticks:202 P4-S ticks:202 P1-S ticks:212 P1-F ticks:222 P4-F ticks:222 P2-S ticks:222 P2-F ticks:232 P3-S ticks:232 P3-F ticks:242 P1-S ticks:242 P1-F ticks:252 P2-S ticks:252 P2-F ticks:262 P4-S ticks:262 P1-S ticks:272 P1-F ticks:282 P3-S ticks:283 P3-F ticks:293 P2-S ticks:293 P2-F ticks:303 P1-S ticks:303 P1-F ticks:313 Signal overrun, fixing the task P4-F ticks:313 P5-F ticks:313 Killing task 5 P3-S ticks:322 P1-S ticks:332 P1-F ticks:342 P3-F ticks:342 P2-S ticks:342 P2-F ticks:352 P4-S ticks:352 P4-F ticks:362 P1-S ticks:362 P1-F ticks:372 P2-S ticks:372 P2-F ticks:382 P3-S ticks:382 P1-S ticks:392 P1-F ticks:402 P4-S ticks:402 P2-S ticks:412 P2-F ticks:422 P1-S ticks:422 P1-F ticks:432 P4-F ticks:432 P3-F ticks:432 P1-S ticks:452 P1-F ticks:462 P2-S ticks:462 P2-F ticks:472 P4-S ticks:472 P1-S ticks:482 P1-F ticks:492 P4-F ticks:492 P2-S ticks:492 P2-F ticks:502 P3-S ticks:502 P1-S ticks:512 P1-F ticks:522 P3-F ticks:522 P2-S ticks:532 P4-S ticks:532 P1-S ticks:542 P1-F ticks:552 P4-F ticks:552 P2-F ticks:552 P1-S ticks:572 P1-F ticks:582 P2-S ticks:582 P3-S ticks:582 P1-S ticks:602 P1-F ticks:612 P3-F ticks:612 P4-S ticks:612 P4-F ticks:622 P2-F ticks:622 P2 - Deadline miss P2-S ticks:622 P1-S ticks:632 P1-F ticks:642 P2-F ticks:642 P1-S ticks:662 P1-F ticks:672 P2-S ticks:672 P4-S ticks:672 P4-F ticks:682 P3-S ticks:682 P1-S ticks:692 P1-F ticks:702 P3-F ticks:702 P2-F ticks:702 P2 - Deadline miss P2-S ticks:702 P2-F ticks:712 P1-S ticks:722 P1-F ticks:732 P2-S ticks:742 P4-S ticks:742 P1-S ticks:752 P1-F ticks:762 P4-F ticks:762 P2-F ticks:762 P3-S ticks:772 P1-S ticks:782 P1-F ticks:792 P2-S ticks:792 P3-F ticks:792 P2-F ticks:802 P1-S ticks:812 P1-F ticks:822 P2-S ticks:822 P4-S ticks:822 P4-F ticks:832 P2-F ticks:832 P1-S ticks:842 *** END OF TEST CBS SCHEDULER 3 ***
我们简单分析一下
至此,我们演示清楚了cbs调度器的示例,希望可以加深大家对cbs调度算法的理解