如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等(转)

简介: 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作。数据专家们,我是在对你们说。你可能有一个4核或更多核的CPU,但我们合适的工具,例如 grep, bzip2, wc, awk, sed等等,都是单线程的,只能使用一个CPU内核。

你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作。数据专家们,我是在对你们说。你可能有一个4核或更多核的CPU,但我们合适的工具,例如 grepbzip2wcawksed等等,都是单线程的,只能使用一个CPU内核。

借用卡通人物Cartman的话,“如何我能使用这些内核”?

要想让Linux命令使用所有的CPU内核,我们需要用到GNU Parallel命令,它让我们所有的CPU内核在单机内做神奇的map-reduce操作,当然,这还要借助很少用到的–pipes 参数(也叫做spreadstdin)。这样,你的负载就会平均分配到各CPU上,真的。

BZIP2

bzip2是比gzip更好的压缩工具,但它很慢!别折腾了,我们有办法解决这问题。

以前的做法:

cat bigfile.bin | bzip2 --best > compressedfile.bz2

现在这样:

cat bigfile.bin | parallel --pipe --recend '' -k bzip2 --best > compressedfile.bz2

尤其是针对bzip2,GNU parallel在多核CPU上是超级的快。你一不留神,它就执行完成了。

GREP

如果你有一个非常大的文本文件,以前你可能会这样:

grep pattern bigfile.txt

现在你可以这样:

cat bigfile.txt | parallel  --pipe grep 'pattern'

或者这样:

cat bigfile.txt | parallel --block 10M --pipe grep 'pattern'

这第二种用法使用了 –block 10M参数,这是说每个内核处理1千万行——你可以用这个参数来调整每个CUP内核处理多少行数据。

AWK

下面是一个用awk命令计算一个非常大的数据文件的例子。

常规用法:

cat rands20M.txt | awk '{s+=$1} END {print s}'

现在这样:

cat rands20M.txt | parallel --pipe awk \'{s+=\$1} END {print s}\' | awk '{s+=$1} END {print s}'

这个有点复杂:parallel命令中的–pipe参数将cat输出分成多个块分派给awk调用,形成了很多子计算操作。这些子计算经过第二个管道进入了同一个awk命令,从而输出最终结果。第一个awk有三个反斜杠,这是GNU parallel调用awk的需要。

WC

想要最快的速度计算一个文件的行数吗?

传统做法:

wc -l bigfile.txt

现在你应该这样:

cat bigfile.txt | parallel  --pipe wc -l | awk '{s+=$1} END {print s}'

非常的巧妙,先使用parallel命令‘mapping’出大量的wc -l调用,形成子计算,最后通过管道发送给awk进行汇总。

SED

想在一个巨大的文件里使用sed命令做大量的替换操作吗?

常规做法:

sed s^old^new^g bigfile.txt

现在你可以:

cat bigfile.txt | parallel --pipe sed s^old^new^g

…然后你可以使用管道把输出存储到指定的文件里。

http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/

Utilizing multi core for tar+gzip/bzip compression/decompression

You can use pigz(http://zlib.net/pigz/) instead of gzip, which does gzip compression on multiple cores. Instead of using the -z option, you would pipe it through pigz:

tar cf - paths-to-archive | pigz > archive.tar.gz
By default, pigz uses the number of available cores, or eight if it could not query that. You can ask for more with -p n, e.g. -p 32. pigz has the same options as gzip, so you can request better compression with -9. E.g.

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

You can also use the tar flag "--use-compress-program=" to tell tar what compression program to use.
For example use:
tar -c --use-compress-program=pigz -f tar.file dir_to_zip

 

Common approach

There is option for tar program:

-I, --use-compress-program PROG
filter through PROG (must accept -d)
You can use multithread version of archiver or compressor utility.

Most popular multithread archivers are pigz (instead of gzip) and pbzip2 (instead of bzip2). For instance:

$ tar -I pbzip2 -cf OUTPUT_FILE.tar.bz2 paths_to_archive
$ tar --use-compress-program=pigz -cf OUTPUT_FILE.tar.gz paths_to_archive
Archiver must accept -d. If your replacement utility hasn't this parameter and/or you need specify additional parameters, then use pipes (add parameters if necessary):

$ tar cf - paths_to_archive | pbzip2 > OUTPUT_FILE.tar.gz
$ tar cf - paths_to_archive | pigz > OUTPUT_FILE.tar.gz
Input and output of singlethread and multithread are compatible. You can compress using multithread version and decompress using singlethread version and vice versa.

http://stackoverflow.com/questions/12313242/utilizing-multi-core-for-targzip-bzip-compression-decompression


 

相关文章
|
7天前
|
SQL 缓存 监控
|
7天前
|
前端开发 Linux Shell
|
1天前
|
存储 安全 Linux
深入理解 Linux 用户和用户组的基本概念 + 相关命令 (一篇就够)
深入理解 Linux 用户和用户组的基本概念 + 相关命令 (一篇就够)
|
1天前
|
Linux 数据库
Linux 常用基础命令(2024年最新篇)新手小白必看 初识Linux
Linux 常用基础命令(2024年最新篇)新手小白必看 初识Linux
|
1天前
|
存储 监控 Ubuntu
Linux 中常用的 systemd 命令讲解
Linux 中常用的 systemd 命令讲解
|
1天前
|
存储 缓存 Linux
【Linux常见基本命令,一文速通(一)】
【Linux常见基本命令,一文速通(一)】
5 0
|
2天前
|
Linux 数据库
如何在 Linux 中使用帮助命令?
【5月更文挑战第5天】
19 1
如何在 Linux 中使用帮助命令?
|
2天前
|
数据可视化 搜索推荐 Linux
|
4天前
|
Linux Shell 网络安全
网络安全中Dos和linux常用命令总结
本篇是对网安学习中,常用的命令做一个图文与命令示例,并对一些比较重要的dos和shell命令进行总结,方便自己后续学习进行查询,并希望能够给更多人有一个总结命令和了解命令的地方.
28 5
|
6天前
|
Linux
Linux系统ps命令
这些是一些常见的 `ps`命令选项和用法,用于查看系统中运行的进程及其相关信息。您可以根据需要选择合适的选项以满足您的任务要求。
18 0