Skip to content

[WIP] Reduce arm_phdr_cb overhead - #1603

Closed
clamp03 wants to merge 1 commit into
dotnet:masterfrom
clamp03:unw
Closed

clamp03 wants to merge 1 commit into
dotnet:masterfrom
clamp03:unw

Conversation

@clamp03

@clamp03 clamp03 commented Jan 10, 2020

Copy link
Copy Markdown
Member
  • arm_phdr_cb is called too many (over 10K) in tizen sample applications.
    It is called when the applications uses reflection assembly load.
  • Found proc informations are the same in the most cases. (libcoreclr.so)
  • The patch caches a proc info, then reuse it.
    It improves launching time of tizen sample applications about 1~2%

- arm_phdr_cb is called too many (over 10K) in tizen sample applications.
  It is called when the applications uses reflection assembly load.
- Found proc informations are the same in the most cases. (libcoreclr.so)
- The patch caches a proc info, then reuse it.
  It improves launching time of tizen sample applications about 1~2%
@clamp03

clamp03 commented Jan 10, 2020

Copy link
Copy Markdown
Member Author

I found overhead in arm_phdr_cb. This patch reduces overhead using a cache.
However, I don't know this is a proper approach.
Could you please review and give some comments?
Thank you.

@jkotas

jkotas commented Jan 10, 2020

Copy link
Copy Markdown
Member

What is the exact method/callstack where you see this overhead? There are more effective ways how to make stackwalks faster.

@jkotas
jkotas requested a review from janvorli January 10, 2020 05:03
@jkotas

jkotas commented Jan 10, 2020

Copy link
Copy Markdown
Member

The change looks fine to me otherwise, but it means we have to make sure to keep it every time we update libunwind.

@jkotas jkotas added area-PAL-coreclr only for closed issues area-VM-coreclr and removed area-PAL-coreclr only for closed issues labels Jan 10, 2020
@clamp03

clamp03 commented Jan 10, 2020 •

Copy link
Copy Markdown
Member Author

Thanks for your comment. I also concerned about that, but I cannot find a better way.

These are two call stacks when arm_phdr_cb is called. (arm_find_proc_info has a callsite to arm_phdr_cb)
If you have any idea, please let me know. Thank you

1.
(lldb) bt
  * frame #0: 0xf5a51394 libcoreclr.so`_ULarm_find_proc_info(as=0xf5c70cb0, ip=4119699268, pi=0xfffe7704, need_unwind_info=1, arg=0xffffffff) at Gex_tables.c:517
    frame #1: 0xf5a50a7e libcoreclr.so`_ULarm_step [inlined] _ULarm_arm_exidx_step(c=0xfffe74e0) at Gstep.c:56
    frame #2: 0xf5a50a42 libcoreclr.so`_ULarm_step(cursor=0xfffe74e0) at Gstep.c:120
    frame #3: 0xf5a25350 libcoreclr.so`::PAL_VirtualUnwind(context=0xfffeb5a0, contextPointers=0xfffeb55c) at seh-unwind.cpp:308
    frame #4: 0xf587b640 libcoreclr.so`LazyMachState::unwindLazyState(baseState=<unavailable>, unwoundstate=0xfffeb770, threadId=<unavailable>, funCallDepth=1, hostCallPreference=AllowHostCalls) at stubs.cpp:549
    frame #5: 0xf576b302 libcoreclr.so`HelperMethodFrame::InsureInit(this=0xfffee2c0, initialInit=<unavailable>, unwindState=<unavailable>, hostCallPreference=<unavailable>) at frames.cpp:0
    frame #6: 0xf576b29e libcoreclr.so`HelperMethodFrame::GetFunction(this=0xfffee2c0) at frames.cpp:1691
    frame #7: 0xf57b240c libcoreclr.so`StackFrameIterator::ProcessCurrentFrame(this=0xfffeb8c8) at stackwalk.cpp:3003
    frame #8: 0xf57b3022 libcoreclr.so`StackFrameIterator::NextRaw(this=0xfffeb8c8) at stackwalk.cpp:2754
    frame #9: 0xf57b2c16 libcoreclr.so`StackFrameIterator::Filter(this=0xfffeb8c8) at stackwalk.cpp:2286
    frame #10: 0xf57b20aa libcoreclr.so`StackFrameIterator::Init(this=0xfffeb8c8, pThread=0x0047af60, pFrame=<unavailable>, pRegDisp=0xfffebbc0, flags=33) at stackwalk.cpp:1271
    frame #11: 0xf57b1ed6 libcoreclr.so`Thread::StackWalkFramesEx(this=0x0047af60, pRD=0xfffebbc0, pCallback=(libcoreclr.so`SystemDomain::CallersMethodCallbackWithStackMark(CrawlFrame*, void*) + 1 at appdomain.cpp:2498), pData=0xfffec198, flags=33, pStartFrame=<unavailable>)(CrawlFrame*, void*), void*, unsigned int, Frame*) at stackwalk.cpp:956
    frame #12: 0xf57b21a0 libcoreclr.so`Thread::StackWalkFrames(this=0x0047af60, pCallback=(libcoreclr.so`SystemDomain::CallersMethodCallbackWithStackMark(CrawlFrame*, void*) + 1 at appdomain.cpp:2498), pData=0xfffec198, flags=33, pStartFrame=<unavailable>)(CrawlFrame*, void*), void*, unsigned int, Frame*) at stackwalk.cpp:1043
    frame #13: 0xf58849f4 libcoreclr.so`SystemDomain::GetCallersModule(stackMark=0xfffee394, ppAppDomain=0x00000000) at appdomain.cpp:2469
    frame #14: 0xf5884ad0 libcoreclr.so`SystemDomain::GetCallersAssembly(stackMark=<unavailable>, ppAppDomain=<unavailable>) at appdomain.cpp:2490
    frame #15: 0xf58da1a2 libcoreclr.so`AssemblyNative::Load(assemblyNameUNSAFE=<unavailable>, codeBaseUNSAFE=<unavailable>, requestingAssemblyUNSAFE=<unavailable>, stackMark=0xfffee394, fThrowOnFileNotFound=<unavailable>, assemblyLoadContextUNSAFE=0x00000000) at assemblynative.cpp:86

(lldb) clrstack
OS Thread Id: 0x1133 (1)
Child SP       IP Call Site
FFFEE2C0 f5a51394 [HelperMethodFrame_PROTECTOBJ: fffee2c0] System.Reflection.RuntimeAssembly.nLoad(System.Reflection.AssemblyName, System.String, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, Boolean, System.Runtime.Loader.AssemblyLoadContext)
FFFEE360 F0BAFEE8 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(System.Reflection.AssemblyName, System.Threading.StackCrawlMark ByRef, System.Runtime.Loader.AssemblyLoadContext)
FFFEE380 F0BD9CCE System.Reflection.Assembly.Load(System.Reflection.AssemblyName, System.Threading.StackCrawlMark ByRef, System.Runtime.Loader.AssemblyLoadContext)

2.
(lldb) bt
  * frame #0: 0xf5a51394 libcoreclr.so`_ULarm_find_proc_info(as=0xf5c70cb0, ip=4121056786, pi=0xfffe7fe4, need_unwind_info=1, arg=0x00000002) at Gex_tables.c:517
    frame #1: 0xf5a50a7e libcoreclr.so`_ULarm_step [inlined] _ULarm_arm_exidx_step(c=0xfffe7dc0) at Gstep.c:56
    frame #2: 0xf5a50a42 libcoreclr.so`_ULarm_step(cursor=0xfffe7dc0) at Gstep.c:120
    frame #3: 0xf5a25350 libcoreclr.so`::PAL_VirtualUnwind(context=0x005d2d88, contextPointers=0x00000000) at seh-unwind.cpp:308
    frame #4: 0xf5a2561a libcoreclr.so`::RaiseException(dwExceptionCode=3762569539, dwExceptionFlags=<unavailable>, nNumberOfArguments=2, lpArguments=0xfffebeac) at seh-unwind.cpp:518
    frame #5: 0xf57d093c libcoreclr.so`DACRaiseException(unsigned int*, unsigned int) [inlined] DACRaiseException(unsigned int*, unsigned int)::$_1::operator()(DACRaiseException(unsigned int*, unsigned int)::Param*) const at util.cpp:2487
    frame #6: 0xf57d092e libcoreclr.so`DACRaiseException(args=<unavailable>, argCount=<unavailable>) at util.cpp:2489
    frame #7: 0xf57d0a24 libcoreclr.so`DACNotifyExceptionHelper(args=<unavailable>, argCount=<unavailable>) at util.cpp:2516
    frame #8: 0xf57d0b30 libcoreclr.so`DACNotify::DoModuleLoadNotification(ModulePtr=<unavailable>) at util.cpp:2597
    frame #9: 0xf58c13aa libcoreclr.so`DomainFile::FinishLoad(this=0x00457fd8) at domainfile.cpp:1119
    frame #10: 0xf58c0f52 libcoreclr.so`DomainFile::DoIncrementalLoad(this=0x00457fd8, level=FILE_LOADED) at domainfile.cpp:567
    frame #11: 0xf5886cb0 libcoreclr.so`AppDomain::TryIncrementalLoad(this=<unavailable>, pFile=0x00457fd8, workLevel=FILE_LOADED, lockHolder=0xfffebf64)), &(FileLoadLock::HolderLeave(FileLoadLock*)), 0u, &(int CompareDefault<FileLoadLock*>(FileLoadLock*, FileLoadLock*)), true>&) at appdomain.cpp:4171
    frame #12: 0xf588656c libcoreclr.so`AppDomain::LoadDomainFile(this=<unavailable>, pLock=0x005d1c60, targetLevel=FILE_LOADED) at appdomain.cpp:4105
    frame #13: 0xf5886954 libcoreclr.so`AppDomain::LoadDomainAssemblyInternal(this=0x0046ad98, pIdentity=0xfffee2e8, pFile=0x005d0b40, targetLevel=FILE_LOADED) at appdomain.cpp:3972
    frame #14: 0xf5883dda libcoreclr.so`AppDomain::LoadDomainAssembly(this=0x0046ad98, pSpec=0xfffee2e8, pFile=0x005d0b40, targetLevel=FILE_LOADED) at appdomain.cpp:3836
    frame #15: 0xf57e35ae libcoreclr.so`AssemblySpec::LoadDomainAssembly(this=0xfffee2e8, targetLevel=FILE_LOADED, fThrowOnFileNotFound=<unavailable>) at assemblyspec.cpp:939
    frame #16: 0xf57e3500 libcoreclr.so`AssemblySpec::LoadAssembly(this=<unavailable>, targetLevel=<unavailable>, fThrowOnFileNotFound=<unavailable>) at assemblyspec.cpp:757
    frame #17: 0xf58da0d0 libcoreclr.so`AssemblyNative::Load(assemblyNameUNSAFE=<unavailable>, codeBaseUNSAFE=<unavailable>, requestingAssemblyUNSAFE=<unavailable>, stackMark=0xfffee43c, fThrowOnFileNotFound=<unavailable>, assemblyLoadContextUNSAFE=0x00000000) at assemblynative.cpp:131

@jkotas

jkotas commented Jan 10, 2020

Copy link
Copy Markdown
Member

AssemblyNative::Load

We should convert AssemblyNative::Load to QCall. It will get rid of the libunwind in this stacktrace and make things even faster than this fix.

https://github.com/dotnet/runtime/blob/master/docs/design/coreclr/botr/mscorlib.md is a doc about FCalls/QCalls. Do you want to give it a shot to convert it?

Any chance this patch should goes upstream: https://github.com/libunwind/libunwind?

This patch is CoreCLR specific. It does not make sense to upstream it.

@clamp03

clamp03 commented Jan 10, 2020

Copy link
Copy Markdown
Member Author

@jkotas Thank you so much.
Sure! If you allow, I want to convert it. (but not now. I will update next week.)

@janvorli

Copy link
Copy Markdown
Member

If we can get the same or better gain using the change @jkotas has suggested (turn AssemblyNative::Load into QCall), I would prefer doing that instead of diverging from the official libunwind.

@janvorli

Copy link
Copy Markdown
Member

However, it seems that making this change upstream in libunwind may still make sense. @jkotas, this basically caches the range of a whole PT_LOAD segment in the elf file, so it seems it would speed up unwinding in general when exception is propagated through the same PT_LOAD segment. For example, in libcoreclr.so, there are two PT_LOAD segments where only the first one contains all the libcoreclr.so executable code. The second one contains .tbss (TLS stuff) and the .data.rel.ro sections.
So it seems unwinding in any executable where there are sequence of frames from the same shared library or executable would benefit.

@jkotas

jkotas commented Jan 10, 2020 •

Copy link
Copy Markdown
Member

it seems that making this change upstream in libunwind may still make sense

Maybe. It is something to discuss with libunwind maintainers.

this basically caches the range of a whole PT_LOAD segment in the elf file

Yes, it caches the range for the one elf file that contains libunwind itself. I am not sure how many other libunwind uses would benefit from this cache. For example, I do not think it would help with the issues explained in libunwind/libunwind#16

@janvorli

Copy link
Copy Markdown
Member

Yes, it caches the range for the one elf file that contains libunwind itself.

Ah, I've misread the change and it seemed to me that it caches the last elf file encountered during the unwind, not just the one containing the libunwind. That was what I've considered as usable for everyone. If done that way, it would recompute the cached value only when crossing boundary between two elf files.

@clamp03

clamp03 commented Jan 13, 2020

Copy link
Copy Markdown
Member Author

I measured another approach that @janvorli mentioned (caches the last elf file).
Performance are improved by 1.19% with this patch and by 0.84% with another approach.
Because another approach needs a lock for cache data write/read. I concerned about lock overhead, but it is smaller than I think.
Performance improvement is smaller, but I think it makes sense that making a patch for libunwind upstream.
After converting the function to QCall, I will handle it.
Thank you.

@clamp03 clamp03 changed the title Reduce arm_phdr_cb overhead [WIP] Reduce arm_phdr_cb overhead Jan 14, 2020
@clamp03

clamp03 commented Jan 20, 2020

Copy link
Copy Markdown
Member Author

Converting to QCall
I pushed a PR which converts to QCall. #1929.
Libunwind upstream patch
"caching last elf file" cannot handle when the cached elf file is unloaded and another elf file is loaded to the address.

Thank you.

@clamp03 clamp03 closed this Jan 20, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 29, 2020
@clamp03
clamp03 deleted the unw branch January 7, 2021 02:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants