Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2053598ybl; Sun, 19 Jan 2020 17:52:18 -0800 (PST) X-Google-Smtp-Source: APXvYqxRY++zuWBgHHdVYp31AUSqGyZ6+yuk0rlERfZV7NuuHzzU/lTOPmzzsrTycARUjsI0IsQn X-Received: by 2002:aca:e084:: with SMTP id x126mr11298216oig.97.1579485138409; Sun, 19 Jan 2020 17:52:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579485138; cv=none; d=google.com; s=arc-20160816; b=DgTQx51obauYnXatHE6DTbiljLwzhTrpmdRm94PGfk4g1/GTzvV7syrwIM+KEGphUY 3qB2jaKbFnY9h5NBaS476uPiWfBWg1HPKfbU83IlQ0EMVudTLcfKHUWMCR45iPrGZlxt ToyMiS6asWawVcoHERyeP8akEyW3vGGJfp6ceupUjNy5IhtzG3yQkadpv678mM4uKrYj gu1KMlW5sE58HmC7oRE5wn58YaeZjweX3+h9ZbMr3gydNbvxRZiGkQw7/7QXnt3G521b +3giEdBLs58XEag1NxB2asdUZB7jjBuonODEYGioHFWvz/aosECYynjgvtWu94eJcTwI 5Z9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=m2d8bxeyku054h74JhVHsqbotLKnuuxbTN+xE8A44Yg=; b=NYu2ZwuMPNUloqZOwBNAZu7gDNqBkvPrSRqpfSX8LsUxGLwWna39GL7T74Mtv0jyC8 SeYI9HZlLmkRqukfq9WjlrtCzF/OW27atp/7pgozs3BSp+unvBtumjbWXfq/Q6utByYj B345/PWAL6w17kkKvJm7k9EXO8hUAYRjlpF+JhUasluKQZiqMDgG9gYD28p2gR7ONATr D2j16PKMZwLdrYdpgU/ebWH2aLRFP/4Jq1/ajpOoxup1dpqUIH1nh6OEa57rG0Ys6XaQ knSfCsPVm8icykBofzNjehVKeEjHNKGV3DmcEYmAVMMI6oGEDfGea7tqHaQYkiC+DK3g h+7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g26si19755936otk.324.2020.01.19.17.52.04; Sun, 19 Jan 2020 17:52:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729008AbgATBvA (ORCPT + 99 others); Sun, 19 Jan 2020 20:51:00 -0500 Received: from out30-56.freemail.mail.aliyun.com ([115.124.30.56]:54554 "EHLO out30-56.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728874AbgATBvA (ORCPT ); Sun, 19 Jan 2020 20:51:00 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07484;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0To7hRgx_1579485053; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0To7hRgx_1579485053) by smtp.aliyun-inc.com(127.0.0.1); Mon, 20 Jan 2020 09:50:58 +0800 From: Hui Zhu To: fengguang.wu@intel.com, linux-kernel@vger.kernel.org Cc: Hui Zhu , Hui Zhu Subject: [PATCH for vm-scalability] usemem: Add new option --fragmentation for generating fragmented pages Date: Mon, 20 Jan 2020 09:50:51 +0800 Message-Id: <1579485051-19624-1-git-send-email-teawater@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This commit adds new option --fragmentation. usemem will generate size/2/pagesize fragmented pages with this option. Its implementation is to use madvise to release a page every other page. For example: usemem --fragment -s -1 400m Ideally, this command will generate 200m fragmented pages in the system. This command can help test anti-fragmentation function and other features that are affected by fragmentation issues of the Linux kernel. Signed-off-by: Hui Zhu --- usemem.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/usemem.c b/usemem.c index 9ac36e4..10e8874 100644 --- a/usemem.c +++ b/usemem.c @@ -95,6 +95,7 @@ int opt_sync_free = 0; int opt_bind_interval = 0; unsigned long opt_delay = 0; int opt_read_again = 0; +int opt_fragmentation = 0; int nr_task; int nr_thread; int nr_cpu; @@ -153,6 +154,7 @@ void usage(int ok) " -O|--anonymous mmap with MAP_ANONYMOUS\n" " -U|--hugetlb allocate hugetlbfs page\n" " -Z|--read-again read memory again after access the memory\n" + " --fragmentation generate size/2/pagesize fragmented pages\n" " -h|--help show this message\n" , ourname); @@ -191,6 +193,7 @@ static const struct option opts[] = { { "delay" , 1, NULL, 'e' }, { "hugetlb" , 0, NULL, 'U' }, { "read-again" , 0, NULL, 'Z' }, + { "fragmentation", 0, NULL, 0 }, { "help" , 0, NULL, 'h' }, { NULL , 0, NULL, 0 } }; @@ -655,6 +658,26 @@ static void timing_free(void *ptrs[], unsigned int nptr, static void wait_for_sigusr1(int signal) {} +static void do_fragmentation(void *addr, unsigned long len) +{ + unsigned long offset; + int should_free = 1; + + for (offset = 0; offset <= len - pagesize; offset += pagesize) { + if (should_free) { + if (madvise(addr + offset, pagesize, + MADV_DONTNEED) != 0) { + fprintf(stderr, + "msync failed with error %s\n", + strerror(errno)); + exit(1); + } + should_free = 0; + } else + should_free = 1; + } +} + long do_units(void) { struct drand48_data rand_data; @@ -752,6 +775,15 @@ long do_units(void) } } + if (opt_fragmentation) { + if (prealloc) + do_fragmentation(prealloc, opt_bytes); + else { + for (i = 0; i < nptr; i++) + do_fragmentation(ptrs[i], lens[i]); + } + } + while (sleep_secs) sleep_secs = sleep(sleep_secs); @@ -896,6 +928,7 @@ int do_tasks(void) int main(int argc, char *argv[]) { int c; + int opt_index = 0; #ifdef DBG /* print the command line parameters passed on to main */ @@ -910,9 +943,18 @@ int main(int argc, char *argv[]) pagesize = getpagesize(); while ((c = getopt_long(argc, argv, - "aAB:f:FPp:gqowRMm:n:t:b:ds:T:Sr:u:j:e:EHDNLWyxOUZh", opts, NULL)) != -1) + "aAB:f:FPp:gqowRMm:n:t:b:ds:T:Sr:u:j:e:EHDNLWyxOUZh", + opts, &opt_index)) != -1) { switch (c) { + case 0: + if (strcmp(opts[opt_index].name, + "fragmentation") == 0) { + opt_fragmentation = 1; + } else + usage(1); + break; + case 'a': opt_malloc++; break; @@ -1045,6 +1087,13 @@ int main(int argc, char *argv[]) } } + if (opt_fragmentation && opt_malloc) { + fprintf(stderr, + "%s: malloc options ignored for fragmentation\n", + ourname); + opt_malloc = 0; + } + if (opt_malloc) { if (map_populate|map_anonymous|map_hugetlb) fprintf(stderr, -- 2.7.4