]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Mass-connect speedups, also now uses less processor time
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 52f7b64792abe29dc7cd1e6e43a29e0bc63b0b6c..701b953819c2f1497d080a8c655fbe5dbae2a788 100644 (file)
@@ -36,7 +36,7 @@ using namespace std;
 
 #ifdef USE_EPOLL
 #include <sys/epoll.h>
-#define EP_DELAY 10
+#define EP_DELAY 50
 #endif
 
 #include <time.h>
@@ -2704,7 +2704,7 @@ int InspIRCd(char** argv, int argc)
        {
                struct kevent ke;
                log(DEBUG,"kqueue: Add listening socket to events, kq=%d socket=%d",lkq,openSockfd[count]);
-               EV_SET(&ke, openSockfd[count], EVFILT_READ, EV_ADD, 0, SOMAXCONN, NULL);
+               EV_SET(&ke, openSockfd[count], EVFILT_READ, EV_ADD, 0, 32, NULL);
                int i = kevent(lkq, &ke, 1, 0, 0, NULL);
                if (i == -1)
                {
@@ -2719,7 +2719,7 @@ int InspIRCd(char** argv, int argc)
                 if (me[t])
                 {
                        log(DEBUG,"kqueue: Add listening SERVER socket to events, kq=%d socket=%d",skq,me[t]->fd);
-                       EV_SET(&ke, me[t]->fd, EVFILT_READ, EV_ADD, 0, SOMAXCONN, NULL);
+                       EV_SET(&ke, me[t]->fd, EVFILT_READ, EV_ADD, 0, 32, NULL);
                        int i = kevent(skq, &ke, 1, 0, 0, NULL);
                        if (i == -1)
                        {
@@ -2796,6 +2796,9 @@ int InspIRCd(char** argv, int argc)
 
 #ifdef USE_EPOLL
                i = epoll_wait(sep, event, 1, EP_DELAY);
+#ifdef _POSIX_PRIORITY_SCHEDULING
+                                sched_yield();
+#endif
                if (i > 0)
                {
                        log(DEBUG,"epoll: Listening server socket event, i=%d, event.data.fd=%d",i,event[0].data.fd);
@@ -2858,37 +2861,43 @@ int InspIRCd(char** argv, int argc)
                        std::deque<std::string> sums;
                        msgs.clear();
                        sums.clear();
-                       if ((me[x]) && (me[x]->RecvPacket(msgs, tcp_host, sums)))
+                       if (me[x])
                        {
-                               for (int ctr = 0; ctr < msgs.size(); ctr++)
+                               sums.clear();
+                               msgs.clear();
+                               while (me[x]->RecvPacket(msgs, tcp_host, sums))
                                {
-                                       strlcpy(tcp_msg,msgs[ctr].c_str(),MAXBUF);
-                                       strlcpy(tcp_sum,msgs[ctr].c_str(),MAXBUF);
-                                       log(DEBUG,"Processing: %s",tcp_msg);
-                                       if (!tcp_msg[0])
-                                       {
-                                               log(DEBUG,"Invalid string from %s [route%lu]",tcp_host,(unsigned long)x);
-                                               break;
-                                       }
-                                       // during a netburst, send all data to all other linked servers
-                                       if ((((nb_start>0) && (tcp_msg[0] != 'Y') && (tcp_msg[0] != 'X') && (tcp_msg[0] != 'F'))) || (is_uline(tcp_host)))
+                                       for (int ctr = 0; ctr < msgs.size(); ctr++)
                                        {
-                                               if (is_uline(tcp_host))
+                                               strlcpy(tcp_msg,msgs[ctr].c_str(),MAXBUF);
+                                               strlcpy(tcp_sum,msgs[ctr].c_str(),MAXBUF);
+                                               log(DEBUG,"Processing: %s",tcp_msg);
+                                               if (!tcp_msg[0])
+                                               {
+                                                       log(DEBUG,"Invalid string from %s [route%lu]",tcp_host,(unsigned long)x);
+                                                       break;
+                                               }
+                                               // during a netburst, send all data to all other linked servers
+                                               if ((((nb_start>0) && (tcp_msg[0] != 'Y') && (tcp_msg[0] != 'X') && (tcp_msg[0] != 'F'))) || (is_uline(tcp_host)))
                                                {
-                                                       if ((tcp_msg[0] != 'Y') && (tcp_msg[0] != 'X') && (tcp_msg[0] != 'F'))
+                                                       if (is_uline(tcp_host))
                                                        {
-                                                               NetSendToAllExcept_WithSum(tcp_host,tcp_msg,tcp_sum);
+                                                               if ((tcp_msg[0] != 'Y') && (tcp_msg[0] != 'X') && (tcp_msg[0] != 'F'))
+                                                               {
+                                                                       NetSendToAllExcept_WithSum(tcp_host,tcp_msg,tcp_sum);
+                                                               }
                                                        }
+                                                       else
+                                                               NetSendToAllExcept_WithSum(tcp_host,tcp_msg,tcp_sum);
                                                }
-                                               else
-                                                       NetSendToAllExcept_WithSum(tcp_host,tcp_msg,tcp_sum);
+                                               std::string msg = tcp_msg;
+                                               FOREACH_MOD OnPacketReceive(msg,tcp_host);
+                                               strlcpy(tcp_msg,msg.c_str(),MAXBUF);
+                                               if (me[x])
+                                                       handle_link_packet(tcp_msg, tcp_host, me[x], tcp_sum);
                                        }
-                                       std::string msg = tcp_msg;
-                                       FOREACH_MOD OnPacketReceive(msg,tcp_host);
-                                       strlcpy(tcp_msg,msg.c_str(),MAXBUF);
-                                       handle_link_packet(tcp_msg, tcp_host, me[x], tcp_sum);
+                                       //goto label;
                                }
-                               goto label;
                        }
                }
        
@@ -2910,6 +2919,9 @@ int InspIRCd(char** argv, int argc)
                if (count2->second)
                        curr = count2->second;
 
+               if ((long)curr == -1)
+                       goto label;
+
                if ((curr) && (curr->fd != 0))
                {
 #ifdef _POSIX_PRIORITY_SCHEDULING
@@ -2926,8 +2938,11 @@ int InspIRCd(char** argv, int argc)
                                if (count2 != clientlist.end())
                                {
                                        curr = count2->second;
+                                       if ((long)curr == -1)
+                                               goto label;
+                                       int currfd = curr->fd;
                                        // we don't check the state of remote users.
-                                       if ((curr->fd != -1) && (curr->fd != FD_MAGIC_NUMBER))
+                                       if ((currfd != -1) && (currfd != FD_MAGIC_NUMBER))
                                        {
                                                curr->FlushWriteBuf();
                                                if (curr->GetWriteError() != "")
@@ -2953,13 +2968,15 @@ int InspIRCd(char** argv, int argc)
                                                        curr->dns_done = true;
                                                        statsDnsBad++;
                                                        FullConnectUser(curr);
-                                                       goto label;
+                                                        if (fd_ref_table[currfd] != curr) // something changed, bail pronto
+                                                                goto label;
                                                }
                                                if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr))) // both NICK and USER... and DNS
                                                {
                                                        log(DEBUG,"dns done, registered=3, and modules ready, OK");
                                                        FullConnectUser(curr);
-                                                       goto label;
+                                                        if (fd_ref_table[currfd] != curr) // something changed, bail pronto
+                                                                goto label;
                                                }
                                                if ((TIME > curr->nping) && (isnick(curr->nick)) && (curr->registered == 7))
                                                {
@@ -2992,8 +3009,11 @@ int InspIRCd(char** argv, int argc)
                                if (count2 != clientlist.end())
                                {
                                        curr = count2->second;
+                                        if ((long)curr == -1)
+                                                goto label;
+                                       int currfd = curr->fd;
                                        // we don't check the state of remote users.
-                                       if ((curr->fd != -1) && (curr->fd != FD_MAGIC_NUMBER))
+                                       if ((currfd != -1) && (currfd != FD_MAGIC_NUMBER))
                                        {
 
                                                 curr->FlushWriteBuf();
@@ -3010,7 +3030,8 @@ int InspIRCd(char** argv, int argc)
                                                {
                                                        log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
                                                        kill_link(curr,"Registration timeout");
-                                                       goto label;
+                                                       goto label;
+
                                                }
                                                if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
                                                {
@@ -3018,13 +3039,15 @@ int InspIRCd(char** argv, int argc)
                                                        curr->dns_done = true;
                                                        statsDnsBad++;
                                                        FullConnectUser(curr);
-                                                       goto label;
+                                                        if (fd_ref_table[currfd] != curr) // something changed, bail pronto
+                                                                goto label;
                                                }
                                                if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
                                                {
                                                        log(DEBUG,"dns done, registered=3, and modules ready, OK");
                                                        FullConnectUser(curr);
-                                                       goto label;
+                                                        if (fd_ref_table[currfd] != curr) // something changed, bail pronto
+                                                                goto label;
                                                }
                                                if ((TIME > curr->nping) && (isnick(curr->nick)) && (curr->registered == 7))
                                                {
@@ -3049,7 +3072,7 @@ int InspIRCd(char** argv, int argc)
         
                        v = 0;
 #ifdef USE_EPOLL
-                       int i = epoll_wait(ep, event, 1, EP_DELAY);
+                       int i = epoll_wait(ep, event, 1, 5);
                        if (i > 0)
                        {
                                log(DEBUG,"epoll_wait call: ep=%d, i=%d",ep,i);
@@ -3216,7 +3239,8 @@ int InspIRCd(char** argv, int argc)
 
                                                        }
                                                }
-                                               goto label;
+                                               if ((currfd < 0) || (!fd_ref_table[currfd]))
+                                                       goto label;
                                        }
 
                                        if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
@@ -3296,7 +3320,13 @@ int InspIRCd(char** argv, int argc)
                        {
 #endif
 #ifdef USE_EPOLL
+#ifdef _POSIX_PRIORITY_SCHEDULING
+                                sched_yield();
+#endif
        i = epoll_wait(lep, event, 32, EP_DELAY);
+#ifdef _POSIX_PRIORITY_SCHEDULING
+                                sched_yield();
+#endif
        if (i > 0) for (j = 0; j < i; j++)
        {
                log(DEBUG,"epoll: Listening socket event, i=%d,events[j].data.fd=%d",i,event[j].data.fd);
@@ -3325,7 +3355,6 @@ int InspIRCd(char** argv, int argc)
                                        AddClient(incomingSockfd, resolved, ports[count], false, inet_ntoa (client.sin_addr));
                                        log(DEBUG,"InspIRCd: adding client on port %lu fd=%lu",(unsigned long)ports[count],(unsigned long)incomingSockfd);
                                }
-                               //goto label;
                        }
                }
        }