Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

made windows stack traces 10 times faster #638

Merged
merged 1 commit into from
Oct 14, 2013

Conversation

Ingrater
Copy link
Contributor

This change makes the tracing part of stack tracing on windows about 10 times faster. This will especially impact throwing exceptions, which will be a lot faster now.

return result;
}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still need the old stack trace code then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because:

  • RtlCaptureStackBackTrace does not work when given a exception context
  • RtlCaptureStackBackTrace does not use the debug information to resolve stack frames (only works when all functions have frame pointers)
  • RtlCaptureStackBackTrace has a limit of 63 stack frames it can walk

So for these three cases the old code is still needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to add this explanation as a comment in the code!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's briefly mentioned in the sources, but I agree a more thorough explanation in the sources or in the commit message would be nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to add this explanation as a comment in the code!

So whats the preffered action now? Should a create a new pull request or can I commit into this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to improve the comment please open a new pull request and reference this one.

}
else
{
auto result = new ulong[backtraceLength];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need to get rid of any GC allocations in this module, but that can remain a TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know thats an issue. Since when is removing allocations from druntime a priority?
I actually have a non allocating version but rewrote it, because its harder to use and somewhat ugly.
https://github.com/Ingrater/druntime/blob/merge63/src/core/sys/windows/stacktrace.d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since always, it complicates the initialization order and most GC allocations are not needed because internally druntime mostly needs allocations to track state, i.e. uses static references. It's a little different for use faced core functions. The particular issue in this module is that it's also used to throw Errors so the runtime is in an undefined state and a GC allocation could for example deadlock.

MartinNowak added a commit that referenced this pull request Oct 14, 2013
made windows stack traces 10 times faster
@MartinNowak MartinNowak merged commit ba9a836 into dlang:master Oct 14, 2013
@Ingrater Ingrater deleted the fastTrace branch October 14, 2013 11:24
@ghost
Copy link

ghost commented Oct 14, 2013

size_t[63] buffer; // On windows xp the sum of "frames to skip" and "frames to capture" can't be greater then 63

Can't we specialize this and allow a bigger buffer for >XP ?

@Ingrater
Copy link
Contributor Author

Can't we specialize this and allow a bigger buffer for >XP ?

We most likely could somehow. How often do you have stack depths > 63?

@ghost
Copy link

ghost commented Oct 14, 2013

We most likely could somehow. How often do you have stack depths > 63?

In some projects I'm forced to catch some exceptions when I'm in a C callback, and then re-throw them once I'm back in D main, here's an example stack-trace:

core.exception.AssertError@test_dragdrop(41): Assertion failure
----------------
0x004B33F7 in onAssertError
0x0040242A in void test_dragdrop.__unittestL11_1().__lambda1(scope dtk.event.DropEvent) at C:\dev\projects\dtk\tests\test_dragdrop.d(43)
0x0045F682 in void dtk.signals.EventHandler!(dtk.event.DropEvent).EventHandler.call(scope dtk.event.DropEvent) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\signals.d(100)
0x0045F4F0 in void dtk.signals.Signal!(dtk.event.DropEvent).Signal.emit(scope dtk.event.DropEvent) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\signals.d(479)
0x0047565A in dtk.dispatch.Dispatch.TkEventFlag dtk.dispatch.Dispatch._targetEvent(dtk.widgets.widget.Widget, scope dtk.event.Event) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\dispatch.d(905)
0x004753C2 in dtk.dispatch.Dispatch.TkEventFlag dtk.dispatch.Dispatch._dispatchEvent(dtk.widgets.widget.Widget, scope dtk.event.Event) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\dispatch.d(775)
0x00475370 in void dtk.dispatch.Dispatch._dispatchInternalEvent(dtk.widgets.widget.Widget, scope dtk.event.Event) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\dispatch.d(759)
0x00456320 in extern (Windows) int dtk.platform.win32.dragdrop.DropTarget.dispatchEvent(dtk.event.DropAction, uint, dtk.platform.win32.defs.POINT, uint*) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\platform\win32\dragdrop.d(700)
0x004561B1 in extern (Windows) int dtk.platform.win32.dragdrop.DropTarget.DragEnterImpl(dtk.platform.win32.defs.IDataObject, uint, dtk.platform.win32.defs.POINT, uint*) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\platform\win32\dragdrop.d(645)
0x00455DC9 in _D3dtk8platform5win328dragdrop10DropTarget183__T15ComThrowWrapperS133_D3dtk8platform5win328dragd72592015E34249E4754F716AB2609543 at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\utils.d(377)
0x75862666 in StgGetIFillLockBytesOnFile
0x74F7586C in NdrServerInitialize
0x74FF05F1 in NdrStubCall2
0x7586D936 in WdtpInterfacePointer_UserUnmarshal
0x7586D9C6 in WdtpInterfacePointer_UserUnmarshal
0x7586DF1F in WdtpInterfacePointer_UserUnmarshal
0x7578213C in CoRevokeInitializeSpy
0x75782031 in CoRevokeInitializeSpy
0x75782FEF in DcomChannelSetHResult
0x7586DE47 in WdtpInterfacePointer_UserUnmarshal
0x7586DCBB in WdtpInterfacePointer_UserUnmarshal
0x7586E34C in WdtpInterfacePointer_UserUnmarshal
0x75782DC7 in DcomChannelSetHResult
0x75782D86 in DcomChannelSetHResult
0x75C96238 in gapfnScSendMessage
0x75C968EA in gapfnScSendMessage
0x75C97D31 in LoadStringW
0x75C97DFA in DispatchMessageW
0x004673FB in void dtk.app.App.run() at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\app.d(150)
0x0047A197 in void dtk.signals.Signal!(dtk.event.DestroyEvent).Signal.opOpAssign!("~", void delegate()).opOpAssign(void delegate()) at C:\dev\projects\dtk\tests\build\\..\..\src\dtk\signals.d(331)
0x00402407 in void test_dragdrop.__unittestL11_1() at C:\dev\projects\dtk\tests\test_dragdrop.d(13)
0x004546EC in void test_dragdrop.__modtest()
0x004B3B75 in int core.runtime.runModuleUnitTests().__foreachbody2(ref object.ModuleInfo*)
0x004BB847 in int rt.minfo.moduleinfos_apply(scope int delegate(ref object.ModuleInfo*)).__foreachbody2(ref rt.sections_win32.SectionGroup)
0x004AB80F in _d_run_main
0x0045474C in main
0x004D6429 in mainCRTStartup
0x75043677 in BaseThreadInitThunk
0x77279D72 in RtlInitializeExceptionChain
0x77279D45 in RtlInitializeExceptionChain

It's still not over the 63 limit though.

@Ingrater
Copy link
Contributor Author

In some projects I'm forced to catch some exceptions when I'm in a C callback, and then re-throw them once I'm back in D main, here's an example stack-trace:

Well its not like it will stop working if there are more then 63 stackframes. It will just be slower. If this is a issue for you I can dig for a way to detect Windows XP and add a condition to use a bigger buffer if running on > XP

@Ingrater Ingrater restored the fastTrace branch October 14, 2013 20:00
@ghost
Copy link

ghost commented Oct 14, 2013

It's not an issue, don't worry about it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants