[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ComsTime in Java - report to Sun?



Dear all

I'm giving up on this.

I'd like to report the strange behaviour or this program to Sun.
Has anybody writtene anything better for Comstime?

Wouldn't it be a better strategy to find the point where this
strange behaviour is switched on and off? I'v tried... 

It runs too slow. It fails and hangs every now and then. When I
look at the core dump, it does not seem to be any particular 
place where the problem is.

Below is all the code, its present state. There isn't
much news since WoTUG-19, just the extra PAR. However, it's
been moulded around a lot - but always back to square one, no
square 2, names have changed, there are fewer public classes,
I use "this" etc.


//{{{  ComsTime.occ
// -- COMMUNICATION TIMER TEST PROGRAM
// -- as suggested by Professor Peter Welch
// -- (University of Kent at Canterbury)
// #INCLUDE "hostio.inc"
//
// PROC eg(CHAN OF SP fs,ts, []INT mem)
//
//   #USE "hostio.lib"
//   CHAN OF INT a, b, c, d :
//   VAL repeat IS 20000 :
//   VAL runs IS 10 :
//   SEQ
//     PAR
//       SEQ
//         a!0
//         WHILE TRUE
//           INT x:
//           SEQ
//             b ? x
//             a ! x
//       SEQ
//         WHILE TRUE
//           INT x:
//           SEQ
//             a ? x
//             PAR
//               c ! x
//               d ! x
//       SEQ
//         WHILE TRUE
//           INT x:
//           SEQ
//             c ? x
//             b ! x+1
//       INT start,stop,sum :
//       TIMER t :
//       SEQ
//         sum := 0
//         SEQ j = 0 FOR runs
//           SEQ
//             t ? start
//             SEQ i = 0 FOR repeat
//               INT x :
//               d ? x
//             t ? stop
//             so.write.int(fs,ts,stop MINUS start,8)
//             so.write.nl(fs,ts)
//             sum := sum PLUS (stop MINUS start)
//         so.write.string(fs,ts,"Average = ")
//         so.write.int(fs,ts,((sum *64) / (repeat*runs)),8)
//         so.write.string.nl(fs,ts,"us / iteration")
//         so.exit(fs,ts,sps.success)
// :
//}}} 
 
//{{{  ComsTime_main
//{{{  Heading
/**
 * Written by \yvind Teig, 1. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
public class  ComsTime_main
{
   public static void  main (String argv [])
   {
      if
        (argv.length != 2)
        {
           //{{{  Usage
           System.out.println ("");
           System.out.println ("Usage:");
           System.out.println ("  java ComsTime Threads_token_rounds 
Threads_work_loop_down_counter");
           System.out.println ("");
           //}}}  
        }
        else
        {
           int Threads_token_rounds           = Integer.parseInt (argv [0]);
           int Threads_work_loop_down_counter = Integer.parseInt (argv [1]);
           ComsTime cs = new ComsTime (Threads_token_rounds, 
Threads_work_loop_down_counter);
           cs.run ();
        }
   }
}
//}}}  

import java.io.*;
import java.util.*;
//{{{  ComsTime
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 *
 * COMMUNICATION TIMER TEST PROGRAM
 * as suggested by Professor Peter Welch
 * (University of Kent at Canterbury)
 * Originally written for occam, this program tests how Java behaves
 */
//}}}  
public class  ComsTime
{
   //{{{  ComsTime
   ComsTime (int  threads_token_rounds, int threads_work_loop_down_counter)
   {
       this.threads_token_rounds = threads_token_rounds;
       this.threads_work_loop_down_counter = threads_work_loop_down_counter;
   }
   //}}}  
   //{{{  run
   public void run ()
   {
      //{{{  Banner
      System.out.println ("");
      System.out.println ("Test of communication between Java threads");
      System.out.println ("Based on occam ComsTime.occ by Peter Welch, University 
of Kent at Canterbury");
      System.out.println ("Ported into Java by Oyvind Teig");
      System.out.println ("");
      //}}}  
      try
      {
         //{{{  TestTimer
         TestTimer timer = new TestTimer (10);
         timer.Print ();
         //}}}  
         //{{{  Streams
         PipedOutputStream out_stream_a  = new PipedOutputStream ();
         //                                Contains: private PipedInputStream sink 
<-

         PipedInputStream  in_stream_a   = new PipedInputStream  (out_stream_a);
         //                                Contains: Thread readSide; Thread 
writeSide;
         out_stream_a.connect (in_stream_a);

         PipedOutputStream out_stream_b  = new PipedOutputStream ();
         PipedInputStream  in_stream_b   = new PipedInputStream  (out_stream_b);
         out_stream_b.connect (in_stream_b);

         PipedOutputStream out_stream_c  = new PipedOutputStream ();
         PipedInputStream  in_stream_c   = new PipedInputStream  (out_stream_c);
         out_stream_c.connect (in_stream_c);

         PipedOutputStream out_stream_d  = new PipedOutputStream ();
         PipedInputStream  in_stream_d   = new PipedInputStream  (out_stream_d);
         out_stream_d.connect (in_stream_d);
         //}}}  
         //{{{  Report
         System.out.println (threads_token_rounds + " loops:");
         //}}}  
         //{{{  Start threads
         ComsTime_a aComsTime_a = new ComsTime_a (in_stream_b, out_stream_a, 
threads_work_loop_down_counter, threads_token_rounds);
         ComsTime_b aComsTime_b = new ComsTime_b (in_stream_a, out_stream_c, 
out_stream_d, threads_work_loop_down_counter);
         ComsTime_c aComsTime_c = new ComsTime_c (in_stream_c, out_stream_b, 
threads_work_loop_down_counter);
         //}}}  
         //{{{  Time tag
         Date date1 = new Date();
         //}}}  
         //{{{  Loop: Wait for report of rounds
         int token = 1;

         while (token > 0)
         {
            token = in_stream_d.read ();
            System.out.println (token);
         }
         //}}}  
         //{{{  Time tag
         Date date2 = new Date();
         //}}}  
         //{{{  Report
         long microseconds   = ((date2.getTime() - date1.getTime()) * 1000);
         long timePerLoop_us = (microseconds / ((long) threads_token_rounds));

         System.out.println ("   " + timePerLoop_us + " microseconds / iteration");
         //}}}  
         //{{{  Stop threads
         // aComsTime_a.stop ();
         // aComsTime_b.stop ();
         // aComsTime_c.stop ();

         try
         {
            aComsTime_a.join ();
            aComsTime_b.join ();
            aComsTime_c.join ();
         }
         catch (InterruptedException ignored) {}
         //}}}  
      }
      catch (IOException ignored) {}
   }
   //}}}  
   //{{{  Instance variables
   private int threads_token_rounds;
   private int threads_work_loop_down_counter;
   //}}}  
}
//}}}  
//{{{  ComsTime_a
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class ComsTime_a extends Thread
{
   //{{{  ComsTime_a
   ComsTime_a (
      PipedInputStream  in_stream,
      PipedOutputStream out_stream,
      int               work_loop_down_counter,
      int               token_start_value)
   {
      setName ("ComsTime_a");
      this.in_stream              = in_stream;
      this.out_stream             = out_stream;
      this.work_loop_down_counter = work_loop_down_counter;
      this.token_start_value      = token_start_value;
      start ();
   }
   //}}}  
   //{{{  run
   public void  run ()
   {
      // System.out.println ("    " + getName() + " " + toString());
      // System.out.println (currentThread().toString());
      try
      {
         out_stream.write (token_start_value);  // starts it all
         int token = 1;
         while (token > 0)
         {
            token = in_stream.read ();
            // System.out.println ("    " + getName() + " " + token);
            out_stream.write (token);
            //{{{  work_loop_down_counter
            int cnt = work_loop_down_counter;
            while (cnt > 0) {cnt = cnt - 1;}
            //}}}  
         }
      }
      catch (IOException caught)
      {
         System.out.println ("    " + getName() + " method run exception");
      }
   }
   //}}}  
   //{{{  Instance variables
   private PipedInputStream  in_stream;
   private PipedOutputStream out_stream;
   private int               work_loop_down_counter;
   private int               token_start_value;
   //}}}  
}
//}}}  
//{{{  ComsTime_b
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class ComsTime_b extends Thread
{
   //{{{  ComsTime_b
   ComsTime_b (
      PipedInputStream  in_stream,
      PipedOutputStream out_stream,
      PipedOutputStream out_stream_report,
      int               work_loop_down_counter)
   {
      setName ("ComsTime_b");
      this.in_stream              = in_stream;
      this.out_stream             = out_stream;
      this.out_stream_report      = out_stream_report;
      this.work_loop_down_counter = work_loop_down_counter;
      start ();
   }
   //}}}  
   //{{{  run
   public void  run ()
   {
      // System.out.println ("    " + getName() + " " + toString());
      // System.out.println (currentThread().toString());
      try
      {
         int token = 1;
         while (token > 0)
         {
            token = in_stream.read ();
            // System.out.println ("    " + getName() + " " + token);
            //{{{  Pass on to  other(s)
            if
               (token > 0)
               {
                  //{{{  Start new threads, let them work and terminate
                  ComsTime_buffer aBuffer_b1 = new ComsTime_buffer (token, 
out_stream_report);
                  ComsTime_buffer aBuffer_b2 = new ComsTime_buffer (token, 
out_stream);

                  try
                  {
                     aBuffer_b1.join ();
                     aBuffer_b2.join ();
                  }
                  catch (InterruptedException caught)
                  {
                     System.out.println ("    " + getName() + " method run 
exception");
                  }
                  //}}}  
               }
            else
               if (token == 0)
               {
                  //{{{  Start new thread, let it work and terminate
                  ComsTime_buffer aBuffer_b1 = new ComsTime_buffer (token, 
out_stream_report);

                  try
                  {
                     aBuffer_b1.join ();
                  }
                  catch (InterruptedException caught)
                  {
                     System.out.println ("    " + getName() + " method run 
exception");
                  }
                  //}}}  
               }
            //}}}  
            //{{{  work_loop_down_counter
            int cnt = work_loop_down_counter;
            while (cnt > 0) {cnt = cnt - 1;}
            //}}}  
         }
      }
      catch (IOException caught)
      {
         System.out.println ("    " + getName() + " method run exception");
      }
   }
   //}}}  
   //{{{  Instance variables
   private PipedInputStream  in_stream;
   private PipedOutputStream out_stream;
   private PipedOutputStream out_stream_report;
   private int               work_loop_down_counter;
   //}}}  
}
//}}}  
//{{{  ComsTime_c
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class ComsTime_c extends Thread
{
   //{{{  ComsTime_c
   ComsTime_c (
      PipedInputStream  in_stream,
      PipedOutputStream out_stream,
      int               work_loop_down_counter)
   {
      setName ("ComsTime_c");
      this.in_stream              = in_stream;
      this.out_stream             = out_stream;
      this.work_loop_down_counter = work_loop_down_counter;
      start ();
   }
   //}}}  
   //{{{  run
   public void  run ()
   {
      // System.out.println ("    " + getName() + " " + toString());
      // System.out.println (currentThread().toString());
      try
      {
         int token = 1;
         while (token > 0)
         {
            token = in_stream.read ();
            token = token - 1;
            // System.out.println ("    " + getName() + " " + token);
            out_stream.write (token);
            //{{{  work_loop_down_counter
            int cnt = work_loop_down_counter;
            while (cnt > 0) {cnt = cnt - 1;}
            //}}}  
         }
      }
      catch (IOException caught)
      {
         System.out.println ("    " + getName() + " method run exception");
      }
   }
   //}}}  
   //{{{  Instance variables
   private PipedInputStream  in_stream;
   private PipedOutputStream out_stream;
   private int               work_loop_down_counter;
   //}}}  
}
//}}}  
//{{{  ComsTime_buffer
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class ComsTime_buffer extends Thread
{
   //{{{  ComsTime_buffer
   ComsTime_buffer (
      int               value,
      PipedOutputStream out_stream)
   {
      this.value      = value;
      this.out_stream = out_stream;
      start ();
   }
   //}}}  
   //{{{  run
   public void  run ()
   {
      // System.out.println (currentThread().toString());
      try
      {
         System.out.println ("    " + getName() + " 1 " + value);
         out_stream.write (value);
         System.out.println ("    " + getName() + " 2 " + value);
      }
      catch (IOException caught)
      {
         System.out.println ("    " + getName() + " method run exception");
      }
   }
   //}}}  
   //{{{  finalize
   // It does not help to catch finalize here, the system still "hangs" every now 
and then
   // protected void finalize()
   // {
   // }
   //}}}  
   //{{{  Instance variables
   private int               value;
   private PipedOutputStream out_stream;
   //}}}  
}
//}}}  
//{{{  TestTimer
//{{{  Heading
/**
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class TestTimer
{
   //{{{  TestTimer
   TestTimer (int NoOfNonZeroTimeDifferences)
   {
      cnt = NoOfNonZeroTimeDifferences;
      while (noOfNonZeroVals < cnt)
      {
         Date date1 = new Date ();
         Date date2 = new Date ();
         long diff  = date2.getTime() - date1.getTime();
         if (diff > 0)
         {
            mean            = mean + diff;
            shortest        = Math.min (diff, shortest);
            longest         = Math.max (diff, longest);
            noOfNonZeroVals = noOfNonZeroVals + 1;
         }
      }
      mean = mean / ((long) noOfNonZeroVals);
   }
   //}}}  
   //{{{  Print
   public void Print ()
   {
      System.out.println ("Timer resolution:");
      System.out.println ("   noOfNonZeroVals " + noOfNonZeroVals);
      System.out.println ("   shortest        " + shortest + " [ms]");
      System.out.println ("   longest         " + longest  + " [ms]");
      System.out.println ("   mean            " + mean     + " [ms]");
   }

   //}}}  
   //{{{  Instance variables
   private int  cnt;
   private long shortest        = Long.MAX_VALUE;
   private long longest         = Long.MIN_VALUE;
   private long mean            = 0;
   private int  noOfNonZeroVals = 0;
   //}}}  
}
//}}}  
//{{{  Local
//{{{  Heading
/**
 * Class that routes all println calls through a single point.
 * It does not help, the system still "hangs" every now and then.
 * However, it is nice to be able to switch display on and off, just in
 * case, so I'll keep the calls.
 *
 * Written by \yvind Teig, 6. May 1996
 * Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx
 */
//}}}  
class Local
{
   static synchronized void synchronized_println (String str)
   {
      System.out.println (str);
      // Unresolved reason for "java.lang.StackOverflowError" here every now and 
then!
   }

   static synchronized void synchronized_println (int value)
   {
      System.out.println (value);
   }
}
//}}}  


Cheers,   |   0yvind Teig, Autronica, Trondheim, Norway       |
Oyvind    |   Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx                |
          |   Presently obsolete address: teig@xxxxxxxxxxxx   |
          |   47 73 58 12 68 (Tel) 47 73 91 93 20 (Fax)       |