This discussion is archived
5 Replies Latest reply: Feb 6, 2009 2:42 AM by 807557 RSS

Exception upon starting a NoHeapRealTimeThread

807557 Newbie
Currently Being Moderated
Hi,
I have an exception I don't understand when a NoHeapRealTimeThread starts.
I've prepared a simpler example of an application of mine.
public class Main {
     static final int basePriority = PriorityScheduler.instance()
               .getMinPriority();
     static final int maxPriority = PriorityScheduler.instance()
               .getMaxPriority();

     static final NoHeapRealtimeThread starter = new NoHeapRealtimeThread(
               new PriorityParameters(maxPriority), ImmortalMemory.instance()) {
          public void run() {

               final String s1 = new String("n1");
               final String s2 = new String("n2");

               PeriodicParameters pep1 = new PeriodicParameters(new RelativeTime(
                         500, 0), new RelativeTime(1000, 0));
               PriorityParameters prp1 = new PriorityParameters(basePriority + 2);

               PeriodicParameters pep2 = new PeriodicParameters(new RelativeTime(
                         250, 0), new RelativeTime(1000, 0));
               PriorityParameters prp2 = new PriorityParameters(basePriority + 1);

               NoHeapRealtimeThread n1 = new NoHeapRealtimeThread(prp1, pep1,
                         null, ImmortalMemory.instance(), null, null) {
                    public void run() {
                         while (waitForNextPeriod()) {
                              System.out.println(s1);
                         }
                    }
               };

               NoHeapRealtimeThread n2 = new NoHeapRealtimeThread(prp2, pep2,
                         null, ImmortalMemory.instance(), null, null) {
                    public void run() {
                         while (waitForNextPeriod()) {
                              System.out.println(s2);
                         }
                    }
               };

               n1.start();
               n2.start();
          }
     };

     public static void main(String[] args) {
          starter.start();
     }
}
I get an exception upon starting "n2" thread. If I run the example the output is the following
Exception in thread "NoHeapRealtimeThread-0" javax.realtime.MemoryAccessError
     at java.lang.System.arraycopy(Native Method)
     at java.lang.ThreadGroup.add(ThreadGroup.java:889)
     at java.lang.Thread.start(Thread.java:688)
     at javax.realtime.RealtimeThread.start(RealtimeThread.java:1175)
     at javax.realtime.NoHeapRealtimeThread.start(NoHeapRealtimeThread.java:267)
     at Main$1.run(Main.java:49)
n1
n1
...
I don't see any memory access error since everything is running in immortal memory. So what could the problem be here?

The same example run smooth on Timesys VM. I am using RTJ 2.1 on Ubuntu 8.04 with the realtime kernel patch.

PS: I've just run the example with eclipse debug and it seems to work, but obviusly it is not a solution. The run-line is: "rts2.1_path"/jre/bin/java -classpath "classpathdir" -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54545 Main

Regards,
Alessandro
  • 1. Re: Exception upon starting a NoHeapRealTimeThread
    807557 Newbie
    Currently Being Moderated
    Alex,

    You've run into a known bug that will be fixed in our next release.

    The default size of the Thread[] in a ThreadGroup is 4, if you start a fifth thread then a new array of size 8 is created and the existing four entries are copied over. In this case the fifth thread is the NHRT and when it tries to do the copy it encounters heap-allocated Thread instances (the main thread and some VM threads) and so you get the error.

    To work-around this you need to prime the ThreadGroup so that it won't need to expand when the NHRT is started.

    David Holmes

    Edited by: davidholmes on Feb 5, 2009 9:57 PM
  • 2. Re: Exception upon starting a NoHeapRealTimeThread
    807557 Newbie
    Currently Being Moderated
    Thanks for the answer. Now I understand the problem.
    But still I've difficulties with that solution. How to increment the size of the default ThreadGroup? Moreover the constructors of the RealTimeThread class don't allow to associate a thread with a group so I can't switch to a different ThreadGroup (and assuming to workaround this, I've again the 4 NHRT limit).
  • 3. Re: Exception upon starting a NoHeapRealTimeThread
    807557 Newbie
    Currently Being Moderated
    There are two things you can do:

    1. Create a number of threads first so that the current ThreadGroup has enough space such that after those initial threads terminate, you can create the NHRTs and they won't need to grow the array. Note that you need to have concurrently alive threads to force the ThreadGroup to grow (the array never shrinks).

    2. You're right that you can't explicitly control the ThreadGroup for a NHRT. You can do it implicitly by creating a new ThreadGroup (in Immortal) then start a new thread (in Immortal) and have that thread do the NHRT starting. In that way the NHRT might have to grow the ThreadGroup array, but that array will not contain any heap allocated threads. (And you get all your NHRTs in a separate group.)

    Hope this helps.

    David Holmes
  • 4. Re: Exception upon starting a NoHeapRealTimeThread
    807557 Newbie
    Currently Being Moderated
    Thanks for the help!
    Your first solution doesn't fit very well with my requirements, your second solution is much better but I don't understand if the thread (well a Thread) which starts all the NHRT must be associated with ThreadGroup I must create in immortal. I've tried it in that way and it works but I don't like the presence of simple Thread in my project, expecially when it serves to start NHRTs (this sounds weird for the different scheduling method with respect to the different levels of priority).

    Anyway I've solved in a similar way. I think it works because of the RealtimeThread which does the array copy correctly in the heap.
    I post my new working example, sorry for the verbosity :-P
    import javax.realtime.*;
    
    public class Main {
    
         static final int basePriority = PriorityScheduler.instance()
                   .getMinPriority();
    
         static final int maxPriority = PriorityScheduler.instance()
                   .getMaxPriority();
    
         static int i;
    
         static {
    
              final PeriodicParameters pp = new PeriodicParameters(new RelativeTime(
                        250, 0), new RelativeTime(1000, 0));
              final PriorityParameters pr = new PriorityParameters(basePriority + 1);
    
              class NHRT extends NoHeapRealtimeThread {
                   private int num;
    
                   public NHRT(int num) {
                        super(pr, pp, null, ImmortalMemory.instance(), null, null);
                        this.num = num;
                   }
    
                   public void run() {
                        while (waitForNextPeriod()) {
                             System.out.println(num);
                        }
                   }
              }
    
              final NHRT[] NHRTs = new NHRT[10];
    
              for (i = 0; i < NHRTs.length; i++) {
                   NHRTs[i] = new NHRT(i);
              }
    
              RealtimeThread starter = new RealtimeThread(new PriorityParameters(
                        maxPriority)) {
                   public void run() {
                        for (i = 0; i < NHRTs.length; i++) {
                             NHRTs.start();
                        }
                   }
              };
              starter.start();
         }

         public static void main(String[] args) {}
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 5. Re: Exception upon starting a NoHeapRealTimeThread
    807557 Newbie
    Currently Being Moderated
    Yep that works too. If your NHRT starter doesn't have to be a NHRT itself then it won't have any problem growing the ThreadGroup's internal array.

    In other words you can avoid this problem by never having a NHRT start another thread.

    David