1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| #include <iostream> #include <vector> #include <map> #include <random> #include <ctime> #include <iomanip> #include <queue> #include <stack> using std::cout; using std::endl; #define show_progress const int max_ins_num = 320; const int ins_per_mb = 10; const int mem_max_mb = 4;
int cur_clock = 0; const int total_mb = max_ins_num/ins_per_mb;
int get_inst_i(int min, int max) { static std::default_random_engine engine(time(nullptr)); return engine() %(max - min + 1) + min; } #define chart_head std::left << std::setw(len+1) << std::setfill('-') << "" #define chart_cell "|" << std::left << std::setw(len) << std::setfill(' ') void show_chart(std::queue<int> fifo, std::stack<int> lru, const std::vector<int>& opt, int ins, int block) { std::cout << "cur_ins = " << ins << "\ncur_block = " << block << std::endl; int len = 5; std::cout << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl << chart_cell << "fifo" << chart_cell<< "lru" << chart_cell << "opt" << "|" << std::endl << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl; for (int i = 0; i < mem_max_mb; ++i) { int lru_cout = -1; int fifo_cout = -1; if (!fifo.empty()) { fifo_cout = fifo.front(); fifo.pop(); } if (!lru.empty()) { lru_cout = lru.top(); lru.pop(); } std::cout << chart_cell << fifo_cout << chart_cell << lru_cout << chart_cell << opt[i] << "|" << std::endl; } std::cout << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl; } int fifo_exchange = 0; int lru_exchange = 0; int opt_exchange = 0;
void exe_inst_i(int ins, const std::vector<int>& ins_arr) {
static std::queue<int> fifo_mem_blocks; static std::vector<bool> fifo_map(total_mb, false);
static std::stack<int> lru_mem_blocks; static std::vector<bool> lru_map(total_mb, false);
static std::vector<int> opt_mem_blocks(mem_max_mb, -1); static std::vector<int> opt_map(total_mb, max_ins_num);
int cur_ins = ins; int cur_block = ins/ins_per_mb; if(!fifo_map[cur_block]) { if (fifo_mem_blocks.size() >= mem_max_mb) { fifo_map[fifo_mem_blocks.front()] = false; fifo_mem_blocks.pop(); fifo_exchange++; } fifo_mem_blocks.push(cur_block); fifo_map[cur_block] = true; } else { } std::stack<int> lru_temp; if (!lru_map[cur_block]) { while (!lru_mem_blocks.empty()) { lru_temp.push(lru_mem_blocks.top()); lru_mem_blocks.pop(); } if (lru_temp.size() >= mem_max_mb) { lru_map[lru_temp.top()] = false; lru_temp.pop(); lru_exchange++; } lru_map[cur_block] = true; while (!lru_temp.empty()) { lru_mem_blocks.push(lru_temp.top()); lru_temp.pop(); } lru_mem_blocks.push(cur_block); } else { while (lru_mem_blocks.top() != cur_block) { lru_temp.push(lru_mem_blocks.top()); lru_mem_blocks.pop(); } lru_mem_blocks.pop(); while (!lru_temp.empty()) { lru_mem_blocks.push(lru_temp.top()); lru_temp.pop(); } lru_mem_blocks.push(cur_block); } for (int i = cur_clock; i < max_ins_num; i++) { int block_i = ins_arr[i]/ins_per_mb; opt_map[block_i] = opt_map[block_i] < i ? opt_map[block_i] : i; } static int opt_num = 0; int opt_exchange_i = 0; for (int i = 0; i < mem_max_mb; ++i) { if (opt_mem_blocks[i] == cur_block || opt_mem_blocks[i] == -1) { opt_exchange_i = i; break; } if (opt_map[opt_mem_blocks[i]] > opt_map[opt_mem_blocks[opt_exchange_i]]) { opt_exchange_i = i; } } if (opt_mem_blocks[opt_exchange_i] == -1) { opt_mem_blocks[opt_num] = cur_block; opt_num++; } else if (opt_mem_blocks[opt_exchange_i] != cur_block) { opt_mem_blocks[opt_exchange_i] = cur_block; opt_exchange++; } for (int i = 0; i < total_mb; i++) { opt_map[i] = max_ins_num; } #ifdef show_progress show_chart(fifo_mem_blocks, lru_mem_blocks, opt_mem_blocks, cur_ins, cur_block); #endif }
void show_info(int fifo, int lru, int opt) { int len = int(std::string("miss_ratio").length()+1); std::cout << chart_head << "" << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl << chart_cell << "item" << chart_cell << "fifo" << chart_cell<< "lru" << chart_cell << "opt" << "|" << std::endl << chart_head << "" << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl << chart_cell << "hit" << chart_cell << max_ins_num-fifo << chart_cell << max_ins_num-lru << chart_cell << max_ins_num-opt << "|" << std::endl << chart_cell << "hit_ratio" << chart_cell << (1-fifo/(double)max_ins_num)*100 << chart_cell << (1-lru/(double)max_ins_num)*100 << chart_cell << (1-opt/(double)max_ins_num)*100 << "|" << std::endl << chart_cell << "miss" << chart_cell << fifo << chart_cell << lru << chart_cell << opt << "|" << std::endl << chart_cell << "miss_ratio" << chart_cell << fifo/(double)max_ins_num*100 << chart_cell << lru/(double)max_ins_num*100 << chart_cell << opt/(double)max_ins_num*100 << "|" << std::endl << chart_head << "" << chart_head << "" << chart_head << "" << chart_head << "-" << std::endl; }
int main() {
std::vector<int> ins_arr(max_ins_num,0); int ins; for(int i = 0; i < max_ins_num; i++) { if (i%6 == 0) { ins = get_inst_i(0,max_ins_num-1); ins_arr[i] = ins; } else if (i%6 == 1 || i%6 == 3 || i%6 == 5) { ins_arr[i] = ins_arr[i-1] + 1 < max_ins_num ? ins_arr[i-1] + 1 : max_ins_num-1; } else if (i%6 == 2){ ins = get_inst_i(0, ins - 1 > 0 ? ins - 1 : 1); ins_arr[i] = ins; } else if (i%6 == 4) { ins = get_inst_i(ins + 2 > max_ins_num-2 ? max_ins_num-2 : ins+2, max_ins_num-1); ins_arr[i] = ins; } } for (cur_clock = 0; cur_clock < max_ins_num; ++cur_clock) { exe_inst_i(ins_arr[cur_clock], ins_arr); } show_info(fifo_exchange, lru_exchange, opt_exchange); return 0; }
|