linux tar命令及cpu控制
如果限制其对CPU的使用率?
1. You can use pv to throttle the bandwidth of a pipe. Since your use case is strongly IO-bound, the added CPU overhead of going through a pipe shouldn't be noticeable, and you don't need to do any CPU throttling.
tar cf - mydata | pv -L 1m >/media/MYDISK/backup.tar
2. You can try the cpulimit tool which does limit the CPU percentage. It is not a standard tool, so you will have to install it.
cpulimit --limit=10 /bin/gzip vzdump-openvz-102-2012_06_26-19_01_11.tar
需要安装cpulimit
3. You can't really get a process to run less. You can use nice to give it a lower priority, but that's in relation to other processes. The way to run the CPU cooler while a process runs is to use usleep(3) to force the process out of the run state a certain amount of time, but that would involve either patching tar or using the LD_PRELOAD mechanism to provide a patched function that tar uses a lot (e.g. fopen(3)).
I suspect your best workarounds are the hardware ones you've mentioned on SuperUser: keeping the laptop cool and/or lowering the CPU clock.
An annoying but possibly viable workaround (a kludge, really) works at a ‘macroscopic’ level. Rather than making tar run 100ms every 200ms, you could make it run one second out of every two. Note: this is a horrible, horrible kludge. But hey, it might even work!
tar cjf some-file.tar.bz2 /some-directory &
while true; do
sleep 1 # Let it run for a second
kill -STOP $! 2>/dev/null || break
sleep 1 # Pause it for a second
kill -CONT $! 2>/dev/null || break
done
The first sleep adjusts sleep time, the second one adjusts runtime. As it stands now, it's got a 50% duty cycle. To keep the temperature down, you will very likely need to reduce the duty cycle to perhaps 25% or lower (1 second run, 3 seconds sleep = 1 of every 4 seconds = 25% duty cycle). The shell command sleep can take fractional times, by the way. So you could even say sleep 0.1. Keep it over 0.001 just to be sure, and don't forget that script execution adds to the time too.
4. A more general purpose way of limiting the CPU is to use /sys. This seems like what you want anyway since things other than tar are capable of performing computationally expensive tasks, its just that youre seeing tar do it the most.
The way to do this is to:
go to /sys/devices/system/cpu/cpuX/cpufreq for each of your CPUs (replace cpuX with each cpu).
Then look in the file scaling_available_frequencies to see what frequencies your CPU supports.
Pick a frequency (lets say 1234567) and do echo 1234567 > scaling_max_freq
This will prevent the CPU from ever going above the specified frequency.
5. If you wish to run a command which typically uses a lot of CPU (for example, running tar on a large file), then you probably don’t want to bog down your whole system with it. Linux systems provide the nice command to control your process priority at runtime, or renice to change the priority of an already running process. The full manpage has help, but the command if very easy to use:
$ nice -n prioritylevel /command/to/run
The priority level runs from -20 (top priority) to 19 (lowest). For example, to run tar and gzip at a the lowest priority level:
$ nice -n 19 tar -czvf file.tar.gz bigfiletocompress
similarly, if you have a process running, use ps to find the process ID, and then use renice to change it’s priority level:
$ renice -n 19 -p 987 32
This would change processes 987 and 32 to priority level 19.
如果限制其对IO的使用率?
1. Then I would recommend ionice for limiting the IO usage, though it is the concurrent access that would be limited, not the maximum throughput... Never-the-less here is how to use it:
ionice -c 3 <your_command>
A sudden outburst of violent disk I/O activity can bring down mail or web server. Usually, a web / mysql or mail server serving millions and millions pages per months are prone to this kind of problem. Backup activity can increase current system load from usual 5 to 20 (or more).
To avoid this kind of sudden outburst problem, run your script with scheduling class and priority. Linux comes with various utilities to manage this kind of madness.
CFQ scheduler
You need Linux kernels 2.6.13+ with the CFQ IO scheduler. CFQ (Completely Fair Queuing) is an I/O scheduler for the Linux kernel, which is default in 2.6.18+ kernel. RHEL 4/ 5 and SuSE Linux has all scheduler built into kernel so no need to rebuild your kernel. To find out your scheduler name, enter:
# for d in /sys/block/sd[a-z]/queue/scheduler; do echo "$d => $(cat $d)" ; done
Sample output for each disk:
/sys/block/sda/queue/scheduler => noop anticipatory deadline [cfq] /sys/block/sdb/queue/scheduler => noop anticipatory deadline [cfq] /sys/block/sdc/queue/scheduler => noop anticipatory deadline [cfq]
CFQ is default and recommended for good performance.
Old good nice program
You can run a program with modified scheduling priority using nice command (19 = least favorable):
# nice -n19 /path/to/backup.sh
Sample cronjob:
@midnight /bin/nice -n19 /path/to/backup.sh
ionice utility
ionice command provide more control as compare to nice command. This program sets the io scheduling class and priority for a program or script. It supports following three scheduling classes (quoting from the man page):
- Idle : A program running with idle io priority will only get disk time when no other program has asked for disk io for a defined grace period. The impact of idle io processes on normal system activity should be zero. This scheduling class does not take a priority argument.
- Best effort : This is the default scheduling class for any process that hasn’t asked for a specific io priority. Programs inherit the CPU nice setting for io priorities. This class takes a priority argument from 0-7, with lower number being higher priority. Programs running at the same best effort priority are served in a round-robin fashion. This is usually recommended for most application.
- Real time : The RT scheduling class is given first access to the disk, regardless of what else is going on in the system. Thus the RT class needs to be used with some care, as it can starve other processes. As with the best effort class, 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window. This is should be avoided for all heavily loaded system.
How do I use ionice command?
Linux refers the scheduling class using following number system and priorities:
Scheduling class | Number | Possible priority |
real time | 1 | 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window |
best-effort | 2 | 0-7, with lower number being higher priority |
idle | 3 | Nil ( does not take a priority argument) |
To display the class and priority of the running process, enter:
# ionice -p {PID}
# ionice -p 1
Sample output:
none: prio 0
Dump full web server disk / mysql backup using best effort scheduling (2) and 7 priority:
# /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full
Open another terminal and watch disk I/O network stats using atop or top or your favorite monitoring tool:
# atop
Sample cronjob:
@weekly /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full >/dev/null 2>&1
You can set process with PID 1004 as an idle io process, enter:
# ionice -c3 -p 1004
Runs rsync.sh script as a best-effort program with highest priority, enter:
# ionice -c2 -n0 /path/to/rsync.sh
Finally, you can combine both nice and ionice together:
# nice -n 19 ionice -c2 -n7 /path/to/shell.script
Related: chrt command to set / manipulate real time attributes of a Linux process and taskset command to retrieve or set a processes's CPU affinity.
Other suggestion to improve disk I/O
- Use hardware RAID controller.
- Use fast SCSI / SA-SCSI / SAS 15k speed disk.
- Use fast SSD based storage (costly option).
- Use slave / passive server to backup MySQL
连接:
命令功能:ionice – 获取或设置程序的IO调度与优先级。 命令格式: ionice [[-c class] [-n classdata] [-t]] -p PID [PID]… ionice [-c class] [-n classdata] [-t] COMMAND [ARG]… IO调度策略: ionice将磁盘IO调度分为三类: ilde:空闲磁盘调度,该调度策略是在当前系统没有其他进程需要进行磁盘IO时,才能进行磁盘;因此该策略对当前系统的影响基本为0;当然,该调度策略不能带有任何优先级参数;目前,普通用户是可以使用该调度策略(自从内核2.6.25开始)。 Best effort:是缺省的磁盘IO调度策略;(1)该调度策略可以指定优先级参数(范围是0~7,数值越小,优先级越高);(2)针对处于同一优先级的程序将采round-robin方式;(3)对于best effort调度策略,8个优先级等级可以说明在给定的一个调度窗口中时间片的大小。(4)目前,普调用户(非root用户)是可以使用该调度策略。(5)在内核2.6.26之前,没有设置IO优先级的进程会使用“none”作为调度策略,但是这种策略使得进程看起来像是采用了best effort调度策略,因为其优先级是通过关于cpu nice有关的公式计算得到的:io_priority = (cpu_nice + 20) /5。(6)在内核2.6.26之后,如果当前系统使用的是CFQ调度器,那么如果进程没有设置IO优先级级别,将采用与内核2.6.26之前版本同样的方式,推到出io优先级级别。 Real time:实时调度策略,如果设置了该磁盘IO调度策略,则立即访问磁盘,不管系统中其他进程是否有IO。因此使用实时调度策略,需要注意的是,该访问策略可能会使得其他进程处于等待状态。 参数说明: -c class :class表示调度策略,其中0 for none, 1 for real time, 2 for best-effort, 3 for idle。 -n classdata:classdata表示IO优先级级别,对于best effort和real time,classdata可以设置为0~7。 -p pid:指定要查看或设置的进程号或者线程号,如果没有指定pid参数,ionice will run the listed program with the given parameters。 -t :忽视设置优先级时产生的错误。
命令用法:nice [选项] [命令 [参数]...]
以指定的优先级运行命令,这会影响相应进程的调度。如果不指定命令,程序会显示当前的优先级。优先级的范围是从 -20(最大优先级) 到 19 (最小优先级)。 -n, --adjustment=N 对优先级数值加上指定整数N (默认为10) --help 显示此帮助信息并退出 --version 显示版本信息并退出注意:您的shell 内含自己的nice 程序版本,它会覆盖这里所提及的相应版本。请查阅您的shell 文档获知它所支持的选项。
Rsync命令参数详解 在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命令来实现将服务器端的文件备份到客户端来。rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。Rsync的命令格式可以为以下六种: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC DEST rsync [OPTION]... [USER@]HOST::SRC DEST rsync [OPTION]... SRC [USER@]HOST::DEST rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] 对应于以上六种命令格式,rsync有六种不同的工作模式: 1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup 2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src 3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data 4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack 5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www 6)列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://172.16.78.192/www rsync参数的具体解释如下: -v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验 -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD -r, --recursive 对子目录以递归模式处理 -R, --relative 使用相对路径信息 -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。 --backup-dir 将备份文件(如~filename)存放在在目录下。 -suffix=SUFFIX 定义备份文件前缀 -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件) -l, --links 保留软链结 -L, --copy-links 想对待常规文件一样处理软链结 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结 --safe-links 忽略指向SRC路径目录树以外的链结 -H, --hard-links 保留硬链结 -p, --perms 保持文件权限 -o, --owner 保持文件属主信息 -g, --group 保持文件属组信息 -D, --devices 保持设备文件信息 -t, --times 保持文件时间信息 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 -n, --dry-run现实哪些文件将被传输 -W, --whole-file 拷贝文件,不进行增量检测 -x, --one-file-system 不要跨越文件系统边界 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件 --delete 删除那些DST中SRC没有的文件 --delete-excluded 同样删除接收端那些被该选项指定排除的文件 --delete-after 传输结束以后再删除 --ignore-errors 及时出现IO错误也进行删除 --max-delete=NUM 最多删除NUM个文件 --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输 --force 强制删除目录,即使不为空 --numeric-ids 不将数字的用户和组ID匹配为用户名和组名 --timeout=TIME IP超时时间,单位为秒 -I, --ignore-times 不跳过那些有同样的时间和长度的文件 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 -T --temp-dir=DIR 在DIR中创建临时文件 --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份 -P 等同于 --partial --progress 显示备份过程 -z, --compress 对备份的文件在传输时进行压缩处理 --exclude=PATTERN 指定排除不需要传输的文件模式 --include=PATTERN 指定不排除而需要传输的文件模式 --exclude-from=FILE 排除FILE中指定模式的文件 --include-from=FILE 不排除FILE指定模式匹配的文件 --version 打印版本信息 --address 绑定到特定的地址 --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件 --port=PORT 指定其他的rsync服务端口 --blocking-io 对远程shell使用阻塞IO -stats 给出某些文件的传输状态 --progress 在传输时现实传输过程 --log-format=formAT 指定日志文件格式 --password-file=FILE 从FILE中得到密码 --bwlimit=KBPS 限制I/O带宽,KBytes per second -h, --help 显示帮助信息
更多推荐
所有评论(0)