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/
정성태

... 91  92  93  94  95  96  97  98  99  100  101  102  103  104  [105]  ...
NoWriterDateCnt.TitleFile(s)
11018정성태8/9/201616859.NET Framework: 600. 단일 메서드 내에서의 할당으로 알아보는 자바와 닷넷의 GC 차이점 [1]
11017정성태8/9/201619961웹: 33. HTTP 쿠키에 한글 값을 설정하는 방법
11016정성태8/7/201616899개발 환경 구성: 291. Windows Server Containers 소개
11015정성태8/7/201615247오류 유형: 348. Windows Server 2016 TP5에서 Windows Containers의 docker run 실행 시 encountered an error during Start failed in Win32
11014정성태8/6/201616221오류 유형: 347. Hyper-V Virtual Machine Management service Account does not have permission to open attachment
11013정성태8/6/201626712개발 환경 구성: 290. Windows 10에서 경험해 보는 Windows Containers와 docker [4]
11012정성태8/6/201616945오류 유형: 346. Windows 10에서 Windows Containers의 docker run 실행 시 encountered an error during CreateContainer failed in Win32 발생
11011정성태8/6/201618698기타: 59. outlook.live.com 메일 서비스의 아웃룩 POP3 설정하는 방법
11010정성태8/6/201616288기타: 58. Outlook에 설정한 SMTP/POP3(예:천리안 메일) 계정 암호를 잊어버린 경우
11009정성태8/3/201621300개발 환경 구성: 289. 2016-08-02부터 시작된 윈도우 10 1주년 업데이트에서 Bash Shell 사용 [8]
11008정성태8/1/201614966오류 유형: 345. 2의 30승 이상의 원소를 갖는 경우 버그가 발생하는 이진 검색(Binary Search) 코드
11007정성태8/1/201616647오류 유형: 344. RDP ActiveX 컨트롤로 특정 PC에 연결할 수 없을 때, 오류 상황을 해결하기 위한 팁파일 다운로드1
11006정성태7/22/201618296개발 환경 구성: 288. SSL 인증서를 Azure Cloud Service에 적용하는 방법
11005정성태7/22/201617175개발 환경 구성: 287. Let's Encrypt 인증서 업데이트 주기: 90일
11004정성태7/22/201613388오류 유형: 343. Invalid service definition or service configuration. Please see the Error List for more details.
11003정성태7/20/201620407VS.NET IDE: 110. Visual Studio 2015에서 .NET Core 응용 프로그램 개발 [1]
11002정성태7/20/201614395개발 환경 구성: 286. Microsoft Azure 서비스의 구독은 반드시 IE로!
11001정성태7/19/201624930.NET Framework: 599. .NET Core/SDK 설치 및 기본 사용법 [6]
11000정성태7/16/201614051오류 유형: 342. Microsoft Visual Studio 2010 Tools for Office Runtime (x86 and x64) 설치 시 오류
10999정성태7/16/201616209오류 유형: 341. .NET Framework 4.5.2가 설치 안 되는 경우
10998정성태7/16/201615842.NET Framework: 598. C# - Excel 시트에 윈도우 폼 기능을 추가하는 방법 [1]파일 다운로드1
10997정성태7/16/201614534오류 유형: 340. HTTP Error 500.23 - Internal Server Error파일 다운로드1
10996정성태7/14/201620304Windows: 118. 유선 접속 상태에서 재부팅하면 무선 연결이 자동 연결 안되는 문제 [4]파일 다운로드1
10995정성태6/27/201614922VS.NET IDE: 109. Visual Studio 유료 버전 사용자의 주기적인 온라인 인증을 없애는 방법
10994정성태6/23/201614257개발 환경 구성: 285. 알고스팟(https://algospot.com)을 위한 Visual C++ 답안 작성 요령파일 다운로드1
10993정성태6/23/201614499.NET Framework: 597. 닷넷 메타데이터에 struct/class(값/참조 형식)의 구분이 있을까요?
... 91  92  93  94  95  96  97  98  99  100  101  102  103  104  [105]  ...