Last active
November 25, 2019 17:57
-
-
Save zion830/3db2fd164331b43aaf22781cb257062b to your computer and use it in GitHub Desktop.
캐시메모리 시나리오 1, 시나리오 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Register { | |
private Integer r1 = null; | |
private Integer r2 = null; | |
public Integer getR1() { | |
return r1; | |
} | |
public void setR1(Integer r1) { | |
this.r1 = r1; | |
} | |
public Integer getR2() { | |
return r2; | |
} | |
public void setR2(Integer r2) { | |
this.r2 = r2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/* | |
* 시나리오 1번 | |
*/ | |
public class Scenario1 { | |
public static void main(String[] args) { | |
Random random = new Random(); | |
// for (int i = 0; i < 400; i++) { | |
int n1 = random.nextInt(5000); // 가수 (4바이트 정수) | |
int n2 = random.nextInt(5000); // 피가수 (4바이트 정수) | |
Register register = new Register(); | |
register.setR1(Storage.search(n1)); | |
register.setR2(Storage.search(n2)); | |
System.out.println("덧셈 결과 : " | |
+ register.getR1() + " + " | |
+ register.getR2() + " = " | |
+ (register.getR1() + register.getR2())); | |
// } | |
System.out.println(String.format("AccessTime : %.2f", Storage.getAccessTime())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/* | |
* 시나리오 2번 | |
*/ | |
public class Scenario2 { | |
public static void main(String[] args) { | |
Random random = new Random(); | |
int n = random.nextInt(5000); // 가수 (4바이트 정수) | |
Register register = new Register(); | |
register.setR1(Storage.search(n)); | |
System.out.println(String.format("AccessTime : %.2f", Storage.getAccessTime())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayDeque; | |
import java.util.Deque; | |
import java.util.Queue; | |
/* | |
* LV1~3이 있는 캐시메모리 | |
* */ | |
public class Storage { | |
private static float accessTime = 0.0f; | |
private static CacheMemory cacheMemory = new CacheMemory(); | |
private static RAM memory = new RAM(); | |
private static HardDisk hardDisk = new HardDisk(); | |
static public void initAccessTime() { | |
accessTime = 0.0f; | |
} | |
static public float getAccessTime() { | |
return accessTime; | |
} | |
static private void increaseAccessTime(float delay) { | |
accessTime += delay; | |
} | |
static public Integer search(int value) { | |
System.out.println("=======검색 시작, value : " + value + "======="); | |
return cacheMemory.search(value); | |
} | |
static class CacheMemory { | |
private Deque<Integer> level1 = new ArrayDeque<>(5); | |
private Deque<Integer> level2 = new ArrayDeque<>(20); | |
private Deque<Integer> level3 = new ArrayDeque<>(200); | |
Integer search(int value) { | |
Integer result = searchCache(value, level1); | |
if (result == null) { // level1 miss | |
System.out.println("cache : level1 miss"); | |
result = searchCache(value, level2); | |
if (result == null) { // level2 miss | |
System.out.println("cache : level2 miss"); | |
result = searchCache(value, level3); | |
if (result == null) { // level3 miss | |
System.out.println("cache : level3 miss, memory에서 탐색 시작"); | |
result = memory.search(value); // memory에서 찾은 값 저장 | |
saveToLV1(result); | |
saveToLV2(result); | |
} | |
} else { | |
saveToLV1(result); | |
System.out.println("cache : level2 hit"); | |
} | |
} else { | |
System.out.println("cache : level1 hit"); | |
} | |
System.out.println(String.format("캐시 탐색 완료, return value : %d, access time : %.2f", result, accessTime)); | |
printCache(); | |
System.out.println("\n==================================="); | |
return result; | |
} | |
private Integer searchCache(int value, Queue<Integer> cache) { | |
increaseAccessTime(0.1f); | |
for (Integer num : cache) { | |
if (num == null || value == num) { | |
return num; // hit | |
} | |
} | |
return null; // miss | |
} | |
private void saveToLV1(int newValue) { | |
if (level1.size() >= 5) { | |
level1.pop(); // 용량 부족시 선입 선출로 제거 | |
} | |
level1.add(newValue); | |
} | |
private void saveToLV2(int newValue) { | |
if (level2.size() >= 20) { | |
level2.pop(); | |
} | |
level2.add(newValue); | |
} | |
private void saveToLV3(int newValue) { | |
if (level3.size() >= 200) { | |
level3.pop(); | |
} | |
level3.add(newValue); | |
} | |
private void printCache() { | |
System.out.print("level1 : "); | |
for (Integer integer : level1) { | |
System.out.print(integer + ", "); | |
} | |
System.out.print("\nlevel2 : "); | |
for (Integer integer : level1) { | |
System.out.print(integer + ", "); | |
} | |
System.out.print("\nlevel3 : "); | |
for (Integer integer : level3) { | |
System.out.print(integer + ", "); | |
} | |
} | |
} | |
static class RAM { | |
private Deque<Integer> memory = new ArrayDeque<>(500); | |
Integer search(int value) { | |
increaseAccessTime(1); | |
if (memory.contains(value)) { | |
System.out.println("memory : hit"); | |
return value; // hit | |
} else { | |
System.out.println("memory : miss, HDD에서 탐색 시작"); | |
Integer valueFromHDD = hardDisk.search(value); | |
save(valueFromHDD); | |
System.out.println("HDD에서 가져온 데이터 : " + valueFromHDD); | |
cacheMemory.saveToLV3(valueFromHDD); | |
return valueFromHDD; | |
} | |
} | |
private void save(int newValue) { | |
if (memory.size() >= 500) { | |
memory.pop(); // 용량 부족시 선입 선출로 제거 | |
} | |
memory.push(newValue); | |
} | |
} | |
static class HardDisk { | |
private Integer[] disk = new Integer[5000]; | |
private HardDisk() { | |
for (int i = 0; i < disk.length; i++) { | |
disk[i] = i + 1; // 유일한 값 저장 (1~5000) | |
} | |
} | |
Integer search(int value) { | |
increaseAccessTime(3); | |
for (Integer num : disk) { | |
if (value == num) { | |
return num; | |
} | |
} | |
return -1; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayDeque; | |
import java.util.Deque; | |
import java.util.Queue; | |
/* | |
* LV1만 있는 캐시메모리 | |
* */ | |
public class StorageOnlyLv1 { | |
private static float accessTime = 0.0f; | |
private static CacheMemory cacheMemory = new CacheMemory(); | |
private static RAM memory = new RAM(); | |
private static HardDisk hardDisk = new HardDisk(); | |
static public void initAccessTime() { | |
accessTime = 0.0f; | |
} | |
static public float getAccessTime() { | |
return accessTime; | |
} | |
static private void increaseAccessTime(float delay) { | |
accessTime += delay; | |
} | |
static public Integer search(int value) { | |
System.out.println("=======검색 시작, value : " + value + "======="); | |
return cacheMemory.search(value); | |
} | |
static class CacheMemory { | |
private Deque<Integer> level1 = new ArrayDeque<>(5); | |
Integer search(int value) { | |
Integer result = searchCache(value, level1); | |
if (result == null) { // level1 miss | |
result = memory.search(value); // memory에서 찾은 값 저장 | |
saveToLV1(result); | |
System.out.println("cache : level1 miss"); | |
} else { | |
System.out.println("cache : level1 hit"); | |
} | |
System.out.println("캐시 탐색 완료, return value : " + result + ", access time : " + accessTime); | |
printCache(); | |
System.out.println("\n==================================="); | |
return result; | |
} | |
private Integer searchCache(int value, Queue<Integer> cache) { | |
increaseAccessTime(0.1f); | |
for (Integer num : cache) { | |
if (num == null || value == num) { | |
return num; // hit | |
} | |
} | |
return null; // miss | |
} | |
private void saveToLV1(int newValue) { | |
if (level1.size() >= 5) { | |
level1.pop(); // 용량 부족시 선입 선출로 제거 | |
} | |
level1.push(newValue); | |
} | |
private void printCache() { | |
System.out.println("level1 : "); | |
for (Integer integer : level1) { | |
System.out.print(integer + ", "); | |
} | |
} | |
} | |
static class RAM { | |
private Deque<Integer> memory = new ArrayDeque<>(500); | |
Integer search(int value) { | |
increaseAccessTime(1); | |
if (memory.contains(value)) { | |
System.out.println("memory : hit"); | |
return value; // hit | |
} else { | |
System.out.println("memory : miss, HDD에서 탐색 시작"); | |
Integer valueFromHDD = hardDisk.search(value); | |
save(valueFromHDD); | |
System.out.println("HDD에서 가져온 데이터 : " + valueFromHDD); | |
return valueFromHDD; | |
} | |
} | |
private void save(int newValue) { | |
if (memory.size() >= 500) { | |
memory.pop(); // 용량 부족시 선입 선출로 제거 | |
} | |
memory.push(newValue); | |
} | |
} | |
static class HardDisk { | |
private Integer[] disk = new Integer[5000]; | |
private HardDisk() { | |
for (int i = 0; i < disk.length; i++) { | |
disk[i] = i + 1; // 유일한 값 저장 (1~5000) | |
} | |
} | |
Integer search(int value) { | |
increaseAccessTime(3); | |
for (Integer num : disk) { | |
if (value == num) { | |
return num; | |
} | |
} | |
return -1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment