Skip to content

Commit 8677947

Browse files
committed
Fix memory leaks and add warm-up step in fixture.c
Memory leaks were addressed with the following changes: - Valgrind inspection detected memory leaks, with 43,632 bytes lost across 909 blocks per test. - Investigation revealed that the initialization function was called multiple times in the testing loop, reallocating the context array without freeing prior allocations. - The initialization is now executed only once, and memory is released when testing ends, eliminating the leaks. The measurement function was modified to include a warm-up step that discards the first batch of data. Key observations and decisions are: - Tests were conducted using queue insertion and removal functions to verify constant-time behavior, showing no notable difference with or without the warm-up step. - The dudect paper notes potential timing variations that may impact t-test results, such as cache misses or allocation delays. - This step is retained to improve t-test precision in future tests, ensuring alignment with the paper's recommended methodology. Change-Id: I34b257c2ae545c7c28c0c0be60e43988efc8158d
1 parent f53314e commit 8677947

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

dudect/fixture.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ static bool report(void)
177177
return true;
178178
}
179179

180+
static void init_once(void)
181+
{
182+
init_dut();
183+
for (size_t i = 0; i < DUDECT_TESTS; i++) {
184+
if (!ctxs[i]) { // Avoid repeated allocation
185+
ctxs[i] = malloc(sizeof(t_context_t));
186+
t_init(ctxs[i]);
187+
}
188+
}
189+
}
190+
180191
static bool doit(int mode)
181192
{
182193
int64_t *before_ticks = calloc(N_MEASURES + 1, sizeof(int64_t));
@@ -187,17 +198,25 @@ static bool doit(int mode)
187198
int64_t *percentiles = calloc(NUM_PERCENTILES, sizeof(int64_t));
188199

189200
if (!before_ticks || !after_ticks || !exec_times || !classes ||
190-
!input_data) {
201+
!input_data || !percentiles) {
191202
die();
192203
}
193204

194205
prepare_inputs(input_data, classes);
195206

196207
bool ret = measure(before_ticks, after_ticks, input_data, mode);
197208
differentiate(exec_times, before_ticks, after_ticks);
198-
prepare_percentiles(exec_times, percentiles);
199-
update_statistics(exec_times, classes, percentiles);
200-
ret &= report();
209+
210+
static bool first_time = true;
211+
if (first_time) {
212+
prepare_percentiles(exec_times, percentiles);
213+
first_time = false;
214+
ret = true; // Discard the first batch of data
215+
} else {
216+
prepare_percentiles(exec_times, percentiles);
217+
update_statistics(exec_times, classes, percentiles);
218+
ret &= report();
219+
}
201220

202221
free(before_ticks);
203222
free(after_ticks);
@@ -209,22 +228,14 @@ static bool doit(int mode)
209228
return ret;
210229
}
211230

212-
static void init_once(void)
213-
{
214-
init_dut();
215-
for (size_t i = 0; i < DUDECT_TESTS; i++) {
216-
ctxs[i] = malloc(sizeof(t_context_t));
217-
t_init(ctxs[i]);
218-
}
219-
}
220-
221231
static bool test_const(char *text, int mode)
222232
{
223233
bool result = false;
224234

235+
init_once(); // Initialize only once
236+
225237
for (int cnt = 0; cnt < TEST_TRIES; ++cnt) {
226238
printf("Testing %s...(%d/%d)\n\n", text, cnt, TEST_TRIES);
227-
init_once();
228239
for (int i = 0; i < ENOUGH_MEASURE / (N_MEASURES - DROP_SIZE * 2) + 1;
229240
++i)
230241
result = doit(mode);
@@ -235,6 +246,7 @@ static bool test_const(char *text, int mode)
235246

236247
for (size_t i = 0; i < DUDECT_TESTS; i++) {
237248
free(ctxs[i]);
249+
ctxs[i] = NULL;
238250
}
239251

240252
return result;

0 commit comments

Comments
 (0)