yeonseong
Cache Flush, Cache Clean, Cache Invalidate 본문
Cache Flush, Cache Clean, Cache Invalidate
1. 개요
현대 멀티코어 프로세서 시스템에서 캐시는 CPU와 메인 메모리 간의 성능 격차를 해결하는 핵심 구성요소이다[1][2]. 그러나 캐시 메모리의 효율적 관리를 위해서는 Cache Flush, Cache Clean, Cache Invalidate라는 세 가지 핵심 기법에 대한 명확한 이해가 필요하다[1][3]. 이들 기법은 캐시와 메인 메모리 간의 데이터 일관성(Data Consistency)을 보장하며, 특히 Write-Back 캐시 정책[4][5] 및 DMA(Direct Memory Access) 환경[6][7]에서 필수적인 역할을 수행한다.
2. 핵심 기술 정의 및 원리
2.1 Cache Clean
Cache Clean은 캐시 라인의 Dirty Data를 메인 메모리에 기록하는 작업이다[1][8]. 이 과정에서 캐시 라인의 Dirty Bit는 0으로 변경되지만, 캐시 라인 자체는 유지된다[3][9].
2.2 Cache Invalidate
Cache Invalidate는 캐시 라인의 Valid Flag를 0으로 변경하여 해당 캐시 라인을 무효화하는 작업이다[1][10]. 이는 캐시 라인을 초기 부팅 상태로 되돌리는 효과를 가진다[10].
2.3 Cache Flush
Cache Flush는 Clean + Invalidate의 복합 동작으로, 캐시 라인의 데이터를 메인 메모리에 기록한 후 해당 캐시 라인을 무효화한다[1][11]. ARM 아키텍처에서는 이를 Clean and Invalidate로 표현하기도 한다[3][12].
3. 캐시 상태 비트 및 동작 원리
3.1 Valid Bit와 Dirty Bit
캐시 라인의 상태는 두 가지 핵심 비트로 관리된다[9][13]:
- Valid Bit: 캐시 라인이 유효한 데이터를 포함하는지 표시
- Dirty Bit: 캐시 데이터가 메인 메모리와 불일치하는지 표시
3.2 Write-Back vs Write-Through
Write-Back 정책에서는 캐시에만 데이터를 기록하고 나중에 메인 메모리에 반영하므로 Cache Clean/Flush가 필수적이다[4][5]. 반면 Write-Through 정책은 캐시와 메인 메모리에 동시에 기록하므로 이러한 기법이 불필요하다[14][4].
4. 캐시 일관성 프로토콜과의 연관성
4.1 MESI 프로토콜
멀티코어 환경에서 MESI 프로토콜[2][15]은 네 가지 캐시 상태를 정의한다:
상태 | 의미 | Clean/Flush 연관성 |
---|---|---|
Modified (M) | 해당 코어만 소유, 메모리와 불일치 | Flush 필요 |
Exclusive (E) | 해당 코어만 소유, 메모리와 일치 | Clean 상태 |
Shared (S) | 다중 코어 공유, 메모리와 일치 | Clean 상태 |
Invalid (I) | 무효한 데이터 | Invalidate 상태 |
4.2 Bus Snooping
버스 스누핑(Bus Snooping)[2][15] 메커니즘을 통해 각 코어는 다른 코어의 메모리 접근을 모니터링하며, 필요시 자동으로 Invalidate 또는 Clean 동작을 수행한다[16].
5. DMA와 캐시 일관성
5.1 DMA 환경에서의 문제점
DMA 컨트롤러는 CPU 캐시를 우회하여 메인 메모리에 직접 접근하므로 캐시 일관성 문제가 발생한다[6][7]:
- DMA Read: 캐시의 최신 데이터가 메인 메모리에 반영되지 않아 DMA가 구식 데이터를 읽을 수 있음
- DMA Write: DMA가 메인 메모리에 기록한 데이터가 CPU 캐시에 반영되지 않음
5.2 해결 방안
1. DMA 시작 전: Cache Clean 수행 (CPU 캐시 → 메인 메모리)
2. DMA 완료 후: Cache Invalidate 수행 (CPU 캐시 무효화)
이러한 방식으로 Non-Coherent DMA 환경에서도 데이터 일관성을 보장할 수 있다[7][17].
6. Write Buffer와의 상호작용
6.1 Write Buffer 개념
Write Buffer[18][19]는 캐시와 메인 메모리 사이의 중간 버퍼로, Write-Through 환경에서 성능을 향상시킨다. CPU는 캐시와 Write Buffer에 병렬로 데이터를 기록하며, Write Buffer는 비동기적으로 메인 메모리에 데이터를 전송한다[20].
6.2 Write Merging 최적화
Write Merging[18] 기법을 통해 연속된 주소의 쓰기 작업을 하나의 버퍼 엔트리로 결합하여 메인 메모리 접근 횟수를 줄일 수 있다.
7. 실제 적용 사례 및 구현 고려사항
7.1 ARM 프로세서에서의 구현
ARM 아키텍처는 다음과 같은 캐시 유지보수 명령어를 제공한다[21][10]:
cleanDCache
: 모든 D캐시를 CleancleanFlushDCache
: 모든 D캐시를 Clean하고 FlushcleanFlushCache
: I캐시와 D캐시를 모두 Clean하고 Flush
7.2 캐시 라인 정렬 고려사항
캐시 유지보수 작업 시 캐시 라인 크기(일반적으로 32바이트)에 맞춰 주소와 길이를 정렬해야 한다[7]. 정렬되지 않은 주소나 길이는 일부 캐시 라인이 처리되지 않을 수 있다.
8. 성능 최적화 및 활용 분야
8.1 컨텍스트 스위칭
프로세스 간 컨텍스트 스위칭에서는 Cache Flush가 발생하여 새로운 프로세스의 데이터로 캐시를 갱신한다[22]. 그러나 스레드 간 스위칭에서는 주소 공간 공유로 인해 선택적 Flush만 수행된다.
8.2 I/O 시스템 통합
EMAC(Ethernet Media Access Controller) 등의 네트워크 인터페이스에서 Buffer Descriptor 기반 DMA 전송 시 적절한 캐시 유지보수가 필수적이다[6].
9. 기술 동향 및 발전 방향
9.1 하드웨어 지원 확대
최신 프로세서들은 Hardware Cache Coherency Interface(CCI)[7]를 통해 자동으로 DMA 일관성을 지원하는 추세이다. 이는 소프트웨어의 명시적 캐시 유지보수 부담을 줄여준다.
9.2 비휘발성 메모리 통합
PCM(Phase Change Memory) 및 STT-RAM 등의 비휘발성 메모리 도입으로 새로운 형태의 Unified Buffer Cache Architecture[23]가 등장하고 있다.
9.3 AI 가속기와의 통합
AI 가속기와 CPU 간의 대용량 데이터 교환에서 효율적인 캐시 관리 기법이 더욱 중요해지고 있으며, Cache-to-Cache Transfer[2] 최적화가 핵심 과제로 부상하고 있다.
10. 결론 및 제언
Cache Flush, Clean, Invalidate는 현대 컴퓨터 시스템의 메모리 계층 구조에서 데이터 일관성을 보장하는 핵심 메커니즘이다. 특히 멀티코어 환경과 DMA 기반 I/O 시스템에서 이들 기법의 정확한 이해와 적용은 시스템 안정성과 성능에 직결된다[2][6].
향후 하드웨어 지원 확대와 새로운 메모리 기술 도입으로 캐시 관리의 복잡성이 증가할 것으로 예상되므로, 시스템 설계자는 이러한 기술 동향을 지속적으로 추적하여 최적화된 캐시 관리 전략을 수립해야 한다. 또한 소프트웨어와 하드웨어의 협력적 접근을 통해 더욱 효율적인 캐시 일관성 솔루션을 개발하는 것이 필요하다.
Sources
[1] clean, Invalidate, Flush - cache - December Dream - 티스토리 https://decdream.tistory.com/552
[2] How the MESI Protocol Keeps Multi-Core CPUs Consistent https://dev.to/sachin_tolay_052a7e539e57/cache-coherence-how-the-mesi-protocol-keeps-multi-core-cpus-consistent-170j
[3] cache의 clean과 flush - Kernel http://www.iamroot.org/xe/index.php?mid=Kernel&document_srl=17266
[4] Write Through and Write Back in Cache https://www.geeksforgeeks.org/computer-organization-architecture/write-through-and-write-back-in-cache/
[5] cpu architecture - Write-back vs Write-Through caching? https://stackoverflow.com/questions/27087912/write-back-vs-write-through-caching
[6] Cache coherency- Risks and Resolution in case of Direct ... https://www.einfochips.com/blog/cache-coherency-risks-and-resolution-in-case-of-direct-memory-access-dma/
[7] Hint: DMA and cache coherency https://community.st.com/t5/stm32-mcus-products/hint-dma-and-cache-coherency/td-p/439559
[8] Cache Clean, Flush < 도리의 디지털라이프 https://blog.skby.net/cache-clean-flush/
[9] What is cache line? http://open-cas.com/cache_line.html
[10] [Arm프로세서] 캐시: 캐시 관련 용어 알아보기(Arm 아키텍처 관점) https://austindhkim.tistory.com/498
[11] Cache - Flush, clean, Invalidate - December Dream https://decdream.tistory.com/194
[12] 캐시를 인에이블(Enable) 하려면? - 임베디드 레시피 https://recipes.tistory.com/221
[13] CPU cache https://en.wikipedia.org/wiki/CPU_cache
[14] What is the main difference between Write-through and ... https://www.infortrend.com/au/support/FAQ/Content/500
[15] Cache Coherence & MESI Protocol: Multi-Core CPU ... https://kitemetric.com/blogs/cache-coherence-mastering-the-mesi-protocol-in-multi-core-cpus
[16] Difference between Write invalidate protocol & ... https://www.geeksforgeeks.org/computer-organization-architecture/difference-between-write-invalidate-protocol-write-update-protocol-bus-snooping/
[17] Cache coherency implications https://developer.arm.com/documentation/den0042/latest/Memory-Ordering/Cache-coherency-implications
[18] Cache Optimization – Merging Write Buffer - The Beard Sage http://thebeardsage.com/cache-optimization-merging-write-buffer/
[19] Write buffer https://en.wikipedia.org/wiki/Write_buffer
[20] 5. Write Strategy - 혼자하는 코딩 - 티스토리 https://gofo-coding.tistory.com/entry/5-Write-Strategy
[21] [ARM] 캐시 2 - 지밍이 블로그 https://jiming.tistory.com/193
[22] 캐시 플러시 관련 궁굼한게 있어서 질문 드립니다!! https://www.inflearn.com/community/questions/1016921/%EC%BA%90%EC%8B%9C-%ED%94%8C%EB%9F%AC%EC%8B%9C-%EA%B4%80%EB%A0%A8-%EA%B6%81%EA%B5%BC%ED%95%9C%EA%B2%8C-%EC%9E%88%EC%96%B4%EC%84%9C-%EC%A7%88%EB%AC%B8-%EB%93%9C%EB%A6%BD%EB%8B%88%EB%8B%A4
[23] A Unified Buffer Cache Architecture that Subsumes Journaling ... https://dl.acm.org/doi/abs/10.1145/2560010
[24] Cache Coherence https://compas.cs.stonybrook.edu/~nhonarmand/courses/sp18/cse502/res/610/04-coherence.pdf
[25] cache flush https://velog.io/@agnusdei1207/cache-flush
[26] Introduction, SMP and Snooping Cache Coherence Protocol https://passlab.github.io/CSCE513/notes/lecture23_TLP_IntroSMPSnooping.pdf
[27] Lecture-22 (Cache Coherence Protocols) CS422-Spring ... https://www.cse.iitk.ac.in/users/biswap/CS422/L22-CC.pdf
[28] Dragon protocol https://en.wikipedia.org/wiki/Dragon_protocol
[29] [컴퓨터구조][Cache Coherence] - Monkey Engineer - 티스토리 https://monkey-engineer.tistory.com/49
[30] MESI protocol https://en.wikipedia.org/wiki/MESI_protocol
[31] Cache, Cash? - 임베디드 레시피 - 티스토리 https://recipes.tistory.com/385
[32] 캐시 메모리 (캐시 일관성, 캐시의 쓰기 정책) - about IT - 티스토리 https://shuu.tistory.com/49
[33] Coherency protocol https://developer.arm.com/documentation/ddi0360/latest/level-1-memory-system/coherency-protocol
[34] Write Through, Write Back - 개발자비행일지 - 티스토리 https://cyber0946.tistory.com/81
[35] Cache Coherence II – Computer Architecture https://www.cs.umd.edu/~meesh/411/CA-online/chapter/312/index.html
[36] Organization of dirty bits for a write-back cache https://patents.google.com/patent/US7380070B2/en
[37] CHI protocol fundamentals https://developer.arm.com/documentation/102407/latest/CHI-protocol-fundamentals
[38] Cache Coherence Protocols in Multiprocessor System https://www.geeksforgeeks.org/computer-organization-architecture/cache-coherence-protocols-in-multiprocessor-system/
[39] [cache] Write Through와 Write Back 비교 - 히포 - 티스토리 https://hippogrammer.tistory.com/286
[40] What does a cache line in a CPU consist of besides ... https://stackoverflow.com/questions/74475788/what-does-a-cache-line-in-a-cpu-consist-of-besides-the-usual-tags-data-and-dir
[41] 캐시 일관성(cache coherence)과 프로토콜 - fish - 티스토리 https://fish9903.tistory.com/entry/%EB%A9%80%ED%8B%B0%EC%BD%94%EC%96%B4%EC%97%90%EC%84%9C%EC%9D%98-%EC%BA%90%EC%8B%9C-%EC%9D%BC%EA%B4%80%EC%84%B1cache-coherence
[42] [컴퓨터 구조] Write-Through/Write-Back, Allocate/no-Allocate https://electronic-hwan.tistory.com/entry/%EC%BB%B4%ED%93%A8%ED%84%B0-%EA%B5%AC%EC%A1%B0-Write-ThroughWrite-Back-Allocate
[43] MESI and MOESI protocols https://developer.arm.com/documentation/den0013/latest/Multi-core-processors/Cache-coherency/MESI-and-MOESI-protocols
[44] 스토리지 cache 모드 비교 : write through vs write back - 쩨로그 https://jaeho.tistory.com/entry/%EC%8A%A4%ED%86%A0%EB%A6%AC%EC%A7%80-cache-%EB%AA%A8%EB%93%9C-%EB%B9%84%EA%B5%90-write-through-vs-write-back
[45] Single Cache Line - an overview https://www.sciencedirect.com/topics/computer-science/single-cache-line
[46] Maintaining Cache Coherence with MESI https://www.youtube.com/watch?v=AAplcLi-2_Q
[47] 캐시(Cache) 알아보기 - yoongrammer - 티스토리 https://yoongrammer.tistory.com/101
[48] Cache invalidation https://en.wikipedia.org/wiki/Cache_invalidation
[49] linux - DMA cache coherence management https://stackoverflow.com/questions/7132284/dma-cache-coherence-management
[50] why we use write buffer in mips?[cache] https://stackoverflow.com/questions/33783862/why-we-use-write-buffer-in-mipscache
[51] [리눅스 커널] DMA - cache consistency https://yohda.tistory.com/entry/%EB%A6%AC%EB%88%85%EC%8A%A4-%EC%BB%A4%EB%84%90-cache-DMA-consistency
[52] Cache architecture https://developer.arm.com/documentation/ddi0092/latest/caches-and-write-buffer/cache-architecture
[53] The word-invalidate cache coherence protocol https://www.sciencedirect.com/science/article/pii/0141933195010602
[54] DMA buffer cache coherence with SMMU - Jetson AGX Orin https://forums.developer.nvidia.com/t/dma-buffer-cache-coherence-with-smmu/231318
[55] About the caches and write buffer https://developer.arm.com/documentation/ddi0135/latest/Caches-and-Write-Buffer/About-the-caches-and-write-buffer
[56] Cache invalidation patterns in shared-memory ... https://courses.grainger.illinois.edu/cs533/sp2012/reading_list/1d.pdf
[57] (Solved) Is DMA cache coherent? - OSDev.org https://forum.osdev.org/viewtopic.php?t=32232
'정보관리기술사' 카테고리의 다른 글
캐시 메모리와 가상 메모리 비교 (0) | 2025.09.07 |
---|---|
메모리 계층구조 (2) | 2025.08.31 |
CPU의 레지스터 종류와 기능설명 (5) | 2025.08.23 |
컴퓨터 메인보드의 노스브릿지와 사우스브릿지 (3) | 2025.08.17 |
제어 신호 생성을 위한 수직적, 수평적 마이크로 프로그래밍 (3) | 2025.08.10 |