简体   繁体   English

如何找出适合由 shell 脚本解析的 linux 机器的总物理内存 (RAM)?

[英]How can I find out the total physical memory (RAM) of my linux box suitable to be parsed by a shell script?

I'm typing a shell script to find out the total physical memory in some RHEL linux boxes.我正在键入一个 shell 脚本来找出某些 RHEL linux 机器中的总物理内存。

First of all I want to stress that I'm interested in the total physical memory recognized by kernel, not just the available memory .首先我想强调的是,我对内核识别的总物理内存感兴趣,而不仅仅是可用内存 Therefore, please, avoid answers suggesting to read /proc/meminfo or to use the free , top or sar commands -- In all these cases, their " total memory " values mean " available memory " ones.因此,请避免建议阅读/proc/meminfo或使用freetopsar命令的答案——在所有这些情况下,它们的“总内存”值意味着“可用内存”值。

The first thought was to read the boot kernel messages:第一个想法是阅读引导内核消息:

Memory: 61861540k/63438844k available (2577k kernel code, 1042516k reserved, 1305k data, 212k init)

But in some linux boxes, due to the use of EMC2's PowerPath software and its flooding boot messages in the kernel startup, that useful boot kernel message is not available, not even in the /var/log/dmesg file.但是在某些 linux 系统中,由于使用了 EMC2 的 PowerPath 软件及其在内核启动时泛滥的引导消息,有用的引导内核消息不可用,甚至在/var/log/dmesg文件中也不可用。

The second option was the dmidecode command (I'm warned against the possible mismatch of kernel recognized RAM and real RAM due to the limitations of some older kernels and architectures).第二个选项是dmidecode命令(由于某些旧内核和架构的限制,我警告说内核识别的 RAM 和实际 RAM 可能不匹配)。 The option --memory simplifies the script but I realized that older releases of that command has no --memory option.选项--memory简化了脚本,但我意识到该命令的旧版本没有--memory选项。

My last chance was the getconf command.我最后的机会是getconf命令。 It reports the memory page size, but not the total number of physical pages -- the _PHYS_PAGES system variable seems to be the available physical pages, not the total physical pages.它报告内存页面大小,但不报告物理页面的总数——_PHYS_PAGES系统变量似乎是可用的物理页面,而不是总物理页面。

# getconf -a | grep PAGES
PAGESIZE                           4096
_AVPHYS_PAGES                      1049978
_PHYS_PAGES                        15466409

My question: Is there another way to get the total amount of physical memory, suitable to be parsed by a shell script?我的问题:有没有另一种方法可以获得物理内存总量,适合由shell脚本解析?

Have you tried cat /proc/meminfo ?你试过cat /proc/meminfo吗? You can then awk or grep out what you want, MemTotal eg然后,您可以使用awkgrep找出您想要的内容,例如MemTotal

awk '/MemTotal/ {print $2}' /proc/meminfo

or

cat /proc/meminfo | grep MemTotal

If you're interested in the physical RAM, use the command dmidecode .如果您对物理 RAM 感兴趣,请使用命令dmidecode It gives you a lot more information than just that, but depending on your use case, you might also want to know if the 8G in the system come from 2x4GB sticks or 4x2GB sticks.它给你的不仅仅是一个更多信息,但根据您的使用情况下,你可能也想知道,如果8G系统来自2x4GB棒或4x2GB棒。

cat /proc/meminfo | grep MemTotal cat /proc/meminfo | grep MemTotal or free gives you the exact amount of RAM your server has. cat /proc/meminfo | grep MemTotal或 free 为您提供服务器拥有的确切 RAM 量。 This is not "available memory".这不是“可用内存”。

I guess your issue comes up when you have a VM and you would like to calculate the full amount of memory hosted by the hypervisor but you will have to log into the hypervisor in that case.我想当您拥有 VM 并且您想计算虚拟机管理程序托管的全部内存量时,您的问题就会出现,但在这种情况下您必须登录虚拟机管理程序。

cat /proc/meminfo | grep MemTotal

is equivalent to相当于

 getconf -a | grep PAGES | awk 'BEGIN {total = 1} {if (NR == 1 || NR == 3) total *=$NF} END {print total / 1024" kB"}'

Add the last 2 entries of /proc/meminfo , they give you the exact memory present on the host.添加/proc/meminfo的最后 2 个条目,它们为您提供主机上存在的确切内存。

Example:示例:

DirectMap4k:       10240 kB
DirectMap2M:     4184064 kB

10240 + 4184064 = 4194304 kB = 4096 MB. 10240 + 4184064 = 4194304 kB = 4096 MB。

One more useful command:另一个有用的命令:
vmstat -s | grep memory
sample output on my machine is:我机器上的示例输出是:

  2050060 K total memory
  1092992 K used memory
   743072 K active memory
   177084 K inactive memory
   957068 K free memory
   385388 K buffer memory

another useful command to get memory information is:另一个获取内存信息的有用命令是:
free
sample output is:样本输出是:

             total       used       free     shared    buffers     cached
Mem:       2050060    1093324     956736        108     385392     386812
-/+ buffers/cache:     321120    1728940
Swap:      2095100       2732    2092368

One observation here is that, the command free gives information about swap space also.这里的一个观察是,命令free提供有关交换空间的信息。
The following link may be useful for you:以下链接可能对您有用:
http://www.linuxnix.com/find-ram-details-in-linuxunix/ http://www.linuxnix.com/find-ram-details-in-linuxunix/

free -h | awk '/Mem\:/ { print $2 }' 

这将以人类可读的格式为您提供系统中的总内存,并自动缩放到适当的单位(例如字节、KB、MB 或 GB)。

dmidecode -t 17 | grep  Size:

添加“大小:”之后显示的所有上述值将给出服务器中所有 RAM 棒的确切总物理大小。

Total online memory总在线内存

Calculate the total online memory using the sys-fs.使用 sys-fs 计算总在线内存。

totalmem=0;
for mem in /sys/devices/system/memory/memory*; do
  [[ "$(cat ${mem}/online)" == "1" ]] \
    && totalmem=$((totalmem+$((0x$(cat /sys/devices/system/memory/block_size_bytes)))));
done

#one-line code
totalmem=0; for mem in /sys/devices/system/memory/memory*; do [[ "$(cat ${mem}/online)" == "1" ]] && totalmem=$((totalmem+$((0x$(cat /sys/devices/system/memory/block_size_bytes))))); done

echo ${totalmem} bytes
echo $((totalmem/1024**3)) GB

Example output for 4 GB system: 4 GB 系统的示例输出:

4294967296 bytes
4 GB

Explanation说明

/sys/devices/system/memory/block_size_bytes /sys/devices/system/memory/block_size_bytes

Number of bytes in a memory block (hex value).内存块中的字节数(十六进制值)。 Using 0x in front of the value makes sure it's properly handled during the calculation.在值前使用 0x 可确保在计算过程中正确处理。

/sys/devices/system/memory/memory* /sys/devices/system/memory/memory*

Iterating over all available memory blocks to verify they are online and add the calculated block size to totalmem if they are.迭代所有可用内存块以验证它们是否在线,如果它们在线,则将计算出的块大小添加到 totalmem 中。

[[ "$(cat ${mem}/online)" == "1" ]] && [["$(cat ${mem}/online)" == "1" ]] &&

You can change or remove this if you prefer another memory state.如果您更喜欢另一种记忆状态,您可以更改或删除它。

Total memory in Mb :Mb总内存:

x=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
echo $((x/1024))

or:或:

x=$(awk '/MemTotal/ {print $2}' /proc/meminfo) ; echo $((x/1024))

In Linux Kernel, present pages are physical pages of RAM which kernel can see.在 Linux Kernel 中,当前页是内核可以看到的 RAM 物理页。 Literally, present pages is total size of RAM in 4KB unit.从字面上看,当前页面是以 4KB 为单位的 RAM 总大小。

grep present /proc/zoneinfo | awk '{sum+=$2}END{print sum*4,"KB"}'

The 'MemTotal' form /proc/meminfo is the total size of memory managed by buddy system.And we can also compute it like this: 'MemTotal' 形式 /proc/meminfo 是伙伴系统管理的内存总大小。我们也可以这样计算:

grep managed /proc/zoneinfo | awk '{sum+=$2}END{print sum*4,"KB"}'

These are the ways :这些是方法:

1. /proc/meminfo 1. /proc/meminfo

MemTotal: 8152200 kB

MemFree: 760808 kB

You can write a code or script to parse it.您可以编写代码或脚本来解析它。

2. Use sysconf by using below macros 2. 使用以下宏来使用 sysconf

sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGESIZE);系统配置(_SC_PHYS_PAGES)* 系统配置(_SC_PAGESIZE);

3. By using sysinfo system call 3. 通过使用 sysinfo 系统调用

int sysinfo(struct sysinfo *info); int sysinfo(struct sysinfo *info);

struct sysinfo { .结构系统信息{。

   .

   unsigned long totalram;  /*Total memory size to use */

   unsigned long freeram;   /* Available memory size*/

   .

   . 

  }; 

I know this question was asked a long time ago, but I wanted to provide another way to do this that I found useful for an issue I just worked on:我知道很久以前就有人问过这个问题,但我想提供另一种方法来做到这一点,我发现它对我刚刚解决的问题很有用:

lshw -c memory

lshw小白

lshw is a small tool to extract detailed information on the hardware configuration of the machine. lshw 是一个提取机器硬件配置详细信息的小工具。 It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines (PowerMac G4 is known to work).它可以在支持 DMI 的 x86 或 IA-64 系统和某些 PowerPC 机器(已知 PowerMac G4 可以工作)上报告确切的内存配置、固件版本、主板配置、CPU 版本和速度、缓存配置、总线速度等。

I find htop a useful tool.我发现htop是一个有用的工具。

sudo apt-get install htop sudo apt-get install htop

and then然后

free -m自由 -m

will give the information you need.将提供您需要的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM