下面占用代码,虽然已经很详细了,但没有一定linux基础 要谨慎操作,操作前最起码要熟悉每行代码功能,知道自己在干嘛!!!【如果占用出现意外不释放等情况不会解决就是个大麻烦】
[root@centos-73-iso-100g-test ~]# lscpu | grep CPU\(s\):CPU(s): 8NUMA node0 CPU(s): 0-7[root@centos-73-iso-100g-test ~]# [root@centos-73-iso-100g-test ~]# cat cpu.sh #!/bin/bash rm -rf /root/file.txt endless_loop() { echo -ne "i=0; while true ;do i=i+100; i=100;done" | /usr/bin/bash & } if [ $# != 2 ]; then echo "USAGE: $0 <cpus,sleep time>" exit 1; fi for i in `seq $1` do endless_loop pid_array[$i]=$!; done for i in "${pid_array[@]}"; do echo 'execute: kill' $i ; echo 'kill' $i >> /root/file.txt doneecho "If executed ctrl+C,Please execute the above lines manually"echo "Please wait $2 seconds" sleep $2 for i in `awk '{print $2}' /root/file.txt` ; doecho "kill $i"kill $idone[root@centos-73-iso-100g-test ~]#
top
并按一下1
,会列出所有cpu的使用率top - 21:43:41 up 13 min, 3 users, load average: 0.00, 0.01, 0.04Tasks: 194 total, 1 running, 193 sleeping, 0 stopped, 0 zombie%Cpu0 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu5 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu7 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 16267428 total, 15500496 free, 392744 used, 374188 buff/cacheKiB Swap: 10485756 total, 10485756 free, 0 used. 15559560 avail Mem
sh cpu.sh 3 120
【意思是占用3颗cpu,占用120秒】# 如果参数不够,该脚本是会报错的[root@centos-73-iso-100g-test ~]# sh cpu.sh 3USAGE: cpu.sh <cpus,sleep time>[root@centos-73-iso-100g-test ~]# # 执行以后可以就会出现下面内容【此时该脚本并不会结束,而是处于 sleep 120状态[root@centos-73-iso-100g-test ~]# sh cpu.sh 3 120execute: kill 3083execute: kill 3085execute: kill 3087If executed ctrl+C,Please execute the above lines manuallyPlease wait 120 seconds
[root@centos-73-iso-100g-test ~]# toptop - 21:45:21 up 15 min, 3 users, load average: 0.67, 0.16, 0.09Tasks: 199 total, 4 running, 195 sleeping, 0 stopped, 0 zombie%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni, 99.7 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu5 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu7 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 16267428 total, 15499112 free, 394012 used, 374304 buff/cacheKiB Swap: 10485756 total, 10485756 free, 0 used. 15558272 avail Mem
[root@centos-73-iso-100g-test ~]# sh cpu.sh 3 120execute: kill 3083execute: kill 3085execute: kill 3087If executed ctrl+C,Please execute the above lines manuallyPlease wait 120 secondskill 3083kill 3085kill 3087[root@centos-73-iso-100g-test ~]#
[root@centos-73-iso-100g-test ~]# toptop - 21:47:48 up 17 min, 3 users, load average: 1.26, 0.84, 0.37Tasks: 193 total, 1 running, 192 sleeping, 0 stopped, 0 zombie%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu4 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu5 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu6 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu7 : 0.8 us, 0.0 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 16267428 total, 15499472 free, 393652 used, 374304 buff/cacheKiB Swap: 10485756 total, 10485756 free, 0 used. 15558620 avail Mem
[root@centos-76-qcow2-50g-3 ~]# free -g total used free shared buff/cache availableMem: 62 0 62 0 0 61Swap: 0 0 0[root@centos-76-qcow2-50g-3 ~]# [root@centos-76-qcow2-50g-3 ~]# cat free.sh#!/bin/bashif [ $# != 2 ]; then echo "USAGE: $0 <free,sleep time>" exit 1;fi free -m > /tmp/freeecat /tmp/freeemkdir /tmp/memorymount -t tmpfs -o size=$1M tmpfs /tmp/memorydd if=/dev/zero of=/tmp/memory/blockfree -m > /tmp/freeecat /tmp/freeeecho "If executed ctrl+C,Please execute the following lines manually"echo "execute: rm -rf /tmp/memory/block"echo "execute: umount /tmp/memory"echo "execute: rmdir /tmp/memory"echo "Please wait $2 seconds" sleep $2rm -rf /tmp/memory/blockumount /tmp/memoryrmdir /tmp/memory[root@centos-76-qcow2-50g-3 ~]#
sh free.sh 2048 120
【2048是内存数量,单位是M,120是时间,单位是秒】# 如果参数不够 会报错的[root@centos-76-qcow2-50g-3 ~]# sh free.sh 1024USAGE: free.sh <free,sleep time>[root@centos-76-qcow2-50g-3 ~]# # 执行以后可以就会出现下面内容【此时该脚本并不会结束,而是处于 sleep 120状态[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120 total used free shared buff/cache availableMem: 64264 451 63559 16 253 63300Swap: 0 0 0dd: writing to ‘/tmp/memory/block’: No space left on device41943041+0 records in41943040+0 records out21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s total used free shared buff/cache availableMem: 64264 451 43033 20496 20779 42797Swap: 0 0 0If executed ctrl+C,Please execute the following lines manuallyexecute: rm -rf /tmp/memory/blockexecute: umount /tmp/memoryexecute: rmdir /tmp/memoryPlease wait 120 seconds
[root@centos-76-qcow2-50g-3 ~]# sh free.sh 20480 120 total used free shared buff/cache availableMem: 64264 451 63559 16 253 63300Swap: 0 0 0dd: writing to ‘/tmp/memory/block’: No space left on device41943041+0 records in41943040+0 records out21474836480 bytes (21 GB) copied, 67.0879 s, 320 MB/s total used free shared buff/cache availableMem: 64264 451 43033 20496 20779 42797Swap: 0 0 0If executed ctrl+C,Please execute the following lines manuallyexecute: rm -rf /tmp/memory/blockexecute: umount /tmp/memoryexecute: rmdir /tmp/memoryPlease wait 120 seconds[root@centos-76-qcow2-50g-3 ~]# [root@centos-76-qcow2-50g-3 ~]# free -m total used free shared buff/cache availableMem: 64264 451 63559 16 253 63300Swap: 0 0 0[root@centos-76-qcow2-50g-3 ~]#
[root@ccx ~]# ls /optmemload-7.0-1.r29766.x86_64.rpm[root@ccx ~]# rpm -ivh /opt/memload-7.0-1.r29766.x86_64.rpm Preparing... ################################# [100%]Updating / installing... 1:memload-7.0-1.r29766 ################################# [100%][root@ccx ~]#
# 可以看到node2现在消耗了1G内存[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 1 28 0 1 29Swap: 0 0 0[root@ccx ~]# #现在回到容器内部开始占用内容,因为我这虚机是32G内存,所以我占用多一点吧#容器中占用了10G[root@ccx ~]# memload 10240Attempting to allocate 10240 Mebibytes of resident memory...# 回到node2,可以看到10G确实可以被占用的[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 1 28 0 1 29Swap: 0 0 0[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 4 24 0 1 25Swap: 0 0 0[root@ccx ~]# [root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 7 22 0 1 23Swap: 0 0 0[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 8 21 0 1 22Swap: 0 0 0[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 11 18 0 1 19Swap: 0 0 0[root@ccx ~]#
#容器ctrl+c即可释放[root@ccx ~]# memload 10240Attempting to allocate 10240 Mebibytes of resident memory...Allocated 10000 pages^C[root@ccx ~]# # 回到node2看是否已经被释放,然后再释放下缓存内存[root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 1 28 0 1 29Swap: 0 0 0[root@ccx ~]# echo 3 > /proc/sys/vm/drop_caches [root@ccx ~]# [root@ccx ~]# free -g total used free shared buff/cache availableMem: 31 1 29 0 0 29Swap: 0 0 0[root@ccx ~]#
# /dev/sda3 是设备名称[root@centos-76-qcow2-50g-3 ~]# hdparm -Tt /dev/sda3/dev/sda3: Timing cached reads: 13034 MB in 2.00 seconds = 6527.09 MB/sec Timing buffered disk reads: 194 MB in 5.60 seconds = 34.64 MB/sec[root@centos-76-qcow2-50g-3 ~]#
可以看到,2秒钟读取了13034MB的缓存,约合6527.09 MB/sec;
在3.11秒中读取了194MB磁盘(物理读),读取速度约合4.64 MB/sec
/dev/null
伪设备,回收站.写该文件不会产生IO/dev/zero
伪设备,会产生空字符流,对它不会产生IO[root@centos-73-iso-100g-test data_vdb1]# time dd if=/dev/zero of=test.dbf bs=8k count=300000300000+0 records in300000+0 records out2457600000 bytes (2.5 GB) copied, 1.46149 s, 1.7 GB/sreal 0m1.464suser 0m0.027ssys 0m1.436s[root@centos-73-iso-100g-test data_vdb1]#
oflag=direct
【这个过程较慢】[root@centos-73-iso-100g-test data_vdb1]# time dd if=/dev/zero of=test.dbf bs=8k count=300000 oflag=direct300000+0 records in300000+0 records out2457600000 bytes (2.5 GB) copied, 423.33 s, 5.8 MB/sreal 7m3.561suser 0m0.992ssys 0m28.849s[root@centos-73-iso-100g-test data_vdb1]#
[root@centos-73-iso-100g-test data_vdb1]# du -sh * | tail -n12.3G test.dbf[root@centos-73-iso-100g-test data_vdb1]# rm -rf test.dbf [root@centos-73-iso-100g-test data_vdb1]#
[root@centos-76-qcow2-50g-3 ~]# dd if=test.dbf bs=8k count=300000 of=/dev/null 300000+0 records in300000+0 records out2457600000 bytes (2.5 GB) copied, 5.97231 s, 411 MB/s[root@centos-76-qcow2-50g-3 ~]#
[root@centos-76-qcow2-50g-3 ~]# dd if=test.dbf bs=8k count=300000 of=/dev/null oflag=directdd: failed to open ‘/dev/null’: Invalid argument[root@centos-76-qcow2-50g-3 ~]#
dd if= of= bs= skip= seek= conv=
if=
初始路径of=
目的路径bs=n
,block size,每次读取 n bytes 写入,可与 count 联用;ibs=n
,一次读入 bytes 个字节 (default is 512);obs=n
,一次性写 n bytes 个字节 (default is 512);bs=
可以同时设置上边两个参数;cbs=n
,一次转换 n 个 bytes,即转换缓冲区大小。;count=n
, bs 操作的次数,仅拷贝 n 个块,如 dvd: - bs=1M count=4430;skip=n
,指 if 后面的原文件跳过 n bytes 再开始读取;seek=n
,指 of 后面的目标文件跳过 n bytes 再开始写入;[root@centos-73-iso-100g-test data_vdb1]# dd if=/dev/zero of=/root/data_vdb1/data-test-0915 bs=1M count=30003000+0 records in3000+0 records out3145728000 bytes (3.1 GB) copied, 1.47652 s, 2.1 GB/s[root@centos-73-iso-100g-test data_vdb1]# du -sh *3.0G data-test-0915[root@centos-73-iso-100g-test data_vdb1]#