-
Notifications
You must be signed in to change notification settings - Fork 13
/
slowpoll.c
69 lines (59 loc) · 2.34 KB
/
slowpoll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* vim: set ts=8 sw=4 sts=4 et: */
/*======================================================================
Copyright (C) 2014 OSSO B.V. <[email protected]>
This file is part of RTPSniff.
RTPSniff is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
RTPSniff 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 General Public License
for more details.
You should have received a copy of the GNU General Public License along
with RTPSniff. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
/*
* WHAT
*
* This module adds a 500ms sleep to every poll call.
*
* HOW
*
* By replacing the poll library call with a custom one that calls
* usleep first. For temporary effect, it can be LD_PRELOADed, or for
* more permanent effect, it must be linked *before* libpcap.
*
* WHY
*
* Libpcap uses PACKET_RX_RING to capture packets into a ring buffer,
* if they pass the SO_ATTACH_FILTER. The pcap loop then polls for
* new packets on the socket. Since RTP flows a lot, this means that
* poll returns almost immediately. And that results in a huge
* performance drop because of all the dummy (directly returning) poll
* calls.
*
* By sleeping for half a second, we allow the buffer to fill up
* before we start looking at the results. If you ensure that the
* buffer is sufficiently large, this causes no trouble.
*
* Compile:
* gcc -D_GNU_SOURCE -fPIC -ldl -shared -o libslowpoll.so slowpoll.c
* Usage:
* LD_PRELOAD=./libslowpoll.so app-to-run
* or:
* gcc app-to-link.o -lslowpoll -lpcap -o app-to-link
*/
#include <dlfcn.h>
#include <poll.h>
#include <unistd.h>
int libslowpoll_wait; /* a global.. feel free to import/change */
static int (*real_poll)(struct pollfd *, nfds_t, int);
__attribute__((constructor)) void init() {
real_poll = dlsym(RTLD_NEXT, "poll");
libslowpoll_wait = 262144; /* 262ms == 0x40000us */
}
int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
usleep(libslowpoll_wait);
return real_poll(fds, nfds, timeout);
}