IT story

쉘 스크립트로 파싱하기에 적합한 리눅스 박스의 총 실제 메모리 (RAM)를 어떻게 알 수 있습니까?

hot-time 2020. 7. 25. 10:27
반응형

쉘 스크립트로 파싱하기에 적합한 리눅스 박스의 총 실제 메모리 (RAM)를 어떻게 알 수 있습니까?


일부 RHEL Linux 상자에서 총 실제 메모리를 찾기 위해 쉘 스크립트를 입력하고 있습니다.

우선 사용 가능한 메모리 뿐만 아니라 커널이 인식 하는 총 실제 메모리에 관심이 있다고 강조하고 싶습니다 . 따라서 / proc / meminfo 를 읽 거나 free , top 또는 sar 명령 을 사용하도록 제안하는 답변을 피하십시오. 이러한 모든 경우에 " 총 메모리 "값은 " 사용 가능한 메모리 " 값을 의미 합니다.

첫 번째 생각은 부팅 커널 메시지를 읽는 것이 었습니다.

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

그러나 일부 Linux 시스템 에서는 커널 시작시 EMC2의 PowerPath 소프트웨어 및 플러딩 부팅 메시지를 사용하기 때문에 / var / log / dmesg 파일 에서도 유용한 부팅 커널 메시지를 사용할 수 없습니다 .

두 번째 옵션은 dmidecode 명령 이었습니다 (일부 오래된 커널 및 아키텍처의 제한으로 인해 커널 인식 RAM과 실제 RAM의 불일치에 대해 경고합니다). 옵션 --memory 는 스크립트를 단순화하지만 해당 명령의 이전 릴리스에는 --memory 옵션 이 없음을 깨달았습니다 .

마지막 기회는 getconf 명령이었습니다. 메모리 페이지 크기는보고하지만 총 실제 페이지 수는보고하지 않습니다. _PHYS_PAGES 시스템 변수는 총 실제 페이지가 아니라 사용 가능한 실제 페이지 인 것으로 보입니다.

# getconf -a | grep PAGES
페이지 크기 4096
_AVPHYS_PAGES 1049978
_PHYS_PAGES 15466409

내 질문 : 셸 스크립트로 구문 분석하기에 적합한 총 실제 메모리 양을 얻는 또 다른 방법이 있습니까?


실제 RAM에 관심이 있으시면 명령을 사용하십시오 dmidecode. 그것 보다 훨씬 더 많은 정보를 제공 하지만 사용 사례에 따라 시스템의 8G가 2x4GB 스틱 또는 4x2GB 스틱에서 오는지 알고 싶을 수도 있습니다.


당신은 시도 했습니까 cat /proc/meminfo? 그런 다음 원하는 것을 Grep 할 수 있습니다. 예 : MemTotal

업데이트 된 예 (Bast. thanks, Masta) :

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

cat /proc/meminfo | grep MemTotal또는 free는 서버의 정확한 RAM 용량을 제공합니다. "사용 가능한 메모리"가 아닙니다.

VM이있을 때 문제가 발생하고 하이퍼 바이저가 호스팅하는 전체 메모리 양을 계산하려고하지만이 경우 하이퍼 바이저에 로그인해야합니다.

cat /proc/meminfo | grep MemTotal

에 해당

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

하나 더 유용한 명령 :
vmstat -s | grep memory
내 컴퓨터의 샘플 출력은 다음과 같습니다.

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

메모리 정보를 얻는 또 다른 유용한 명령은 다음과 같습니다.
free
샘플 출력은 다음과 같습니다.

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

여기서 한 가지 관찰은 명령 free이 스왑 공간에 대한 정보도 제공 한다는 것 입니다.
다음 링크가 유용 할 수 있습니다.
http://www.linuxnix.com/find-ram-details-in-linuxunix/


Add the last 2 entries of /proc/meminfo, they give you the exact memory present on the host.

Example:

DirectMap4k:       10240 kB
DirectMap2M:     4184064 kB

10240 + 4184064 = 4194304 kB = 4096 MB.


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

This will provide you with the total memory in your system in human readable format and automatically scale to the appropriate unit ( e.g. bytes, KB, MB, or GB).


dmidecode -t 17 | grep  Size:

Adding all above values displayed after "Size: " will give exact total physical size of all RAM sticks in server.


Total memory in Mb:

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

or:

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

These are the ways :

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

sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGESIZE);

3. By using sysinfo system call

int sysinfo(struct sysinfo *info);

struct sysinfo { .

   .

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

   unsigned long freeram;   /* Available memory size*/

   .

   . 

  }; 

Total online memory

Calculate the total online memory using the 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:

4294967296 bytes
4 GB

Explanation

/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.

/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.

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

You can change or remove this if you prefer another memory state.


I find htop a useful tool.

sudo apt-get install htop

and then

free -m

will give the information you need.

참고URL : https://stackoverflow.com/questions/20348007/how-can-i-find-out-the-total-physical-memory-ram-of-my-linux-box-suitable-to-b

반응형