Commit e8be1919 authored by Reda NOUSHI's avatar Reda NOUSHI

nocache retrieves max fds from actual process limits.

parent 113adf08
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <assert.h>
#include "fcntl_helpers.h" #include "fcntl_helpers.h"
...@@ -48,7 +51,6 @@ int (*_original_close)(int fd); ...@@ -48,7 +51,6 @@ int (*_original_close)(int fd);
FILE *(*_original_fopen)(const char *path, const char *mode); FILE *(*_original_fopen)(const char *path, const char *mode);
int (*_original_fclose)(FILE *fp); int (*_original_fclose)(FILE *fp);
#define _MAX_FDS 1024
struct fadv_info { struct fadv_info {
int fd; int fd;
...@@ -56,7 +58,8 @@ struct fadv_info { ...@@ -56,7 +58,8 @@ struct fadv_info {
unsigned int nr_pages; unsigned int nr_pages;
unsigned char *info; unsigned char *info;
}; };
static struct fadv_info fds[_MAX_FDS]; static int max_fds;
static struct fadv_info *fds;
static size_t PAGESIZE; static size_t PAGESIZE;
static pthread_mutex_t lock; /* protects access to fds[] */ static pthread_mutex_t lock; /* protects access to fds[] */
...@@ -64,6 +67,14 @@ static void init(void) ...@@ -64,6 +67,14 @@ static void init(void)
{ {
int i; int i;
char *error; char *error;
struct rlimit rlim;
getrlimit(RLIMIT_NOFILE, &rlim);
max_fds=(int) rlim.rlim_max;
fds=(struct fadv_info *) malloc(max_fds * sizeof(struct fadv_info));
assert(fds != NULL);
_original_open = (int (*)(const char *, int, mode_t)) _original_open = (int (*)(const char *, int, mode_t))
dlsym(RTLD_NEXT, "open"); dlsym(RTLD_NEXT, "open");
...@@ -82,8 +93,10 @@ static void init(void) ...@@ -82,8 +93,10 @@ static void init(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
PAGESIZE = getpagesize(); PAGESIZE = getpagesize();
for(i = 0; i < _MAX_FDS; i++) for(i = 0; i < max_fds; i++)
fds[i].fd = -1; fds[i].fd = -1;
init_mutex(); init_mutex();
handle_stdout(); handle_stdout();
...@@ -118,7 +131,7 @@ static void destroy(void) ...@@ -118,7 +131,7 @@ static void destroy(void)
{ {
int i; int i;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
for(i = 0; i < _MAX_FDS; i++) { for(i = 0; i < max_fds; i++) {
if(fds[i].fd == -1) if(fds[i].fd == -1)
continue; /* slot is empty */ continue; /* slot is empty */
if(!valid_fd(fds[i].fd)) if(!valid_fd(fds[i].fd))
...@@ -231,9 +244,9 @@ static void store_pageinfo(int fd) ...@@ -231,9 +244,9 @@ static void store_pageinfo(int fd)
/* check if there's space to store the info */ /* check if there's space to store the info */
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
for(i = 0; i < _MAX_FDS && fds[i].fd != -1; i++) for(i = 0; i < max_fds && fds[i].fd != -1; i++)
; ;
if(i == _MAX_FDS) { if(i == max_fds) {
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return; /* no space! */ return; /* no space! */
} }
...@@ -282,11 +295,11 @@ static void free_unclaimed_pages(int fd) ...@@ -282,11 +295,11 @@ static void free_unclaimed_pages(int fd)
return; return;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
for(i = 0; i < _MAX_FDS; i++) for(i = 0; i < max_fds; i++)
if(fds[i].fd == fd) if(fds[i].fd == fd)
break; break;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
if(i == _MAX_FDS) if(i == max_fds)
return; /* not found */ return; /* not found */
sync_if_writable(fd); sync_if_writable(fd);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment