Skip to content

Commit

Permalink
add chap23 and eviction policy cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyatgithub committed Nov 1, 2022
1 parent 840c302 commit 5d39615
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 4 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Binary file added Textbook/chap23-vm-complete.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions eviction/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
62 changes: 62 additions & 0 deletions eviction/SwapManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Created by Fangyuan Huang on 10/30/22.
//

#include "SwapManager.h"

#include <iostream>

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<int>();
hit = 0;
miss = 0;
total = 0;
setCapacity(capacity);
setStrategy(strategy);
}
34 changes: 34 additions & 0 deletions eviction/SwapManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Fangyuan Huang on 10/30/22.
//

#ifndef EVICTION_SWAPMANAGER_H
#define EVICTION_SWAPMANAGER_H

#include <vector>
#include <string>

using namespace std;

class SwapManager {
public:
SwapManager(int capacity, string strategy);
vector<int> 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
20 changes: 20 additions & 0 deletions eviction/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

#include "SwapManager.h"

int main() {
vector<int> 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;
}

0 comments on commit 5d39615

Please sign in to comment.