#1 2019-03-14 07:34:13

franfj
Member
Registered: 2018-03-28
Posts: 12

wepoll - epoll for windows to use as TPollSocketClass

Maybe it will be interesting to improve the performance of the servers on windows...

https://github.com/piscisaureus/wepoll

This library implements the epoll API for Windows applications. It is fast and scalable, and it closely resembles the API and behavior of Linux' epoll.

Rationale
Unlike Linux, OS X, and many other operating systems, Windows doesn't have a good API for receiving socket state notifications. It only supports the select and WSAPoll APIs, but they don't scale and suffer from other issues.

Using I/O completion ports isn't always practical when software is designed to be cross-platform. Wepoll offers an alternative that is much closer to a drop-in replacement for software that was designed to run on Linux.

Compiling as a dll and using it is easy, just a copy&paste the types definition from SynFPCSock.pas, some small modifications, and load the functions from the external dll.

unit wepolldll;

interface

const
  // associated file is available for read operations
  EPOLLIN  = $01;
  // urgent data available for read operations
  EPOLLPRI = $02;
  // associated file is available for write operations
  EPOLLOUT = $04;
  // error condition happened on the associated file descriptor
  EPOLLERR = $08;
  // hang up happened on the associated file descriptor
  EPOLLHUP = $10;
  // sets the One-Shot behaviour for the associated file descriptor
  // - i.e. after an event is pulled out, the file descriptor is disabled
  EPOLLONESHOT = $40000000;
  // sets the Edge-Triggered (ET) behaviour  for  the  associated file descriptor
  EPOLLET  = $80000000;

  EPOLL_CTL_ADD = 1;
  EPOLL_CTL_DEL = 2;
  EPOLL_CTL_MOD = 3;

type
  /// application-level data structure for epoll
  TEPollData = record
    case integer of
      0: (ptr: pointer);
      1: (fd: integer);
      2: (u32: cardinal);
      3: (u64: Int64);
  end;
  PEPollData = ^TEPollData;

  /// epoll descriptor data structure
  TEPollEvent = packed record
    events: cardinal;
    __padding: cardinal;   ///////// don't know why, but data returned by epoll_wait is aligned to next 4bytes...
    data: TEpollData;
  end;
  PEPollEvent = ^TEPollEvent;
  TEPollEventDynArray = array of TEPollEvent;

  function epoll_create(size: integer): integer; cdecl; external 'wepoll.dll';
  function epoll_close(epfd: integer): Integer; cdecl; external 'wepoll.dll';
  function epoll_ctl(epfd, op, fd: integer; event: PEPollEvent): Integer; cdecl; external 'wepoll.dll';
  function epoll_wait(epfd: THandle; events: PEPollEvent; maxevents, timeout: integer): Integer; cdecl; external 'wepoll.dll';

implementation

end.

Static linking will need more work, but should be possible.

I haven't done extensive performance or stability tests, but seems to work fine...

Offline

#2 2019-03-14 08:51:38

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: wepoll - epoll for windows to use as TPollSocketClass

Interresting.

When I investigated about the official WSAPool() Windows API - https://docs.microsoft.com/en-us/window … k2-wsapoll - I discovered its emulation was quite poor and slow.
It seems to use plain socket behind the hook.
See TPollSocketSelect comments: https://synopse.info/files/html/api-1.1 … CKETSELECT

https://github.com/piscisaureus/wepoll seems to be a much better alternative, since it uses IOCP behind the wood.

I wrote some performance numbers in
https://github.com/synopse/mORMot/blob/ … xy.dpr#L48


We may eventually use this wepoll (with static linking) to speed up multiple connections on Windows, with minimal code change.
Implementing IOCP may be pretty tricky - and Linux is now our main target for servers development, for sure.
I have added a link to wepoll repository in the sample comments, as reference.

Offline

#3 2019-03-14 09:57:56

franfj
Member
Registered: 2018-03-28
Posts: 12

Re: wepoll - epoll for windows to use as TPollSocketClass

Yes, as this library uses internally IOCP it seems an easy way to add that functionality into windows.

I know that sample, but unfortunately on my configuration it does not give consistent results. I've done some tests and results sometimes seems 3x faster than Select, others it shows near the same performance... but even two consecutive tests using Select shows a lot of variation on times, so I can't trust the results.

Last edited by franfj (2019-03-14 09:59:10)

Offline

#4 2019-03-14 15:57:01

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: wepoll - epoll for windows to use as TPollSocketClass

Sounds very cool!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#5 2022-07-01 12:54:36

keymark
Member
Registered: 2022-07-01
Posts: 1

Re: wepoll - epoll for windows to use as TPollSocketClass

@franfj
wepoll demo?
How to use it

Offline

#6 2022-07-02 09:54:18

franfj
Member
Registered: 2018-03-28
Posts: 12

Re: wepoll - epoll for windows to use as TPollSocketClass

keymark wrote:

@franfj
wepoll demo?
How to use it

Sorry, I don't have any full demo now.

I don't remember very well, but it should be easy to replicate the code from other poll classes (have a look at wsapoll or select) and implement the wepoll.

Anyway, the performance of the select implementation worked well enough for my requirements, so I left away this approach.

Offline

Board footer

Powered by FluxBB