2018-06-16 09:37:07 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
* Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
|
2018-06-16 09:37:07 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "core/private.h"
|
|
|
|
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
struct lws *
|
|
|
|
wsi_from_fd(const struct lws_context *context, int fd)
|
|
|
|
{
|
|
|
|
struct lws **p, **done;
|
|
|
|
|
|
|
|
if (!context->max_fds_unrelated_to_ulimit)
|
|
|
|
return context->lws_lookup[fd - lws_plat_socket_offset()];
|
|
|
|
|
|
|
|
/* slow fds handling */
|
|
|
|
|
|
|
|
p = context->lws_lookup;
|
|
|
|
done = &p[context->max_fds];
|
|
|
|
|
|
|
|
while (p != done) {
|
|
|
|
if (*p && (*p)->desc.sockfd == fd)
|
|
|
|
return *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
insert_wsi(const struct lws_context *context, struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws **p, **done;
|
|
|
|
|
|
|
|
if (!context->max_fds_unrelated_to_ulimit) {
|
|
|
|
assert(context->lws_lookup[wsi->desc.sockfd -
|
|
|
|
lws_plat_socket_offset()] == 0);
|
|
|
|
|
|
|
|
context->lws_lookup[wsi->desc.sockfd - \
|
|
|
|
lws_plat_socket_offset()] = wsi;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slow fds handling */
|
|
|
|
|
|
|
|
p = context->lws_lookup;
|
|
|
|
done = &p[context->max_fds];
|
|
|
|
|
2019-08-01 18:31:11 +01:00
|
|
|
#if defined(_DEBUG)
|
|
|
|
|
|
|
|
/* confirm it doesn't already exist */
|
|
|
|
|
|
|
|
while (p != done && *p != wsi)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
assert(p == done);
|
|
|
|
p = context->lws_lookup;
|
|
|
|
|
|
|
|
/* confirm fd doesn't already exist */
|
|
|
|
|
|
|
|
while (p != done && (!*p || (*p && (*p)->desc.sockfd != wsi->desc.sockfd)))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (p != done) {
|
|
|
|
lwsl_err("%s: wsi %p already says it has fd %d\n",
|
|
|
|
__func__, *p, wsi->desc.sockfd);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
p = context->lws_lookup;
|
|
|
|
#endif
|
|
|
|
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
/* find an empty slot */
|
|
|
|
|
|
|
|
while (p != done && *p)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (p == done) {
|
|
|
|
lwsl_err("%s: reached max fds\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = wsi;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
delete_from_fd(const struct lws_context *context, int fd)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct lws **p, **done;
|
|
|
|
|
|
|
|
if (!context->max_fds_unrelated_to_ulimit) {
|
|
|
|
context->lws_lookup[fd - lws_plat_socket_offset()] = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slow fds handling */
|
|
|
|
|
|
|
|
p = context->lws_lookup;
|
|
|
|
done = &p[context->max_fds];
|
|
|
|
|
2019-08-01 18:31:11 +01:00
|
|
|
/* find the match */
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
|
2019-08-01 18:31:11 +01:00
|
|
|
while (p != done && (!*p || (*p && (*p)->desc.sockfd != fd)))
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (p == done)
|
|
|
|
lwsl_err("%s: fd %d not found\n", __func__, fd);
|
|
|
|
else
|
|
|
|
*p = NULL;
|
2019-08-01 18:31:11 +01:00
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
p = context->lws_lookup;
|
|
|
|
while (p != done && (!*p || (*p && (*p)->desc.sockfd != fd)))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (p != done) {
|
|
|
|
lwsl_err("%s: fd %d in lws_lookup again at %d\n", __func__,
|
|
|
|
fd, (int)(p - context->lws_lookup));
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
#endif
|
unix plat: add minimal wsi fd map option
An lws context usually contains a processwide fd -> wsi lookup table.
This allows any possible fd returned by a *nix type OS to be immediately
converted to a wsi just by indexing an array of struct lws * the size of
the highest possible fd, as found by ulimit -n or similar.
This works modestly for Linux type systems where the default ulimit -n for
a process is 1024, it means a 4KB or 8KB lookup table for 32-bit or
64-bit systems.
However in the case your lws usage is much simpler, like one outgoing
client connection and no serving, this represents increasing waste. It's
made much worse if the system has a much larger default ulimit -n, eg 1M,
the table is occupying 4MB or 8MB, of which you will only use one.
Even so, because lws can't be sure the OS won't return a socket fd at any
number up to (ulimit -n - 1), it has to allocate the whole lookup table
at the moment.
This patch looks to see if the context creation info is setting
info->fd_limit_per_thread... if it leaves it at the default 0, then
everything is as it was before this patch. However if finds that
(info->fd_limit_per_thread * actual_number_of_service_threads) where
the default number of service threads is 1, is less than the fd limit
set by ulimit -n, lws switches to a slower lookup table scheme, which
only allocates the requested number of slots. Lookups happen then by
iterating the table and comparing rather than indexing the array
directly, which is obviously somewhat of a performance hit.
However in the case where you know lws will only have a very few wsi
maximum, this method can very usefully trade off speed to be able to
avoid the allocation sized by ulimit -n.
minimal examples for client that can make use of this are also modified
by this patch to use the smaller context allocations.
2019-05-17 01:20:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 09:37:07 +08:00
|
|
|
void
|
|
|
|
lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
if (context->event_loop_ops->io)
|
|
|
|
context->event_loop_ops->io(wsi, LWS_EV_START | LWS_EV_READ);
|
|
|
|
|
|
|
|
pt->fds[pt->fds_count++].revents = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_plat_delete_socket_from_fds(struct lws_context *context,
|
|
|
|
struct lws *wsi, int m)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
if (context->event_loop_ops->io)
|
|
|
|
context->event_loop_ops->io(wsi,
|
|
|
|
LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
|
|
|
|
|
|
|
|
pt->fds_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_plat_change_pollfd(struct lws_context *context,
|
|
|
|
struct lws *wsi, struct lws_pollfd *pfd)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|