Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #85

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ static constexpr size_t kAltStackSize = 16 * 1024UL; // 16k sigaltstacks
#define SIGQUIESCE (SIGRTMIN + 7)
#define SIGDUMP (SIGRTMIN + 8)

static constexpr size_t kForkCopyFileSize = 32 * 1024 * 1024ul;

// BinnedTracker
static constexpr size_t kBinnedTrackerBinCount = 1;
static constexpr size_t kBinnedTrackerMaxEmpty = 128;
Expand Down Expand Up @@ -235,6 +237,7 @@ inline mt19937_64 *initSeed() {
unsigned long buf;
auto sz = read(fd, (void *)&buf, sizeof(unsigned long));
hard_assert(sz == sizeof(unsigned long));
close(fd);
// std::random_device rd;
// return new (mtBuf) std::mt19937_64(rd());
return new (mtBuf) std::mt19937_64(buf);
Expand Down
10 changes: 10 additions & 0 deletions src/libmesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ int MESH_EXPORT epoll_pwait(int __epfd, struct epoll_event *__events, int __maxe
return mesh::runtime().epollPwait(__epfd, __events, __maxevents, __timeout, __ss);
}

#endif
#if __linux__

ssize_t MESH_EXPORT recv(int sockfd, void *buf, size_t len, int flags) {
return mesh::runtime().recv(sockfd, buf, len, flags);
}

ssize_t MESH_EXPORT recvmsg(int sockfd, struct msghdr *msg, int flags) {
return mesh::runtime().recvmsg(sockfd, msg, flags);
}
#endif
}

Expand Down
14 changes: 7 additions & 7 deletions src/meshable_arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,13 @@ void MeshableArena::afterForkChild() {

const int oldFd = _fd;

const auto bitmap = allocatedBitmap();
for (auto const &i : bitmap) {
int result = internal::copyFile(newFd, oldFd, i * kPageSize, kPageSize);
d_assert(result == CPUInfo::PageSize);
const size_t end = _end * kPageSize;
for (size_t i = 0; i < end; i += kForkCopyFileSize) {
int result = internal::copyFile(newFd, oldFd, i, std::min(end - i, kForkCopyFileSize));
d_assert(result >= 0);
}

while (write(_forkPipe[1], "ok", strlen("ok")) == EAGAIN) {
}

int r = mprotect(_arenaBegin, kArenaSize, PROT_READ | PROT_WRITE);
Expand Down Expand Up @@ -766,10 +769,7 @@ void MeshableArena::afterForkChild() {

close(oldFd);

while (write(_forkPipe[1], "ok", strlen("ok")) == EAGAIN) {
}
close(_forkPipe[1]);

_forkPipe[0] = -1;
_forkPipe[1] = -1;
}
Expand Down
4 changes: 4 additions & 0 deletions src/real.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace real {
#ifdef __linux__
DEFINE_REAL(epoll_pwait);
DEFINE_REAL(epoll_wait);
DEFINE_REAL(recv);
DEFINE_REAL(recvmsg);
#endif

DEFINE_REAL(pthread_create);
Expand All @@ -40,6 +42,8 @@ void init() {
#ifdef __linux__
INIT_REAL(epoll_pwait, RTLD_NEXT);
INIT_REAL(epoll_wait, RTLD_NEXT);
INIT_REAL(recv, RTLD_NEXT);
INIT_REAL(recvmsg, RTLD_NEXT);
#endif

INIT_REAL(pthread_create, RTLD_NEXT);
Expand Down
4 changes: 4 additions & 0 deletions src/real.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#ifdef __linux__
#include <sys/epoll.h>
#include <sys/socket.h>
#include <unistd.h>
#endif

#pragma once
Expand All @@ -23,6 +25,8 @@ void init();
#ifdef __linux__
DECLARE_REAL(epoll_pwait);
DECLARE_REAL(epoll_wait);
DECLARE_REAL(recv);
DECLARE_REAL(recvmsg);
#endif

DECLARE_REAL(pthread_create);
Expand Down
29 changes: 29 additions & 0 deletions src/runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,35 @@ int Runtime::epollPwait(int __epfd, struct epoll_event *__events, int __maxevent

return mesh::real::epoll_pwait(__epfd, __events, __maxevents, __timeout, __ss);
}

ssize_t Runtime::recv(int sockfd, void *buf, size_t len, int flags) {
if (unlikely(mesh::real::recv == nullptr)) {
mesh::real::init();
}
ssize_t ret = mesh::real::recv(sockfd, buf, len, flags);
while (ret < 0 && errno == EFAULT && heap().okToProceed(buf)) {
ret = mesh::real::recv(sockfd, buf, len, flags);
}
return ret;
}

ssize_t Runtime::recvmsg(int sockfd, struct msghdr *msg, int flags) {
if (unlikely(mesh::real::recvmsg == nullptr))
mesh::real::init();

ssize_t ret = mesh::real::recvmsg(sockfd, msg, flags);
while (ret < 0 && errno == EFAULT && msg) {
for (size_t i = 0; i < msg->msg_iovlen; ++i) {
auto ptr = msg->msg_iov[i].iov_base;
if (ptr) {
heap().okToProceed(ptr);
}
}
ret = mesh::real::recvmsg(sockfd, msg, flags);
}
return ret;
}

#endif

static struct sigaction sigbusAction;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Runtime {
#ifdef __linux__
int epollWait(int __epfd, struct epoll_event *__events, int __maxevents, int __timeout);
int epollPwait(int __epfd, struct epoll_event *__events, int __maxevents, int __timeout, const __sigset_t *__ss);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
#endif

int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
Expand Down