`

Linux System and Performance Monitoring(Memory篇)

阅读更多

Linux System and Performance Monitoring(Memory篇) 
Date: 2009.07.21 
Author: Darren Hoch 
译: Tonnyom[AT]hotmail.com
5.0 Virtual Memory介绍
虚拟内存就是采用硬盘对物理内存进行扩展,所以对可用内存的增加是要相对在一个有效范围内的.内核会写当前未使用内存块的内容到硬盘上,此时这部分内存被用于其它用途.当再一次需要原始内容时,此时再读回到内存中.这对于用户来说,是完全透明的;在Linux 下运行的程序能够看到,也仅仅是大量的可用内存,同时也不会留意到,偶尔还有部分是驻留在磁盘上的.当然,在硬盘上进行读和写,都是很慢的(大约会慢上千倍),相对于使用真实内存的话,因此程序无法运行的更快.用硬盘的一部分作为Virtual Memory,这就被称为"swap space"(译注:交换空间).
5.1 Virtual Memory Pages
虚拟内存被分为很多 pages(译注:页),在X86架构中,每个虚拟内存页为 4KB.当内核写内存到磁盘或者读磁盘到内存,这就是一次写内存到页的过程.内核通常是在swap 分区和文件系统之间进行这样的操作.
5.2 Kernel Memory Paging
内存分页在正常情况下总是活跃的,与memory swapping(译注:内存交换)之间不要搞错了.内存分页是指内核会定期将内存中的数据同步到硬盘,这个过程就是Memory Paging.日复一日,应用最终将会消耗掉所有的内存空间.考虑到这点,内核就必须经常扫描内存空间并且收回其中未被使用的内存页,然后再重新分配内存空间给其他应用使用.
5.3 The Page Frame Reclaim Algorithm(PFRA)(译注:页框回收算法)
PFRA 就是OS 内核用来回收并释放内存空间的算法.PFRA 选择哪个内存页被释放是基于内存页类型的.页类型有以下几种:
Unreclaimable –锁定的,内核保留的页面 
Swappable –匿名的内存页 
Syncable –通过硬盘文件备份的内存页 
Discardable –静态页和被丢弃的页
除了第一种(Unreclaimable)之外其余的都可以被PFRA进行回收.
与PFRA 相关的,还包括kswapd 内核线程以及Low On Memory Reclaiming(LMR算法) 这2种进程和实现.
5.4 kswapd
kswapd 进程负责确保内存空间总是在被释放中.它监控内核中的pages_high和pages_low阀值.如果空闲内存的数值低于 pages_low,则每次 kswapd 进程启动扫描并尝试释放32个free pages.并一直重复这个过程,直到空闲内存的数值高于 pages_high.
kswapd 进程完成以下几个操作:
1,如果该页处于未修改状态,则将该页放置回空闲列表中. 
2,如果该页处于已修改状态并可备份回文件系统,则将页内容写入到磁盘. 
3,如果该页处于已修改状态但没有任何磁盘备份,则将页内容写入到swap device.
# ps -ef | grep kswapd 
root 30 1 0 23:01 ? 00:00:00 [kswapd0]
5.5 Kernel Paging with pdflush
pdflush 进程负责将内存中的内容和文件系统进行同步操作.也就是说,当一个文件在内存中进行修改后, pdflush 将负责写回到磁盘上.
# ps -ef | grep pdflush 
root 28 3 0 23:01 ? 00:00:00 [pdflush] 
root 29 3 0 23:01 ? 00:00:00 [pdflush]
当内存中存在10% 的脏页,pdflush 将被启动同步脏页回文件系统里.这个参数值可以通过 vm.dirty_background_ratio 来进行调整.
(译注:
Q:什么是脏页? 
A:由于内存中页缓存的缓存作用,写操作实际上都是延迟的.当页缓存中的数据比磁盘存储的数据还要更新时,那么该数据就被称做脏页.)
# sysctl -n vm.dirty_background_ratio 
10
在多数环境下,Pdflush与PFRA是独立运行的,当内核调用LMR时,LMR 就触发pdflush将脏页写入到磁盘里.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
在2.4 内核下,一个高负荷的内存环境中,系统将遇到交换过程中不断的崩溃.这是因为PFRA 从一个运行进程中,偷取其中一个内存页并尝试使用.导致结果就是,这个进程如果要回收那个页时,要是没有就会尝试再去偷取这个页,这样一来,就越来越糟糕了.在2.6 内核下,使用"Swap token"修复了这个BUG,用来防止PFRA 不断从一个进程获取同一个页. 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5.6 案例学习:大量的入口I/O
vmstat 工具报告里除了CPU 使用情况,还包括了虚拟内存.以下就是vmstat 输出中关于虚拟内存的部分:
Table 2: The vmstat Memory Statistics 
Field Description 
Swapd The amount of virtual memory in KB currently in use. As free memory reaches low thresholds, more data is paged to the swap device. 
当前虚拟内存使用的总额(单位:KB).空闲内存达到最低的阀值时,更多的数据被转换成页到交换设备中. 
Free The amount of physical RAM in kilobytes currently available to running applications. 
当前内存中可用空间字节数. 
Buff The amount of physical memory in kilobytes in the buffer cache as a result of read() and write() operations. 
当前内存中用于read()和write()操作的缓冲区中缓存字节数 
Cache The amount of physical memory in kilobytes mapped into process address space. 
当前内存中映射到进程地址空间字节数 
So The amount of data in kilobytes written to the swap disk. 
写入交换空间的字节数总额 
Si The amount of data in kilobytes written from the swap disk back into RAM. 
从交换空间写回内存的字节数总额 
Bo The amount of disk blocks paged out from the RAM to the filesystem or swap device. 
磁盘块页面从内存到文件或交换设备的总额 
Bi The amount of disk blocks paged into RAM from the filesystem or swap device. 
磁盘块页面从文件或交换设备到内存的总额
以下 vmstat 的输出结果,就是演示一个在I/O 应用中,虚拟内存在高负荷情况下的环境
# vmstat 3 
procs memory swap io system cpu 
r b swpd free buff cache si so bi bo in cs us sy id wa 
3 2 809192 261556 79760 886880 416 0 8244 751 426 863 17 3 6 75 
0 3 809188 194916 79820 952900 307 0 21745 1005 1189 2590 34 6 12 48 
0 3 809188 162212 79840 988920 95 0 12107 0 1801 2633 2 2 3 94 
1 3 809268 88756 79924 1061424 260 28 18377 113 1142 1694 3 5 3 88 
1 2 826284 17608 71240 1144180 100 6140 25839 16380 1528 1179 19 9 12 61 
2 1 854780 17688 34140 1208980 1 9535 25557 30967 1764 2238 43 13 16 28 
0 8 867528 17588 32332 1226392 31 4384 16524 27808 1490 1634 41 10 7 43 
4 2 877372 17596 32372 1227532 213 3281 10912 3337 678 932 33 7 3 57 
1 2 885980 17800 32408 1239160 204 2892 12347 12681 1033 982 40 12 2 46 
5 2 900472 17980 32440 1253884 24 4851 17521 4856 934 1730 48 12 13 26 
1 1 904404 17620 32492 1258928 15 1316 7647 15804 919 978 49 9 17 25 
4 1 911192 17944 32540 1266724 37 2263 12907 3547 834 1421 47 14 20 20 
1 1 919292 17876 31824 1275832 1 2745 16327 2747 617 1421 52 11 23 14 
5 0 925216 17812 25008 1289320 12 1975 12760 3181 772 1254 50 10 21 19 
0 5 932860 17736 21760 1300280 8 2556 15469 3873 825 1258 49 13 24 15
根据观察值,我们可以得到以下结论:
1,大量的disk pages(bi)被写入内存,很明显在进程地址空间里,数据缓存(cache)也在不断的增长.
2,在这个时间点上,空闲内存(free) 始终保持在17MB,即使数据从硬盘读入而在消耗RAM.
3,为了维护空闲列表, kswapd 从读/写缓存区(buff)中获取内存并分配到空闲列表里.很明显可以看到buffer cache(buff) 在逐渐的减少中.
4, 同时kswapd 进程不断的写脏页到swap device(so)时,很明显虚拟内存的利用率是在逐渐的增加中(swpd).
5.7 结论
监控虚拟内存性能由以下几个部分组成:
1,当系统中出现较少的页错误,获得最好的响应时间,是因为memory caches(译注:内存高速缓存)比disk caches更快(译注:磁盘高速缓存).
2,较少的空闲内存,是件好事情,那意味着缓存的使用更有效率.除非在不断的写入swap device和disk.
3,如果系统不断报告,swap device总是繁忙中,那就意味着内存已经不足,需要升级了.
分享到:
评论

相关推荐

    Linux System and Performance Monitoring

    在 Linux 系统中,memory pages 有 3 种,分别是:Read Pages、Dirty Pages 和 Anonymous Pages。Read Pages 是通过 MPF 从磁盘中读入的页,且是只读的。Dirty Pages 是内核在内存中已经被修改过的数据页。当这些页...

    Linux System and Performance Monitoring

    分CPU篇,memory篇,i/o篇,network篇 讲述如何对系统性能进行监测。 讲得很透彻 , 而且还很全面。 理论结合实际 , 其中案例分析都很好。不花哨 , 采用的工具及命令都是最基本的 , 有助于实际操作 。

    Extreme Linux Performance Monitoring Part II

    Disk IO subsystems are the slowest part of any Linux system. This is due mainly to their distance from the CPU and the fact that disks require the physics to work (rotation and seek). If the time ...

    Prentice.Hall.Performance.Tuning.for.Linux.Servers

    and benchmarking Interpret performance data to analyze your Linux server's real-world behavior Optimize Linux system schedulers, memory, I/O, file systems, and networking Tune web, file, database, ...

    Linux Performance Monitoring

    procs -----------memory---------- ---swap-- -----io---- --system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 138592 17932 126272 21424 440 0 118 109 192 1196 0 0 ...

    everest硬件检测工具

    CPU, FPU and memory benchmarks are available to measure the actual system performance and compare it to previous states or other systems. Furthermore, complete software, operating system and security...

    Wiley.Publishing.Fedora.Linux.Toolbox.1000+.Commands.for.Fedora.CentOS.and.Red.Hat.Power.Users.and.Red.Hat.Power.Users.2008.pdf

    Understanding and managing running processes is essential for system stability and performance. Key topics include: - **Process Overview**: Basic concepts related to processes and threads. - **...

    SQLServer Performance and Balanced System Design.ppt

    Tools & Monitoring Siebel Database Siebel Queries Siebel Query Repro Siebel & Statistics Wait Types Locating Long Running Queries Optimizing Data Loading Balanced System Design - Network Performance ...

    Intel® 64 and IA-32 Architectures Software Developer Manuals

    thermal and power management features, debugging, performance monitoring, system management mode, virtual machine extensions (VMX) instructions, Intel® Virtualization Technology (Intel® VT), and ...

    检测电脑硬件的经典程序

    CPU, FPU and memory benchmarks are available to measure the actual system performance and compare it to previous states or other systems. Furthermore, complete software, operating system and security...

    everest 电脑硬件检测软件

    CPU, FPU and memory benchmarks are available to measure the actual system performance and compare it to previous states or other systems. Furthermore, complete software, operating system and security...

    Everest_系统自检

    CPU, FPU and memory benchmarks are available to measure the actual system performance and compare it to previous states or other systems. Furthermore, complete software, operating system and security...

    Intel 64 and IA-32 Architectures Software Developer's Manual

    thermal and power management features, debugging, performance monitoring, system management mode, virtual machine extensions (VMX) instructions, and Intel® Virtualization Technology (Intel® VT).

    查看本机的硬件所有信息

    CPU, FPU and memory benchmarks are available to measure the actual system performance and compare it to previous states or other systems. Furthermore, complete software, operating system and security...

    Intel® 64 and IA-32 Architectures Software Developer’s Manual Combined.pdf

    thermal and power management features, debugging, performance monitoring, system management mode, virtual machine extensions (VMX) instructions, Intel® Virtualization Technology (Intel® VT), and ...

    微软内部资料-SQL性能优化2

    Although every process has its own private memory space, kernel mode code and drivers share system space. Windows 2000 does not provide any protection to private memory being use by components running...

    Python System Monitor (Psymon)

    -System load history (cpu,memory,netwok and disks) -Disks informations -Network connections -Detailed informations and cpu, memory percentage history per process *Graphs* The graphs of the Psymon on ...

    TimesTen In-Memory Database Installation Guide

    - **Definition**: TimesTen is an in-memory database management system designed for high-speed transaction processing and real-time analytics. - **Purpose**: It is used to enhance application ...

    vmware虚拟化解决方案

    Performance monitoring capabilities, including utilization graphs of CPU, memory, Disk I/O, and Network I/O provide the detail needed to analyze the performance of physical servers, and the virtual ...

    MySQL5.7-performance

    #### 十八、迁移到性能方案系统和状态变量表 (Migrating to Performance Schema System and Status Variable Tables) 当从旧版本迁移到 MySQL 5.7 时,可能需要迁移一些监控数据到新的性能方案表中。这部分内容提供...

Global site tag (gtag.js) - Google Analytics