Optimize ngx_ff_module

This commit is contained in:
logwang 2017-08-09 14:50:21 +08:00
parent 615f2d3c02
commit 5825eee0c3
3 changed files with 47 additions and 94 deletions

View File

@ -156,6 +156,7 @@ int ff_hook_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
return mt_real_func(bind)(fd, addr, addrlen);
}
}
int ff_hook_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
{
if (ff_fdisused(fd)) {

View File

@ -94,10 +94,6 @@
#define unlikely(x) __builtin_expect((x),0)
#endif
#define INIT_FUNCTION(func) \
real_##func = dlsym(RTLD_NEXT, #func); \
assert(real_##func)
static int (*real_close)(int);
static int (*real_socket)(int, int, int);
static int (*real_bind)(int, const struct sockaddr*, socklen_t);
@ -117,32 +113,23 @@ static ssize_t (*real_readv)(int, const struct iovec *, int);
static int (*real_ioctl)(int, int, void *);
static int (*real_select) (int, fd_set *, fd_set *, fd_set *, struct timeval *);
static int (*real_gettimeofday)(struct timeval *tv, struct timezone *tz);
static int inited;
#define SYSCALL(func) \
({ \
if (!real_##func) { \
real_##func = dlsym(RTLD_NEXT, #func); \
} \
real_##func; \
})
void
ff_mod_init(int argc, char * const *argv) {
int rc;
INIT_FUNCTION(socket);
INIT_FUNCTION(bind);
INIT_FUNCTION(connect);
INIT_FUNCTION(close);
INIT_FUNCTION(listen);
INIT_FUNCTION(setsockopt);
INIT_FUNCTION(accept);
INIT_FUNCTION(accept4);
INIT_FUNCTION(recv);
INIT_FUNCTION(send);
INIT_FUNCTION(sendto);
INIT_FUNCTION(sendmsg);
INIT_FUNCTION(writev);
INIT_FUNCTION(readv);
INIT_FUNCTION(ioctl);
INIT_FUNCTION(select);
rc = ff_init(argc, argv);
assert(0 == rc);
@ -152,35 +139,28 @@ ff_mod_init(int argc, char * const *argv) {
int
socket(int domain, int type, int protocol)
{
int rc;
if (unlikely(inited == 0)) {
INIT_FUNCTION(socket);
return real_socket(domain, type, protocol);
return SYSCALL(socket)(domain, type, protocol);
}
if ((AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) {
rc = real_socket(domain, type, protocol);
return rc;
return SYSCALL(socket)(domain, type, protocol);
}
rc = ff_socket(domain, type, protocol);
return rc;
return ff_socket(domain, type, protocol);
}
int
bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(bind);
return real_bind(sockfd, addr, addrlen);
return SYSCALL(bind)(sockfd, addr, addrlen);
}
if (ff_fdisused(sockfd)) {
return ff_bind(sockfd, (struct linux_sockaddr *)addr, addrlen);
} else {
return real_bind(sockfd, addr, addrlen);
return SYSCALL(bind)(sockfd, addr, addrlen);
}
}
@ -188,14 +168,13 @@ int
connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(connect);
return real_connect(sockfd, addr, addrlen);
return SYSCALL(connect)(sockfd, addr, addrlen);
}
if (ff_fdisused(sockfd)) {
return ff_connect(sockfd, (struct linux_sockaddr *)addr, addrlen);
} else {
return real_connect(sockfd, addr, addrlen);
return SYSCALL(connect)(sockfd, addr, addrlen);
}
}
@ -203,14 +182,13 @@ ssize_t
send(int sockfd, const void *buf, size_t len, int flags)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(send);
return real_send(sockfd, buf, len, flags);
return SYSCALL(send)(sockfd, buf, len, flags);
}
if (ff_fdisused(sockfd)) {
return ff_send(sockfd, buf, len, flags);
} else {
return real_send(sockfd, buf, len, flags);
return SYSCALL(send)(sockfd, buf, len, flags);
}
}
@ -219,15 +197,14 @@ sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(sendto);
return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
return SYSCALL(sendto)(sockfd, buf, len, flags, dest_addr, addrlen);
}
if (ff_fdisused(sockfd)) {
return ff_sendto(sockfd, buf, len, flags,
(struct linux_sockaddr *)dest_addr, addrlen);
} else {
return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
return SYSCALL(sendto)(sockfd, buf, len, flags, dest_addr, addrlen);
}
}
@ -235,14 +212,13 @@ ssize_t
sendmsg(int sockfd, const struct msghdr *msg, int flags)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(sendmsg);
return real_sendmsg(sockfd, msg, flags);
return SYSCALL(sendmsg)(sockfd, msg, flags);
}
if (ff_fdisused(sockfd)) {
return ff_sendmsg(sockfd, msg, flags);
} else {
return real_sendmsg(sockfd, msg, flags);
return SYSCALL(sendmsg)(sockfd, msg, flags);
}
}
@ -250,14 +226,13 @@ ssize_t
recv(int sockfd, void *buf, size_t len, int flags)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(recv);
return real_recv(sockfd, buf, len, flags);
return SYSCALL(recv)(sockfd, buf, len, flags);
}
if (ff_fdisused(sockfd)) {
return ff_recv(sockfd, buf, len, flags);
} else {
return real_recv(sockfd, buf, len, flags);
return SYSCALL(recv)(sockfd, buf, len, flags);
}
}
@ -265,14 +240,13 @@ int
listen(int sockfd, int backlog)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(listen);
return real_listen(sockfd, backlog);
return SYSCALL(listen)(sockfd, backlog);
}
if (ff_fdisused(sockfd)) {
return ff_listen(sockfd, backlog);
} else {
return real_listen(sockfd, backlog);
return SYSCALL(listen)(sockfd, backlog);
}
}
@ -281,14 +255,13 @@ setsockopt (int sockfd, int level, int optname,
const void *optval, socklen_t optlen)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(setsockopt);
return real_setsockopt(sockfd, level, optname, optval, optlen);
return SYSCALL(setsockopt)(sockfd, level, optname, optval, optlen);
}
if (ff_fdisused(sockfd)) {
return ff_setsockopt(sockfd, level, optname, optval, optlen);
} else {
return real_setsockopt(sockfd, level, optname, optval, optlen);
return SYSCALL(setsockopt)(sockfd, level, optname, optval, optlen);
}
}
@ -296,14 +269,13 @@ int
accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(accept);
return real_accept(sockfd, addr, addrlen);
return SYSCALL(accept)(sockfd, addr, addrlen);
}
if (ff_fdisused(sockfd)) {
return ff_accept(sockfd, (struct linux_sockaddr *)addr, addrlen);
} else {
return real_accept(sockfd, addr, addrlen);
return SYSCALL(accept)(sockfd, addr, addrlen);
}
}
@ -311,14 +283,13 @@ int
accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(accept4);
return real_accept4(sockfd, addr, addrlen, flags);
return SYSCALL(accept4)(sockfd, addr, addrlen, flags);
}
if (ff_fdisused(sockfd)) {
return ff_accept(sockfd, (struct linux_sockaddr *)addr, addrlen);
} else {
return real_accept4(sockfd, addr, addrlen, flags);
return SYSCALL(accept4)(sockfd, addr, addrlen, flags);
}
}
@ -326,14 +297,13 @@ int
close(int sockfd)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(close);
return real_close(sockfd);
return SYSCALL(close)(sockfd);
}
if (ff_fdisused(sockfd)) {
return ff_close(sockfd);
} else {
return real_close(sockfd);
return SYSCALL(close)(sockfd);
}
}
@ -341,14 +311,13 @@ ssize_t
writev(int sockfd, const struct iovec *iov, int iovcnt)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(writev);
return real_writev(sockfd, iov, iovcnt);
return SYSCALL(writev)(sockfd, iov, iovcnt);
}
if (ff_fdisused(sockfd)) {
return ff_writev(sockfd, iov, iovcnt);
} else {
return real_writev(sockfd, iov, iovcnt);
return SYSCALL(writev)(sockfd, iov, iovcnt);
}
}
@ -356,14 +325,13 @@ ssize_t
readv(int sockfd, const struct iovec *iov, int iovcnt)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(readv);
return real_readv(sockfd, iov, iovcnt);
return SYSCALL(readv)(sockfd, iov, iovcnt);
}
if (ff_fdisused(sockfd)) {
return ff_readv(sockfd, iov, iovcnt);
} else {
return real_readv(sockfd, iov, iovcnt);
return SYSCALL(readv)(sockfd, iov, iovcnt);
}
}
@ -371,33 +339,13 @@ int
ioctl(int sockfd, int request, void *p)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(ioctl);
return real_ioctl(sockfd, request, p);
return SYSCALL(ioctl)(sockfd, request, p);
}
if (ff_fdisused(sockfd)) {
return ff_ioctl(sockfd, request, p);
} else {
return real_ioctl(sockfd, request, p);
}
}
int
select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
{
if (unlikely(inited == 0)) {
INIT_FUNCTION(select);
return real_select(nfds, readfds, writefds, exceptfds, timeout);
}
if (ff_fdisused(nfds)) {
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
return ff_select(nfds, readfds, writefds, exceptfds, &tv);
} else {
return real_select(nfds, readfds, writefds, exceptfds, timeout);
return SYSCALL(ioctl)(sockfd, request, p);
}
}
@ -417,6 +365,10 @@ kevent(int kq, const struct kevent *changelist, int nchanges,
int
gettimeofday(struct timeval *tv, struct timezone *tz)
{
if (unlikely(inited == 0)) {
return SYSCALL(gettimeofday)(tv, tz);
}
return ff_gettimeofday(tv, tz);
}

View File

@ -4112,7 +4112,7 @@ ff_fdisused(int fd)
{
struct thread *td = curthread;
return (fd < td->td_proc->p_fd->fd_nfiles &&
return (td && fd < td->td_proc->p_fd->fd_nfiles &&
fdisused(td->td_proc->p_fd, fd) &&
td->td_proc->p_fd->fd_ofiles[fd].fde_file != NULL);
}