comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@callnetuk.com>
Subject: Re: delay until   and GNAT
Date: 1999/05/10
Date: 1999-05-10T00:00:00+00:00	[thread overview]
Message-ID: <3736e104@eeyore.callnetuk.com> (raw)
In-Reply-To: rracine.11.00086657@draper.com

Okay here it is:

with Ada.Calendar, Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO,
Ada.Numerics.Elementary_Functions;
use  Ada.Calendar, Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO,
Ada.Numerics.Elementary_Functions;

procedure Test_Task_Switching is

   Tasks:      constant := 10;
   Iterations: constant := 2000;

--   package Duration_IO is new Fixed_IO(Duration);

   task type Test_Task is
      entry Results (Task_Start,
                     Task_Stop:  out Time;
                     Switches:   out Natural;
                     Diff_Sum:   out Float;
                     Sum_Square: out Float);
   end;

   task body Test_Task is

      Task_Start,
      Task_Stop:  Time;
      Switches:   Natural := 0;
      Diff_Sum:   Float   := 0.0;
      Sum_Square: Float   := 0.0;

      Start: Time;
      Stop:  Time;
      Diff:  Float;

   begin
      Put('<');
      Task_Start := Clock;
      for i in Natural range 1..Iterations loop
         Start := Clock;
         delay 0.0; -- task switch?
         Stop := Clock;
         if Stop /= Start then
            Switches   := Switches + 1;
            Diff       := Float(Stop-Start);
            Diff_Sum   := Diff_Sum + Diff;
            Sum_Square := Sum_Square + Diff**2;
         end if;
      end loop;
      Task_Stop := Clock;
      Put('>');
      accept Results (Task_Start,
                      Task_Stop:  out Time;
                      Switches:   out Natural;
                      Diff_Sum:   out Float;
                      Sum_Square: out Float) do
         Task_Start := Test_Task.Task_Start;
         Task_Stop  := Test_Task.Task_Stop;
         Switches   := Test_Task.Switches;
         Diff_Sum   := Test_Task.Diff_Sum;
         Sum_Square := Test_Task.Sum_Square;
      end;
   end Test_Task;

   Testers: array (1..Tasks) of Test_Task;

   Task_Start,
   Task_Stop:  Time;
   Switches:   Natural;
   Diff_Sum:   Float;
   Sum_Square: Float;

   Grand_Switches:   Natural := 0;
   Grand_Diff_Sum:   Float   := 0.0;
   Grand_Sum_Square: Float   := 0.0;

   Average, Std_Dev: Float;

begin

   for i in Testers'Range loop
      Testers(i).Results(Task_Start,Task_Stop,Switches,Diff_Sum,Sum_Square);
      Grand_Switches   := Grand_Switches + Switches;
      Grand_Diff_Sum   := Grand_Diff_Sum + Diff_Sum;
      Grand_Sum_Square := Grand_Sum_Square + Sum_Square;
   end loop;

   New_Line;
   Put("Tasks:      ");   Put(Tasks,Width=>9);                  New_Line;
   Put("Iterations: ");   Put(Iterations,Width=>9);             New_Line;
   Put("Switches:   ");   Put(Grand_Switches,Width=>9);         New_Line;

   if Grand_Switches /= 0 then

   Average := Grand_Diff_Sum/Float(Grand_Switches);
   Std_Dev := Sqrt(Grand_Sum_Square/Float(Grand_Switches)-Average**2);

   Put("Average:    ");   Put(Average,Fore=>2,Aft=>6,Exp=>0);   New_Line;
   Put("Std Dev:    ");   Put(Std_Dev,Fore=>2,Aft=>6,Exp=>0);   New_Line;

   end if;

   Put_Line("Program completed.");

end;

At least two caveats however: (a) the clock used (be it Ada.Calendar or
whatever) must be HIGH RESOLUTION (i.e. higher than the lengths of the
context switches it's measuring) or the results will be bogus; (b) the
heuristic for deciding whether a task switch occurred (assume not if no
apparent time elapsed) is potentially dodgy both ways (false negative, false
positive) and you should replace it with something better if you can.

There are lots of obvious extensions and improvements to this program (e.g.
minima and maxima).

Have fun.

-------------------------------------
Nick Roberts
-------------------------------------

Roger Racine wrote in message ...
[...]
|Please note that all of the problems are associated with the operating
|system(s), not GNAT.  The bottom line is: if you want to use multitasking,
in
|any language, and want an upper bound on timing, use a real-time operating
|system.
[...]

You don't necessarily need a real-time operating system, just a half decent
one :-)











  reply	other threads:[~1999-05-10  0:00 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-05-05  0:00 delay until and GNAT isaac buchwald
1999-05-05  0:00 ` David C. Hoos, Sr.
1999-05-05  0:00 ` dennison
1999-05-06  0:00   ` Buz Cory
1999-05-06  0:00     ` Robert Dewar
1999-05-06  0:00       ` delay until and GNAT - expand isaac buchwald
1999-05-07  0:00         ` Roger Racine
1999-05-08  0:00           ` dewar
1999-05-10  0:00             ` Roger Racine
1999-05-11  0:00               ` Robert Dewar
1999-05-11  0:00                 ` dennison
1999-05-11  0:00                   ` Robert Dewar
1999-05-12  0:00                 ` delay until and GNAT - where to get the info isaac buchwald
1999-05-12  0:00                   ` Robert Dewar
1999-05-10  0:00             ` Context switching (was: delay until and GNAT) Nick Roberts
1999-05-11  0:00               ` Robert Dewar
1999-05-11  0:00               ` Robert Dewar
1999-05-11  0:00                 ` Tarjei Tj�stheim Jensen
1999-05-11  0:00                   ` Robert Dewar
1999-05-11  0:00                   ` David Brown
1999-05-10  0:00             ` delay until and GNAT - expand Roger Racine
1999-05-10  0:00               ` Joel Sherrill
1999-05-11  0:00               ` Robert Dewar
1999-05-11  0:00                 ` dennison
1999-05-11  0:00               ` isaac buchwald
1999-05-11  0:00                 ` dennison
1999-05-12  0:00                 ` Robert Dewar
1999-05-11  0:00             ` Roger Racine
     [not found]             ` <rracine.14.00 <rracine.15.000968A0@draper.com>
1999-05-11  0:00               ` Robert Dewar
     [not found]             ` <rracine.14.00 <rracine.17.0007DA28@draper.com>
1999-05-12  0:00               ` dennison
1999-05-12  0:00             ` Roger Racine
1999-05-06  0:00 ` delay until and GNAT Roger Racine
1999-05-10  0:00   ` Nick Roberts [this message]
1999-05-11  0:00     ` Context Switching Nick Roberts
1999-05-11  0:00       ` Robert Dewar
1999-05-11  0:00         ` Robert I. Eachus
1999-05-12  0:00           ` dennison
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox