Programming

tar + gzip / bzip 압축 / 압축 해제를위한 멀티 코어 활용

procodes 2020. 5. 10. 11:49
반응형

tar + gzip / bzip 압축 / 압축 해제를위한 멀티 코어 활용


나는 보통 사용하여 압축 tar zcvf하고 압축을 해제합니다 tar zxvf(습관으로 인해 gzip 사용).

최근에 하이퍼 스레딩이 포함 된 쿼드 코어 CPU를 얻었으므로 8 개의 논리 코어가 있으며 압축 / 압축 해제 중에 많은 코어가 사용되지 않는 것을 알 수 있습니다.

사용하지 않는 코어를 활용하여 더 빠르게 만들 수있는 방법이 있습니까?


여러 코어에서 gzip 압축을 수행하는 gzip 대신 pigz사용할 수 있습니다 . -z 옵션을 사용하는 대신 pigz를 통해 파이프합니다.

tar cf - paths-to-archive | pigz > archive.tar.gz

기본적으로 pigz는 사용 가능한 코어 수를, 또는 쿼리 할 수없는 경우 8을 사용합니다. -pn로 더 많은 것을 요청할 수 있습니다 (예 : -p 32) pigz는 gzip과 동일한 옵션을 가지므로 -9로 더 나은 압축을 요청할 수 있습니다. 예 :

tar cf - paths-to-archive | pigz -9 -p 32 > archive.tar.gz

tar 플래그 "--use-compress-program ="을 사용하여 tar에 사용할 압축 프로그램을 알려줄 수도 있습니다.

예를 들어 다음을 사용하십시오.

tar -c --use-compress-program=pigz -f tar.file dir_to_zip 

일반적인 접근법

tar프로그램 옵션이 있습니다 :

-I, --use-compress-program PROG
      filter through PROG (must accept -d)

멀티 스레드 버전의 아카이버 또는 압축기 유틸리티를 사용할 수 있습니다.

가장 인기있는 멀티 스레드 등록 아카이브는 pigz (gzip 대신) 및 pbzip2 (대신의 bzip2). 예를 들어 :

$ tar -I pbzip2 -cf OUTPUT_FILE.tar.bz2 paths_to_archive
$ tar --use-compress-program=pigz -cf OUTPUT_FILE.tar.gz paths_to_archive

아카이버는 -d를 승인해야합니다. 교체 유틸리티가이 매개 변수가 아니거나 추가 매개 변수를 지정해야하는 경우 파이프를 사용하십시오 (필요한 경우 매개 변수 추가).

$ tar cf - paths_to_archive | pbzip2 > OUTPUT_FILE.tar.gz
$ tar cf - paths_to_archive | pigz > OUTPUT_FILE.tar.gz

단일 스레드 및 다중 스레드의 입력 및 출력이 호환됩니다. 다중 스레드 버전을 사용하여 압축하고 단일 스레드 버전을 사용하여 압축을 풀거나 그 반대로 압축을 풀 수 있습니다.

p7zip

압축을위한 p7zip의 경우 다음과 같은 작은 쉘 스크립트가 필요합니다.

#!/bin/sh
case $1 in
  -d) 7za -txz -si -so e;;
   *) 7za -txz -si -so a .;;
esac 2>/dev/null

7zhelper.sh로 저장하십시오. 사용 예는 다음과 같습니다.

$ tar -I 7zhelper.sh -cf OUTPUT_FILE.tar.7z paths_to_archive
$ tar -I 7zhelper.sh -xf OUTPUT_FILE.tar.7z

xz

멀티 스레드 XZ 지원과 관련하여. XZ Utils 버전 5.2.0 이상을 실행중인 경우 환경 변수 XZ_DEFAULTS (예 :)를 통해 적절한 값 으로 설정 -T하거나 압축하여 여러 코어를 압축 할 수 있습니다 .--threadsXZ_DEFAULTS="-T 0"

이것은 5.1.0alpha 버전의 사람 조각입니다.

멀티 스레드 압축 및 압축 해제는 아직 구현되지 않았으므로이 옵션은 현재 적용되지 않습니다.

그러나 스레딩이 활성화 된 상태에서 압축되지 않은 파일의 압축 해제에는 작동하지 않습니다. 버전 5.2.2의 man에서 :

스레드 압축 해제가 아직 구현되지 않았습니다. 블록 헤더에 크기 정보가있는 여러 블록이 포함 된 파일에서만 작동합니다. 멀티 스레드 모드로 압축 된 모든 파일은이 조건을 충족하지만 --block-size = size를 사용하더라도 단일 스레드 모드로 압축 된 파일은 그렇지 않습니다.

교체를 통한 재 컴파일

소스에서 tar를 빌드하면 매개 변수를 사용하여 다시 컴파일 할 수 있습니다.

--with-gzip=pigz
--with-bzip2=lbzip2
--with-lzip=plzip

이 옵션으로 tar를 다시 컴파일 한 후 tar의 도움말 출력을 확인할 수 있습니다.

$ tar --help | grep "lbzip2\|plzip\|pigz"
  -j, --bzip2                filter the archive through lbzip2
      --lzip                 filter the archive through plzip
  -z, --gzip, --gunzip, --ungzip   filter the archive through pigz

-Itar --use-compress-program스위치 바로 가기 사용하고 pbzip2여러 코어에서 bzip2 압축을 호출 할 수 있습니다 .

tar -I pbzip2 -cf OUTPUT_FILE.tar.bz2 DIRECTORY_TO_COMPRESS/

파일 이름 및 압축 옵션을보다 유연하게 사용하려면 다음을 사용할 수 있습니다.

find /my/path/ -type f -name "*.sql" -o -name "*.log" -exec \
tar -P --transform='s@/my/path/@@g' -cf - {} + | \
pigz -9 -p 4 > myarchive.tar.gz

1 단계: find

find /my/path/ -type f -name "*.sql" -o -name "*.log" -exec

This command will look for the files you want to archive, in this case /my/path/*.sql and /my/path/*.log. Add as many -o -name "pattern" as you want.

-exec will execute the next command using the results of find: tar

Step 2: tar

tar -P --transform='s@/my/path/@@g' -cf - {} +

--transform is a simple string replacement parameter. It will strip the path of the files from the archive so the tarball's root becomes the current directory when extracting. Note that you can't use -C option to change directory as you'll lose benefits of find: all files of the directory would be included.

-P tells tar to use absolute paths, so it doesn't trigger the warning "Removing leading `/' from member names". Leading '/' with be removed by --transform anyway.

-cf - tells tar to use the tarball name we'll specify later

{} + uses everyfiles that find found previously

Step 3: pigz

pigz -9 -p 4

Use as many parameters as you want. In this case -9 is the compression level and -p 4 is the number of cores dedicated to compression. If you run this on a heavy loaded webserver, you probably don't want to use all available cores.

Step 4: archive name

> myarchive.tar.gz

Finally.

참고URL : https://stackoverflow.com/questions/12313242/utilizing-multi-core-for-targzip-bzip-compression-decompression

반응형