JDK-5096173 : Thread.setPriority() doesn't work: The system call fails (EINVAL) on Linux.
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2004-09-02
  • Updated: 2004-09-20
  • Resolved: 2004-09-20
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 09/02/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux manu-home.homelinux.net 2.6.8-1.521 #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Calling Thread.setPriority() doesn't work on Linux. From monitoring the system calls,
(using strace) we see the following system call being made by the JVM when Thread.setPriority() is called.

sched_setscheduler(ThreadID, SCHED_OTHER, { priority }) = -1 EINVAL (Invalid argument)

This system call fails, because for the scheduling policy, SCHED_OTHER, the only priority valid is 0. The priorities should be set using the setpriority() and getpriority() system calls on Linux.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Any program which calls Thread.setPriority()

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The native thread's priority should be changed.
ACTUAL -
system call fails.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public static void main(String[] args) {
 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Can use JNI to make the right system call.
(Incident Review ID: 301514) 
======================================================================

Comments
EVALUATION I cannot verify this on Fedora Core 2 with kernel 2.6.7-1.494.2.2. We probably need more info. ###@###.### 2004-09-02 The relevant code appears to be: ./os/linux/vm/os_linux.cpp:1901:OSReturn os::set_native_priority(Thread* thread, int newpri) { Transferring to hotspot/runtime ###@###.### 2004-09-02 I am closing this bug as a duplicate of 4813310. Although the bug submitter is correct that the SCHED_OTHER class does not allow priority values other than 0, the setpriority call only works on processes and not threads. It might be possible to use the SCHED_RR policy class, however you cannot set a priority higher than the default without root privileges. The Solaris implementation has to deal with this problem by allowing two different models of priority setting but both models map priorities lower than default putting the VM at a disadvantage wrt other processes in the system. Refer to bug 4813310 to track the status of this problem ###@###.### 2004-09-20
2004-09-20