diff --git a/README.md b/README.md index 8d3e5d7..f582263 100644 --- a/README.md +++ b/README.md @@ -314,22 +314,22 @@ cache replacement: LRU (least recently used) policy, random policy bottleneck: TLB access can easily become a bottleneck in the CPU pipeline -Cache - spatial locality: the idea is that if a program accesses memory at address x, it will likely soon access memory near x. +Cache - spatial locality: the idea is that if a program accesses memory at address x, it will likely soon access memory near x. Even though this is the first time the program accesses the array, the TLB improves performance due to spatial locality. The elements of the array are packed tightly into pages (i.e., they are close to one another in space), and thus only the first access to an element on a page yields a TLB miss. -Cache - temporal locality: an instruction or data item that has been recently accessed will likely be re-accessed soon in the future +Cache - temporal locality: an instruction or data item that has been recently accessed will likely be re-accessed soon in the future ### Chapter 20: Paging with smaller tables Crux: Simple array-based page tables (usually called linear page tables) are too big, taking up far too much memory on typical systems. How to make page tables smaller? -Solution 1 - Bigger tables: +Solution 1 - Bigger tables: This type of large page usage is common in database management systems and other high-end commercial applications. The main reason for multiple page sizes is not to save page table space, however; it is to reduce pressure on the TLB, enabling a program to access more of its address space without suffering from too many TLB misses. Problem - internal fragmentation (big pages lead to waste within each page.) -Solution 2 - Hybrid Paging + Segmentation: +Solution 2 - Hybrid Paging + Segmentation: we use the base not to point to the segment itself but rather to hold the physical address of the page table of that segment. The bounds register is used to indicate the end of the page table @@ -353,3 +353,8 @@ Motivate: We will now relax these big assumptions, and assume that we wish to su Crux: How can the OS make use of a larger, slower device to transparently provide the illusion of a large virtual address space? +### Chap 23: Complete VIrtual Memory Systems + +- key elements: page-table designs, interactions with the TLB, abd eviction strategies. + +- The kernel is mapped into each address space: 1. make swap pages much easier (vs. locate kernel entirely in physical memory) 2. make it easy (vs. kernel have its own address space.) 3. now kernel appears almost like a library to applications (good). diff --git a/Textbook/chap23-vm-complete.pdf b/Textbook/chap23-vm-complete.pdf new file mode 100644 index 0000000..4411ee8 Binary files /dev/null and b/Textbook/chap23-vm-complete.pdf differ diff --git a/eviction/CMakeLists.txt b/eviction/CMakeLists.txt new file mode 100644 index 0000000..8bb04c5 --- /dev/null +++ b/eviction/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(eviction) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(eviction main.cpp SwapManager.cpp SwapManager.h) diff --git a/eviction/SwapManager.cpp b/eviction/SwapManager.cpp new file mode 100644 index 0000000..1a77c5a --- /dev/null +++ b/eviction/SwapManager.cpp @@ -0,0 +1,62 @@ +// +// Created by Fangyuan Huang on 10/30/22. +// + +#include "SwapManager.h" + +#include + +using namespace std; + +void SwapManager::push(int address) { + if (space.size() >= capacity) { + if (strategy == "FILO") { + space.pop_back(); + } else if (strategy == "FIFO") { + space.erase(space.begin()); + } else { + cout << "unrecognized strategy:" << strategy << endl; + } + } + space.push_back(address); +} + +bool SwapManager::get(int address) { + bool exist = find(space.begin(), space.end(), address) != space.end(); +// this->total += 1; + total += 1; + if (exist) { + hit += 1; + printf("Job %d found! HIT = %d, MISS = %d, TOTAL = %d, HITRATE = %.2f\n", + address, hit, miss, total, 100. * hit / total); + } else { + miss += 1; + push(address); + printf("Job %d miss! HIT = %d, MISS = %d, TOTAL = %d, HITRATE = %.2f\n", + address, hit, miss, total, 100. * hit / total); + } + + return true; +} + +void SwapManager::setCapacity(int capacity) { + if (capacity < 0) + throw invalid_argument("capacity"); + this->capacity = capacity; +} + +void SwapManager::setStrategy(string strategy) { + if (strategy != "FIFO" && strategy != "FILO") + throw invalid_argument("strategy"); + this->strategy = strategy; +} + +SwapManager::SwapManager(int capacity, string strategy) { + cout << "Constructing a swap manager..." << endl; + this->space = vector(); + hit = 0; + miss = 0; + total = 0; + setCapacity(capacity); + setStrategy(strategy); +} \ No newline at end of file diff --git a/eviction/SwapManager.h b/eviction/SwapManager.h new file mode 100644 index 0000000..8c1c3e3 --- /dev/null +++ b/eviction/SwapManager.h @@ -0,0 +1,34 @@ +// +// Created by Fangyuan Huang on 10/30/22. +// + +#ifndef EVICTION_SWAPMANAGER_H +#define EVICTION_SWAPMANAGER_H + +#include +#include + +using namespace std; + +class SwapManager { +public: + SwapManager(int capacity, string strategy); + vector space; + // add an address into memory, if full, kick one out according to strategy + void push(int address); + // tells whether the space contains the address in its memory + bool get(int address); + void setCapacity(int capacity); + void setStrategy(string strategy); + +private: + int capacity; + string strategy; + int hit; + int miss; + int total; + +}; + + +#endif //EVICTION_SWAPMANAGER_H diff --git a/eviction/main.cpp b/eviction/main.cpp new file mode 100644 index 0000000..6aae0e5 --- /dev/null +++ b/eviction/main.cpp @@ -0,0 +1,20 @@ +#include + +#include "SwapManager.h" + +int main() { + vector jobList = {1,2,3,4,5,6,7,1,2,3,4,5,6,7}; + // test 1, FIFO + SwapManager FIFO = SwapManager(5, "FIFO"); + for( int job: jobList) { + FIFO.get(job); + } + + SwapManager FILO = SwapManager(5, "FILO"); + for( int job: jobList) { + FILO.get(job); + } + + std::cout << "Hello, World!" << std::endl; + return 0; +}