home *** CD-ROM | disk | FTP | other *** search
- OPSTACK - Unit for Monitoring Heap and Stack Usage
- -------------------------------------------------------
- Brian Foley
- TurboPower Software
- 11/90
- Version 1.1
- Released to the public domain
-
- Overview
- ------------------------------------------------------------------------------
- OPSTACK allows you to accurately monitor a program's stack and heap usage
- simply by adding OPSTACK to the beginning of the program's USES list. The
- following program demonstrates how to use it:
-
- program OpStackTest;
- {-Simple program to test OpStack}
- uses
- OpStack, Crt;
- var
- P : Pointer;
-
- procedure StackEater;
- {-Use some stack space}
- var
- BigArray : array[1..12331] of Byte; {this is on the stack}
- begin
- {delay a moment to insure that OpStack sees what we've used}
- Delay(10);
- end;
-
- begin
- {use some stack and heap space}
- GetMem(P, 56789);
- StackEater;
- end.
-
- If you run this little demo program, you'll see that OpStack automatically
- displays the amount of heap and stack space used by the program when it ends.
- In addition, it also shows the greatest amount of space used on the free list
- at any one time (under Turbo Pascal 4.0-5.5), and an estimate of the total
- amount of memory actually used by the program (for code, data, stack, and
- heap). This last value includes any unused memory set aside for the stack
- segment. It does not include the amount of space used for the free list (if
- any), nor does it include any heap space allocated for an overlay buffer.
-
- Normally it is just this easy. In some cases, however, you may want or need to
- handle the reporting of results yourself. If so, you would set
- ReportStackUsage to False and call CalcStackUsage to obtain the necessary
- information.
-
- If you want to disable stack monitoring temporarily--when executing other
- programs, for example--you can call RestoreInt8 to disable it and InstallInt8
- to reenable it.
-
- OpStack can monitor only a single stack segment, normally the one in use when
- the program began. If you need to use it in a program that uses an alternate
- stack (a memory resident program, perhaps), you'll have to initialize three
- global variables before the first time that you switch to that stack. Here's
- an example:
-
- {disable OpStack momentarily}
- RestoreInt8;
-
- {OurSS has the stack segment to watch}
- OurSS := AlternateStackSegment;
-
- {InitialSP has the value of "SP" when the program began}
- InitialSP := TopOfAlternateStack;
-
- {LowestSP has the lowest value of SP so far, same as InitialSP at first}
- LowestSP := InitialSP;
-
- {reactivate OpStack}
- InstallInt8;
-
- Finally, in order to improve the accuracy of its results, OpStack reprograms
- the timer chip so that it can get control of the machine over 1000 times a
- second. For this reason, OpStack should not be used in programs that
- themselves reprogram the chip, or in programs that are using the PEP unit in
- Turbo Analyst. If you wish to change the rate at which samples are taken, you
- can call the SetSampleRate routine:
-
- {select 100 samples per second}
- SetSampleRate(100);
-
- The minimum value allowed is 18 samples per second, which is what the rate
- would be OpStack didn't reprogram the timer.