代码之家  ›  专栏  ›  技术社区  ›  Jé Queue

Linux JVM真的实现了线程优先级吗?

  •  24
  • Jé Queue  · 技术社区  · 17 年前

    我知道 Bug4813310

    在C中做这件事并不容易,但我们能假设Linux JVM上从未设置过本机优先级吗?

    $uname -r && grep bogomips /proc/cpuinfo
    2.4.33.3
    bogomips        : 4312.26
    $java -version 2>&1 |head -1
    Java version "1.6.0_01"
    $javac T.java && java -Xmx32m -XX:+UseThreadPriorities T
    1:3112
    2:2636
    3:2662
    4:3118
    5:2870
    6:3319
    7:3412
    8:3304
    9:3299
    10:3069
    

    看起来并没有人们所期望的偏差!那是在一台小型虚拟Linux机器上。也许只是孙的?我们将尝试IBM J9 VM:

    1:4091
    2:4142
    3:3957
    4:3905
    5:3984
    6:3985
    7:4130
    8:4055
    9:3752
    10:4071
    

    $uname -r && grep bogomips /proc/cpuinfo
    2.6.9-67.ELsmp
    bogomips        : 3992.93
    bogomips        : 3990.00
    $java -version 2>&1 |head -1
    java version "1.4.2_14"
    $javac T.java && java -Xmx32m -XX:+UseThreadPriorities T
    1:63200
    2:64388
    3:62532
    4:58529
    5:62292
    6:64872
    7:64885
    8:64584
    9:61653
    10:61575
    

    让我们在一个只有2.6内核的真实板上尝试IBM的J9,由于系统更大,我将迭代次数增加到2000000次。

    $uname -r && grep bogomips /proc/cpuinfo
    2.6.9-78.ELsmp
    bogomips        : 5989.03
    bogomips        : 5985.03
    bogomips        : 5985.01
    bogomips        : 5985.02
    bogomips        : 5984.99
    bogomips        : 5985.02
    bogomips        : 5984.99
    bogomips        : 5985.02
    $java -Xmx32m T # this is the IBM J9
    1:1718
    2:1569
    3:1989
    4:1897
    5:1839
    6:1688
    7:1634
    8:1552
    9:2027
    10:1522
    

    正常

    C:\>java -version
    java version "1.6.0_11"
    C:\>java -Xmx32m T
    1:12578
    2:12625
    3:11469
    4:11453
    5:10781
    6:8937
    7:10516
    8:8406
    9:9953
    10:7391
    

    所以Linux JVM显然没有线程优先级?

    3 回复  |  直到 10 年前
        1
  •  22
  •   Adam Goode    17 年前

    从JDK 8源代码中 comment reads

    ////////////////////////////////////////////////////////////////////////////////
    // thread priority support
    
    // Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER
    // only supports dynamic priority, static priority must be zero. For real-time
    // applications, Linux supports SCHED_RR which allows static priority (1-99).
    // However, for large multi-threaded applications, SCHED_RR is not only slower
    // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out
    // of 5 runs - Sep 2005).
    //
    // The following code actually changes the niceness of kernel-thread/LWP. It
    // has an assumption that setpriority() only modifies one kernel-thread/LWP,
    // not the entire user process, and user level threads are 1:1 mapped to kernel
    // threads. It has always been the case, but could change in the future. For
    // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
    // It is only used when ThreadPriorityPolicy=1 and requires root privilege.
    

    ...

    Later on

    static int prio_init() {
      if (ThreadPriorityPolicy == 1) {
        // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
        // if effective uid is not root. Perhaps, a more elegant way of doing
        // this is to test CAP_SYS_NICE capability, but that will require libcap.so
        if (geteuid() != 0) {
          if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
            warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");
          }
    
          ThreadPriorityPolicy = 0;
        }
      }
    
      if (UseCriticalJavaThreadPriority) {
        os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];
      }
    
      return 0;
    }
    

    ...

    和 subsequently :

    OSReturn os::set_native_priority(Thread* thread, int newpri) {
      if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;
    
      int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);
      return (ret == 0) ? OS_OK : OS_ERR;
    }
    

    -XX:ThreadPriorityPolicy

        2
  •  1
  •   Kenny Drobnack    17 年前

    这只是一个黑暗中的尝试,但在JVM中设置优先级线程不需要能够调整操作系统线程的优先级吗?

    Linux(以及任何类Unix操作系统)限制了将进程赋予root更高优先级的能力。我认为线程也会有类似的限制。

        3
  •  1
  •   jarnbjo    17 年前

    默认的Linux线程调度程序策略SCHED_OTHER不支持优先级。或者更确切地说,它支持具有一个值的优先级设置:0。其他所谓的“实时”策略SCHED_FIFO和SCHED_RR支持更高的优先级,但仅适用于具有超级用户权限的进程。

    推荐文章