|
1 | 1 | #include "CacheManager.h" |
2 | 2 | #include <iostream> |
3 | 3 | #include <thread> |
| 4 | +#include <iomanip> // For std::fixed and std::setprecision |
4 | 5 |
|
5 | | -CacheManager::CacheManager(int capacity) |
6 | | - : lfu_cache(capacity), lru_cache(capacity), current_policy(LRU) {} |
| 6 | +CacheManager::CacheManager(size_t capacity) |
| 7 | + : lfu_cache(capacity), lru_cache(capacity), current_policy(LRU_P) {} |
7 | 8 |
|
8 | | -void CacheManager::setPolicy(int policy) { |
9 | | - std::lock_guard<std::mutex> lock(policy_mutex); |
10 | | - current_policy = policy; |
11 | | -} |
| 9 | +// NEW: Send GET traffic to both caches to compare performance |
| 10 | +int CacheManager::get(int key) { |
| 11 | + // Both caches process the get request to update their internal state and stats |
| 12 | + int lru_val = lru_cache.get(key); |
| 13 | + int lfu_val = lfu_cache.get(key); |
12 | 14 |
|
13 | | -void CacheManager::accessCache(int key, int value) { |
14 | | - std::lock_guard<std::mutex> lock(access_mutex); |
15 | | - if (current_policy == LFU) { |
16 | | - lfu_cache.put(key, value); |
17 | | - } else if (current_policy == LRU) { |
18 | | - lru_cache.put(key, value); |
| 15 | + // Return the value from the currently active policy |
| 16 | + if (current_policy.load() == LRU_P) { |
| 17 | + return lru_val; |
| 18 | + } else { |
| 19 | + return lfu_val; |
19 | 20 | } |
20 | 21 | } |
21 | 22 |
|
22 | | -// Modified to use the stop flag |
| 23 | +// NEW: Send PUT traffic to both caches |
| 24 | +void CacheManager::put(int key, int value) { |
| 25 | + lru_cache.put(key, value); |
| 26 | + lfu_cache.put(key, value); |
| 27 | +} |
| 28 | + |
| 29 | +std::string CacheManager::getCurrentPolicyName() const { |
| 30 | + return (current_policy.load() == LRU_P) ? "LRU" : "LFU"; |
| 31 | +} |
| 32 | + |
| 33 | +// REWRITTEN: The adaptive switching logic |
23 | 34 | void CacheManager::switchPolicy(std::atomic<bool>& stop_flag) { |
24 | | - // Loop only until the flag is set to true |
25 | 35 | while (!stop_flag.load()) { |
26 | | - std::this_thread::sleep_for(std::chrono::seconds(2)); // Shortened for test |
27 | | - |
28 | | - // Check the flag again in case it was set while sleeping |
| 36 | + std::this_thread::sleep_for(std::chrono::seconds(5)); |
29 | 37 | if (stop_flag.load()) break; |
30 | 38 |
|
31 | | - setPolicy(current_policy == LFU ? LRU : LFU); |
32 | | - std::cout << "Cache policy switched!" << std::endl; |
| 39 | + std::lock_guard<std::mutex> lock(policy_mutex); |
| 40 | + |
| 41 | + double lru_hit_rate = lru_cache.getHitRate(); |
| 42 | + double lfu_hit_rate = lfu_cache.getHitRate(); |
| 43 | + |
| 44 | + std::cout << "\n--- Policy Check ---" << std::endl; |
| 45 | + std::cout << std::fixed << std::setprecision(2); |
| 46 | + std::cout << "Current Policy: " << getCurrentPolicyName() << std::endl; |
| 47 | + std::cout << "LRU Hit Rate: " << lru_hit_rate * 100 << "%" << std::endl; |
| 48 | + std::cout << "LFU Hit Rate: " << lfu_hit_rate * 100 << "%" << std::endl; |
| 49 | + |
| 50 | + // Adaptive switching condition |
| 51 | + // Switch if the other policy is performing better by a meaningful margin (e.g., 5%) |
| 52 | + if (current_policy == LRU_P && lfu_hit_rate > lru_hit_rate + 0.05) { |
| 53 | + current_policy = LFU_P; |
| 54 | + std::cout << "Switching to LFU policy due to better performance." << std::endl; |
| 55 | + // Reset stats after switching to start a new measurement period |
| 56 | + lru_cache.resetStats(); |
| 57 | + lfu_cache.resetStats(); |
| 58 | + } else if (current_policy == LFU_P && lru_hit_rate > lfu_hit_rate + 0.05) { |
| 59 | + current_policy = LRU_P; |
| 60 | + std::cout << "Switching to LRU policy due to better performance." << std::endl; |
| 61 | + lru_cache.resetStats(); |
| 62 | + lfu_cache.resetStats(); |
| 63 | + } else { |
| 64 | + std::cout << "No policy change needed." << std::endl; |
| 65 | + } |
| 66 | + std::cout << "--------------------\n" << std::endl; |
33 | 67 | } |
34 | 68 | } |
0 commit comments