mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Shuah Khan <shuah@kernel.org>, Tejun Heo <tj@kernel.org>,
	<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] selftests: cgroup: memcontrol: add basic test for socket accounting
Date: Wed, 16 May 2018 15:04:08 +0100	[thread overview]
Message-ID: <20180516140332.GA19754@castle.DHCP.thefacebook.com> (raw)
In-Reply-To: <1526470089-27010-1-git-send-email-rppt@linux.vnet.ibm.com>

Hi Mike!

On Wed, May 16, 2018 at 02:28:09PM +0300, Mike Rapoport wrote:
> The test verifies that with active TCP traffic memory.current and
> memory.stat.sock have similar values.
> 
> Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
> ---
>  tools/testing/selftests/cgroup/test_memcontrol.c | 184 +++++++++++++++++++++++
>  1 file changed, 184 insertions(+)
> 
> diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
> index beae06c9c899..0efdb1009175 100644
> --- a/tools/testing/selftests/cgroup/test_memcontrol.c
> +++ b/tools/testing/selftests/cgroup/test_memcontrol.c
> @@ -9,6 +9,12 @@
>  #include <sys/stat.h>
>  #include <sys/types.h>
>  #include <unistd.h>
> +#include <sys/socket.h>
> +#include <sys/wait.h>
> +#include <arpa/inet.h>
> +#include <netinet/in.h>
> +#include <netdb.h>
> +#include <errno.h>
>  
>  #include "../kselftest.h"
>  #include "cgroup_util.h"
> @@ -772,6 +778,183 @@ static int test_memcg_oom_events(const char *root)
>  	return ret;
>  }
>  
> +struct tcp_server_args {
> +	unsigned short port;
> +	int ctl[2];
> +};
> +
> +static int tcp_server(const char *cgroup, void *arg)
> +{
> +	struct tcp_server_args *srv_args = arg;
> +	struct sockaddr_in6 saddr = { 0 };
> +	socklen_t slen = sizeof(saddr);
> +	int sk, client_sk, ctl_fd, yes = 1, ret = -1;
> +
> +	close(srv_args->ctl[0]);
> +	ctl_fd = srv_args->ctl[1];
> +
> +	saddr.sin6_family = AF_INET6;
> +	saddr.sin6_addr = in6addr_any;
> +	saddr.sin6_port = htons(srv_args->port);
> +
> +	sk = socket(AF_INET6, SOCK_STREAM, 0);
> +	if (sk < 0)
> +		return ret;
> +
> +	if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
> +		goto cleanup;
> +
> +	if (bind(sk, (struct sockaddr *)&saddr, slen)) {
> +		write(ctl_fd, &errno, sizeof(errno));
> +		goto cleanup;
> +	}
> +
> +	if (listen(sk, 1))
> +		goto cleanup;
> +
> +	ret = 0;
> +	if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
> +		ret = -1;
> +		goto cleanup;
> +	}
> +
> +	client_sk = accept(sk, NULL, NULL);
> +	if (client_sk < 0)
> +		goto cleanup;
> +
> +	ret = -1;
> +	for (;;) {
> +		uint8_t buf[0x100000];
> +
> +		if (write(client_sk, buf, sizeof(buf)) <= 0) {
> +			if (errno == ECONNRESET)
> +				ret = 0;
> +			break;
> +		}
> +	}
> +
> +	close(client_sk);
> +
> +cleanup:
> +	close(sk);
> +	return ret;
> +}
> +
> +static int tcp_client(const char *cgroup, unsigned short port)
> +{
> +	const char server[] = "localhost";
> +	struct addrinfo *ai;
> +	char servport[6];
> +	int retries = 0x10; /* nice round number */
> +	int sk, ret;
> +
> +	snprintf(servport, sizeof(servport), "%hd", port);
> +	ret = getaddrinfo(server, servport, NULL, &ai);
> +	if (ret)
> +		return ret;
> +
> +	sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
> +	if (sk < 0)
> +		goto free_ainfo;
> +
> +	ret = connect(sk, ai->ai_addr, ai->ai_addrlen);
> +	if (ret < 0)
> +		goto close_sk;
> +
> +	ret = KSFT_FAIL;
> +	while (retries--) {
> +		uint8_t buf[0x100000];
> +		long current, sock;
> +
> +		if (read(sk, buf, sizeof(buf)) <= 0)
> +			goto close_sk;
> +
> +		current = cg_read_long(cgroup, "memory.current");
> +		if (current < 0)
> +			goto close_sk;
> +
> +		sock = cg_read_key_long(cgroup, "memory.stat", "sock ");
> +		if (sock < 0)
> +			goto close_sk;
> +
> +		if (values_close(current, sock, 3)) {
> +			ret = KSFT_PASS;
> +			break;
> +		}

The test is flapping (at least on my dev machine) because of this condition.

I believe it's because of the batching we're using on the page charge path.
So, in theory, it should be possible to calculate the maximum difference
like num_cpus * PAGE_SIZE * batch_size.

Alternatively, just bump allowed error percentage :)

> +	}
> +
> +close_sk:
> +	close(sk);

It would be great to check that sock and current are getting 0 values
after we're closing the socket.


Thanks!

  reply	other threads:[~2018-05-16 14:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-16 11:28 Mike Rapoport
2018-05-16 14:04 ` Roman Gushchin [this message]
2018-05-16 14:48   ` Mike Rapoport
2018-05-16 16:04     ` Roman Gushchin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180516140332.GA19754@castle.DHCP.thefacebook.com \
    --to=guro@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=rppt@linux.vnet.ibm.com \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®