Skip to content

Commit fe60b86

Browse files
committed
sockets add SO_MEMINFO data gathering which allows to check
socket memory consumption and returning those in php friendly map.
1 parent 7061c40 commit fe60b86

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

ext/sockets/sockets.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
# if HAVE_IF_NAMETOINDEX
5656
# include <net/if.h>
5757
# endif
58+
# ifdef SO_MEMINFO
59+
# include <linux/sock_diag.h>
60+
# endif
5861
#endif
5962

6063
#include <stddef.h>
@@ -550,6 +553,9 @@ static PHP_MINIT_FUNCTION(sockets)
550553
#ifdef SO_INCOMING_CPU
551554
REGISTER_LONG_CONSTANT("SO_INCOMING_CPU", SO_INCOMING_CPU, CONST_CS | CONST_PERSISTENT);
552555
#endif
556+
#ifdef SO_MEMINFO
557+
REGISTER_LONG_CONSTANT("SO_MEMINFO", SO_MEMINFO, CONST_CS | CONST_PERSISTENT);
558+
#endif
553559
#ifdef TCP_NODELAY
554560
REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
555561
#endif
@@ -1833,6 +1839,37 @@ PHP_FUNCTION(socket_get_option)
18331839
add_assoc_long(return_value, "sec", tv.tv_sec);
18341840
add_assoc_long(return_value, "usec", tv.tv_usec);
18351841
return;
1842+
#ifdef SO_MEMINFO
1843+
case SO_MEMINFO: {
1844+
uint32_t minfo[SK_MEMINFO_VARS];
1845+
optlen = sizeof(minfo);
1846+
1847+
if (getsockopt(php_sock->bsd_socket, level, optname, (char*)minfo, &optlen) != 0) {
1848+
PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
1849+
RETURN_FALSE;
1850+
}
1851+
1852+
if (UNEXPECTED(optlen != sizeof(minfo))) {
1853+
// unlikely since the kernel fills up the whole array if getsockopt succeeded
1854+
// but just an extra precaution in case.
1855+
php_error_docref(NULL, E_WARNING, "Unable to retrieve all socket meminfo data");
1856+
RETURN_FALSE;
1857+
}
1858+
1859+
array_init(return_value);
1860+
1861+
add_assoc_long(return_value, "rmem_alloc", minfo[SK_MEMINFO_RMEM_ALLOC]);
1862+
add_assoc_long(return_value, "rcvbuf", minfo[SK_MEMINFO_RCVBUF]);
1863+
add_assoc_long(return_value, "wmem_alloc", minfo[SK_MEMINFO_WMEM_ALLOC]);
1864+
add_assoc_long(return_value, "sndbuf", minfo[SK_MEMINFO_SNDBUF]);
1865+
add_assoc_long(return_value, "fwd_alloc", minfo[SK_MEMINFO_FWD_ALLOC]);
1866+
add_assoc_long(return_value, "wmem_queued", minfo[SK_MEMINFO_WMEM_QUEUED]);
1867+
add_assoc_long(return_value, "optmem", minfo[SK_MEMINFO_OPTMEM]);
1868+
add_assoc_long(return_value, "backlog", minfo[SK_MEMINFO_BACKLOG]);
1869+
add_assoc_long(return_value, "drops", minfo[SK_MEMINFO_DROPS]);
1870+
return;
1871+
}
1872+
#endif
18361873
#ifdef SO_ACCEPTFILTER
18371874
case SO_ACCEPTFILTER: {
18381875

ext/sockets/tests/socket_meminfo.phpt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
socket_get_option MEMINFO;
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
if (!defined("SO_MEMINFO")) die('skip SO_MEMINFO test');
8+
?>
9+
--FILE--
10+
<?php
11+
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
12+
if (!$server) {
13+
die('Unable to create AF_INET socket [server]');
14+
}
15+
16+
if (!socket_bind($server, '127.0.0.1', 0)) {
17+
die("Unable to bind to 127.0.0.1");
18+
}
19+
20+
if (!socket_listen($server, 2)) {
21+
die('Unable to listen on socket');
22+
}
23+
24+
socket_getsockname($server, $unused, $port);
25+
26+
/* Connect to it */
27+
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
28+
if (!$client) {
29+
die('Unable to create AF_INET socket [client]');
30+
}
31+
if (!socket_connect($client, '127.0.0.1', $port)) {
32+
die('Unable to connect to server socket');
33+
}
34+
35+
/* Accept that connection */
36+
$socket = socket_accept($server);
37+
if (!$socket) {
38+
die('Unable to accept connection');
39+
}
40+
41+
socket_write($client, "SO_MEMINFO\n");
42+
43+
socket_read($socket, strlen("SO_MEMINFO"), PHP_BINARY_READ);
44+
$data = socket_get_option($socket, SOL_SOCKET, SO_MEMINFO);
45+
var_dump($data);
46+
47+
socket_close($client);
48+
socket_close($socket);
49+
socket_close($server);
50+
?>
51+
--EXPECTF--
52+
array(9) {
53+
["rmem_alloc"]=>
54+
int(%d)
55+
["rcvbuf"]=>
56+
int(%d)
57+
["wmem_alloc"]=>
58+
int(%d)
59+
["sndbuf"]=>
60+
int(%d)
61+
["fwd_alloc"]=>
62+
int(%d)
63+
["wmem_queued"]=>
64+
int(%d)
65+
["optmem"]=>
66+
int(%d)
67+
["backlog"]=>
68+
int(%d)
69+
["drops"]=>
70+
int(%d)
71+
}

0 commit comments

Comments
 (0)