Skip to content

Commit

Permalink
Merge pull request #3863 from hoopoepg/topic/added-madvice-into-unmap…
Browse files Browse the repository at this point in the history
…-group-v1.6

UCM: added madvise into VM_UNMAP events group - v1.6
  • Loading branch information
yosefe authored Jul 11, 2019
2 parents 66638b4 + 3ba97ff commit 71a70da
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/ucm/event/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

#define UCM_NATIVE_EVENT_VM_UNMAPPED (UCM_EVENT_MMAP | UCM_EVENT_MUNMAP | \
UCM_EVENT_MREMAP | UCM_EVENT_SHMDT | \
UCM_EVENT_SHMAT | UCM_EVENT_SBRK)
UCM_EVENT_SHMAT | UCM_EVENT_SBRK | \
UCM_EVENT_MADVISE)


typedef struct ucm_event_handler {
Expand Down
4 changes: 2 additions & 2 deletions src/ucm/mmap/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ ucm_fire_mmap_events_internal(int events, ucm_mmap_test_events_data_t *data)
data, (void)sbrk(-sbrk_size));
}

if (events & UCM_EVENT_MADVISE) {
if (events & (UCM_EVENT_MADVISE|UCM_EVENT_VM_UNMAPPED)) {
UCM_FIRE_EVENT(events, UCM_EVENT_MMAP|UCM_EVENT_VM_MAPPED, data,
p = mmap(NULL, ucm_get_page_size(), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANON, -1, 0));
if (p != MAP_FAILED) {
UCM_FIRE_EVENT(events, UCM_EVENT_MADVISE, data,
madvise(p, ucm_get_page_size(), MADV_NORMAL));
madvise(p, ucm_get_page_size(), MADV_DONTNEED));
UCM_FIRE_EVENT(events, UCM_EVENT_MUNMAP|UCM_EVENT_VM_UNMAPPED, data,
munmap(p, ucm_get_page_size()));
} else {
Expand Down
80 changes: 56 additions & 24 deletions test/gtest/ucm/malloc_hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ class malloc_hook : public ucs::test {
UCM_BISTRO_EPILOGUE;
return res;
}

static int madvise(void *addr, size_t length, int advise)
{
UCM_BISTRO_PROLOGUE;
malloc_hook::bistro_call_counter++;
if (C) {
/* notify aggregate vm_munmap event only */
ucm_vm_munmap(addr, length);
}
int res = (intptr_t)syscall(SYS_madvise, addr, length, advise);
UCM_BISTRO_EPILOGUE;
return res;
}
};

class bistro_patch {
public:
bistro_patch(const char* symbol, void *hook)
{
ucs_status_t status;

status = ucm_bistro_patch(symbol, hook, &m_rp);
ASSERT_UCS_OK(status);
EXPECT_NE((intptr_t)m_rp, 0);
}

~bistro_patch()
{
ucm_bistro_restore(m_rp);
}

protected:
ucm_bistro_restore_point_t *m_rp;
};

void mem_event(ucm_event_type_t event_type, ucm_event_t *event)
Expand Down Expand Up @@ -388,6 +421,14 @@ void test_thread::test() {

EXPECT_TRUE(is_ptr_in_range(ptr, shm_seg_size, m_unmap_ranges));

ptr = mmap(NULL, shm_seg_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
ASSERT_NE(MAP_FAILED, ptr) << strerror(errno);
madvise(ptr, shm_seg_size, MADV_DONTNEED);

EXPECT_TRUE(is_ptr_in_range(ptr, shm_seg_size, m_unmap_ranges));
munmap(ptr, shm_seg_size);

/* Print results */
pthread_mutex_lock(&lock);
UCS_TEST_MESSAGE << m_name
Expand Down Expand Up @@ -996,8 +1037,8 @@ UCS_TEST_F(malloc_hook, test_event) {
UCS_TEST_F(malloc_hook, test_event_failed) {
mmap_event<malloc_hook> event(this);
ucs_status_t status;
const char *symbol = "munmap";
ucm_bistro_restore_point_t *rp = NULL;
const char *symbol_munmap = "munmap";
const char *symbol_madvise = "madvise";

if (RUNNING_ON_VALGRIND) {
UCS_TEST_SKIP_R("skipping on valgrind");
Expand All @@ -1010,27 +1051,24 @@ UCS_TEST_F(malloc_hook, test_event_failed) {
status = event.set(UCM_EVENT_MUNMAP | UCM_EVENT_VM_UNMAPPED);
ASSERT_UCS_OK(status);

/* set hook to mmap call */
status = ucm_bistro_patch(symbol, (void*)bistro_hook<0>::munmap, &rp);
ASSERT_UCS_OK(status);
EXPECT_NE((intptr_t)rp, NULL);

status = ucm_test_events(UCM_EVENT_MUNMAP);
EXPECT_TRUE(status == UCS_ERR_UNSUPPORTED);

status = ucm_test_events(UCM_EVENT_VM_UNMAPPED);
EXPECT_TRUE(status == UCS_ERR_UNSUPPORTED);

/* restore original munmap body */
status = ucm_bistro_restore(rp);
ASSERT_UCS_OK(status);
/* set hook to munmap call */
{
bistro_patch patch(symbol_munmap, (void*)bistro_hook<0>::munmap);
EXPECT_TRUE(ucm_test_events(UCM_EVENT_MUNMAP) == UCS_ERR_UNSUPPORTED);
EXPECT_TRUE(ucm_test_events(UCM_EVENT_VM_UNMAPPED) == UCS_ERR_UNSUPPORTED);
}
/* set hook to madvise call */
{
bistro_patch patch(symbol_madvise, (void*)bistro_hook<0>::madvise);
EXPECT_TRUE(ucm_test_events(UCM_EVENT_MADVISE) == UCS_ERR_UNSUPPORTED);
EXPECT_TRUE(ucm_test_events(UCM_EVENT_VM_UNMAPPED) == UCS_ERR_UNSUPPORTED);
}
}

UCS_TEST_F(malloc_hook, test_event_unmap) {
mmap_event<malloc_hook> event(this);
ucs_status_t status;
const char *symbol = "munmap";
ucm_bistro_restore_point_t *rp = NULL;

if (RUNNING_ON_VALGRIND) {
UCS_TEST_SKIP_R("skipping on valgrind");
Expand All @@ -1044,9 +1082,7 @@ UCS_TEST_F(malloc_hook, test_event_unmap) {
ASSERT_UCS_OK(status);

/* set hook to mmap call */
status = ucm_bistro_patch(symbol, (void*)bistro_hook<1>::munmap, &rp);
ASSERT_UCS_OK(status);
EXPECT_NE((intptr_t)rp, NULL);
bistro_patch patch(symbol, (void*)bistro_hook<1>::munmap);

/* munmap should be broken */
status = ucm_test_events(UCM_EVENT_MUNMAP);
Expand All @@ -1059,10 +1095,6 @@ UCS_TEST_F(malloc_hook, test_event_unmap) {
/* mmap should still work */
status = ucm_test_events(UCM_EVENT_MMAP);
EXPECT_TRUE(status == UCS_OK);

/* restore original munmap body */
status = ucm_bistro_restore(rp);
ASSERT_UCS_OK(status);
}

/* test for mmap events are fired from non-direct load modules
Expand Down

0 comments on commit 71a70da

Please sign in to comment.