본문 바로가기
Server

[VM] Linux Buffer/Cache 정리

by VENUSIM 2024. 12. 27.

여러 가지 방법 중 Jenkins를 통한 스케줄링을 선택하였다.

캐시를 지우는 3가지 명령어가 존재한다.

  • echo 1 > /proc/sys/vm/drop_caches는 pagecache 정리
  • echo 2 > /proc/sys/vm/drop_caches는 dentries, inode 정리
  • echo 3 > /proc/sys/vm/drop_caches는 dentries, inode, pagecache 정리

pagecache : 파일의 입출력(I/O)의 속도와 퍼포먼스를 높이기 위해 시스템이 할당한 메모리 영역(임시 메모리 저장소). 예를 들어 어떤 경로의 파일을 한 번 읽어들이면 시스템이 해당 파일 내용을 임시메모리에 저장시키는데 이후에 해당 파일을 다시 읽을 때 이를 새로 읽어들이지 않고 이 메모리에서 바로 불러오면 디스크의 읽기/쓰기 속도가 빨라지므로 효율이 높아짐. 윈도우 OS의 페이지 파일 같은 역할.

Linux 커널 2.4 이후부터는 buffercache는 pagecache 내부로 통합됨


dentry : directory entry의 줄임말로 예를 들어 /usr/share 같은 경로에서 usr과 share를 지칭.

inode : 파일과 디렉토리에 관한 정보를 담고 있는 자료구조. 예를 들어 파일의 퍼미션 정보, 디스크 상의 파일의 물리적 위치, 크기, 생성된 일시 정보 등을 저장.


따라서 현재 상황에서 공공존 메모리 임계에 도달하였을 때 가장 많은 비중을 차지하고 있었던 pagecache를 비우기 위해 echo 1 > /proc/sys/vm/drop_caches 명령어를 사용함.


cloud > clear-cache-cron 의 파이프라인으로 매일 자정 근처에서 실행되도록 구성하였다.

  1. ssh를 통해 터미널을 사용하기 위해 Publish over ssh plugin을 설치
  2. msocket 과 esocket ssh 설정
  3. 파이프라인 구성
    @Library('shared-library-teams') _ // Shared Library (Jenkins > System > Global Pipeline)
    
    pipeline {
        agent any
        
        stages {
            stage('clear english socket cache') {
                steps {
                    script {
                        sshPublisher(
        					publishers: [
        						sshPublisherDesc(
        							configName: 'esocket',
        							verbose: true,
        							transfers: [
        								sshTransfer(
        									cleanRemote: false,
        									excludes: '',
        									execCommand: '''
        									    cd /home/user
        									    ./cache_clear.sh
        									''',
        									execTimeout: 120000
        								)
        							]
        						)
        					]
        				)
                        SendInfraMsg(1, "buffer/cache clear")
                    }
                }
            }
            
            stage('clear math socket cache') {
                steps {
                    script {
                        sshPublisher(
        					publishers: [
        						sshPublisherDesc(
        							configName: 'msocket',
        							verbose: true,
        							transfers: [
        								sshTransfer(
        									cleanRemote: false,
        									excludes: '',
        									execCommand: '''
        									    cd /home/user
        									    ./cache_clear.sh
        									''',
        									execTimeout: 120000
        								)
        							]
        						)
        					]
        				)
                        SendInfraMsg(1, "buffer/cache clear")
                    }
                }
            }
        }
    }
     
  4. cache_clear 쉘 스크립트 작성

    LOG_FILE="/home/user/log/cache_clear.log"
    
    get_cache_info() {
        free -h
    }
    
    echo "$(date): Before dropping caches" >> "$LOG_FILE"
    get_cache_info >> "$LOG_FILE"
    
    echo 1 | sudo -s sync
    echo 1 | sudo -s tee /proc/sys/vm/drop_caches
    
    echo "$(date): After dropping caches" >> "$LOG_FILE"
    get_cache_info >> "$LOG_FILE"
     

로그 확인

 

 

댓글