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

... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12369정성태10/12/202013955Windows: 176. Raymond Chen이 한글날에 밝히는 윈도우의 한글 자모 분리 현상 [3]
12368정성태10/12/202010001오류 유형: 668. VSIX 확장 빌드 - The "GetDeploymentPathFromVsixManifest" task failed unexpectedly.
12367정성태10/12/202022790오류 유형: 667. Ubuntu - Temporary failure resolving 'kr.archive.ubuntu.com' [2]
12366정성태10/12/202011739.NET Framework: 950. C# 9.0 - (4) 원시 크기 정수(Native ints) [1]파일 다운로드1
12365정성태10/12/202010635.NET Framework: 949. C# 9.0 - (3) 람다 메서드의 매개 변수 무시(Lambda discard parameters)파일 다운로드1
12364정성태10/11/202011830.NET Framework: 948. C# 9.0 - (2) localsinit 플래그 내보내기 무시(Suppress emitting localsinit flag)파일 다운로드1
12363정성태10/11/202012765.NET Framework: 947. C# 9.0 - (1) 대상으로 형식화된 new 식(Target-typed new expressions) [2]파일 다운로드1
12362정성태10/11/20209562VS.NET IDE: 151. Visual Studio 2019에 .NET 5 rc/preview 적용하는 방법
12361정성태10/11/202011188.NET Framework: 946. C# 9.0을 위한 개발 환경 구성
12360정성태10/8/20208377오류 유형: 666. The type or namespace name '...' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?)
12359정성태10/7/20209903오류 유형: 665. Windows - 재부팅 후 iSCSI 연결이 끊기는 문제
12358정성태10/7/20209939오류 유형: 664. Web Deploy 설치 시 "A newer version of Microsoft Web Deploy 3.6 was found on this machine." 오류 [3]
12357정성태10/7/20207998오류 유형: 663. 이벤트 로그 - The storage optimizer couldn't complete retrim on New Volume
12356정성태10/7/202022936오류 유형: 662. ASP.NET Core와 500.19, 500.21 오류 (0x8007000d)
12355정성태10/3/20208073오류 유형: 661. Hyper-V Linux VM의 Internal 유형의 가상 Switch에 대한 IP 연결이 되지 않는 경우
12354정성태10/2/202021014오류 유형: 660. Web Deploy (msdeploy.axd) 실행 시 오류 기록 [1]
12353정성태10/2/202010814개발 환경 구성: 518. 비주얼 스튜디오에서 IIS 웹 서버로 "Web Deploy"를 이용해 배포하는 방법
12352정성태10/2/202011291개발 환경 구성: 517. Hyper-V Internal 네트워크에 NAT을 이용한 인터넷 연결 제공
12351정성태10/2/202010835오류 유형: 659. Nox 실행이 안 되는 경우 - Unable to bind to the underlying transport for ...
12350정성태9/25/202014332Windows: 175. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 [2]파일 다운로드1
12349정성태9/25/20209197Linux: 32. Ubuntu 20.04 - docker를 위한 tcp 바인딩 추가
12348정성태9/25/20209940오류 유형: 658. 리눅스 docker - Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
12347정성태9/25/202024122Windows: 174. WSL 2의 네트워크 통신 방법 [4]
12346정성태9/25/20209190오류 유형: 657. IIS - http://localhost 방문 시 Service Unavailable 503 오류 발생
12345정성태9/25/20208868오류 유형: 656. iisreset 실행 시 "Restart attempt failed." 오류가 발생하지만 웹 서비스는 정상적인 경우파일 다운로드1
12344정성태9/25/202010036Windows: 173. 서비스 관리자에 "IIS Admin Service"가 등록되어 있지 않다면?
... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...