基于OpenVZ技术的VPS 虽然很便宜,但有些黑心的主机商,也会将SWAP虚拟内存 计算到你的内存里,今天我介绍个方法,可以让大家看到自己OPENVZ vps 的真实内存。
/proc/user_beancounters
user_beancounters 包括了 OpenVZ VPS 的系统资源分配和使用信息。
Version: 2.5
uid resource held maxheld barrier limit failcnt
578: kmemsize 3489984 6660867 2147483646 2147483646 0
lockedpages 0 5 999999 999999 0
privvmpages 22221 65689 65536 65536 17
shmpages 898 18947 32768 32768 0
dummy 0 0 0 0 0
numproc 26 50 999999 999999 0
physpages 10601 49908 0 2147483647 0
vmguarpages 0 0 32768 2147483647 0
oomguarpages 12183 51581 32768 2147483647 0
numtcpsock 6 494 7999992 7999992 0
numflock 3 9 999999 999999 0
numpty 1 3 500000 500000 0
numsiginfo 0 7 999999 999999 0
tcpsndbuf 116104 8576064 214748160 396774400 0
tcprcvbuf 98304 43651104 214748160 396774400 0
othersockbuf 17832 355992 214748160 396774400 0
dgramrcvbuf 0 44232 214748160 396774400 0
numothersock 21 57 7999992 7999992 0
dcachesize 0 0 2147483646 2147483646 0
numfile 696 1804 23999976 23999976 0
dummy 0 0 0 0 0
dummy 0 0 0 0 0
dummy 0 0 0 0 0
numiptent 24 24 999999 999999 0
uid:用来辨别虚拟机(the container)的。
held:用来显示当前使用状况。
maxheld:显示的是在 lifetime 时间段里,使用的最大值。(lifetime 是指从启动 VPS 到结束 VPS 之间的时间)
barrier 和 limit:用来控制资源的设定值。
failcn:用来显示被拒绝分配资源的次数。如果这里 failcn 的值增大很可能表示没有足够的内存分配给应用程序。
kmemsize:kernel memory size 这个参数显示内核使用的内存大小,内核只能常驻内存不能 swap。的内存大小,这里显示我的内核正在使用 3378602 byte 大小的内存。
privvmpages:private virtual memory 这个参数实际上显示的分配的 mem+swap 的大小,注意这里使用page做单位不是用byte,看上面的 privvmpages 是:61869 * 4KB / 1024byte = 241MB(1page = 4K,1K=1024B)这个参数只是显示分配的内存大小,但并不是实际使用内存的大小。实际大小由physpages 来显示。还有就是看完了后面的 vmguarpages 和 oomguarpages 会发现实际上:privvmpages barrier = vmguarpages barrier + oomguarpages barrier。
physpages:Physical pages 用来显示实际使用的内存大小。所有单独 VPS 虚拟机的使用内存加起来就是这台服务器整体的内存消耗。
vmguarpages:这个参数用来显示用多少内存分配给虚拟机,就是我们查看各种 VPS 产品时关心的Guaranteed/Dedicated RAM,计算方法是:58368 pages * 4K / 1024B = 228MB,正是我 VPS 的Dedicated RAM 大小。
oomguarpages:这个参数用来显示在 Dedicated RAM 用完了的情况下多少额外内存可以用。计算:32768 pages * 4K / 1024B = 128MB,所以加上 Dedicated RAM 后,总共的 Burstable RAM 是:228MB + 128MB = 356MB,正是我现在 Burstable RAM 的大小。
更方便的方法
awk 的简单脚本
我写了的一个 awk 的简单脚本来显示有用信息,这个脚本解析 user_beancounters 文件,并输出想要的内存数据,添加权限之后,直接在 shell 下运行:
awk ‘/privvmpages/ {print “[",$1,"]n -efish.tk-n”,
” Held: “,$3*4/1024,”MBn “,
“Barrier: ” $4*4/1024,”MBn “,
“Limit: “, $5*4/1024,”MBn “,
“Fail Count: “, $6,”nn”}’ /proc/user_beancounters
运行上面脚本后显示如下:
[ privvmpages ]
-efish.tk-
Held: 256.598 MB
Barrier: 256 MB
Limit: 256 MB
Fail Count: 17
计算当前使用的RAM内存的脚本:
#!/usr/bin/perl
use strict;
use warnings;# Author: David J. Weller-Fahy
# Inspired by
# Placed in the public domainmy($field, $res_bytes, $res_pages, $value, $total);
print(“$0 must be run by a SuperUser!n”) and exit(1) if ($< != 0);
# If no command-line parameters are used, stick with the default.
@ARGV = (‘/proc/user_beancounters’) unless @ARGV;# These are the resources we care about.
$res_bytes = “dgramrcvbuf|kmemsize|othersockbuf|tcprcvbuf|tcpsndbuf|”;
$res_pages .= “privvmpages|vmguarpages”;$total = 0;
# Iterate over the files specified in @ARGV.
while (<>) {
# Get rid of the UID and trailing colon.
s![[:digit:]]+:!! if m!kmemsize!;# Strip leading/trailing whitespace.
s!^[[:space:]]+!!;
s![[:space:]]+$!!;# We only care about the lines containing the resources in
# $res_(bytes|pages), so skip all other lines.
next unless m!^(?:$res_bytes$res_pages)!;# Set the field to extract.
$field = (m!^kmemsize! ? 2 : 1);# Grab the resource and value.
$value = (split)[$field];# If this is a ‘page’ resource, multiply by the number of bytes in a page.
$value *= 4096 if m!^(?:$res_pages)!;# Add this value to the total.
$total += $value;
}# Original used 1024000 (??). Not sure why, the value below is 1024*1024.
$total /= 1048576;# Print the number of MB used, rounded to 2 decimal places.
printf(“Total MiB used: %.2fn”, $total);






最新评论
话说博主不见更新,正考虑是否
我喜欢用的时候man一下,这
:razz: 来啦
博主是哲人啊,这么多哲理
能坚持这么久的,再烂都是牛站
很现实,转了一圈,回到原点.
刚用这个续费一年。
我看到有这样的说法: 谷歌