1 / 35

حافظه cache

حافظه cache. اگر قسمت فعال برنامه و داده ها را در حافظه سریع و کوچکی قرار دهیم، میتوانیم با کم کردن میانگین زمان دسترسی به حافظه زمان اجرای برنامه را کاهش دهیم . این حافظه سریع و کوچک را حافظه cache می نامند .

Download Presentation

حافظه cache

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. حافظه cache • اگرقسمتفعالبرنامهودادههارادرحافظهسریعوکوچکیقراردهیم،میتوانیمباکمکردنمیانگینزماندسترسیبهحافظهزماناجرایبرنامهراکاهشدهیم. • اینحافظهسریعوکوچکراحافظه cache مینامند. • معمولا حافظه cache زماندسترسیبسیارکمترینسبتبهحافظهاصلیدارد 5 )تا 10 برابرکمتر( • دریککامپیوترممکناستعمل cache تاچندسطحتکرارگردد.

  2. L1 cache holds cache lines retrieved from the L2 cache memory. L2 cache holds cache lines retrieved from main memory. Main memory holds disk blocks retrieved from local disks. Local disks hold files retrieved from disks on remote network servers. مثالیازسلسلهمراتبحافظه Smaller, faster, and costlier (per byte) storage devices L0: registers CPU registers hold words retrieved from L1 cache. on-chip L1 cache (SRAM) L1: off-chip L2 cache (SRAM) L2: main memory (DRAM) L3: Larger, slower, and cheaper (per byte) storage devices local secondary storage (local disks) L4: remote secondary storage (distributed file systems, Web servers) L5:

  3. نحوهعمل cache • وقتیکه cpu نیازبهدسترسیبهحافظهداردابتداحافظه cache راجستجومینماید: اگردادهدراینحافظهسریعموجودبودازآناستفادهمیشوددرغیراینصورتبارجوعبهحافظهبلوکیازدادهکهشاملدادهموردنیاز cpu میشودازحافظهاصلیبهحافظه cache منتقلمیگردد. CPU حافظهاصلی 32K x 12 حافظه cache 512 x 12 Word access Block transfer

  4. Hit Rate and Miss Penalty • Hit Rate • Ratio of hits to attempted accesses • Greater than 0.9 essential for high performance • Miss Penalty • Extra time needed to bring desired info into the cache

  5. درصدموفقیت • کارائیحافظه cache باضریبیبانامدرصدموفقیت hit ratio اندازهگیریمیشود. • ایندرصدعبارتاستازنسبتتعداددفعاتیکهدادهدر cache یافتشدهبهتعدادکلدفعاترجوعبهحافظه. • هرچهمقدارایندرصدبیشترباشدسرعتدسترسیبهحافظهبهسرعت cache نزدیکترمیشود. • مثال: • یککامپیوتربازماندسترسی 100ns برایحافظه cache و 1000nsبرایحافظهاصلیدرصورتداشتندرصدموفقیت 0.9زماندسترسیبرابربا 200ns خواهدداشت.

  6. Cache Operation (Read) • Read Hit • Item in cache • Read Miss • Block copied from main memory to cache • word sent to processor after copy • word sent to processor as soon as available (load-through)

  7. Cache Operation (Write) • Write Hit • Write-through • cache and main memory updated simultaneously • Write-back • only cache updated • dirty (modified) bit • block written back when removed from cache

  8. Cache Operation (Write) • Write Miss • Write-through • main memory written directly • Write-back • block first brought into cache • then desired word in cache overwritten

  9. تاثیر اندازه بلوک های cache • در حالت کلی با بزرگ شدن اندازه بلوک های cache میزان hit rate افزایش می یابد. اما به همین ترتیب میزاmiss penalty یعنی زمانی که برای انتقال اطلاعات به کاشه لازم است نیز افزایش خواهد یافت. Average Access Time Increased Miss Penalty & Miss Rate Block Size • Average Access Time: • = Hit Time x Hit Ratio + Miss Penalty x (1 – Hit Ratio)

  10. Valid Bit Main memory address in cache block Index Tag Data 00 + 00 = 0000 Invalid Invalid 01 + 11 = 0111 00 01 10 11 1 0 0 1 00 11 01 01 حالت شروع • در حالت شروع به کار، حافظه خالی است و هیچ اطلاعات معتبری در آن وجود ندارد. برای نشان دادن این وضعیت از یک بیت VALID استفاده میشود. • در حالت شروع بکار کامپیوتر همه این بیت ها با صفر پر میشوند. با نوشتن داده در cache این بیت به 1 تغییر داده میشود.

  11. نگاشت • عملانتقالدادهازحافظهاصلیبهحافظه cache رانگاشتمینامند. اینکاربهسهطریقانجاممیشود: • Direct Mapping • Associate Mapping • Set-associative mapping

  12. نگاشتمستقیم Direct Mapping • دراینروشبرایپیادهسازی cache ازیکحافظه RAM سریعاستفادهمیشود: • اگرحافظهاصلیدارای 2nکلمهوحافظه cache دارای 2kکلمهباشنددرنتیجهبهترتیببه n و k بیتآدرسنیازخواهندداشت. • آدرس n بیتیحافظهاصلیبصورتزیرتقسیممیشود Index Tag n-k k

  13. نگاشتمستقیم Direct Mapping • درحافظه cache علاوهبردادهاطلاعاتمربوطبه Tag همذخیرهمیشود. 00000 00777 01000 01777 02000 27777 1220 Index Tag Data 000 00 1220 2340 3450 4560 5670 777 27 6710 6710 Cache Main memory

  14. نگاشتمستقیم Direct Mapping • وقتیکهآدرسیتوسط CPU تولیدمیشودبااستفادهازقسمت Index آنیککلمهازحافظه cache خواندهشدهومقدار Tag ذخیرهشدهدرآنبامقدار Tag آدرسمقایسهمیگردد. • اگردو Tag یکسانبودندداده cacheمورداستفادهقرارمیگیرد (hit) • درغیراینصورت (miss) دادهازحافظهخواندهشدهوهمراهبا Tag جدیددر cache نوشتهمیشود. • دراینروشاگررجوعبهآدرسهائیبا index یکسانزیاداتفاقبیافتد،درصدموفقیتپائینمیاید.

  15. Index Valid Tag Data Address (32 bits) 0 1 2 3 ... ... 1022 1023 22 10 Index To CPU Tag Hit = دسترسی به داده در نگاشت مستقیم

  16. Index Valid Tag Data Address (32 bits) 0 1 2 3 ... ... ... 22 10 Index Tag Data 1 نوشتن داده در cache • The lowest k bits of the address specify a cache block. • The upper (m-k) address bits are stored in the block’s tag field. • The data from main memory is stored in the block’s data field. • The valid bit is set to 1.

  17. نگاشتمستقیمبا اندازه بلوک بزرگتر • معمولا اندازه بلوک بزرگتر از یک در نظر گرفته میشود: Tag Data Index 000 007 01 01 3450 6578 Block 0 010 017 00 00 1340 1658 Block 1 Block Tag Word Cache n-k k

  18. Set-associative mapping • دراینروشهرمحلازحافظه cache میتواندچندینکلمهباآدرس Index یکسانراذخیرهنماید. • تعداد data-tag هایذخیرهشدهدرحافظهیک set خواندهمیشود. • برایمقایسه tag آدرستولیدشدهبامقادیرذخیرهشدهازحافظه associative استفادهمیشود. • بابزرگشدن set هادرصدموفقیت cache افزایشمییابد

  19. 2-way Set-associative mapping Index Tag Data Tag Data 000 00 1220 02 5670 777 02 6710 00 2340 Cache Cache Cache that has k blocks per set is referred to as a k-way set-associative cache

  20. الگوریتمهایجایگزینی • وقتیکهیک miss اتفاقمیافتد،درصورتیکهیک set پرباشدلازممیشودتایکیازدادههایآنخالیشدهودادهجدیدوارد cache شود. • روشهایمختلفیبرایاینکاروجوددارد: • first-in, first-out • Random replacement • Least recently used (LRU)

  21. Cache Index Valid Cache Tag Cache Data Cache Data Cache Tag Valid Cache Block 0 Cache Block 0 : : : : : : Adr Tag Compare Compare 1 0 Mux Sel1 Sel0 OR Cache Block Hit

  22. Example: 4-way set associative Cache

  23. نوشتندرحافظه cache • یکیازمسایلمهمدرارتباطباحافظه cache نحوهعملدرهنگامنوشتناطلاعاتدرحافظهاست. • هنگامخواندندادههاوقتیدادهدر cache وجودداشتهباشد،نیازیبهرجوعبهحافظهاصلینیستاماوقتیدادهرادرحافظهمینویسیمممکناستبهدوطریقعملشود: • : write through دراینروشدرهربارنوشتندرحافظهدادههمدر cache وهمدرحافظهاصلینوشتهمیشود. • : write back دراینروشدادهفقطدر cache نوشتهشدهوبایکپرچم set میشود. تازمانیکهایندادهدر cache قرارداردازایندادهاستفادهخوادشد،امادرصورتانتقالدادهاز cacheمقدارآندرحافظهاصلینیز update میگردد.

  24. حافظه cache در 80486 • درداخلاینپردازندهیککاشه 8k بایتیدرنظرگرفتهشدهاست. وازبلوکهای 16 بایتیاستفادهمیشود. • براینوشتندرکاشهازروش write through استفادهمیشود. • درصدموفقیتبالاتراز 90 درصداست. • میتوانازیککاشهخارجیدیگرنیزاستفادهنمود.

  25. سلسلهمراتبحافظهدرپردازندههایجدیدسلسلهمراتبحافظهدرپردازندههایجدید Intel Pentium 4, 2.2 GHz Processor.

  26. حافظه associative • ازاینحافظهبرایتسریععملجستجودربیندادههایذخیرهشدهاستفادهمیشود. • برایکاهشزمانجستجوبجایاستفادهازآدرس،ازمحتویخوددادهاستفادهمیشود. اینروشراگاهی content addressable memory (CAM) هممینامند • هنگامنوشتندادهدراینحافظهنیازیبهآدرسنیست! • اینحافظهقادراستتاجایخالیراپیدانمودهودادهرادرآنجاذخیرهمینماید. • هنگامخواندن دادهازحافظه،مقداردادهویابخشیازآنبهحافظهارائهشدهوحافظهتمامیکلماتذخیرهشدهایراکهبادادهموردنظریکسانهستندراعلامتگذاریکردهوآمادهخواندنمینماید

  27. سختافزارمربوطه Argument Register (A) Key Register (K) match register Associative memory array and logic m words n bit per words N Input read write Output

  28. Associate mapping • در این روش که سریعترین راه پیاده سازی یک cache است از یک حافظه associative استفاده میشود. • در این حافظه هم آدرس و هم محتوی یک کلمه ذخیره میشوند. در نتیجه cache میتواند محتوی هر محل از حافظه را ذخیره نماید. • هنگام جستجو برای یک داده آدرس آن به حافظه associativeعرضه میشود. در صورتیکه ورودی متناظری در حافظه باشد، داده مربوطه در خروجی ظاهر میگردد. در غیر اینصورت هر دوی آدرس و داه در حافظه associative ذخیره خواهند شد. • معمولا از الگوریتم FIFOبرای جایگزینی در cache استفاده میشود

  29. Associate mapping CPU Address Argument Register Address Data 00 1220 02 6710 Cache

  30. Associative Mapping • Main memory block can be placed into any cache block • Contention occurs only when cache is full • Replacement algorithm required to chose block to repalce • Associative search of tags in parallel • Most flexible • Costly

  31. Instructions versus Data • Modern system designs frequently use a pair of separate cache memories, one for storing processor instructions and another for storing the program’s data • Why is this a good idea?

  32. AMD Opteron – caches, etc

  33. Cache memory, L1 cache, L2 cache This is a Pentium from 1993! Now processor cores are even smaller.

  34. Cache memory, L1 cache, L2 cache

  35. Questions?

More Related