<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yoichi Kawasaki&#039;s Web &#187; lru</title>
	<atom:link href="http://yk55.com/blog/tags/lru/feed/" rel="self" type="application/rss+xml" />
	<link>http://yk55.com/blog</link>
	<description>my publicly accessible private memorandums</description>
	<lastBuildDate>Mon, 14 May 2012 01:31:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<!-- google_ad_section_end --><!-- google_ad_section_start(weight=ignore) -->	<item>
		<title>ページ置換アルゴリズム(Page Replacement Algorithms)のシュミレーション</title>
		<link>http://yk55.com/blog/2010/03/13/page_replacement_algo_simulation/</link>
		<comments>http://yk55.com/blog/2010/03/13/page_replacement_algo_simulation/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 00:46:33 +0000</pubDate>
		<dc:creator>yoichi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[fifo]]></category>
		<category><![CDATA[lru]]></category>
		<category><![CDATA[optimal]]></category>

		<guid isPermaLink="false">http://yk55.com/blog/?p=227</guid>
		<description><![CDATA[システムのオペレーションをやっているとスラッシングが原因でサーバが一時的にサービス提供不能になっていまうなんてことがたまにある。スラッシングとは、例えば大量の処理リクエストが発生してしまい利用可能な物理メモリ量を大きく超えてしまった場合に起ったりする。OSのメモリ割り当て処理（ページアウト・イン）が追いつかないため全体のパフォーマンスが極端に低下、最悪固まってしまいlsすらできなくなる。この記事のタイトルにあるページ置換アルゴリズム（Page Replacement Algorithm）はこのページ割り当てを効率的に行うためのアルゴリズム群のことでどれを選択するかによってパフォーマンスが大きく左右される。ちなみにこのアルゴリズム群はページに限らずキャッシュやメモリプールなど限られたリソースを効率的に割り当てる場合には必ずといっていいくらい登場するものなのでプログラマとしては是非とも押さえておきたいところ。 前置きが少し長くなってきたのでここらで本題に入る。ページ置換アルゴリズムを調べていたときにumassの教材として作られたであろう置換アルゴリズムのシュミレーションツールを見つけた。JavaScript版とJava Applet版の2つ。アルゴリズム学習者にとっては目で見て挙動が確認できるので便利なはず。 これ見て自分もなんとなく作ってみたくなったので勉強がてらにcで作ってみた。もちろんCUI。アルゴリズムはLRU、FIFO、OPTIMALの3つに絞った。これがc版ページ置換アルゴリズムシュミレーションツール。 http://github.com/yokawasa/any/raw/master/page_rep_algos/page_rep_algos.c 内容は、見つけたツールと同じで物理メモリのページ・フレームの数、置換アルゴリズム、そして今後メモリ割り当ての必要なページ番号の数列を指定して順番にそのページ番号に対して物理メモリの割り当てを行うといったもの。利用ページ番号が既に物理メモリの割り当てが既にされていればHit、されていなければMiss（ページフォルト発生）として過去に割り当てたページの置き換えを行い、最終的なページフォールト発生数とHit率を出力する。 以下、ツールのコンパイルとその使い方、そして各アルゴリズムの挙動をツールの実行結果とあわせて説明する。 ツールのコンパイルとその使い方 上記URLからソースコードを取得してからコンパイルを行う。 gcc -o page_rep_algos page_rep_algos.c 使い方はアルゴリズム、アクセスするページ番号の数列、ページフレーム数の3つのオプションを次のように指定。 ./page_rep_algos [options] Options are: -a &#160;&#60;アルゴリズム&#62; &#160; LRU, FIFO または OPTIMAL -p &#160;&#60;ページ番号列&#62; &#160; Page # sequences in which the pages are accessed by some program &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;in the order. [...]]]></description>
			<content:encoded><![CDATA[<p>システムのオペレーションをやっているとスラッシングが原因でサーバが一時的にサービス提供不能になっていまうなんてことがたまにある。スラッシングとは、例えば大量の処理リクエストが発生してしまい利用可能な物理メモリ量を大きく超えてしまった場合に起ったりする。OSのメモリ割り当て処理（ページアウト・イン）が追いつかないため全体のパフォーマンスが極端に低下、最悪固まってしまいlsすらできなくなる。この記事のタイトルにある<a href="http://ja.wikipedia.org/wiki/%E3%83%9A%E3%83%BC%E3%82%B8%E7%BD%AE%E6%8F%9B%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0">ページ置換アルゴリズム</a>（<a href="http://en.wikipedia.org/wiki/Page_replacement_algorithm">Page Replacement Algorithm</a>）はこのページ割り当てを効率的に行うためのアルゴリズム群のことでどれを選択するかによってパフォーマンスが大きく左右される。ちなみにこのアルゴリズム群はページに限らずキャッシュやメモリプールなど限られたリソースを効率的に割り当てる場合には必ずといっていいくらい登場するものなのでプログラマとしては是非とも押さえておきたいところ。</p>
<p>前置きが少し長くなってきたのでここらで本題に入る。ページ置換アルゴリズムを調べていたときに<a href="http://www.ecs.umass.edu/">umass</a>の教材として作られたであろう置換アルゴリズムのシュミレーションツールを見つけた。<a href="http://www.ecs.umass.edu/ece/koren/architecture/PReplace/">JavaScript版</a>と<a href="http://www.ecs.umass.edu/ece/koren/architecture/RepPolicies/">Java Applet版</a>の2つ。アルゴリズム学習者にとっては目で見て挙動が確認できるので便利なはず。 これ見て自分もなんとなく作ってみたくなったので勉強がてらにcで作ってみた。もちろんCUI。アルゴリズムはLRU、FIFO、OPTIMALの3つに絞った。これがc版ページ置換アルゴリズムシュミレーションツール。</p>
<p><a href="http://github.com/yokawasa/any/raw/master/page_rep_algos/page_rep_algos.c">http://github.com/yokawasa/any/raw/master/page_rep_algos/page_rep_algos.c</a></p>
<p>内容は、見つけたツールと同じで物理メモリのページ・フレームの数、置換アルゴリズム、そして今後メモリ割り当ての必要なページ番号の数列を指定して順番にそのページ番号に対して物理メモリの割り当てを行うといったもの。利用ページ番号が既に物理メモリの割り当てが既にされていればHit、されていなければMiss（ページフォルト発生）として過去に割り当てたページの置き換えを行い、最終的なページフォールト発生数とHit率を出力する。</p>
<p>以下、ツールのコンパイルとその使い方、そして各アルゴリズムの挙動をツールの実行結果とあわせて説明する。</p>
<h2><strong> ツールのコンパイルとその使い方</strong></h2>
<p><a href="http://github.com/yokawasa/any/raw/master/page_rep_algos.c">上記URL</a>からソースコードを取得してからコンパイルを行う。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">gcc -o page_rep_algos page_rep_algos.c</div></div>
<p>使い方はアルゴリズム、アクセスするページ番号の数列、ページフレーム数の3つのオプションを次のように指定。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">./page_rep_algos [options]<br />
Options are:<br />
-a &nbsp;&lt;アルゴリズム&gt; &nbsp; LRU, FIFO または OPTIMAL<br />
-p &nbsp;&lt;ページ番号列&gt; &nbsp; Page # sequences in which the pages are accessed by some program<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in the order. max entry num is 100. &nbsp;(space between each)<br />
-f &nbsp;&lt;フレーム数&gt; &nbsp; &nbsp; The number of page frames. max frame num is 10.<br />
-h &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Display usage information (this message)<br />
<br />
実行例:<br />
./page_rep_algos -a LRU -p &quot;0 1 2 3 2 1 4 3 6 0 9&quot; -f 3</div></div>
<h2><strong>LRU（Least Recently Used）</strong></h2>
<p><a href="http://ja.wikipedia.org/wiki/%E3%83%9A%E3%83%BC%E3%82%B8%E7%BD%AE%E6%8F%9B%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0#LRU.EF.BC.88Least_Recently_Used.EF.BC.89">LRU</a>ではその名のとおり「最も長く使われないページ」を置換の対象とする。なのでいつ追加されたということよりも最後にアクセスされてからどれだけ経過しているかを測り最も長いものから置換していく。 例えばページフレーム数3でアクセスページ番号列を「0 1 2 3 2 1 4 1 6 0 2 1」とした場合、1つずつトレースすると次のような図になる。Faultとはページフォールト(Page Fault)のことでFaultのない場合はページHitしたことを意味する。</p>
<p><a title="LRU by yoichi*, on Flickr" href="http://www.flickr.com/photos/yk55/4423171522/"><img src="http://farm3.static.flickr.com/2721/4423171522_0f239aa4ab_o.png" alt="LRU" width="598" height="202" /></a></p>
<p>Page Referenceが4の時のページ置換計算を例にする。置換される前の各ページフレームPF[0]、PF[1]、PF[2]にはそれぞれページ番号3、1、2が割り当てられている。ページ番号4がアクセスされるタイミングでページ番号3、1、2がアクセスされていない期間はそれぞれ3、1、2となる。よって最も長くアクセスされていないPF[0](ページ番号3)が置換対象となる。ツールで実行してみると次のとおり。ページフォールトが9回で、Hit率25%。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[kawasaki@debian:~] $ ./page_rep_algos -a LRU -p &quot;0 1 2 3 2 1 4 1 6 0 2 1&quot; -f 3<br />
0: &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
3: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
1: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
4: &nbsp; &nbsp;4 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;4 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
6: &nbsp; &nbsp;4 &nbsp; 1 &nbsp; 6 &nbsp; &nbsp;Miss<br />
0: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 6 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;0 &nbsp; 2 &nbsp; 6 &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;0 &nbsp; 2 &nbsp; 1 &nbsp; &nbsp;Miss</div></div>
<p>Number of Page Frames = 3<br />
Total Number of References = 12<br />
Number of Hits = 3<br />
Number of Page Fault = 9<br />
HitRatio = 0.25</p>
<h2><strong>FIFO（First-In First-Out）</strong></h2>
<p><a href="http://ja.wikipedia.org/wiki/%E3%83%9A%E3%83%BC%E3%82%B8%E7%BD%AE%E6%8F%9B%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0#FIFO.EF.BC.88First-In_First-Out.EF.BC.89">FIFO</a>もその名のとおり「早く追加されたページ」を置換の対象とする。この場合はLRUと違っていつ追加されたということが重要であり最も早いものから置換していく。<br />
LRUと同くサンプル( ページフレーム数3でアクセスページ番号列を「0 1 2 3 2 1 4 1 6 0 2 1」とした場合、1つずつトレースすると次のような図になる。</p>
<p><a title="FIFO by yoichi*, on Flickr" href="http://www.flickr.com/photos/yk55/4422406289/"><img src="http://farm3.static.flickr.com/2724/4422406289_e4f1663cc2_o.png" alt="FIFO" width="597" height="202" /></a></p>
<p>ツールで実行してみると次のとおり。ページフォールトが10回で、Hit率17%。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[kawasaki@debian:~] $ ./page_rep_algos -a FIFO -p &quot;0 1 2 3 2 1 4 1 6 0 2 1&quot; -f 3<br />
0: &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
3: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
1: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
4: &nbsp; &nbsp;3 &nbsp; 4 &nbsp; 2 &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;3 &nbsp; 4 &nbsp; 1 &nbsp; &nbsp;Miss<br />
6: &nbsp; &nbsp;6 &nbsp; 4 &nbsp; 1 &nbsp; &nbsp;Miss<br />
0: &nbsp; &nbsp;6 &nbsp; 0 &nbsp; 1 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;6 &nbsp; 0 &nbsp; 2 &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;1 &nbsp; 0 &nbsp; 2 &nbsp; &nbsp;Miss</div></div>
<p>Number of Page Frames = 3<br />
Total Number of References = 12<br />
Number of Hits = 2<br />
Number of Page Fault = 10<br />
HitRatio = 0.17</p>
<h2><strong>OPTIMAL</strong></h2>
<p><a href="http://ja.wikipedia.org/wiki/%E3%83%9A%E3%83%BC%E3%82%B8%E7%BD%AE%E6%8F%9B%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0#.E7.90.86.E8.AB.96.E4.B8.8A.E6.9C.80.E9.81.A9.E3.81.AA.E3.83.9A.E3.83.BC.E3.82.B8.E7.BD.AE.E6.8F.9B.E3.82.A2.E3.83.AB.E3.82.B4.E3.83.AA.E3.82.BA.E3.83.A0">OPTIMAL</a>とは未来に対して「最も長い間使われないであろう」ページを置換の対象とする。LRUが過去に対して「最も長く使われないページ」であるのと対象的であるといえる。ただし、実行時に未来の計測をするのは不可能であり経験則でパターン分析ができているとか、今回のように予めパターンを指定する場合においてのみ有効である。理論上一番いいのはOPTIMALアルゴリズムといわれているが未来予測どうするのかといったところでしょうか。</p>
<p><a title="OPTIMAL by yoichi*, on Flickr" href="http://www.flickr.com/photos/yk55/4422406307/"><img src="http://farm5.static.flickr.com/4071/4422406307_9ab8e71c4a_o.png" alt="OPTIMAL" width="599" height="202" /></a></p>
<p>Page Referenceが4の時のページ置換計算を例にする。 置換される前の各ページフレームPF[0]、PF[1]、PF[2]にはそれぞれページ番号3、1、2が割り当てられている。ページ番号4がアクセスされるタイミングからページ番号3、1、2が未来にアクセスされるまでの期間はそれぞれ5以上、1、4となる。よって最も遠い未来までアクセスされないPF[0](ページ番号3)が置換対象となる。ツールで実行してみると次のとおり。ページフォールトが7回で、Hit率42%と最も高い。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[kawasaki@debian:~] $ ./page_rep_algos -a OPTIMAL -p &quot;0 1 2 3 2 1 4 1 6 0 2 1&quot; -f 3<br />
0: &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
3: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
1: &nbsp; &nbsp;3 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
4: &nbsp; &nbsp;4 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
1: &nbsp; &nbsp;4 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
6: &nbsp; &nbsp;6 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
0: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Miss<br />
2: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit<br />
1: &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; &nbsp;Hit</div></div>
<p>Number of Page Frames = 3<br />
Total Number of References = 12<br />
Number of Hits = 5<br />
Number of Page Fault = 7<br />
HitRatio = 0.42</p>
<p>シュミレーションの結果、OPTIMALアルゴリズムのHit率が最も高く、これが最も置換効率がよいということになる。今回はインプットするサンプル数が少ないのでまともな結果を出すならばデータサンプルをもっと増やす必要があるのだろうけどこの辺で終わりにしておきます。</p>
<p>おわり。</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://yk55.com/blog/2010/03/13/page_replacement_algo_simulation/&amp;layout=button_count&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=arial" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe>]]></content:encoded>
			<wfw:commentRss>http://yk55.com/blog/2010/03/13/page_replacement_algo_simulation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	<!-- google_ad_section_end --></channel>
</rss>

