alfred: Stabilize synchronization period using timerfd
PATCH 1/2 alfred: Stabilize synchronization period using timerfd
The current way of scheduling the synchronization related events tends to cause drift from a perfect periodic timer. This happens because it doesn't calculate the next event based on a fixed start time + period * the number of past synchronization periods. Instead, the next event was scheduled based on the time before the last select() - ignoring that non-zero time was spend processing events.
For a 10 second period, this usually looks somthing like:
[24.043904208] announce primary ... [34.044216187] announce primary ... [44.053485658] announce primary ... [54.063562062] announce primary ... [64.073517069] announce primary ...
To avoid this drift, just use timerfd as rather stable periodic timer event sources. It also has the benefit of making it easier to use multiple periodic timers with different periods.
Only some small jitter can be seen with this external timer implementation:
[12.673756426] announce primary ... [22.673779811] announce primary ... [32.673778362] announce primary ... [42.673775216] announce primary ...
--- alfred.h | 2 ++ server.c | 89 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 58 insertions(+), 33 deletions(-)
alfred.h +2 -0
@@ -124,6 +124,8 @@ struct globals { uint8_t ipv4mode:1; uint8_t force:1; +int check_timerfd; +int unix_sock;const char *unix_path;
server.c +56 -33
135 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 2/2 alfred: Switch to epoll for socket handling
The select syscall has various problems when working with many sockets:
* only a fd less than FD_SETSIZE (usually 1024) is supported
* it is necessary to register all relevant fds manually before each select call
* when events are received, all the fds must be checked again the returned data
This can be solved by using epoll. File descriptors are only registered once and only the relevant file descriptors are returned. And epoll will also take care of walking in a round-robin fashion through all relevant file descriptors in case a lot of file descriptors could be returned by the kernel.
--- alfred.h | 20 ++++-- batadv_querynl.c | 4 -- epoll_handle.h | 25 +++++++ netsock.c | 171 ++++++++++++++++++++++++++--------------------- server.c | 127 ++++++++++++++++++++--------------- unix_sock.c | 59 ++++++++++------ 6 files changed, 243 insertions(+), 163 deletions(-) create mode 100644 epoll_handle.h
alfred.h +14 -6
@@ -16,9 +16,10 @@#include <stdbool.h> #include <stdint.h> #include <time.h> -#include <sys/select.h> +#include <sys/epoll.h> #include <sys/types.h> #include "bitops.h" +#include "epoll_handle.h" #include "list.h" #include "packet.h"
@@ -30,6 +31,10 @@#define ALFRED_SOCK_PATH_DEFAULT "/var/run/alfred.sock"#define NO_FILTER -1 +#ifndef __unused +#define __unused __attribute__((unused)) +#endif +#define FIXED_TLV_LEN (__tlv_type) \ htons(sizeof(__tlv_type) - sizeof((__tlv_type).header))
@@ -101,9 +106,12 @@ struct interface {alfred_addr address;uint32_t scope_id;char *interface; +int netsock;int netsock_mcast; -int netsock_arp; + +struct epoll_handle netsock_epoll; +struct epoll_handle netsock_mcast_epoll;struct hashtable_t *server_hash;
@@ -124,9 +132,13 @@ struct globals { uint8_t ipv4mode:1; uint8_t force:1; +int epollfd; +int check_timerfd; +struct epoll_handle check_epoll;int unix_sock; +struct epoll_handle unix_epoll;const char *unix_path;const char *update_command;
@@ -182,7 +194,6 @@ int sync_data(struct globals *globals);ssize_t send_alfred_packet (struct globals *globals,struct interface *interface,const alfred_addr *dest,void *buf,int length);/* unix_sock.c */ -int unix_sock_read (struct globals *globals);int unix_sock_open_daemon (struct globals *globals);int unix_sock_open_client (struct globals *globals);int unix_sock_close (struct globals *globals);
@@ -197,9 +208,6 @@ void netsock_close_all(struct globals *globals);int netsock_set_interfaces (struct globals *globals,char *interfaces);struct interface *netsock_first_interface (struct globals *globals);void netsock_reopen (struct globals *globals); -int netsock_prepare_select (struct globals *globals,fd_set *fds,int maxsock); -void netsock_check_error (struct globals *globals,fd_set *errfds); -int netsock_receive_packet (struct globals *globals,fd_set *fds);int netsock_own_address (const struct globals *globals,const alfred_addr *address);/* util.c */
batadv_querynl.c +0 -4
@@ -26,10 +26,6 @@#include "batadv_query.h" #include "netlink.h" -#ifndef __unused -#define __unused __attribute__((unused)) -#endif -static const int translate_mac_netlink_mandatory[] = {BATADV_ATTR_TT_ADDRESS ,BATADV_ATTR_ORIG_ADDRESS ,
epoll_handle.h new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) B.A.T.M.A.N. contributors: +* +* Sven Eckelmann +* +* License-Filename: LICENSES/preferred/GPL-2.0 +*/ + +#ifndef _ALFRED_EPOLL_HANDLE_H +#define _ALFRED_EPOLL_HANDLE_H + +#include <sys/epoll.h> + +struct globals ; +struct epoll_handle ; + +typedef void (*epoll_handler )(struct globals *globals, +struct epoll_handle *handle, +struct epoll_event *ev); + +struct epoll_handle { +epoll_handler handler; +}; + +#endif /* _ALFRED_EPOLL_HANDLE_H */
netsock.c +95 -76
228 lines not on this page · raw message
server.c +72 -55
165 lines not on this page · raw message
unix_sock.c +37 -22
116 lines not on this page · raw message
Signature · 1 line
2.30.2