Skip to content

Commit 1c6d2df

Browse files
committed
Fix memory leaks and add warm-up in fixture.c
This commit resolves memory leaks in the original implementation and improves stability with a warm-up phase. Previously, the initialization function was called multiple times within the testing function, reallocating memory for the context array without releasing earlier allocations, resulting in leaks detected by Valgrind (43,632 bytes across 909 blocks per test). Now, the initialization happens only once, and memory is released when testing completes, preventing any leaks. Additionally, a warm-up step is added in the measurement function by discarding the first batch of data. This filters out initial anomalies (such as cache misses or memory allocation delays), aligning with the design principles of the dudect framework and enhancing measurement consistency. Previous Chinese comments are also translated to English for better readability. These changes ensure reliability, eliminate memory issues, and adhere to the lightweight and stable testing approach outlined in the dudect paper. Change-Id: I34b257c2ae545c7c28c0c0be60e43988efc8158d
1 parent f53314e commit 1c6d2df

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

dudect/fixture.c

+26-14
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)