Skip to content

Commit

Permalink
Merge pull request #309 from shintaro-iwasaki/pr/leakcheck
Browse files Browse the repository at this point in the history
fix memory leak
  • Loading branch information
shintaro-iwasaki authored Mar 12, 2021
2 parents b0afc3b + ec7b578 commit edf86c8
Show file tree
Hide file tree
Showing 40 changed files with 4,416 additions and 288 deletions.
62 changes: 62 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,67 @@ AC_CHECK_LIB(pthread, pthread_join)
# check pthread_barrier
AC_CHECK_FUNCS(pthread_barrier_init)

# check dlvsym
ABT_RT_CFLAGS=""
ABT_RT_LDFLAGS=""
AC_CHECK_LIB(dl, dlvsym)
if test x"$ac_cv_lib_dl_dlvsym" = x"yes"; then
# dl can be used for testing.
# check objdump since it is needed
if test x"$(which objdump || true)" != x; then
# objdump exists. Dump GLIBC versions.
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <pthread.h>
#include <stdint.h>
#include <unistd.h>
],[[
typedef void *(*fty)(void *);
volatile fty fs[] = {
(fty)malloc, (fty)calloc, (fty)realloc, (fty)posix_memalign,
(fty)free, (fty)mmap, (fty)munmap, (fty)pthread_create,
(fty)pthread_join, (fty)pthread_mutex_init,
(fty)pthread_mutex_destroy, (fty)pthread_cond_init,
(fty)pthread_cond_destroy,
#if defined(_POSIX_BARRIERS) && _POSIX_BARRIERS > 0
(fty)pthread_barrier_init, (fty)pthread_barrier_destroy
#endif
};
int i;
for (i = 0; i < (int)(sizeof(fs)/ sizeof(fs[0])); i++) {
pthread_t pth;
volatile int r1 = pthread_create(&pth, 0, fs[i], 0);
volatile int r2 = pthread_join(pth, 0);
(void)r1; (void)r2;
}
]])],[
dump="$(objdump -t conftest$EXEEXT 2>/dev/null || true)"
ABT_RT_CFLAGS="-DABT_RT_USE_DLVSYM=1"
for target_func in malloc calloc realloc posix_memalign free \
mmap munmap pthread_create pthread_join \
pthread_mutex_init pthread_mutex_destroy \
pthread_cond_init pthread_cond_destroy \
pthread_barrier_init pthread_barrier_destroy;
do
ABT_RT_VER="$(echo ${dump} | grep -E -o "${target_func}@@.*" \
| sed -e s/${target_func}@@//g \
| cut -d' ' -f1 \
| sed -e 's/[^a-Z0-9._-]//g' \
|| true)"
ABT_RT_MACRO_NAME="ABT_RT_$(echo ${target_func} | tr a-z A-Z)_VER"
ABT_RT_CFLAGS="${ABT_RT_CFLAGS} -D${ABT_RT_MACRO_NAME}=${ABT_RT_VER}"
done
],[])
fi
ABT_RT_LDFLAGS="-ldl"
fi
AC_SUBST(ABT_RT_LDFLAGS)
AC_SUBST(ABT_RT_CFLAGS)

# check -lrt library
AC_CHECK_LIB(rt, timer_create)
if test "$ac_cv_lib_rt" = "yes" ; then
Expand Down Expand Up @@ -1016,6 +1077,7 @@ AC_CONFIG_FILES([Makefile
test/Makefile
test/basic/Makefile
test/benchmark/Makefile
test/leakcheck/Makefile
test/util/Makefile
examples/Makefile
examples/fibonacci/Makefile
Expand Down
88 changes: 49 additions & 39 deletions src/arch/abtd_affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,47 +299,38 @@ void ABTD_affinity_init(ABTI_global *p_global, const char *affinity_str)
g_affinity.cpusets = NULL;
g_affinity.initial_cpuset.cpuids = NULL;
pthread_t self_native_thread = pthread_self();
ABTD_affinity_list *p_list = NULL;

uint32_t i;
int ret;
ret = get_num_cores(self_native_thread, &p_global->num_cores);
if (ret != ABT_SUCCESS || p_global->num_cores == 0) {
p_global->set_affinity = ABT_FALSE;
return;
}
if (ret != ABT_SUCCESS || p_global->num_cores == 0)
goto FAILED;
ret = create_cpuset(self_native_thread, &g_affinity.initial_cpuset);
if (ret != ABT_SUCCESS) {
p_global->set_affinity = ABT_FALSE;
return;
} else if (g_affinity.initial_cpuset.num_cpuids == 0) {
ABTD_affinity_cpuset_destroy(&g_affinity.initial_cpuset);
p_global->set_affinity = ABT_FALSE;
return;
}
if (ret != ABT_SUCCESS || g_affinity.initial_cpuset.num_cpuids == 0)
goto FAILED;
p_global->set_affinity = ABT_TRUE;
ABTD_affinity_list *p_list = ABTD_affinity_list_create(affinity_str);
if (p_list) {
ret = ABTD_affinity_list_create(affinity_str, &p_list);
if (ret == ABT_SUCCESS) {
if (p_list->num == 0) {
ABTD_affinity_list_free(p_list);
p_list = NULL;
}
}
if (p_list) {
/* Create cpusets based on the affinity list.*/
g_affinity.num_cpusets = p_list->num;
ret = ABTU_calloc(g_affinity.num_cpusets, sizeof(ABTD_affinity_cpuset),
ret = ABTU_calloc(p_list->num, sizeof(ABTD_affinity_cpuset),
(void **)&g_affinity.cpusets);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ret != ABT_SUCCESS)
goto FAILED;
g_affinity.num_cpusets = p_list->num;
for (i = 0; i < p_list->num; i++) {
const ABTD_affinity_id_list *p_id_list = p_list->p_id_lists[i];
uint32_t j, num_cpuids = 0, len_cpuids = 8;
ret = ABTU_malloc(sizeof(int) * len_cpuids,
(void **)&g_affinity.cpusets[i].cpuids);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ABTI_IS_ERROR_CHECK_ENABLED && ret != ABT_SUCCESS) {
ABTD_affinity_list_free(p_list);
p_global->set_affinity = ABT_FALSE;
return;
}
if (ret != ABT_SUCCESS)
goto FAILED;
for (j = 0; j < p_id_list->num; j++) {
int cpuid_i = int_rem(p_id_list->ids[j],
g_affinity.initial_cpuset.num_cpuids);
Expand All @@ -359,37 +350,55 @@ void ABTD_affinity_init(ABTI_global *p_global, const char *affinity_str)
sizeof(int) * len_cpuids * 2,
(void **)&g_affinity.cpusets[i]
.cpuids);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ret != ABT_SUCCESS)
goto FAILED;
len_cpuids *= 2;
}
g_affinity.cpusets[i].cpuids[num_cpuids] = cpuid;
num_cpuids++;
}
}
/* Adjust the size of cpuids. */
if (num_cpuids != len_cpuids)
if (num_cpuids != len_cpuids) {
ret = ABTU_realloc(sizeof(int) * len_cpuids,
sizeof(int) * num_cpuids,
(void **)&g_affinity.cpusets[i].cpuids);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ret != ABT_SUCCESS)
goto FAILED;
}
g_affinity.cpusets[i].num_cpuids = num_cpuids;
}
ABTD_affinity_list_free(p_list);
} else {
/* Create default cpusets. */
g_affinity.num_cpusets = g_affinity.initial_cpuset.num_cpuids;
ret = ABTU_calloc(g_affinity.num_cpusets, sizeof(ABTD_affinity_cpuset),
ret = ABTU_calloc(g_affinity.initial_cpuset.num_cpuids,
sizeof(ABTD_affinity_cpuset),
(void **)&g_affinity.cpusets);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ret != ABT_SUCCESS)
goto FAILED;
g_affinity.num_cpusets = g_affinity.initial_cpuset.num_cpuids;
for (i = 0; i < g_affinity.num_cpusets; i++) {
g_affinity.cpusets[i].num_cpuids = 1;
ret = ABTU_malloc(sizeof(int) * g_affinity.cpusets[i].num_cpuids,
(void **)&g_affinity.cpusets[i].cpuids);
ABTI_ASSERT(ret == ABT_SUCCESS);
if (ret != ABT_SUCCESS)
goto FAILED;
g_affinity.cpusets[i].cpuids[0] =
g_affinity.initial_cpuset.cpuids[i];
}
}
return;
FAILED:
if (p_list)
ABTD_affinity_list_free(p_list);
ABTD_affinity_cpuset_destroy(&g_affinity.initial_cpuset);
for (i = 0; i < g_affinity.num_cpusets; i++)
ABTD_affinity_cpuset_destroy(&g_affinity.cpusets[i]);
g_affinity.num_cpusets = 0;
ABTU_free(g_affinity.cpusets);
g_affinity.cpusets = NULL;
p_global->set_affinity = ABT_FALSE;
return;
}

void ABTD_affinity_finalize(ABTI_global *p_global)
Expand All @@ -403,16 +412,17 @@ void ABTD_affinity_finalize(ABTI_global *p_global)
* possibly the CPU affinity policy has been changed while running
* a user program. Let's ignore this error. */
(void)abt_errno;

/* Free g_affinity. */
ABTD_affinity_cpuset_destroy(&g_affinity.initial_cpuset);
uint32_t i;
for (i = 0; i < g_affinity.num_cpusets; i++) {
ABTD_affinity_cpuset_destroy(&g_affinity.cpusets[i]);
}
ABTU_free(g_affinity.cpusets);
g_affinity.cpusets = NULL;
g_affinity.num_cpusets = 0;
}
/* Free g_afinity. */
ABTD_affinity_cpuset_destroy(&g_affinity.initial_cpuset);
uint32_t i;
for (i = 0; i < g_affinity.num_cpusets; i++) {
ABTD_affinity_cpuset_destroy(&g_affinity.cpusets[i]);
}
ABTU_free(g_affinity.cpusets);
g_affinity.cpusets = NULL;
g_affinity.num_cpusets = 0;
}

ABTU_ret_err int ABTD_affinity_cpuset_read(ABTD_xstream_context *p_ctx,
Expand Down
Loading

0 comments on commit edf86c8

Please sign in to comment.