Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

COR_PRF_USE_PROFILE_IMAGES 옵션과 NGen의 "profiler-enhanced images"

.NET Profiler를 만드는 경우, 설정하는 옵션 중에 COR_PRF_USE_PROFILE_IMAGES라는 것이 있습니다. 문서 상의 설명을 보면,

COR_PRF_MONITOR Enumeration
; https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/cor-prf-monitor-enumeration

Causes the native image search to look for profiler-enhanced images. If no profiler-enhanced image is found for a given assembly, the common language runtime falls back to JIT for that assembly. If this flag and the COR_PRF_DISABLE_ALL_NGEN_IMAGES flag are both specified, COR_PRF_DISABLE_ALL_NGEN_IMAGES is used.


COR_PRF_USE_PROFILE_IMAGES 옵션을 설정하게 되면 CLR에게 (일반적인 NGen 이미지를 로드하지 말고) "profiler-enhanced images"(또는 "image with Profile flavor")를 로드하도록 만들고, 만약 없다면 원본 어셈블리로부터 JIT하라는 것으로 나옵니다.

여기서 말하는 "profiler-enhanced image"에 대한 설명은 다음의 문서들에서 찾아 볼 수 있습니다.

Creating an IL-rewriting profiler - What about NGEN?
; https://learn.microsoft.com/en-us/archive/blogs/davbr/creating-an-il-rewriting-profiler

Enter, Leave, Tailcall Hooks Part 1: The Basics
; https://learn.microsoft.com/en-us/archive/blogs/davbr/enter-leave-tailcall-hooks-part-1-the-basics

정리해 보면, 일반적으로 NGen 이미지들은 해당 머신의 환경에 최적화된 기계어로 컴파일되어 있습니다. 그런데, 문제는 .NET Profiler로 해당 어셈블리들을 프로파일링하려면 IL 코드가 기계어로 jit되는 시점에 뭔가를 조작해야 하는데 NGen된 이미지들은 그럴 수가 없기 때문에 Profiler 입장에서는 할 수 있는 것이 없습니다. 따라서 프로파일러로 하여금 제어가 가능하도록 생성한 NGen 이미지들을 가리켜 "profiler-enhanced image"라고 하는 것입니다.

일반적으로, 마이크로소프트는 새로운 .NET Framework 버전 또는 업데이트를 설치하는 경우 BCL을 NGen시켜 둡니다. 이를 확인하는 방법은 다음과 같이 명령을 내려보면 됩니다.

c:\temp>ngen display mscorlib /verbose

...[생략]...

Native Images:

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
        Source MVID:    {97707D7E-03B0-454F-ADE5-26C74B667F46}
        Source HASH:    9e5ef9b3a78b8eecec12d101fed42ad3f7ed8c0d
        NGen GUID sign: {5057723C-3845-D0BB-8882-AF5AFEFB29A3}
        OS:             WinNT
        Processor:      amd64
        Runtime:        2.0.50727.8762
        mscorwks.dll:   TimeStamp=58E462F0, CheckSum=00994AA9
        Flags:
        Scenarios:              <no debug info> <no debugger> <no profiler> <no instrumentation>
        Granted set:    <PermissionSet class="System.Security.PermissionSet" version="1"/>

        File:           C:\Windows\assembly\NativeImages_v2.0.50727_64\mscorlib\3c7257504538bbd08882af5afefb29a3\mscorlib.ni.dll
        Dependencies:
                mscorlib, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089:
                        Guid:{97707D7E-03B0-454F-ADE5-26C74B667F46}
                        Sign:9e5ef9b3a78b8eecec12d101fed42ad3f7ed8c0d

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
        Source MVID:    {19E40EE1-9924-457B-A371-5254132FC075}
        NGen GUID sign: {202CF701-7ECA-50F7-5AA3-E474EB475713}
        OS:             WinNT
        Processor:      amd64
        Runtime:        4.0.30319.0
        clr.dll:        TimeStamp=59A63E48, VirtualSize=009DF000
        Flags:
        Scenarios:              <no debug info> <no debugger> <no profiler> <no instrumentation>
        File:           C:\Windows\assembly\NativeImages_v4.0.30319_64\mscorlib\01f72c20ca7ef7505aa3e474eb475713\mscorlib.ni.dll
        No dependencies

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 <debug>
        Source MVID:    {2A8D77DF-0308-4EE3-8039-AD703E62FC78}
        NGen GUID sign: {B5B834D2-D442-D558-C658-964842CA41A3}
        OS:             WinNT
        Processor:      x86(Pentium 4) (features: 00008001)
        Runtime:        4.0.30319.0
        clr.dll:        TimeStamp=597BD742, VirtualSize=006E6000
        Flags:          <debug>
        Scenarios:              <debugger> <no profiler> <no instrumentation>
        File:           C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\mscorlib\d234b8b542d458d5c658964842ca41a3\mscorlib.ni.dll
        No dependencies

위의 결과를 보면, mscorlib.dll의 경우 .NET 2.0 버전은 "C:\Windows\assembly\NativeImages_v2.0.50727_64\mscorlib\3c7257504538bbd08882af5afefb29a3\mscorlib.ni.dll" 파일로 ngen되어 있고 순수하게 기계어 변환 작업만 한 것입니다.

반면, .NET 4.0 버전의 mscorlib.dll은 ngen된 이미지가 2개가 있는데, "C:\Windows\assembly\NativeImages_v4.0.30319_64\mscorlib\01f72c20ca7ef7505aa3e474eb475713\mscorlib.ni.dll"은 release 버전의 ngen 이미지인 반면 "C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\mscorlib\d234b8b542d458d5c658964842ca41a3\mscorlib.ni.dll" 파일은 debug 버전의 ngen 이미지임을 알 수 있습니다. (debug 버전의 경우, 제가 알기로는 Visual Studio가 생성합니다.)

이렇게, 마이크로소프트는 새로운 버전의 닷넷이나 그에 대한 업데이트를 설치하는 경우 기본적으로 "profiler-enhanced image"는 생성해 두지 않습니다.




물론, 원한다면 "profiler-enhanced image"를 생성하는 것이 가능합니다. 예를 들어, 다음의 명령은 "mscorlib.dll"을 대상으로 "profiler-enhanced image"를 생성하게 됩니다.

//  관리자 권한으로 실행한 cmd.exe

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ngen install mscorlib.dll /Profile
Microsoft (R) CLR Native Image Generator - Version 4.7.2046.0
Copyright (c) Microsoft Corporation.  All rights reserved.
1>    Compiling assembly C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll (CLR v4.0.30319) ...

그런 다음 다시 "ngen display mscorlib /verbose" 명령어로 확인해 보면 다음과 같은 항목이 추가된 것을 볼 수 있습니다.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ngen display mscorlib /verbose
Microsoft (R) CLR Native Image Generator - Version 4.7.2046.0
Copyright (c) Microsoft Corporation.  All rights reserved.

NGEN Roots:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
    ScenarioProfile, Runtime version = v4.0.30319
        C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll
            DisplayName = mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
            Native image = {B94A5D4E-67C3-3334-F48D-732F7F3F719F}
            Hard Dependencies:
            Soft Dependencies:

NGEN Roots that depend on "mscorlib":

...[생략]...

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 <profiling>
        Source MVID:    {9FACBD76-75F5-45B1-86B6-BE0E55785D7F}
        NGen GUID sign: {B94A5D4E-67C3-3334-F48D-732F7F3F719F}
        OS:             WinNT
        Processor:      amd64
        Runtime:        4.0.30319.0
        clr.dll:        TimeStamp=597BD642, VirtualSize=009DF000
        Flags:          <profiling>
        Scenarios:              <no debug info> <no debugger> <instrumenting profiler> <no instrumentation>
        File:           C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\mscorlib\354feb5569398af61314e480a5b8d7ad\mscorlib.ni.dll
        No dependencies

이렇게 "profiler-enhanced image"가 있으면 (COR_PRF_USE_PROFILE_IMAGES 옵션을 사용하는) .NET Profiler의 동작이 다음과 같이 달라집니다.

// COR_PRF_USE_PROFILE_IMAGES + "No profiler-enhanced image" == 새로 JIT
C:\Windows\Microsoft.NET\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll

// COR_PRF_USE_PROFILE_IMAGES + "profiler-enhanced image" == instrumenting profiler로 지정된 native image 로드
C:\Windows\assembly\NativeImages_v4.0.30319_64\mscorlib\354feb5569398af61314e480a5b8d7ad\mscorlib.ni.dll

"profiler-enhanced image"가 없을 때는 원본 mscorlib.dll로부터 JIT를 다시 했지만, "profiler-enhanced image"가 있을 때는 NGen 처리된 그 모듈을 로드해 처리하기 때문에 JIT로 인한 로딩 시간이 그만큼 감소하는 효과를 보게 됩니다.




그런데, 여기서 궁금한 것이 있습니다. 그렇다면 닷넷 BCL의 경우 ngen을 이용해 "profiler-enhanced image"를 한번 생성해 두었는데 이후 보안 업데이트나 기능 업데이트로 인해 원본 파일이 바뀐다면 어떻게 되는 걸까요?

제 짐작으로는 다시 풀려서 그에 맞게 새로 생성해야 할 거라고 생각했는데요. 확인을 위해 Windows 7 VM을 올려 테스트해 봤습니다.

우선 .NET Framework 4를 설치 후 ngen /profile을 한 다음 .NET Framework 4.7을 설치해 본 것인데요. 예상과는 달리 닷넷 프레임워크 설치 프로그램은 똑똑했습니다. ^^ /profile 이미지가 있는 경우 새로운 버전의 닷넷 프레임워크가 설치되는 경우 그에 대해서도 함께 생성해 주었습니다. (혹시, 이렇게 동작한다는 문서 상의 기록을 찾으신 분 있으면 덧글 부탁드립니다. ^^)
일단 기존 생성한 /profile 이미지가 삭제되지 않은 체로 남아 있게 되지만 새로 설치된 .NET DLL들과 맞지 않으므로 사용이 안됩니다. 따라서 이런 경우 새롭게 /profile 이미지를 생성해야 합니다.




참고로, ngen display 결과가 너무 많은데 profiler-enhanced image가 생성되었음을 확인하는 정도의 용도라면 다음과 같이 하는 것이 더 간결한 출력 결과를 얻을 수 있습니다.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ngen display | find "<profiling>"
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 <profiling>




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/23/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2021-05-24 01시51분
Conversation about PGO
; https://devblogs.microsoft.com/dotnet/conversation-about-pgo/

.NET 7.0's Performance Improvements with Dynamic PGO are Incredible
; https://petabridge.com/blog/dotnet7-pgo-performance-improvements/
정성태

1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13311정성태4/7/20234537C/C++: 163. Visual Studio 2022 - DirectShow 예제 컴파일(WAV Dest)
13310정성태4/6/20234169C/C++: 162. Visual Studio - /NODEFAULTLIB 옵션 설정 후 수동으로 추가해야 할 library
13309정성태4/5/20234313.NET Framework: 2107. .NET 6+ FileStream의 구조 변화
13308정성태4/4/20234212스크립트: 47. 파이썬의 time.time() 실숫값을 GoLang / C#에서 사용하는 방법
13307정성태4/4/20233967.NET Framework: 2106. C# - .NET Core/5+ 환경의 Windows Forms 응용 프로그램에서 HINSTANCE 구하는 방법
13306정성태4/3/20233753Windows: 243. Win32 - 윈도우(cbWndExtra) 및 윈도우 클래스(cbClsExtra) 저장소 사용 방법
13305정성태4/1/20234140Windows: 242. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (쉬운 버전)파일 다운로드1
13304정성태3/31/20234447VS.NET IDE: 181. Visual Studio - C/C++ 프로젝트에 application manifest 적용하는 방법
13303정성태3/30/20233823Windows: 241. 환경 변수 %PATH%에 DLL을 찾는 규칙
13302정성태3/30/20234454Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234573Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20234219Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20233974Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20233959Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234618Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233941Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234225Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234380.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234442오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234558Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20234949.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234428.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233640Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233754Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20233931Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234375Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...