Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1022542pxu; Wed, 16 Dec 2020 23:30:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJx1ZlVTnVzspkfY4q2FgPdtkwM30S1YJgn91hCyn4jhhoyokehc2dfDG3Y0lTNfej0pwQ1P X-Received: by 2002:a17:906:ae41:: with SMTP id lf1mr14628517ejb.369.1608190258447; Wed, 16 Dec 2020 23:30:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608190258; cv=none; d=google.com; s=arc-20160816; b=nd5BZEtbOY8hz7VVIvskKjOBFMR8hPm8j3HLBl1RJwOb71TSKkJ62eZpDrd4/XtGG/ PaO49fzfwwM6/bh6QcEqtMkXMJFrVf5NCoPgwgodvodI87ApTYhiTEvFgQz1LP/+W/VL 6DGidLT22xp1keE9Apm+LoYXIoW0LyI/0/cjU9+129UWEklcj6OnrZUqYbkFEIHET48f xTA+fgAgY288ql6M+jRm5adqAbQBiXJluOF7J57VjkfUhtdINcdaDjf1QEE50xnU+/0E 5whQKAerBPoGBoQPj8lQf1TlEG0jXMtmG1M3LrjGTzLnrAT245lPHwOvPa0Ug+fzIALI hTRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=YztRt1YmlhZF8jNQvjV3TCQ+jnSikd/X499NAtUBxeo=; b=vlHkIfX0TL9cYrgZQdzSvmgi5qGq7q9nooZmPV9CZfA1FElZXKJXO3ah4oeE7YFMh4 M3FcyJgUYimqEnzE47CW1kgupxBnP3H0aEKc+wR/HHRkabTVWf8e966MighHyVq0GVaa SBehJP6lLbUPoUlreHjgyd82yzjM8NjWZVQuWn9QNEk1Jx2h3XRegU6oDiDsTXIxpUUJ aocOdyc8H77eg1MH1mG8SSTpm829tfjsK8/8qf20PQC/Nw7CefPy784dz0fSGqePiniL I0Zra3uvm2Zae8hL2fZy9JC0NJcO5+Sbr+xYW/x7afIVJbIb09/JwO5vbEtfCHRm7n7B t2NA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id mh22si2260568ejb.506.2020.12.16.23.30.35; Wed, 16 Dec 2020 23:30:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726825AbgLQH1d (ORCPT + 99 others); Thu, 17 Dec 2020 02:27:33 -0500 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:36724 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726416AbgLQH10 (ORCPT ); Thu, 17 Dec 2020 02:27:26 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04420;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0UItlrWK_1608189998; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0UItlrWK_1608189998) by smtp.aliyun-inc.com(127.0.0.1); Thu, 17 Dec 2020 15:26:42 +0800 From: Hui Zhu To: wufengguang@huawei.com, linux-kernel@vger.kernel.org Cc: Hui Zhu , Hui Zhu Subject: [PATCH] usemem: Add option touch-alloc Date: Thu, 17 Dec 2020 15:26:31 +0800 Message-Id: <20201217072631.6440-1-teawater@gmail.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some environment will not fault in memory even if MAP_POPULATE is set. This commit add option touch-alloc to read memory after allocate it to make sure the pages is fault in. Signed-off-by: Hui Zhu --- usemem.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/usemem.c b/usemem.c index 6d1d575..d93691b 100644 --- a/usemem.c +++ b/usemem.c @@ -97,6 +97,7 @@ unsigned long opt_delay = 0; int opt_read_again = 0; int opt_punch_holes = 0; int opt_init_time = 0; +int opt_touch_alloc = 0; int nr_task; int nr_thread; int nr_cpu; @@ -157,6 +158,7 @@ void usage(int ok) " -Z|--read-again read memory again after access the memory\n" " --punch-holes free every other page after allocation\n" " --init-time remove the initialization time from the run time and show the initialization time\n" + " --touch-alloc read memory after allocate it\n" " -h|--help show this message\n" , ourname); @@ -197,6 +199,7 @@ static const struct option opts[] = { { "read-again" , 0, NULL, 'Z' }, { "punch-holes" , 0, NULL, 0 }, { "init-time" , 0, NULL, 0 }, + { "touch-alloc" , 0, NULL, 0 }, { "help" , 0, NULL, 'h' }, { NULL , 0, NULL, 0 } }; @@ -326,6 +329,18 @@ void detach(void) } } +unsigned long do_access(unsigned long *p, unsigned long idx, int read) +{ + volatile unsigned long *vp = p; + + if (read) + return vp[idx]; /* read data */ + else { + vp[idx] = idx; /* write data */ + return 0; + } +} + unsigned long * allocate(unsigned long bytes) { unsigned long *p; @@ -352,6 +367,14 @@ unsigned long * allocate(unsigned long bytes) p = (unsigned long *)ALIGN((unsigned long)p, pagesize - 1); } + if (opt_touch_alloc) { + unsigned long i; + unsigned long m = bytes / sizeof(*p); + + for (i = 0; i < m; i += 1) + do_access(p, i, 1); + } + return p; } @@ -433,18 +456,6 @@ void shm_unlock(int seg_id) shmctl(seg_id, SHM_UNLOCK, NULL); } -unsigned long do_access(unsigned long *p, unsigned long idx, int read) -{ - volatile unsigned long *vp = p; - - if (read) - return vp[idx]; /* read data */ - else { - vp[idx] = idx; /* write data */ - return 0; - } -} - #define NSEC_PER_SEC (1UL * 1000 * 1000 * 1000) long nsec_sub(long nsec1, long nsec2) @@ -950,6 +961,8 @@ int main(int argc, char *argv[]) opt_punch_holes = 1; } else if (strcmp(opts[opt_index].name, "init-time") == 0) { opt_init_time = 1; + } else if (strcmp(opts[opt_index].name, "touch-alloc") == 0) { + opt_touch_alloc = 1; } else usage(1); break; -- 2.17.1