comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Yes it is possible. Re: Stackusage at runtime
Date: Tue, 04 Nov 2008 19:41:15 GMT
Date: 2008-11-04T19:41:15+00:00	[thread overview]
Message-ID: <vz1Qk.16070$_Y1.14888@bgtnsc05-news.ops.worldnet.att.net> (raw)
In-Reply-To: bf764407-9606-4ee4-aa22-cbb6d00dd84c@d36g2000prf.googlegroups.com

GNAT maintain two internal stacks but both uses the allocated system
stacks from the System.Memory package that can be monitored.  Will 
work for all version of GNAT with the understanding that the "Alloc" 
function may look a little different. 

To monitor the main (system) stack that GNAT uses, you need to alter the 
System.Memory package to include some monitoring information like in 
the following code. Some may not like it but it get the job done.  In this 
version I only show how to monitor allocations, but if you need to monitor 
Free allocation as well you can either add another variable such as 
"Free_Stack_Value" and assoc statements or just insert a subtract
Alloc_Stack_Value statement in the Free procedure.


1. Create a Tempory dir or use your program dir. This way maintain GNAT 
system integrity.

2. Copy from your version of GNAT "System.Memory" package aka files
   s-memory.ad? (need both body amd specs files) to temp dir.

3. Alter the s-memory.adb file by adding the following lines before the Alloc 
   function:

   -- -- -- -- -- -- --
   -- Debuging links --
   -- -- -- -- -- -- --
   Alloc_Stack_Value : size_t := 0;
      pragma Export (Ada, Alloc_Stack_Value, "Alloc_Stack_Value");


4. then insert the following line in the Alloc function:

      Alloc_Stack_Value := Alloc_Stack_Value + Size;

5. An Example of how the code placement can look using GNAT Ada 3.15p
   aka a version of GNAT Ada 95. System.Memory package:


    ... -- removed to save space

   -- -- -- -- -- -- --
   -- Debuging links --
   -- -- -- -- -- -- --
   Alloc_Stack_Value : size_t := 0;
      pragma Export (Ada, Alloc_Stack_Value, "Alloc_Stack_Value");


   -----------
   -- Alloc --
   -----------

   function Alloc (Size : size_t) return System.Address is
      Result      : System.Address;
      Actual_Size : size_t := Size;

   begin
      if Size = size_t'Last then
         Raise_Exception (Storage_Error'Identity, "object too large");
      end if;

      --  Change size from zero to non-zero. We still want a proper pointer
      --  for the zero case because pointers to zero length objects have to
      --  be distinct, but we can't just go ahead and allocate zero bytes,
      --  since some malloc's return zero for a zero argument.

      if Size = 0 then
         Actual_Size := 1;
      end if;

      Abort_Defer.all;
      Result := c_malloc (Actual_Size);
      Abort_Undefer.all;

      if Result = System.Null_Address then
         Raise_Exception (Storage_Error'Identity, "heap exhausted");
      end if;

      --
      --  Insert monitoring code after all error checking is done
      --
      Alloc_Stack_Value := Alloc_Stack_Value + Size;

      return Result;
   end Alloc;

    ... -- removed to save space


6. Compile the new "System.Memory" package:

     gcc -c -gnatpg s-memory.adb

7. Now a Test program to illustrate usage.  Needs to be in the same 
   temporary directory with the newly compiled s-memory package.

with Ada.Text_IO ;
use Ada.Text_IO ;

procedure Test is

                                                   -- copied from s-memory.ads
    type size_t is mod 2 ** Standard'Address_Size ;

    package S_IO is new Ada.Text_IO.Modular_IO ( size_t ) ;

    Stack_Monitor : size_t ;                     -- linked to s-memory.adb
    pragma Import ( Ada, Stack_Monitor, "Alloc_Stack_Value" ) ;


    type integer_access is access all integer ;
    type float_access is access all float ;

    A : Integer_access ;
    B : float_access ;

  begin
    Put ( "Before create Integer := " ) ;
    S_IO.Put ( Stack_Monitor ) ;
    A := new Integer ;
    Put ( " after create operation := " ) ;
    S_IO.Put ( Stack_Monitor ) ;
    New_Line ;
    --
    Put ( "Before create Float := " ) ;
    S_IO.Put ( Stack_Monitor ) ;
    B := new Float ;
    Put ( " after create operation := " ) ;
    S_IO.Put ( Stack_Monitor ) ;
    New_Line ;
  end ;



8: Compile test.  The compiler will use the newer version of System.Memory 
   package when linking.

    gnat make test.adb

9. Run Test program.

     test 


In <bf764407-9606-4ee4-aa22-cbb6d00dd84c@d36g2000prf.googlegroups.com>, andi.vontobel@gmx.ch writes:
>Hi.
>
>Is it possible to figure out how much of the stack is used at a
>specific moment?
>-> Ada95 Crosscompiler to PPC
>(It is a little bit hard to optimize the stacksize ...)
>
>Thanks,
>Andy




  parent reply	other threads:[~2008-11-04 19:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-04  7:55 Stackusage at runtime andi.vontobel
2008-11-04 11:44 ` Georg Bauhaus
2008-11-04 12:56   ` Andy
2008-11-04 15:40 ` Stuart
2008-11-04 19:05   ` Andi
2008-11-04 20:44     ` Niklas Holsti
2008-11-05 18:50     ` Niklas Holsti
2008-11-04 19:41 ` anon [this message]
2008-11-05  9:31   ` Yes it is possible. " Ludovic Brenta
2008-11-05 21:55     ` anon
2008-11-05 23:07       ` Ludovic Brenta
2008-11-06  5:00         ` anon
2008-11-06 10:17           ` Georg Bauhaus
2008-11-06 18:18         ` Pascal Obry
2008-11-07 10:19           ` Georg Bauhaus
2008-11-07 11:58           ` anon
2008-11-04 20:07 ` Per Sandberg
2008-11-05  7:33   ` Andy
2008-11-05 16:50     ` Per Sandberg
2008-11-05 17:31       ` Jean-Pierre Rosen
2008-11-05 18:19         ` Keith Thompson
2008-11-05 21:38   ` nobody
2008-11-05 20:14 ` sjw
replies disabled

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