进程友好性(优先级)设置对Linux没有影响

时间:2012-04-26 22:46:39

标签: linux kernel scheduling nice

我写了一个测试程序,它只包含一些无限循环 内部计算,并执行否 I / O操作。我尝试启动程序的两个实例,一个高 niceness值,另一个具有低niceness值:

sudo nice -n 19 taskset 1 ./test
sudo nice -n -20 taskset 1 ./test

taskset命令确保两个程序在同一核心上执行。 与我的预期相反,顶级报道称这两个项目获得了约50%的成绩 计算时间。这是为什么? nice命令是否有效果?

4 个答案:

答案 0 :(得分:2)

我把test.c放在一起:

for(;;)
   {
   }

然后和你的好人一起跑。我没有为每个sudo运行不同的sudo,而是sudo一个交互式shell并从那里运行它们。我使用了两个& s。

我有一个./test很难击中我的CPU,而且几乎没有触及它。

当然,该系统仍然感觉非常敏感;在现代处理器上需要大量的CPU占用过程来获得如此多的负载,你可以“感觉”它。

与I / O占用过程和内存占用过程形成鲜明对比;在这些情况下,单个贪婪的过程会使系统使用起来很痛苦。

我猜你的系统有一个相对独特的优先级错误(或微妙),或者你的方法有所不同。

我在Ubuntu 11.04系统上运行了测试。

答案 1 :(得分:1)

我假设命令行末尾缺少&。否则,第二行在第一行完成之前不会运行。

当两个进程都在运行时,请使用类似top的内容,并确保它们各自具有您分配的良好值。

如果仅使用taskset启动流程,然后在renice运行后调整优先级,会发生什么情况?

答案 2 :(得分:1)

处理好处(优先级)设置对Linux有影响! (在实践中,但只有你给它足够的工作要做!)

在我的系统上,只要所有核心都满载,那么 就会产生影响。在ubuntu 14.04上,使用nice-N运行的进程通过0.807 ** N个操作,而不是在不改变nice值的情况下运行的进程(假设你为每个不错的级别运行每个核心一个实例)。

在我的情况下,我有关闭超线程的四核i7,所以如果我运行四个或更少的进程,那么它们的好值是什么并不重要 - 它们每个都得到一个完整的核心。如果我在好的等级12和好的等级12上运行4个过程,那么12等级的过程会通过0.807 ^ 12,即大约7%的工作得到好的等级0。该比率似乎是一个合理的预测器,从好的0到14级,之后它会波动(例如,一些运行的18级处理比16级更好) - 运行测试更长时间可以平滑结果。

(使用ruby 2.1.2)

,cl文件:

uptime
nices='-0 -6 -12 -18'
nices='-0 -18'
nices='-0 -2 -4 -6 -8 -10 -12 -14 -16 -18'
rm -f ,n-*
for i in 1 2 3 4
do
  for n in $nices
  do
    nice $n ruby ,count_loops.rb > ,n${n}-$i &
  done
done
ps -l
uptime
wait
uptime
ps -l
c=`cat ,n-0-[1234] | total`
last=$c
for n in $nices
do
  echo
  c2=`cat ,n${n}-[1234] | total`
  echo total of `cat ,n${n}-[1234]` is $c2
  echo -n "nice $n count $2, percentage: "
  echo "3 k $c2 100 * $c / p" | dc
  echo -n "                  percent of last: "
  echo "3 k $c2 100 * $last / p" | dc
  last=$c2
done
uptime
echo total count: `cat ,n-*-[1234] | total`

,count_loops.rb文件

#!/usr/bin/env  ruby
limit = Time.new + 70
i=0
while Time.new < limit
 i += 1
 j = 0
 while (j += 1) < 10000
   t = j
 end
end
puts i

sh ,cl的结果 - 初始诊断输出:

 19:16:25 up 20:55,  2 users,  load average: 3.58, 3.59, 2.88
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  4987  4977  0  80   0 -  7297 wait   pts/3    00:00:00 bash
0 S  1000 11743  2936  0  80   0 -  2515 wait   pts/3    00:00:00 rubymine.sh
0 S  1000 11808 11743  6  80   0 - 834604 futex_ pts/3   00:18:10 java
0 S  1000 11846 11808  0  80   0 -  4061 poll_s pts/3    00:00:02 fsnotifier64
0 S  1000 19613  4987  0  80   0 -  2515 wait   pts/3    00:00:00 sh
0 R  1000 19616 19613  0  80   0 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19617 19613  0  82   2 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19618 19613  0  84   4 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19619 19613  0  86   6 -  7416 -      pts/3    00:00:00 ruby
0 R  1000 19620 19613  0  88   8 -  6795 -      pts/3    00:00:00 ruby
0 R  1000 19621 19613  0  90  10 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19622 19613  0  92  12 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19623 19613  0  94  14 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19624 19613  0  96  16 -  6078 -      pts/3    00:00:00 ruby
0 R  1000 19625 19613  0  98  18 -  6012 -      pts/3    00:00:00 ruby
0 R  1000 19626 19613  0  80   0 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19627 19613  0  82   2 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19628 19613  0  84   4 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19629 19613  0  86   6 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19630 19613  0  88   8 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19631 19613  0  90  10 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19632 19613  0  92  12 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19633 19613  0  94  14 -  6144 -      pts/3    00:00:00 ruby
0 R  1000 19634 19613  0  96  16 -  4971 -      pts/3    00:00:00 ruby
0 R  1000 19635 19613  0  98  18 -  4971 -      pts/3    00:00:00 ruby
0 R  1000 19636 19613  0  80   0 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19637 19613  0  82   2 -  7449 -      pts/3    00:00:00 ruby
0 R  1000 19638 19613  0  84   4 -  7344 -      pts/3    00:00:00 ruby
0 R  1000 19639 19613  0  86   6 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19640 19613  0  88   8 -  7416 -      pts/3    00:00:00 ruby
0 R  1000 19641 19613  0  90  10 -  6210 -      pts/3    00:00:00 ruby
0 R  1000 19642 19613  0  92  12 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19643 19613  0  94  14 -  5976 -      pts/3    00:00:00 ruby
0 R  1000 19644 19613  0  96  16 -  6111 -      pts/3    00:00:00 ruby
0 R  1000 19645 19613  0  98  18 -  4971 -      pts/3    00:00:00 ruby
0 R  1000 19646 19613  0  80   0 -  7582 -      pts/3    00:00:00 ruby
0 R  1000 19647 19613  0  82   2 -  7516 -      pts/3    00:00:00 ruby
0 R  1000 19648 19613  0  84   4 -  7416 -      pts/3    00:00:00 ruby
0 R  1000 19649 19613  0  86   6 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19650 19613  0  88   8 -  6177 -      pts/3    00:00:00 ruby
0 R  1000 19651 19613  0  90  10 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19652 19613  0  92  12 -  6078 -      pts/3    00:00:00 ruby
0 R  1000 19653 19613  0  94  14 -  6247 -      pts/3    00:00:00 ruby
0 R  1000 19654 19613  0  96  16 -  4971 -      pts/3    00:00:00 ruby
0 R  1000 19655 19613  0  98  18 -  4971 -      pts/3    00:00:00 ruby
0 R  1000 19656 19613  0  80   0 -  3908 -      pts/3    00:00:00 ps
 19:16:26 up 20:55,  2 users,  load average: 3.58, 3.59, 2.88
 19:17:37 up 20:56,  3 users,  load average: 28.92, 11.25, 5.59
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  4987  4977  0  80   0 -  7297 wait   pts/3    00:00:00 bash
0 S  1000 11743  2936  0  80   0 -  2515 wait   pts/3    00:00:00 rubymine.sh
0 S  1000 11808 11743  6  80   0 - 834604 futex_ pts/3   00:18:10 java
0 S  1000 11846 11808  0  80   0 -  4061 poll_s pts/3    00:00:02 fsnotifier64
0 S  1000 19613  4987  0  80   0 -  2515 wait   pts/3    00:00:00 sh
0 R  1000 19794 19613  0  80   0 -  3908 -      pts/3    00:00:00 ps

sh ,cl的结果 - 统计数据:(最后一个百分比是此总数的百分比与最后一组流程的计数相比)

total of 99951 101725 100681 104046 is 406403
nice -0 count , percentage: 100.000
                  percent of last: 100.000

total of 64554 62971 64006 63462 is 254993
nice -2 count , percentage: 62.743
                  percent of last: 62.743

total of 42997 43041 43197 42717 is 171952
nice -4 count , percentage: 42.310
                  percent of last: 67.434

total of 26882 28250 27151 27244 is 109527
nice -6 count , percentage: 26.950
                  percent of last: 63.696

total of 17228 17189 17427 17769 is 69613
nice -8 count , percentage: 17.129
                  percent of last: 63.557

total of 10815 10792 11021 11307 is 43935
nice -10 count , percentage: 10.810
                  percent of last: 63.113

total of 7023 6923 7885 7323 is 29154
nice -12 count , percentage: 7.173
                  percent of last: 66.357

total of 5005 4881 4938 5159 is 19983
nice -14 count , percentage: 4.917
                  percent of last: 68.542

total of 3517 5537 3555 4092 is 16701
nice -16 count , percentage: 4.109
                  percent of last: 83.576

total of 4372 4307 5552 4527 is 18758
nice -18 count , percentage: 4.615
                  percent of last: 112.316
 19:17:37 up 20:56,  3 users,  load average: 28.92, 11.25, 5.59
total count: 1141019

(纯粹主义者会注意到我正在混合红宝石,贝壳和直流 - 他们将不得不原谅我上世纪的旧习惯;)

答案 3 :(得分:0)

I run an example program from APUE and nice does have the effect.

The example program mainly fork a child and both the parent and child execute a i++ increment operation for given time(10s). By giving the child different nice value, the result shows if nice makes a difference.

The book warns that I should run the program with a uniprocessor PC, fisrt I tried with my own PC, i5-7500 CPU @ 3.40GHz × 4 (4 cores), giving different nice value, almost no difference.

Then I log into my remote server, 1 processor 1 GB, and get the expected difference.


1 core processor 1 GB Test result:

./a.out

NZERO = 20 
current nice value in parent is 0
current nice value in child is 0, adjusting by 0
now child nice value is 0
parent count = 13347219
child count = 13357561

./a.out 20 //child nice set to 20

NZERO = 20
current nice value in parent is 0
current nice value in child is 0, adjusting by 20
now child nice value is 19
parent count = 29770491
ubuntu@VM-0-2-ubuntu:~$ child count = 441330

Test program(I made a little modification), from Section 8.16, APUE:

apue.h is merely a header wrapper
err_sys() is also a error handler wrapper, you can use printf temporarily.

#include "apue.h"
#include <errno.h>
#include <sys/time.h>

#if defined(MACOS)
#include <sys/syslimits.h>
#elif defined(SOLARIS)
#include <limits.h>
#elif defined(BSD)
#include <sys/param.h>
#endif

unsigned long long count;
struct timeval end;

void
checktime(char *str)
{
    struct timeval  tv;

    gettimeofday(&tv, NULL);
    if (tv.tv_sec >= end.tv_sec && tv.tv_usec >= end.tv_usec) {
        printf("%s count = %lld\n", str, count);
        exit(0);
    }
}

int
main(int argc, char *argv[])
{
    pid_t   pid;
    char    *s;
    int     nzero, ret;
    int     adj = 0;

    setbuf(stdout, NULL);
#if defined(NZERO)
    nzero = NZERO;
#elif defined(_SC_NZERO)
    nzero = sysconf(_SC_NZERO);
#else
#error NZERO undefined
#endif
    printf("NZERO = %d\n", nzero);
    if (argc == 2)
        adj = strtol(argv[1], NULL, 10);
    gettimeofday(&end, NULL);
    end.tv_sec += 10;   /* run for 10 seconds */

    if ((pid = fork()) < 0) {
        err_sys("fork failed");
    } else if (pid == 0) {  /* child */
        s = "child";
        printf("current nice value in child is %d, adjusting by %d\n",
          nice(0), adj);
        errno = 0;
        if ((ret = nice(adj)) == -1 && errno != 0)
            err_sys("child set scheduling priority");
        printf("now child nice value is %d\n", ret);
    } else {        /* parent */
        s = "parent";
        printf("current nice value in parent is %d\n", nice(0));
    }
    for(;;) {
        if (++count == 0)
            err_quit("%s counter wrap", s);
        checktime(s);
    }
}

Complete source code link: https://wandbox.org/permlink/8iryAZ48sIbaq27y