Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "stack thrash"?

Tags:

c

embedded

What is "stack thrash"? Or "a stack thrash"? (Since I don't know the definition I'm not sure if it is a countable or uncountable term.)

like image 333
talkaboutquality Avatar asked Feb 13 '10 18:02

talkaboutquality


2 Answers

Stack thrashing is like heap thrashing, but on the stack.

There, now that's explained.

Oh, you want more detail huh ?

If you emulate a stack based processor on a processor that isn't you're thrashing the stack.

If your C code malloc's and free's every other line of code, you're thrashing the heap.

The point of stack thrashing as a problem is that if you profiled your code, the CPU spending pretty much all it's time popping and pushing.

For heap thrashing that's malloc() & free() being your #1 & #2 most used functions.

Now some CPU's ( rockwell make some) actually are optimized to run a stack based language in hardware.

  • Internal ram that caches the top N kilobtyes of stack inside the CPU
  • Few registers
  • All instructions stack relative

Oddly enough, the Java Virtual Machine is a stack based model.

Running a really dumb FORTH implementation on x86 hardware will thrash the stack. The sort of thing you might write after reading the Forth spec, so you emit x86 machine code for forth instructions and DONT optimize it. Forth guys, I apologise, I know your implmentations are a lot better.

Postscript is stack based too, which makes early postscript printers exciting: they had limited ram and slow CPU's: and ran a stack-thrashing language. I'm sure a lot of effort went into things like the original Apple Laserwriter to make it run better. It had a Motorola 68000 CPU running at (10ish) megahertz and 1Mb of ram IIRC.

Again, stack thrashers.

Did that help ?

like image 159
Tim Williscroft Avatar answered Nov 15 '22 01:11

Tim Williscroft


I've seen this term used in the context of Forth, where the lack of stack frame access sometimes requires excessive use of stack manipulations ("thrashing the stack") to get to certain words to the top of the stack.

Also, This glossary defines it as "Frequent stack expansion (overflow) and contraction (underflow)". Clearly a definition in need of further explanation. Perhaps someone more familiar with the Cray X1 can explain.

like image 22
ergosys Avatar answered Nov 14 '22 23:11

ergosys