4 Replies Latest reply: Mar 27, 2009 7:03 AM by 807557 RSS

    RTSJ thread priorities, Lower priority thread running

    807557
      Hi,

      I'm experimenting with a thread pool that I've written containing javax.realtime.RealtimeThreads. I'm trying to use this to replace a java.util.concurrent.Exectutor type thread pool in an existing application. My design involves a Queue where the RealtimeThreads wait on until a Task is submitted for execution. All the threads in the pool start at the lowest priority and upon the submission of a task, I have written some logic to increase the priorities of the threads that I want run.

      At a given time, there could be many threads with tasks assigned to them and some without tasks. My algorithm to decide which thread is to be given the CPU, runs every time a task is submitted to the system as well as at the end of the current thread execution. I assumed that If I set a thread's priority higher than the others it would automatically be given the CPU and all I have to do is to set the Priority using rtThreadObject.setPriority() method. I obtain the highest priority in the system through PriorityScheduler.instance().getMaxPriority() method and do similarity to obtain the Normal and Lowest priorities in the system.

      Recently I added some code to change the priority of a thread from another, and noticed that the target thread's priority is higher than the one that is currently running. I'm not sure how a lower priority thread can run, when a higher priority thread is already in the system. I'm wondering whether it's to do with the current thread of execution getting woken up by a task submission (I do a QueueObject.notify() ) and continue to run at the lower priority, I have my doubts on it. Can anyone give some ideas as to how such an instance could exist?

      I'm developing on a Linux-RT kernel with RTSJ 2.1

      Thanks,

      Vidura
        • 1. Re: RTSJ thread priorities, Lower priority thread running
          807557
          One possibility is that the current thread is being boosted by the priority-inheritance protocol. If you query the priority you get the base priority which is <= active priority.

          Beyond that I'd need to understand in more detail exactly what you are doing ... and refresh my memory on which priority related methods are synchronized etc.

          David Holmes
          • 2. Re: RTSJ thread priorities, Lower priority thread running
            807557
            Hi David,

            Thanks a lot for the answer. This certainly might be the case in my scenario as well. However, is there a way that I could query the active priority from a Thread and confirm? I also have a few questions just to confirm my thoughts on such a scenario.

            1) Can Priority inheritance take place if a high priority thread is blocked on some IO operation say even a simple System.out.println ?

            2) In the simple scenario of a couple of threads with different being active at the same time with Java or RTSJ in particular, as long as the threads are not blocked or competing for a shared resource, is it possible to assume that the highest priority thread will get the CPU 100% of the time?

            3) If we as programmers takes great care in preventing IO operations (or controlling any IO operations, i.e. where to place them in the code) even simple as a System.out, priority inversions or inheritance would not take place?

            4) In such a scenario highlighted in 3, what practices could be used to debug the code if needed?

            As for exactly what I'm trying to do, it's a bit convoluted as I'm trying to achieve predictability in an existing product using RTSJ, that wouldn't be used in the trivial real-time sense, like an embedded environment, rather as a normal application. I will follow up with more details of it if needed.

            Thanks for your reply once again and bearing with some basic questions from me.

            Cheers,

            Vidura
            • 3. Re: RTSJ thread priorities, Lower priority thread running
              807557
              Sorry for the delayed response.
              Thanks a lot for the answer. This certainly might be the case in my scenario as well. However, is there a way that I could query the active priority from a Thread and confirm? I also have a few questions just to confirm my thoughts on such a scenario.
              There is no way to query the active priority of a thread.
              1) Can Priority inheritance take place if a high priority thread is blocked on some IO operation say even a simple System.out.println ?
              Priority inheritance occurs when a thread holding a monitor causes a higher-priority thread to block trying to acquire that monitor. At that point the thread holding the monitor is given the priority of the thread it is blocking. So this can happen anytime a lock is held, regardless of what the thread is doing at the time.
              2) In the simple scenario of a couple of threads with different being active at the same time with Java or RTSJ in particular, as long as the threads are not blocked or competing for a shared resource, is it possible to assume that the highest priority thread will get the CPU 100% of the time?
              The simple answer is "yes" but reality is more complex. Shared resources occur at the application, VM/runtime-library, and OS levels. You would have to avoid them at all levels to ensure the highest base priority thread is also the highest active priority.
              3) If we as programmers takes great care in preventing IO operations (or controlling any IO operations, i.e. where to place them in the code) even simple as a System.out, priority inversions or inheritance would not take place?
              Priority inheritance is not directly related to I/O operations - the issue is whether methods are synchronized or not.
              4) In such a scenario highlighted in 3, what practices could be used to debug the code if needed?
              On Solaris we use DTrace and the Thread Scheduling Visualizer for debugging timing/scheduling relationships. I'm afraid we don't have anything equivalent on Linux at this this time.
              As for exactly what I'm trying to do, it's a bit convoluted as I'm trying to achieve predictability in an existing product using RTSJ, that wouldn't be used in the trivial real-time sense, like an embedded environment, rather as a normal application. I will follow up with more details of it if needed.
              Don't worry, we have numerous users applying real-time Java to "normal" applications. :)

              David Holmes
              • 4. Re: RTSJ thread priorities, Lower priority thread running
                807557
                Hi David,

                Thanks a lot again for the answers. Although I develop on Linux, I run my experiments ultimately on a Solaris box. Hence, I was able to use the TSV and experience exactly what you described.

                Thanks,

                Vidura