Skip to content

Commit f1a56cd

Browse files
authored
fix early cast when calculating cache size (#301)
* Fix cast early and check cache size * clang format
1 parent f249ad3 commit f1a56cd

8 files changed

Lines changed: 50 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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ 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(
110+
"Invalid cache size configuration: fifo=%lld bytes, main=%lld bytes\n",
111+
(long long)fifo_cache_size, (long long)main_cache_size);
112+
}
113+
108114
common_cache_params_t ccache_params_local = ccache_params;
109115
ccache_params_local.cache_size = fifo_cache_size;
110116
params->small_cache = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFO.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,19 @@ 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(
122+
"Invalid cache size configuration: small_fifo=%lld bytes, "
123+
"main_fifo=%lld "
124+
"bytes\n",
125+
(long long)small_fifo_size, (long long)main_fifo_size);
126+
}
127+
120128
common_cache_params_t ccache_params_local = ccache_params;
121129
ccache_params_local.cache_size = small_fifo_size;
122130
params->small_fifo = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFOd.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ 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(
110+
"Invalid cache size configuration: fifo=%lld bytes, main_fifo=%lld "
111+
"bytes\n",
112+
(long long)fifo_cache_size, (long long)main_fifo_size);
113+
}
114+
108115
common_cache_params_t ccache_params_local = ccache_params;
109116
ccache_params_local.cache_size = fifo_cache_size;
110117
params->small_fifo = FIFO_init(ccache_params_local, NULL);

libCacheSim/cache/eviction/S3FIFOv0.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,18 @@ 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(
120+
"Invalid cache size configuration: fifo=%lld bytes, main_fifo=%lld "
121+
"bytes\n",
122+
(long long)fifo_cache_size, (long long)main_fifo_size);
123+
}
124+
118125
common_cache_params_t ccache_params_local = ccache_params;
119126
ccache_params_local.cache_size = fifo_cache_size;
120127
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: %lld bytes\n", i,
460+
(long long)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=%lld bytes, main=%lld bytes\n",
116+
(long long)LRU_cache_size, (long long)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=%lld bytes, disk=%lld bytes\n",
99+
(long long)ram_cache_size, (long long)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)