Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1958236
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2008-09-20 10:44:24

计算机系统中,物理内存被分为物理页来进行管理,paging(swapping)是指物理页在主存和辅存(例如磁盘)之间进行转移的过程。paging(swapping)是现代很多主流的通用OS虚拟内存系统的实现的一部分。通过pagingswapping),OS可以将没有存储在RAM中的数据加载到RAM中,貌似是扩大了可用RAM的容量。

概述

为了方便叙述将“页”分为两种(1)虚拟页(简称页)和(2)物理页(简称页帧)。

本文中的辅存不是单单的指硬盘,而是通指非主存的存储系统,比如磁盘,交换分区,页面文件等等

什么时候需要进行pagingswapping)呢?简而言之,就是包含程序所存取的数据或指令的虚拟页不在RAM中的时候。这时,程序就会报告page fault异常,然后操作系统就会接手这个棘手的问题。为了保证程序的正常运行,操作系统会执行以下操作:

1.         确定包含程序所需要存取的数据的虚拟页在辅存中什么地方。

2.         RAM种获得一个空的物理页。

3.         将虚拟页的内容加载到物理页种。

4.         将控制返还给程序,重新开始执行引发page fault异常的指令。

在步骤2中,如果没有可用的空的物理页,那么paging系统就会根据“页替换算法”选出某些物理页,将其中的内容保存到辅存中,使之成为空的物理页。现代操作系统常用的“页替换算法”是LRULeast Recently Used)算法:将使用最少的物理页的内容存储到辅存中,使之成为空的物理页。

除了在发生page fault时会paging外,还有一些策略用于预测那些虚拟页未来会被用到,然后把其paging到物理内存中。

l         Demand paging

Demand paging不会进行猜测,只有在发生Demand paging请求时才进行paging

l         Loader paging

将整个程序所要用到的所有虚拟页一次加载到内存中。

l         Anticipatory paging/Swap prefetch

根据“局部性原理”,当发生page fault时,除了将请求的虚拟页paging到物理内存中,还会将该虚拟页后连续的几个虚拟也也加载到物理内存中。实际应用中,这种方法有很好的命中率,但是对于特殊的系统和应用来说将会造成更差的效率。

l         Precleaning

Unix操作系统会利用sync阶段性的清除dirty page,就是将修改的页保存到磁盘中。这有助于新程序运行的速度,因为这时就有充足的“干净”的物理页用于新程序,否则,还需要先将被修改的物理页写回到磁盘。

Thrashing(页抖动)

Most programs reach a steady state in their demand for memory both in terms of instructions fetched and data being accessed. This steady state is usually much less than the total memory required by the program. This steady state is sometimes referred to as the : the set of memory pages that are most frequently accessed.

Virtual memory systems work most efficiently when the ratio of the working set to the total number of pages that can be stored in RAM is low enough to minimize the number of page faults. A program that works with huge data structures will sometimes require a working set that is too large to be efficiently managed by the page system resulting in constant page faults that drastically slow down the system. This condition is referred to as : pages are swapped out and then accessed causing frequent faults.

An interesting characteristic of thrashing is that as the working set grows, there is very little increase in the number of faults until the critical point (when faults go up dramatically and majority of system's processing power is spent on handling them).

An extreme example of this sort of situation occurred on the and series mainframe computers, in which a particular instruction could consist of an execute instruction, which crosses a page boundary, that the instruction points to a move instruction, that itself also crosses a page boundary, targeting a move of data from a source that crosses a page boundary, to a target of data that also crosses a page boundary. The total amount of pages thus being used by this particular instruction is eight, and all eight pages must be present in memory at the same time. If the operating system will allocate less than eight pages of actual memory in this example, when it attempts to swap out some part of the instruction or data to bring in the remainder, the instruction will again page fault, and it will thrash on every attempt to restart the failing instruction.

To decrease excessive paging, and thus possibly resolve thrashing problem, a user can do any of the following:

  • Increase the amount of RAM in the computer (generally the best long-term solution).
  • Decrease the number of programs being concurrently run on the computer.

The term thrashing is also used in contexts other than virtual memory systems, for example to describe issues in computing or in networking.

术语

从历史的观点来看,paging有时涉及到两种内存分配的机制:定长的页分配(page)和可变长的段分配(segment)机制。随着时间的推移,变长的段分配(segment)机制已经逐渐的推出了历史舞台。

一些现代的操作系统同时使用swappingpaging。以前,swapping是指在辅存种移动整个程序,称为roll inroll out20世纪60年代,虚拟内存出现后,swapping是指在主存和辅存之间移动段或页。现在,虚拟内存系统一般都是基于页的(不使用段),因此swapping也就等同于paging了。

在许多流行的系统中,都有页缓存的概念。页可在RAM和普通的磁盘文件之间移动,而不必使用专有的磁盘分区。在类UNIX的系统中(包括linuxpaging指在RAM和磁盘之间移动页,而swapping指在交换分区或交换文件(非磁盘的地方)与RAM之间移动页。

Windows系统中,pagingswapping是等同的,因为windows将特定的辅存统称为page fie

实现

Windows 3.x and Windows 9x

Virtual memory has been a feature of since in 1990. Microsoft introduced virtual memory in response to the failures of and , attempting to slash resource requirements for the operating system.

Confusion abounds about Microsoft's decision to refer to the swap file as "virtual memory". Novices unfamiliar with the concept accept this definition without question, and speak of adjusting Windows' virtual memory size. In fact every process has a fixed, unchangeable virtual memory size, usually 2 . The user has only an option to change disk capacity dedicated to paging.

Windows 3.x creates a named 386SPART.PAR or WIN386.SWP for use as a swap file. It is generally found in the , but it may appear elsewhere (typically in the WINDOWS directory). Its size depends on how much swap space the system has (a setting selected by the user under → Enhanced under "Virtual Memory".) If the user moves or deletes this file, a will appear the next time Windows is started, with the "The permanent swap file is corrupt". The user will be prompted to choose whether or not to delete the file (whether or not it exists).

, and use a similar file, and the settings for it are located under Control Panel → System → Performance tab → Virtual Memory. Windows automatically sets the size of the page file to start at 1.5× the size of physical memory, and expand up to 3× physical memory if necessary. If a user runs memory-intensive applications on a system with low physical memory, it is preferable to manually set these sizes to a value higher than default.

Windows NT

In NT-based versions of Windows (such as and ), the file used for paging is named pagefile.sys. The default location of the page file is in the root directory of the partition where Windows is installed. Windows can be configured to use free space on any available drives for pagefiles. It is required, however, for the boot partition (i.e. the drive containing the Windows directory) to have a pagefile on it if the system is configured to write either kernel or full memory dumps after a . Windows uses the paging file as temporary storage for the memory dump. When the system is rebooted, Windows copies the memory dump from the pagefile to a separate file and frees the space that was used in the pagefile.

Fragmentation

In Windows's default configuration the pagefile is allowed to expand beyond its initial allocation when necessary. If this happens gradually, it can become heavily which can potentially cause performance problems. The common advice given to avoid this is to set a single "locked" pagefile size so that Windows will not expand it. However, the pagefile only expands when it has been filled, which, in its default configuration, is 150% the total amount of physical memory. Thus the total demand for pagefile-backed virtual memory must exceed 250% of the computer's physical memory before the pagefile will expand.

The fragmentation of the pagefile that occurs when it expands is temporary. As soon as the expanded regions are no longer in use (at the next reboot, if not sooner) the additional disk space allocations are freed and the pagefile is back to its original state.

Locking a page file's size can be problematic in the case that a Windows application requests more memory than the total size of physical memory and the pagefile. In this case, requests to allocate memory fail, which may cause applications and system processes to fail. Supporters of this view will note that the pagefile is rarely read or written in sequential order, so the performance advantage of having a completely sequential pagefile is minimal. However, it is generally agreed that a large pagefile will allow use of memory-heavy applications, and there is no penalty except that more disk space is used.

the page file is also occasionally recommended to improve performance when a Windows system is chronically using much more memory than its total physical memory. This view ignores the fact that, aside from the temporary results of expansion, the pagefile does not become fragmented over time. In general, performance concerns related to pagefile access are much more effectively dealt with by adding more physical memory.

Linux

and other operating systems use the term "swap" to describe both the act of moving memory pages between RAM and disk, and the region of a disk the pages are stored on. It is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions, although Red Hat recommends using a swap partition. The administrative flexibility of swap files outweighs that of partitions; since modern high capacity hard drives can remap physical sectors, no partition is guaranteed to be contiguous.

Linux supports using a virtually unlimited number of swapping devices, each of which can be assigned a priority. When the operating system needs to swap pages out of physical memory, it uses the highest-priority device with free space. If multiple devices are assigned the same priority, they are used in a fashion similar to level 0 arrangements. This provides improved performance as long as the devices can be accessed efficiently in parallel. Therefore, care should be taken assigning the priorities. For example, swaps located on the same physical disk should not be used in parallel, but in order ranging from the fastest to the slowest (i.e.: the fastest having the highest priority).

Recently, some experimental improvement to the 2.6 have been made by , published in his popular . The improvement, called "swap prefetch", employs a mechanism of prefetching previously swapped pages back to physical memory even before they are actually needed, as long as the system is relatively idle (so as not to impair performance) and there is available physical memory to use. This applies to a situation when a "heavy" application has been temporarily used, causing other processes to swap out. After it is closed, both freeing large areas of memory and reducing disk load, prefetch of other processes starts, reducing their initial user response time.

Mac OS X

, like Linux, supports both swap partitions and the use of swap files, but the default and recommended configuration is to use multiple swap files.

Sun Solaris

Solaris allows swapping to raw disk slices as well as files. The traditional method is to use slice 1 (ie. the second slice) on the OS disk to house swap. Swap setup is managed by the system boot process if there are entries in the "vfstab" file, but can also be managed manually through the use of the "swap" command. While it is possible to remove, at runtime, all swap from a lightly loaded system, Sun does not recommend it. Recent additions to the file system allow creation of ZFS Devices that can be used as swap partitions. Swapping to normal files on ZFS file systems is not supported.

性能

基于虚拟内存的OS的辅存慢于RAM。因此,减少或消除swapping是理想的事情。一些操作系统提供了一些设置来控制内核的决策。

  1. Linux offers the /proc/sys/vm/swappiness parameter, which changes the balance between swapping out runtime memory, as opposed to dropping pages from the system .
  2. Windows 2000, XP, and Vista offer the DisablePagingExecutive registry setting, which controls whether kernel-mode code and data can be eligible for paging out.
  3. Mainframe computers frequently used head-per-track disk drives or drums for swap storage to eliminate the latency implicit in seeking a moveable head.
  4. Flash memory devices have an inherent life limitation which makes them inappropriate for general-purpose swap space. However schemes such as may be used to preload binaries or other read-only data into the virtual memory space.

许多类UNIX OS 允许并行使用多重存储设备用于交换空间来提高性能。

调整交换空间大小

In some older virtual memory operating systems, space in swap backing store is reserved when programs allocate memory for runtime data. OS vendors typically issue guidelines about how much swap space should be allocated. Between 1.5 or 2 times the installed RAM is a typical number . With a large amount of RAM, the disk space needed for the backing store can be very large. Newer versions of these operating systems attempt to solve this problem: for example, some kernels offer a tunable swapmem_on that controls whether RAM can be used for memory reservations. In systems with sufficient RAM, this significantly reduces the needed space allocation for the backing store.

 

阅读(1108) | 评论(0) | 转发(0) |
0

上一篇:鸡翅的N种做法

下一篇:

给主人留下些什么吧!~~