Stack 'Used' shown in Ranger/SysMon diffs

A forum for general AmigaOS 4.x support questions that are not platform-specific
User avatar
javierdlr
Beta Tester
Beta Tester
Posts: 389
Joined: Sun Jun 19, 2011 11:13 pm
Location: Donostia (GUIPUZCOA) - Spain
Contact:

Stack 'Used' shown in Ranger/SysMon diffs

Post 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
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 218
Joined: Mon Aug 15, 2011 10:20 am
Location: Brisbane, QLD. Australia.

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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.
User avatar
nbache
Beta Tester
Beta Tester
Posts: 1744
Joined: Mon Dec 20, 2010 7:25 pm
Location: Copenhagen, Denmark
Contact:

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1483
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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?
cheers
tony
User avatar
javierdlr
Beta Tester
Beta Tester
Posts: 389
Joined: Sun Jun 19, 2011 11:13 pm
Location: Donostia (GUIPUZCOA) - Spain
Contact:

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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)
...
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1483
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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.
cheers
tony
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 218
Joined: Mon Aug 15, 2011 10:20 am
Location: Brisbane, QLD. Australia.

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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.
zzd10h
Posts: 546
Joined: Sun Sep 16, 2012 6:40 am
Location: France

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post by zzd10h »

Thank you for the information.

I will remove "Stack used" from SysMon in the next update.
http://apps.amistore.net/zTools
X1000 - AmigaOS 4.1.6 / 4.1 FE
User avatar
nbache
Beta Tester
Beta Tester
Posts: 1744
Joined: Mon Dec 20, 2010 7:25 pm
Location: Copenhagen, Denmark
Contact:

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 218
Joined: Mon Aug 15, 2011 10:20 am
Location: Brisbane, QLD. Australia.

Re: Stack 'Used' shown in Ranger/SysMon diffs

Post 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.
Post Reply