- #include<</span>stdio.h>
- #include<</span>stdlib.h>
- #include<</span>unistd.h>
- #include<</span>sys/time.h>
- #include<</span>sys/types.h>
- #include<</span>sys/sysinfo.h>
- #include<</span>time.h>
-
- #define __USE_GNU
- #include<</span>sched.h>
- #include<</span>ctype.h>
- #include<</span>string.h>
-
- #define COUNT 300000
- #define MILLION 1000000L
- #define NANOSECOND 1000
-
- void test_func()
- {
-
int i = 0;
-
unsigned long long result = 0;;
-
-
for(i = 0; i<</span>8000 ;i++)
-
{
-
result += 2;
-
}
-
- }
- int
main(int argc,char* argv[])
- {
-
int i;
-
struct timespec sleeptm; -
long interval; -
struct timeval tend,tstart;
-
struct tm lcltime = {0};
-
struct sched_param param; -
int ret = 0;
-
-
if(argc != 3)
-
{
-
fprintf(stderr,"usage:./test sched_method sched_priority\n");
-
return -1;
-
}
-
-
cpu_set_t mask ; -
CPU_ZERO(&mask);
-
CPU_SET(1,&mask);
-
-
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
-
{
-
printf("warning: could not set CPU affinity, continuing...\n");
-
}
-
int sched_method = atoi(argv[1]);
-
int sched_priority = atoi(argv[2]);
-
-
/* if(sched_method > 2 || sched_method <</span> 0)
-
{
-
fprintf(stderr,"sched_method scope [0,2]\n");
-
return -2;
-
}
-
if(sched_priority > 99 || sched_priority <</span> 1)
-
{
-
fprintf(stderr,"sched_priority scope [1,99]\n");
-
return -3;
-
}
-
-
if(sched_method == 1 || sched_method == 2)*/
-
{
-
param.sched_priority = sched_priority;
-
ret = sched_setscheduler(getpid(),sched_method,¶m);
-
if(ret)
-
{
-
fprintf(stderr,"set scheduler to %d %d failed %m\n");
-
return -4;
-
}
-
}
-
-
int scheduler = sched_getscheduler(getpid());
-
-
fprintf(stderr,"the scheduler of PID(%ld) is %d, priority (%d),BEGIN time is :%ld\n", -
getpid(),scheduler,sched_priority,time(NULL));
-
-
sleep(2);
-
sleeptm.tv_sec = 0;
-
sleeptm.tv_nsec = NANOSECOND; -
-
for(i = 0;i<</span>COUNT;i++)
-
{
-
test_func();
-
}
-
-
-
-
interval = MILLION*(tend.tv_sec - tstart.tv_sec)
-
+(tend.tv_usec-tstart.tv_usec);
-
-
fprintf(stderr," PID = %d\t priority: %d\tEND TIME is %ld\n",getpid(),sched_priority,time(NULL));
-
return 0; - }
- struct sched_param
{ - /*
... */
- int
sched_priority;
- /*
... */
- };
int sched_setscheduler (pid_t pid,
- #define SCHED_OTHER 0
- #define SCHED_FIFO 1
- #define SCHED_RR 2
- #ifdef __USE_GNU
- # define SCHED_BATCH 3
- #endif
- /*
-
* Valid priorities for SCHED_FIFO and SCHED_RR are -
* 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
-
* SCHED_BATCH and SCHED_IDLE is 0.
-
*/
- #include
<</span>sched.h>
- int
sched_get_priority_min (int policy);
- int
sched_get_priority_max (int policy);
- int
sched_setparam (pid_t pid, const struct sched_param *sp);
- PID
PRI CMD TIME PSR -
6303 139 ./test 1 99 00:00:04 1
- static
const int prio_to_weight[40] = {
-
/* -20 */ 88761, 71755, 56483, 46273, 36291,
-
/* -15 */ 29154, 23254, 18705, 14949, 11916,
-
/* -10 */ 9548, 7620, 6100, 4904, 3906,
-
/* -5 */ 3121, 2501, 1991, 1586, 1277,
-
/* 0 */ 1024, 820, 655, 526, 423,
-
/* 5 */ 335, 272, 215, 172, 137,
-
/* 10 */ 110, 87, 70, 56, 45,
-
/* 15 */ 36, 29, 23, 18, 15,
- };
- [root@localhost sched]# cat comp.sh
- #/bin/sh
- ./test $1 99 &
- usleep 1000;
- ./test $1 70 &
- usleep 1000;
- ./test $1 70 &
- usleep 1000;
- ./test $1 70 &
- usleep 1000;
- ./test $1 50 &
- usleep 1000;
- ./test $1 30 &
- usleep 1000;
- ./test $1 10 &
- #define DEF_TIMESLICE
(100 * HZ / 1000)
- [root@localhost sched]# cat getpsinfo.sh
- #!/bin/sh
- for((i = 0; i < 40; i++))
- do
-
ps -C test -o pid,pri,cmd,time,psr >>psinfo.log 2>&1 -
sleep 2; - done
- [root@localhost sched]#
cat cswmon_spec.stp
-
- global time_offset
- probe begin
{ time_offset = gettimeofday_us() }
-
- probe scheduler.ctxswitch
{
-
if(next_task_name == "test" ||prev_task_name == "test")
-
{
-
t = gettimeofday_us()
-
printf(" time_off (� ) s(m)(pri=M)(state=%d)-> s(m)(pri=M)(state=%d)\n", -
t-time_offset,
-
prev_task_name, -
prev_pid, -
prev_priority, -
(prevtsk_state),
-
next_task_name, -
next_pid, -
next_priority, -
(nexttsk_state))
-
}
- }
-
-
- probe scheduler.process_exit
- {
-
if(execname() == "test")
-
printf("task :%s PID(%d) PRI(%d) EXIT\n",execname(),pid,priority);
- }
-
- probe timer.s($1)
{
-
printf("--------------------------------------------------------------\n")
-
exit();
- }
A) FIFO调度策略的输出:
输出结果如下:
- 终端1 :
- stap ./cswmon_spec.stp 70
- 终端2 :
- ./getpsinfo.sh
- 终端3
- ./comp.sh 1
![linux进程调度之 <wbr>FIFO <wbr>和 <wbr>RR <wbr>调度策略 linux进程调度之 <wbr>FIFO <wbr>和 <wbr>RR <wbr>调度策略](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
- time_off ( 689546 ) test( 6305)(pri= 120)(state=0)-> migration/2( 11)(pri= 0)(state=0)
- time_off ( 689977 ) stap( 5895)(pri= 120)(state=0)-> test( 6305)(pri= 120)(state=0)
- time_off ( 690067 ) test( 6305)(pri= 29)(state=1)-> stap( 5895)(pri= 120)(state=0)
- time_off ( 697899 ) test( 6303)(pri= 120)(state=0)-> migration/2( 11)(pri= 0)(state=0)
- time_off ( 698042 ) test( 6307)(pri= 120)(state=0)-> migration/0( 3)(pri= 0)(state=0)
- time_off ( 699114 ) stap( 5895)(pri= 120)(state=0)-> test( 6303)(pri= 120)(state=0)
- time_off ( 699307 ) test( 6303)(pri= 0)(state=1)-> test( 6307)(pri= 120)(state=0)
- time_off ( 699371 ) test( 6307)(pri= 29)(state=1)-> stap( 5895)(pri= 120)(state=0)
- time_off ( 699392 ) test( 6309)(pri= 120)(state=0)-> migration/3( 15)(pri= 0)(state=0)
- time_off ( 699966 ) events/1( 20)(pri= 120)(state=1)-> test( 6309)(pri= 120)(state=0)
- time_off ( 700034 ) test( 6309)(pri= 29)(state=1)-> stap( 5895)(pri= 120)(state=0)
- time_off ( 707379 ) test( 6311)(pri= 120)(state=0)-> migration/3( 15)(pri= 0)(state=0)
- time_off ( 707587 ) test( 6313)(pri= 120)(state=0)-> migration/0( 3)(pri= 0)(state=0)
- time_off ( 712021 ) stap( 5895)(pri= 120)(state=0)-> test( 6311)(pri= 120)(state=0)
- time_off ( 712145 ) test( 6311)(pri= 49)(state=1)-> test( 6313)(pri= 120)(state=0)
- time_off ( 712252 ) test( 6313)(pri= 69)(state=1)-> stap( 5895)(pri= 120)(state=0)
- time_off ( 727057 ) test( 6315)(pri= 120)(state=0)-> migration/0( 3)(pri= 0)(state=0)
- time_off ( 727952 ) stap( 5895)(pri= 120)(state=0)-> test( 6315)(pri= 120)(state=0)
- time_off ( 728047 ) test( 6315)(pri= 89)(state=1)-> stap( 5895)(pri= 120)(state=0)
- time_off ( 2690181 ) stap( 5895)(pri= 120)(state=0)-> test( 6305)(pri= 29)(state=0)
- time_off ( 2699316 ) test( 6305)(pri= 29)(state=0)-> test( 6303)(pri= 0)(state=0)
- task :test PID(6303) PRI(0) EXIT
- time_off (13057854 ) test( 6303)(pri= 0)(state=64)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (13057864 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6305)(pri= 29)(state=0)
- time_off (15333340 ) test( 6305)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (15333354 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6305)(pri= 29)(state=0)
- time_off (18743409 ) test( 6305)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (18743422 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6305)(pri= 29)(state=0)
- time_off (22154757 ) test( 6305)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (22154771 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6305)(pri= 29)(state=0)
- task :test PID(6305) PRI(29) EXIT
- time_off (22466855 ) test( 6305)(pri= 29)(state=64)-> test( 6307)(pri= 29)(state=0)
- time_off (25563548 ) test( 6307)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (25563566 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6307)(pri= 29)(state=0)
- time_off (28973602 ) test( 6307)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (28973616 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6307)(pri= 29)(state=0)
- task :test PID(6307) PRI(29) EXIT
- time_off (31846121 ) test( 6307)(pri= 29)(state=64)-> test( 6309)(pri= 29)(state=0)
- time_off (32383671 ) test( 6309)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (32383683 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6309)(pri= 29)(state=0)
- time_off (35793735 ) test( 6309)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (35793747 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6309)(pri= 29)(state=0)
- time_off (39203797 ) test( 6309)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (39203809 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6309)(pri= 29)(state=0)
- task :test PID(6309) PRI(29) EXIT
- time_off (41200440 ) test( 6309)(pri= 29)(state=64)-> test( 6311)(pri= 49)(state=0)
- time_off (42613866 ) test( 6311)(pri= 49)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (42613898 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6311)(pri= 49)(state=0)
- time_off (46024070 ) test( 6311)(pri= 49)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (46024082 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6311)(pri= 49)(state=0)
- time_off (49434004 ) test( 6311)(pri= 49)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (49434017 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6311)(pri= 49)(state=0)
- task :test PID(6311) PRI(49) EXIT
B) RR的情况
- 终端1 :
- stap ./cswmon_spec.stp 70
-
- 终端2 :
- ./getpsinfo.sh
-
- 终端3
- ./comp.sh 1
![linux进程调度之 <wbr>FIFO <wbr>和 <wbr>RR <wbr>调度策略 linux进程调度之 <wbr>FIFO <wbr>和 <wbr>RR <wbr>调度策略](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
- time_off ( 4188015 ) test( 6428)(pri= 0)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off ( 4188025 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6428)(pri= 0)(state=0)
- time_off ( 7612014 ) test( 6428)(pri= 0)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off ( 7612024 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6428)(pri= 0)(state=0)
- task :test PID(6428) PRI(0) EXIT
- time_off (10679062 ) test( 6428)(pri= 0)(state=64)-> test( 6430)(pri= 29)(state=0)
- time_off (10964413 ) test( 6430)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (10964422 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6430)(pri= 29)(state=0)
- time_off (11709024 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (12736030 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (13779022 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (13879021 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (13984075 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (14084020 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (14184023 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (14284024 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (14374486 ) test( 6434)(pri= 29)(state=0)-> watchdog/1( 10)(pri= 0)(state=0)
- time_off (14374502 ) watchdog/1( 10)(pri= 0)(state=1)-> test( 6434)(pri= 29)(state=0)
- time_off (14384097 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (14484066 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (14584023 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (14684020 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (14786032 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (14886020 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (14986026 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (15089023 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (15192030 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (15292026 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (15396085 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (15496022 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
- time_off (15596027 ) test( 6434)(pri= 29)(state=0)-> test( 6430)(pri= 29)(state=0)
- time_off (15696153 ) test( 6430)(pri= 29)(state=0)-> test( 6432)(pri= 29)(state=0)
- time_off (15796022 ) test( 6432)(pri= 29)(state=0)-> test( 6434)(pri= 29)(state=0)
参考文献
1 深入linux 内核架构
2 linux system program
3 systemtap example