]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
* added the ability to enforce limits on memory usage.
authorwilly tarreau <willy@wtap.(none)>
Sat, 25 Mar 2006 10:14:35 +0000 (11:14 +0100)
committerwilly tarreau <willy@wtap.(none)>
Sat, 25 Mar 2006 19:33:22 +0000 (20:33 +0100)
haproxy.c

index 113d0b5f2de9957ac0af6eca1a5cc21109cf89bc..a0f9f4a40af6e551e6b6691199f05ff6f0158199 100644 (file)
--- a/haproxy.c
+++ b/haproxy.c
@@ -653,6 +653,7 @@ static struct {
     int maxconn;
     int maxsock;               /* max # of sockets */
     int rlimit_nofile;         /* default ulimit-n value : 0=unset */
+    int rlimit_memmax;         /* default ulimit-d in megs value : 0=unset */
     int mode;
     char *chroot;
     char *pidfile;
@@ -867,7 +868,8 @@ void usage(char *name) {
 #if STATTIME > 0
            "sl"
 #endif
-           "D ] [ -n <maxconn> ] [ -N <maxpconn> ] [ -p <pidfile> ]\n"
+           "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
+           "        [ -p <pidfile> ] [ -m <max megs> ]\n"
            "        -v displays version\n"
            "        -d enters debug mode\n"
            "        -V enters verbose mode (disables quiet mode)\n"
@@ -879,6 +881,7 @@ void usage(char *name) {
            "        -q quiet mode : don't display messages\n"
            "        -c check mode : only check config file and exit\n"
            "        -n sets the maximum total # of connections (%d)\n"
+           "        -m limits the usable amount of memory (in MB)\n"
            "        -N sets the default, per-proxy maximum # of connections (%d)\n"
            "        -p writes pids of all children to this file\n"
 #if defined(ENABLE_EPOLL)
@@ -7945,6 +7948,10 @@ void init(int argc, char **argv) {
        exit(1);
     }
 
+#ifdef HAPROXY_MEMMAX
+    global.rlimit_memmax = HAPROXY_MEMMAX;
+#endif
+
     /* initialize the libc's localtime structures once for all so that we
      * won't be missing memory if we want to send alerts under OOM conditions.
      */
@@ -8035,6 +8042,7 @@ void init(int argc, char **argv) {
 
                switch (*flag) {
                case 'n' : cfg_maxconn = atol(*argv); break;
+               case 'm' : global.rlimit_memmax = atol(*argv); break;
                case 'N' : cfg_maxpconn = atol(*argv); break;
                case 'f' : cfg_cfgfile = *argv; break;
                case 'p' : cfg_pidfile = *argv; break;
@@ -8417,6 +8425,22 @@ int main(int argc, char **argv) {
        }
     }
 
+    if (global.rlimit_memmax) {
+       limit.rlim_cur = limit.rlim_max =
+               global.rlimit_memmax * 1048576 / global.nbproc;
+#ifdef RLIMIT_AS
+       if (setrlimit(RLIMIT_AS, &limit) == -1) {
+           Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
+                   argv[0], global.rlimit_memmax);
+       }
+#else
+       if (setrlimit(RLIMIT_DATA, &limit) == -1) {
+           Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
+                   argv[0], global.rlimit_memmax);
+       }
+#endif
+    }
+
     /* setgid / setuid */
     if (global.gid && setgid(global.gid) == -1) {
        Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);