diff options
author | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-08-25 01:00:17 +0000 |
---|---|---|
committer | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-08-25 01:00:17 +0000 |
commit | 549ac33242cc201737290866159da702807e7f53 (patch) | |
tree | 07931f7d8c57fb00a26e89a9f7af2d78366c518f | |
parent | d807048fc6e50f4cee4848680319837ae5c878f8 (diff) |
Forward-port poll socket engine as it seems reasonably stable, and half-hack it into configure. If someone could help me (easily) add it via our wonderful configure macros so it's built, that would be swell.
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10264 e03df62e-2008-0410-955e-edbf42e46eb7
-rwxr-xr-x | configure | 34 | ||||
-rw-r--r-- | include/socketengines/socketengine_poll.h | 66 | ||||
-rw-r--r-- | src/socketengines/socketengine_epoll.cpp | 1 | ||||
-rw-r--r-- | src/socketengines/socketengine_poll.cpp | 180 |
4 files changed, 275 insertions, 6 deletions
@@ -275,6 +275,7 @@ if (defined $opt_nokqueue) { $config{USE_KQUEUE} = "n"; } +$config{USE_POLL} = "y"; # poll enabled $config{USE_EPOLL} = "y"; # epoll enabled if (defined $opt_epoll) { @@ -801,22 +802,36 @@ should NOT be used. You should probably specify a newer compiler.\n\n"; dir_check("is the IRCd binary to be placed", "BINARY_DIR"); dir_check("are the IRCd libraries to be placed", "LIBRARY_DIR"); + my $chose_hiperf = 0; if ($has_kqueue) { yesno('USE_KQUEUE',"You are running a BSD operating system, and kqueue\nwas detected. Would you like to enable kqueue support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable kqueue?"); print "\n"; + if ($config{USE_KQUEUE} eq "y") { + $chose_hiperf = 1; + } } if ($has_epoll) { yesno('USE_EPOLL',"You are running a Linux 2.6+ operating system, and epoll\nwas detected. Would you like to enable epoll support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable epoll?"); print "\n"; + if ($config{USE_EPOLL} eq "y") { + $chose_hiperf = 1; + } } if ($has_ports) { yesno('USE_PORTS',"You are running Solaris 10.\nWould you like to enable I/O completion ports support?\nThis is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable support for I/O completion ports?"); print "\n"; + if ($config{USE_PORTS} eq "y") { + $chose_hiperf = 1; + } } - my $chose_hiperf = (($config{USE_EPOLL} eq "y") || ($config{USE_KQUEUE} eq "y") || ($config{USE_PORTS} eq "y")); + if (!$chose_hiperf) { - print "No high-performance socket engines are available, or you chose\n"; - print "not to enable one. Defaulting to select() engine.\n\n"; + yesno('USE_POLL', "Would you like to use poll?\n This is likely to increase performance.\nIf you are unsure, answer yes.\n\nEnable poll?\n"); + if ($config{USE_POLL} ne "y") + { + print "No high-performance socket engines are available, or you chose\n"; + print "not to enable one. Defaulting to select() engine.\n\n"; + } } yesno('IPV6',"Would you like to build InspIRCd with IPv6 support?"); @@ -1221,8 +1236,17 @@ print FILEHANDLE "#define MAXBUF " . ($config{MAXBUF}+2) . "\n"; # user didn't choose either epoll or select for their OS. # default them to USE_SELECT (ewwy puke puke) if (!$use_hiperf) { - print FILEHANDLE "#define USE_SELECT\n"; - $se = "socketengine_select"; + print "no hi-perf, " . $config{USE_POLL}; + if ($config{USE_POLL} eq "y") + { + print FILEHANDLE "#define USE_POLL\n"; + $se = "socketengine_poll"; + } + else + { + print FILEHANDLE "#define USE_SELECT\n"; + $se = "socketengine_select"; + } } print FILEHANDLE "\n#include \"threadengines/threadengine_pthread.h\"\n\n#endif\n"; close(FILEHANDLE); diff --git a/include/socketengines/socketengine_poll.h b/include/socketengines/socketengine_poll.h new file mode 100644 index 000000000..5156e1f4c --- /dev/null +++ b/include/socketengines/socketengine_poll.h @@ -0,0 +1,66 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2008 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_POLL__ +#define __SOCKETENGINE_POLL__ + +#include <vector> +#include <string> +#include <map> +#include "inspircd_config.h" +#include "globals.h" +#include "inspircd.h" +#include "socketengine.h" +#ifndef __USE_XOPEN + #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/ +#endif +#include <poll.h> + +class InspIRCd; + +/** A specialisation of the SocketEngine class, designed to use poll(). + */ +class PollEngine : public SocketEngine +{ +private: + /** These are used by poll() to hold socket events + */ + struct pollfd *events; +public: + /** Create a new PollEngine + * @param Instance The creator of this object + */ + PollEngine(InspIRCd* Instance); + /** Delete a PollEngine + */ + virtual ~PollEngine(); + virtual bool AddFd(EventHandler* eh); + virtual int GetMaxFds(); + virtual int GetRemainingFds(); + virtual bool DelFd(EventHandler* eh, bool force = false); + virtual int DispatchEvents(); + virtual std::string GetName(); + virtual void WantWrite(EventHandler* eh); +}; + +/** Creates a SocketEngine + */ +class SocketEngineFactory +{ +public: + /** Create a new instance of SocketEngine based on PollEngine + */ + SocketEngine* Create(InspIRCd* Instance) { return new PollEngine(Instance); } +}; + +#endif diff --git a/src/socketengines/socketengine_epoll.cpp b/src/socketengines/socketengine_epoll.cpp index de06103fb..657505d2d 100644 --- a/src/socketengines/socketengine_epoll.cpp +++ b/src/socketengines/socketengine_epoll.cpp @@ -13,7 +13,6 @@ #include "inspircd.h" #include "exitcodes.h" -#include <sys/epoll.h> #include "socketengines/socketengine_epoll.h" #include <ulimit.h> diff --git a/src/socketengines/socketengine_poll.cpp b/src/socketengines/socketengine_poll.cpp new file mode 100644 index 000000000..ddeb057d6 --- /dev/null +++ b/src/socketengines/socketengine_poll.cpp @@ -0,0 +1,180 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2008 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#include "inspircd.h" +#include "exitcodes.h" +#include "socketengines/socketengine_poll.h" + +PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance) +{ + // Poll requires no special setup (which is nice). + CurrentSetSize = 0; + + ref = new EventHandler* [GetMaxFds()]; + events = new struct epoll_event[GetMaxFds()]; + + memset(&events, 0, GetMaxFds() * sizeof(struct pollfds*)); +} + +PollEngine::~PollEngine() +{ + // No destruction required, either. + delete[] ref; + delete[] events; +} + +bool PollEngine::AddFd(EventHandler* eh) +{ + int fd = eh->GetFd(); + if ((fd < 0) || (fd > GetMaxFds() - 1)) + { + ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds()); + return false; + } + + if (GetRemainingFds() <= 1) + { + ServerInstance->Logs->Log("SOCKET",DEBUG,"No remaining FDs cannot add fd: %d", fd); + return false; + } + + if (ref[fd]) + { + ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd); + return false; + } + + ref[fd] = eh; + events[fd]->fd = fd; + if (eh->Readable()) + { + events[fd]->events = POLLIN; + } + else + { + events[fd]->events = POLLOUT; + } + + ServerInstance->Log(DEBUG,"New file descriptor: %d (%d)", fd, events[fd]->events); + CurrentSetSize++; + return true; +} + +void PollEngine::WantWrite(EventHandler* eh) +{ + events[eh->GetFd()]->events = POLLIN | POLLOUT; +} + +bool PollEngine::DelFd(EventHandler* eh, bool force) +{ + int fd = eh->GetFd(); + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + { + ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds()); + return false; + } + + events[fd]->fd = -1; + events[fd]->events = 0; + + CurrentSetSize--; + ref[fd] = NULL; + + ServerInstance->Log(DEBUG,"Remove file descriptor: %d", fd); + return true; +} + +int PollEngine::GetMaxFds() +{ + if (MAX_DESCRIPTORS) + return MAX_DESCRIPTORS; + + int max = ulimit(4, 0); + if (max > 0) + { + MAX_DESCRIPTORS = max; + return max; + } + else + { + ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets!"); + printf("ERROR: Can't determine maximum number of open sockets!\n"); + ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE); + } + return 0; +} + +int PollEngine::GetRemainingFds() +{ + return MAX_DESCRIPTORS - CurrentSetSize; +} + +int PollEngine::DispatchEvents() +{ + int i = poll(events, GetMaxFds() - 1, 1000); + int fd = 0; + socklen_t codesize = sizeof(int); + int errcode; + int processed = 0; + + if (i > 0) + { + for (fd = 0; fd < GetMaxFds() - 1 && processed != i; fd++) + { + if (events[fd].revents) + processed++; + + if (events[fd].revents & POLLHUP) + { + if (ref[fd]) + ref[fd]->HandleEvent(EVENT_ERROR, 0); + continue; + } + + if (events[fd].revents & POLLERR) + { + // Get error number + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0) + errcode = errno; + if (ref[fd]) + ref[fd]->HandleEvent(EVENT_ERROR, errcode); + continue; + } + + if (events[fd].revents & POLLOUT) + { + // Switch to wanting read again + // event handlers have to request to write again if they need it + events[fd].events = POLLIN; + + + if (ref[fd]) + ref[fd]->HandleEvent(EVENT_WRITE); + } + + if (events[fd].revents & POLLIN) + { + if (ref[fd]) + ref[fd]->HandleEvent(EVENT_READ); + } + } + } + + return i; +} + +std::string PollEngine::GetName() +{ + return "poll"; +} + |