public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Zhao Gongyi <zhaogongyi@huawei.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2 3/3] syscalls/madvise11: new test for madvise(MADV_DONTNEED)
Date: Wed, 12 Oct 2022 10:52:49 +0200	[thread overview]
Message-ID: <Y0aAYQ+C1aTfQMRv@yuki> (raw)
In-Reply-To: <20221011121607.55575-4-zhaogongyi@huawei.com>

Hi!
> +static void run(void)
> +{
> +	char cmd[BUF_SIZE];
> +	char line[BUF_SIZE];
> +	char vm_area_addr[128];
> +
> +	TEST(madvise(addr, MAP_SIZE, MADV_DONTNEED));
> +	if (TST_RET == -1) {
> +		tst_brk(TBROK | TTERRNO, "madvise(%p, %d, 0x%x)",
> +			addr, MAP_SIZE, MADV_DONTNEED);
> +	}
> +
> +	sprintf(vm_area_addr, "%p", addr);
> +	sprintf(cmd,
> +		"cat /proc/%d/smaps | grep %s -A 4 | grep Rss: | grep '0 kB'",
> +		getpid(), &(vm_area_addr[2]));

This is way too ugly and may break easily too.

If we are going to parse the file we should do it properly in C instead.
Why can't we just read the file line by line until we find the right
address at the start of the line and once we do look for the Rss?

> +	fp = popen(cmd, "r");
> +	if (!fp)
> +		tst_brk(TBROK, "popen failed");
> +
> +	if (fgets(line, sizeof(line), fp) != NULL) {
> +		if (strstr(line, " 0 kB"))
> +			tst_res(TPASS, "RSS is released");
> +		else
> +			tst_res(TFAIL, "RSS is not released");
> +
> +	} else
> +		tst_brk(TBROK, "There is no 'Rss:' in smaps?");
> +}
> +
> +static void setup(void)
> +{
> +	addr = SAFE_MMAP(NULL, MAP_SIZE,
> +			PROT_READ | PROT_WRITE,
> +			MAP_PRIVATE | MAP_ANONYMOUS,
> +			-1, 0);
> +	memset(addr, 1, MAP_SIZE);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (addr)
> +		SAFE_MUNMAP(addr, MAP_SIZE);
> +	if (fp)
> +		pclose(fp);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +};
> +
> --
> 2.17.1
> 

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2022-10-12  8:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11 12:16 [LTP] [PATCH v2 0/3] new test for madvise(MADV_DONTNEED) Zhao Gongyi via ltp
2022-10-11 12:16 ` [LTP] [PATCH v2 1/3] syscalls/madvise03: " Zhao Gongyi via ltp
2022-10-11 14:25   ` Cyril Hrubis
2022-10-11 12:16 ` [LTP] [PATCH v2 2/3] syscalls/madvise04: " Zhao Gongyi via ltp
2022-10-11 14:29   ` Cyril Hrubis
2022-10-11 12:16 ` [LTP] [PATCH v2 3/3] syscalls/madvise11: " Zhao Gongyi via ltp
2022-10-12  8:52   ` Cyril Hrubis [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-10-12  8:56 zhaogongyi via ltp

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=Y0aAYQ+C1aTfQMRv@yuki \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=zhaogongyi@huawei.com \
    /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