Skip to content

Commit c469179

Browse files
committed
Fix memory leaks and add warm-up step in fixture.c
The original plan was to explore adding a warm-up step to the measurement function, motivated by a privately implemented earlier version that included this feature and successfully passed tests for queue-related functions, unlike the current stricter version which lacked it and failed those tests. Before implementing this change, Valgrind inspection was performed on the original code without the warm-up step, revealing memory leaks with 43,632 bytes lost across 909 blocks per test. Analysis showed that the initialization function was being called repeatedly within the testing loop, each time reallocating the context array without any provision to free prior memory allocations. This uncontrolled repetition caused memory to accumulate not freed over multiple test iterations, leading to the observed leaks. To address this issue in the original code, the initialization process was revised to execute only once before testing begins, and a cleanup was added to release all allocated memory when testing concludes, fully resolving the memory leak problem. After resolving the memory leaks, the warm-up step was added to discard the first batch of data, and tests were conducted to determine its impact on the constant-time behavior of queue insertion and removal functions. To isolate its effect, the original flawed memory release approach was temporarily restored, revealing persistent memory-related issues that prevented reliable test outcomes. Once the corrected memory release method was applied alongside the warm-up step, all memory-related problems disappeared, and the tests passed successfully. The decision to retain the warm-up code is based primarily on the dudect paper, which points out that factors like cache misses or memory allocation delays could, under different conditions, affect t-test results. The expectation is that in certain future test scenarios, the warm-up step may enhance t-test precision, justifying its inclusion in this submission. Change-Id: I06a74a7ecf1ca08f79ded70d91e4b33ed7cf141b
1 parent f53314e commit c469179

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)