Skip to content

Commit 904cd8a

Browse files
committed
Fix cast early and check cache size
1 parent f249ad3 commit 904cd8a

8 files changed

Lines changed: 42 additions & 8 deletions

File tree

libCacheSim/cache/eviction/ClockPro.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ static void ClockPro_parse_params(cache_t *cache,
510510
params->init_ref = strtol(value, &end, 10);
511511
} else if (strcasecmp(key, "init-ratio-cold") == 0) {
512512
const double ratio = strtod(value, &end);
513-
params->mem_cold_max = (int64_t)((double)cache->cache_size * ratio);
513+
params->mem_cold_max = (int64_t)(cache->cache_size * ratio);
514514
} else if (strcasecmp(key, "print") == 0) {
515515
printf("current parameters: %s\n",
516516
ClockPro_current_params(cache, params));

libCacheSim/cache/eviction/QDLP.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ cache_t *QDLP_init(const common_cache_params_t ccache_params,
100100
}
101101

102102
int64_t fifo_cache_size =
103-
(int64_t)ccache_params.cache_size * params->small_size_ratio;
103+
(int64_t)(ccache_params.cache_size * params->small_size_ratio);
104104
int64_t main_cache_size = ccache_params.cache_size - fifo_cache_size;
105105
int64_t ghost_cache_size =
106106
(int64_t)(ccache_params.cache_size * params->ghost_size_ratio);
107107

108+
if (fifo_cache_size <= 0 || main_cache_size <= 0) {
109+
ERROR("Invalid cache size configuration: fifo=%ld bytes, main=%ld bytes\n",
110+
fifo_cache_size, main_cache_size);
111+
}
112+
108113
common_cache_params_t ccache_params_local = ccache_params;
109114
ccache_params_local.cache_size = fifo_cache_size;
110115
params->small_cache = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFO.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,16 @@ cache_t *S3FIFO_init(const common_cache_params_t ccache_params,
112112
}
113113

114114
int64_t small_fifo_size =
115-
(int64_t)ccache_params.cache_size * params->small_size_ratio;
115+
(int64_t)(ccache_params.cache_size * params->small_size_ratio);
116116
int64_t main_fifo_size = ccache_params.cache_size - small_fifo_size;
117117
int64_t ghost_fifo_size =
118118
(int64_t)(ccache_params.cache_size * params->ghost_size_ratio);
119119

120+
if (small_fifo_size <= 0 || main_fifo_size <= 0) {
121+
ERROR("Invalid cache size configuration: small_fifo=%ld bytes, main_fifo=%ld bytes\n",
122+
small_fifo_size, main_fifo_size);
123+
}
124+
120125
common_cache_params_t ccache_params_local = ccache_params;
121126
ccache_params_local.cache_size = small_fifo_size;
122127
params->small_fifo = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFOd.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ cache_t *S3FIFOd_init(const common_cache_params_t ccache_params,
101101
}
102102

103103
int64_t fifo_cache_size =
104-
(int64_t)ccache_params.cache_size * params->small_fifo_size_ratio;
104+
(int64_t)(ccache_params.cache_size * params->small_fifo_size_ratio);
105105
int64_t main_fifo_size = ccache_params.cache_size - fifo_cache_size;
106106
int64_t ghost_fifo = main_fifo_size;
107107

108+
if (fifo_cache_size <= 0 || main_fifo_size <= 0) {
109+
ERROR("Invalid cache size configuration: fifo=%ld bytes, main_fifo=%ld bytes\n",
110+
fifo_cache_size, main_fifo_size);
111+
}
112+
108113
common_cache_params_t ccache_params_local = ccache_params;
109114
ccache_params_local.cache_size = fifo_cache_size;
110115
params->small_fifo = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFOv0.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,16 @@ cache_t *S3FIFOv0_init(const common_cache_params_t ccache_params,
110110
}
111111

112112
int64_t fifo_cache_size =
113-
(int64_t)ccache_params.cache_size * params->small_size_ratio;
113+
(int64_t)(ccache_params.cache_size * params->small_size_ratio);
114114
int64_t main_fifo_size = ccache_params.cache_size - fifo_cache_size;
115115
int64_t ghostfifo__cachee_siz =
116116
(int64_t)(ccache_params.cache_size * params->ghost_size_ratio);
117117

118+
if (fifo_cache_size <= 0 || main_fifo_size <= 0) {
119+
ERROR("Invalid cache size configuration: fifo=%ld bytes, main_fifo=%ld bytes\n",
120+
fifo_cache_size, main_fifo_size);
121+
}
122+
118123
common_cache_params_t ccache_params_local = ccache_params;
119124
ccache_params_local.cache_size = fifo_cache_size;
120125
params->small_fifo = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/SLRU.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ cache_t *SLRU_init(const common_cache_params_t ccache_params,
135135
params->lru_max_n_bytes = calloc(params->n_seg, sizeof(int64_t));
136136
for (int i = 0; i < params->n_seg; i++) {
137137
params->lru_max_n_bytes[i] =
138-
(int64_t)ccache_params.cache_size / params->n_seg;
138+
(int64_t)(ccache_params.cache_size / params->n_seg);
139139
}
140140
}
141141

@@ -455,6 +455,10 @@ static void SLRU_parse_params(cache_t *cache,
455455
params->lru_max_n_bytes[i] =
456456
(int64_t)((double)seg_size_array[i] / seg_size_sum *
457457
cache->cache_size);
458+
if (params->lru_max_n_bytes[i] <= 0) {
459+
ERROR("Invalid segment size for segment %d: %ld bytes\n", i,
460+
params->lru_max_n_bytes[i]);
461+
}
458462
}
459463
} else if (strcasecmp(key, "print") == 0) {
460464
printf("current parameters: %s\n", SLRU_current_params(cache, params));

libCacheSim/cache/eviction/other/S3LRU.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,16 @@ cache_t *S3LRU_init(const common_cache_params_t ccache_params,
106106
}
107107

108108
int64_t LRU_cache_size =
109-
(int64_t)ccache_params.cache_size * params->LRU_size_ratio;
109+
(int64_t)(ccache_params.cache_size * params->LRU_size_ratio);
110110
int64_t main_cache_size = ccache_params.cache_size - LRU_cache_size;
111111
int64_t LRU_ghost_cache_size =
112112
(int64_t)(ccache_params.cache_size * params->ghost_size_ratio);
113113

114+
if (LRU_cache_size <= 0 || main_cache_size <= 0) {
115+
ERROR("Invalid cache size configuration: LRU=%ld bytes, main=%ld bytes\n",
116+
LRU_cache_size, main_cache_size);
117+
}
118+
114119
common_cache_params_t ccache_params_local = ccache_params;
115120
ccache_params_local.cache_size = LRU_cache_size;
116121
// params->LRU = LRU_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/other/flashProb.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ cache_t *flashProb_init(const common_cache_params_t ccache_params,
9191
}
9292

9393
int64_t ram_cache_size =
94-
(int64_t)ccache_params.cache_size * params->ram_size_ratio;
94+
(int64_t)(ccache_params.cache_size * params->ram_size_ratio);
9595
int64_t disk_cache_size = ccache_params.cache_size - ram_cache_size;
9696

97+
if (ram_cache_size <= 0 || disk_cache_size <= 0) {
98+
ERROR("Invalid cache size configuration: ram=%ld bytes, disk=%ld bytes\n",
99+
ram_cache_size, disk_cache_size);
100+
}
101+
97102
common_cache_params_t ccache_params_local = ccache_params;
98103
ccache_params_local.cache_size = ram_cache_size;
99104
if (strcasecmp(params->ram_cache_type, "ARC") == 0) {

0 commit comments

Comments
 (0)