Stack Performance Optimization

I am currently using the dbghelp library to go through the thread stack of some process (using GetThreadContext () and StackWalk64 () ). and collect only the return addresses contained in each frame.

However, the overhead for this is too high for system requirements - the total time is apx. 5 ms per stack stroke (with 10-15 frames). This time includes GetThreadContext () and a loop that calls StackWalk64 () to get all the frames.

Anyway, I have to find a way to do this much faster. Does anyone know how I can do this?


Edit:

Does anyone know the ETW mechanism (event tracing for Windows)?

If so, how can I keep track of all the context switches that occurred over a given period of time? Is there an event provider that publishes an event for each context switch?

+4
source share
3 answers

The fastest way I can come up with is to create my own version of GetThreadContext and StackWalk64 by creating a kernel driver that captures the kernelStack field of the kernelStack structure of the thread you are trying to control. Here is a good article on this topic.

+3
source

If you are using Windows Vista or higher, you should use the ETW period. You can activate everything you talk about, including context switches and profile events, and this is pretty effective. For X86, it basically moves around the EBP register chain, which is a linked list of addresses to be sorted through. In 64-bit ground, the stack walker should unwind the stack, and therefore it is a little less efficient, but I can tell you if you are doing any reasonable amount of work in your application, the effects of walking on the stack will not be displayed up. This, of course, is not in milliseconds.

+2
source

Part of ETW is actually an independent issue. Windows performance analysis tools can capture all context switches, as well as the Visual Studio Profiler in the "Concurrency Profiling Resource Context" section. You can also dump all events into a file manually using logman, see the instructions here .

+1
source

Source: https://habr.com/ru/post/1384998/


All Articles