Skip to content

Commit

Permalink
Add TotalMemoryAllocated() and memory_allocated.
Browse files Browse the repository at this point in the history
These are analogs of Runtime() and time, but for allocated memory.
  • Loading branch information
stevelinton committed Oct 31, 2017
1 parent 2be5f06 commit 80b7532
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
28 changes: 26 additions & 2 deletions doc/ref/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,14 @@ should be treated as an estimate.
</ManSection>

<ManSection>
<Var Name="time"/>
<Var Name="time"/>
<Var Name="memory_allocated"/>

<Description>
In the read-eval-print loop,
<Ref Var="time"/> stores the number of milliseconds the last command took.
<Ref Var="time"/> stores the number of milliseconds the last command
took. and <Ref Var="memory_allocated"/> stored the number of bytes of
memory it allocated.
</Description>
</ManSection>

Expand All @@ -444,6 +447,27 @@ in nanoseconds.

</Section>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Tracking Memory Usage">
<Heading>Tracking Memory Usage</Heading>

<ManSection>
<Func Name="TotalMemoryAllocated" Arg=""/>
<Description> <Ref Func="TotalMemoryAllocated"/> returns the total amount of memory
in bytes allocated by the &GAP; memory manager since &GAP; started.
</Description>
</ManSection>
<ManSection>
<Var Name="memory_allocated"/>

<Description>
In the read-eval-print loop,
<Ref Var="memory_allocated"/> stores the number of bytes of
memoryallocated by the last completed statement.
</Description>
</ManSection>

</Section>

<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Profiling">
Expand Down
8 changes: 6 additions & 2 deletions doc/ref/mloop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interactive environment in which you use &GAP;.
<Index Key="last"><C>last</C></Index>
<Index Key="last2"><C>last2</C></Index>
<Index Key="last3"><C>last3</C></Index>
<Index Key="time"><C>time</C></Index>
<Index Key="memory_allocated"><C>memory_allocated</C></Index>
<Index>previous result</Index>
The normal interaction with &GAP; happens in the so-called
<E>read-eval-print</E> loop.
Expand Down Expand Up @@ -154,8 +156,10 @@ gap> last3 + last2 * last;
]]></Example>
<P/>
Also in each statement the time spent by the last statement, whether it
produced a value or not, is available in the variable <Ref Var="time"/>.
This is an integer that holds the number of milliseconds.
produced a value or not, is available in the variable <Ref
Var="time"/>. This is an integer that holds the number of
milliseconds. Similarly the amount of memory allocated during that
statement (in bytes) is stored in the variable <Ref Var="memory_allocated"/>.

</Section>

Expand Down
36 changes: 33 additions & 3 deletions src/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ UInt Last3;
*/
UInt Time;

/****************************************************************************
**
*V MemoryAllocated. . . . . . . . . . . global variable 'memory_allocated'
**
** 'MemoryAllocated' is the global variable 'memory_allocated',
** which is automatically assigned the amount of memory allocated while
** executing the last command.
*/
UInt MemoryAllocated;


/****************************************************************************
**
Expand Down Expand Up @@ -226,6 +236,7 @@ Obj Shell ( Obj context,
Char *outFile)
{
UInt time = 0;
UInt mem = 0;
UInt status;
Obj evalResult;
UInt dualSemicolon;
Expand Down Expand Up @@ -261,8 +272,10 @@ Obj Shell ( Obj context,
while ( 1 ) {

/* start the stopwatch */
if (setTime)
time = SyTime();
if (setTime) {
time = SyTime();
mem = SizeAllBags;
}

/* read and evaluate one command */
STATE(Prompt) = prompt;
Expand Down Expand Up @@ -339,8 +352,14 @@ Obj Shell ( Obj context,
}

/* stop the stopwatch */
if (setTime)
if (setTime) {
AssGVar( Time, INTOBJ_INT( SyTime() - time ) );
/* This should be correct for small allocated totals at least,
even on 32 bits, but this functionality will never be very useful
on 32 bits */
AssGVar(MemoryAllocated,
INTOBJ_INT((SizeAllBags - mem) % (1L << NR_SMALL_INT_BITS)));
}

if (STATE(UserHasQuit))
{
Expand Down Expand Up @@ -1944,6 +1963,14 @@ Obj FuncSHALLOW_SIZE (
return ObjInt_UInt( SIZE_BAG( obj ) );
}

/****************************************************************************
**
*F FuncTotalMemoryAllocated( <self> ) .expert function 'TotalMemoryAllocated'
*/

Obj FuncTotalMemoryAllocated( Obj self ) {
return INTOBJ_INT(SizeAllBags);
}

/****************************************************************************
**
Expand Down Expand Up @@ -2871,6 +2898,7 @@ static StructGVarFunc GVarFuncs [] = {
GVAR_FUNC(GASMAN_MESSAGE_STATUS, 0, ""),
GVAR_FUNC(GASMAN_LIMITS, 0, ""),
GVAR_FUNC(SHALLOW_SIZE, 1, "object"),
GVAR_FUNC(TotalMemoryAllocated, 0, ""),
GVAR_FUNC(TNUM_OBJ, 1, "object"),
GVAR_FUNC(TNUM_OBJ_INT, 1, "object"),
GVAR_FUNC(OBJ_HANDLE, 1, "object"),
Expand Down Expand Up @@ -2969,7 +2997,9 @@ static Int PostRestore (
Last2 = GVarName( "last2" );
Last3 = GVarName( "last3" );
Time = GVarName( "time" );
MemoryAllocated = GVarName( "memory_allocated" );
AssGVar(Time, INTOBJ_INT(0));
AssGVar(MemoryAllocated, INTOBJ_INT(0));
QUITTINGGVar = GVarName( "QUITTING" );

/* return success */
Expand Down

0 comments on commit 80b7532

Please sign in to comment.