Page 1 of 2
Stack 'Used' shown in Ranger/SysMon diffs
Posted: Sun Jun 22, 2014 1:36 am
by javierdlr
Hi, just noticed that stack 'Used' shown on those 2 progs (Ranger and SysMon) have different values.
Ranger:
DOS->Processes: Ranger - Min:80000 Size:81912 Used:20332 (24%)
SysMon:
Tasks: Ranger - Used:1196 Size:81912
The different in 'Used' stack is quite evident, the author of SysMon (Guillaume) uses for its calculation the same function/algorithm used in Scout IIRC.
Are both values ok/valid? Which one shows more "real" value?
TIA
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Sun Jun 22, 2014 4:21 am
by colinw
The one Ranger uses is as close to "real" as you are going to get.
It physically runs the stack frame backwards and finds where it hasn't been changed from the default 0 startup value for
a respectable bunch of bytes. This will give you a maximum real value to determine the point of what one would normally deem "usage".
I don't know how the other tool calculates the stack usage.
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Sun Jun 22, 2014 7:24 pm
by nbache
Is the actual stack pointer for a process available in some way? Obviously, only the currently running one has the stack pointer in a register, but I assume the value must be stored in a well-defined place for a waiting/ready one?
If so, maybe that's what Sysmon uses, in which case it will be showing the current stack usage, while Ranger (re your description) shows the maximum stack used so far.
Both are of course "real", but the maximum value is probably the most useful one if you want to know whether you have been running out of stack space

.
Best regards,
Niels
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Mon Jun 23, 2014 1:01 am
by tonyw
At a guess, I would say that the "tc_ETask" field in struct Task points to an internal Exec structure that contains the real current stack pointer. That structure is private and I don't know how SysMon gets hold of it. Perhaps it intercepts StackSwap() and reads the value of stk_Lower from the StackSwapStruct structure? Dunno.
Presumably, Colin's "stackuse" utility uses the same algorithm as Ranger?
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Tue Jun 24, 2014 12:20 am
by javierdlr
AFAIK SysMon uses scout sources, so it's something like this:
Code: Select all
...
struct Task *task = IExec->FindTask("Workbench");
if(task != NULL)
{
IDOS->Printf("Workbench: stack=%p (H=%p L=%p)\n",task->tc_SPReg, task->tc_SPUpper,task->tc_SPLower);
IDOS->Printf(" %ld bytes, free:%ld, used:%ld\n",task->tc_SPUpper-task->tc_SPLower,
task->tc_SPReg-task->tc_SPLower, task->tc_SPUpper-task->tc_SPReg);
..
And I get:
#StackTrace
Workbench: stack=0x62867ea0 (H=0x62867ffc L=0x62858004)
65528 bytes, free:65180,
used:348 (task->tc_SPUpper - task->tc_SPReg)
...
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Tue Jun 24, 2014 1:25 am
by tonyw
Yes, I didn't notice the tc_SPReg field.
If you run a program to read the stack of another Task, that Task can't be running at the time, so is probably sitting in an idle state, waiting for input. Its stack usage in that state may be very low.
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Tue Jun 24, 2014 3:56 am
by colinw
javierdlr wrote:AFAIK SysMon uses scout sources, so it's something like this:
...
And I get:
Workbench: stack=0x62867ea0 (H=0x62867ffc L=0x62858004)
65528 bytes, free:65180, used:348 (task->tc_SPUpper - task->tc_SPReg)
Unfortunately that does not tell you how much stack is being "used", it never really would, it only tells you where the stack pointer
is right now, and that is only very approximate because the tc_SPReg only gets updated on a task switch AFAIK, it' s not in real time.
Also depending on the program structure, it could have just come out of a deep recursion, where it could have already overrun
the stack frame, but that would not indicate it unless you took the reading at exactly the right time and point where it was
in the deepest part of the program, as soon as it backed out, it would only show where it was now again.
The "Stackuse" and "Ranger" programs show how deep the stack was actually used up, to save data for function call return addresses
or other general stack data storing requirements.
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Thu Jun 26, 2014 8:05 am
by zzd10h
Thank you for the information.
I will remove "Stack used" from SysMon in the next update.
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Thu Jun 26, 2014 11:28 pm
by nbache
zzd10h wrote:I will remove "Stack used" from SysMon in the next update.
Why remove it? Why not just change it to use the same method as Ranger and Stackuse (as described earlier in the thread)?
Best regards,
Niels
Re: Stack 'Used' shown in Ranger/SysMon diffs
Posted: Fri Jun 27, 2014 12:21 am
by colinw
Yes exactly, you are welcome to a copy of my source code for the test, give me a PM and i'll send it to you.