Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2397677ybv; Mon, 24 Feb 2020 04:36:41 -0800 (PST) X-Google-Smtp-Source: APXvYqynwmx/qrYr2kItG+1vwLPoEDsNWB+GCeBVzTMP0SZUCVa+x4tcU2ABRR7Ptx4nH91Ws5fk X-Received: by 2002:a05:6808:658:: with SMTP id z24mr12498925oih.91.1582547801012; Mon, 24 Feb 2020 04:36:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582547801; cv=none; d=google.com; s=arc-20160816; b=mB7ei4lVZGPSWF2bSWGgPSgYDxBzrCUKKyywC6x2HyH1rgIafwnC8bGlx497weYr9D /vN5ZgNZrz7syah1pr0sqHEwe/BaLxHJeujq7BQoykzhoGl/syZpMKOKUq92wviA95eP 1OepLQ52caUSXUuc10Uvamv7vyhjvJFxxFcwnvfyYdqQWHC+D6ogUXxjFK94GIClBhor N+3eYqzqXIE0BcWgKvAqgLYiSLvvnUfb9sg77WFjyaRSpxrgTDT4hejZrvnSsu5PFPyy cWitI2BlhP79Cbj+Ua/PCrftve/NOk7On8hKM4vvo8kFnyn5tWl0wTDqD2mAHmoHB6Eq aJGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:dkim-signature; bh=bzF3/ExaKR/laCSZiJHUhatde1k96n9iNoY6ahBp1FM=; b=1E3QjdEujButkjldDLktNvDeBk6b0oKPqjJVWby426tvN7zL98zASxgcadAkVre258 ebE2QBNiVa8jbt3cWFunUPJxqxDxW+Zwwgtp95llhcoIBSkZaDJEiGsElq2dh0Uhvkj9 DuehFmWMB53x7kcUC9bbDvqJaq5OeOeC2y5DTe7ty2USsaO5W27tF1tCkiPGET7B4naY bAUtro+wINpHzHqMWPIZw48GV6ootOSEqrPJRcrrU1VVlcsTo4/mnPUsTHbNq3z6bBt3 ECDkatLoAkB+x/cf5/LAUmximiB2OYe7JFEYR7rSR0zc36FvlQcvyuYt8c95zvTso4oY Echw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=EgV0BLMO; 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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n14si6251920otk.179.2020.02.24.04.36.29; Mon, 24 Feb 2020 04:36:40 -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; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=EgV0BLMO; 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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727450AbgBXMgV (ORCPT + 99 others); Mon, 24 Feb 2020 07:36:21 -0500 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:62544 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727351AbgBXMgU (ORCPT ); Mon, 24 Feb 2020 07:36:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1582547781; x=1614083781; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=bzF3/ExaKR/laCSZiJHUhatde1k96n9iNoY6ahBp1FM=; b=EgV0BLMOX6sTkmfxWvSsMcMhOv4RoE4Vk0F/jQno5naDQpVBa8NYRF9h BDGoJ7lTwjj4qpQtbML85LxlQSUcnNQretUnsrHR9ZtbeCBQq1x5BR7CA 0M+ImTTNTQBJu5mdSvH8B74VaKpBC3p0jHJloKxX2PQ5S+ks5YlrdTvHG Q=; IronPort-SDR: rKyAEm+BRwMHieATIMoVrmFibFzK2q6N8MRD7R9QpBStf86PxfIXDNNcdyJDH+HZnesWuS82Ed lfz97yIdHB1g== X-IronPort-AV: E=Sophos;i="5.70,480,1574121600"; d="scan'208";a="18696855" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-4e24fd92.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 24 Feb 2020 12:36:20 +0000 Received: from EX13MTAUEA002.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-4e24fd92.us-west-2.amazon.com (Postfix) with ESMTPS id 7E3B1A21E3; Mon, 24 Feb 2020 12:36:17 +0000 (UTC) Received: from EX13D31EUA001.ant.amazon.com (10.43.165.15) by EX13MTAUEA002.ant.amazon.com (10.43.61.77) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Mon, 24 Feb 2020 12:36:17 +0000 Received: from u886c93fd17d25d.ant.amazon.com (10.43.162.53) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Mon, 24 Feb 2020 12:36:05 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 13/14] mm/damon: Add user selftests Date: Mon, 24 Feb 2020 13:30:46 +0100 Message-ID: <20200224123047.32506-14-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200224123047.32506-1-sjpark@amazon.com> References: <20200224123047.32506-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.53] X-ClientProxiedBy: EX13D19UWC003.ant.amazon.com (10.43.162.184) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: SeongJae Park This commit adds a simple user space tests for DAMON. The tests are using kselftest framework. Signed-off-by: SeongJae Park --- tools/testing/selftests/damon/Makefile | 7 + .../selftests/damon/_chk_dependency.sh | 28 ++++ tools/testing/selftests/damon/_chk_record.py | 89 +++++++++++ .../testing/selftests/damon/debugfs_attrs.sh | 139 ++++++++++++++++++ .../testing/selftests/damon/debugfs_record.sh | 50 +++++++ 5 files changed, 313 insertions(+) create mode 100644 tools/testing/selftests/damon/Makefile create mode 100644 tools/testing/selftests/damon/_chk_dependency.sh create mode 100644 tools/testing/selftests/damon/_chk_record.py create mode 100755 tools/testing/selftests/damon/debugfs_attrs.sh create mode 100755 tools/testing/selftests/damon/debugfs_record.sh diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile new file mode 100644 index 000000000000..cfd5393a4639 --- /dev/null +++ b/tools/testing/selftests/damon/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for damon selftests + +TEST_FILES = _chk_dependency.sh _chk_record_file.py +TEST_PROGS = debugfs_attrs.sh debugfs_record.sh + +include ../lib.mk diff --git a/tools/testing/selftests/damon/_chk_dependency.sh b/tools/testing/selftests/damon/_chk_dependency.sh new file mode 100644 index 000000000000..814dcadd5e96 --- /dev/null +++ b/tools/testing/selftests/damon/_chk_dependency.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + +DBGFS=/sys/kernel/debug/damon + +if [ $EUID -ne 0 ]; +then + echo "Run as root" + exit $ksft_skip +fi + +if [ ! -d $DBGFS ] +then + echo "$DBGFS not found" + exit $ksft_skip +fi + +for f in attrs record pids monitor_on +do + if [ ! -f "$DBGFS/$f" ] + then + echo "$f not found" + exit 1 + fi +done diff --git a/tools/testing/selftests/damon/_chk_record.py b/tools/testing/selftests/damon/_chk_record.py new file mode 100644 index 000000000000..ef55f478c2af --- /dev/null +++ b/tools/testing/selftests/damon/_chk_record.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +"Check whether the DAMON record file is valid" + +import argparse +import struct +import sys + +def err_percent(val, expected): + return abs(val - expected) / expected * 100 + +def chk_task_info(f): + pid = struct.unpack('L', f.read(8))[0] + nr_regions = struct.unpack('I', f.read(4))[0] + + if nr_regions > max_nr_regions: + print('too many regions: %d > %d' % (nr_regions, max_nr_regions)) + exit(1) + + nr_gaps = 0 + eaddr = 0 + for r in range(nr_regions): + saddr = struct.unpack('L', f.read(8))[0] + if eaddr and saddr != eaddr: + nr_gaps += 1 + eaddr = struct.unpack('L', f.read(8))[0] + nr_accesses = struct.unpack('I', f.read(4))[0] + + if saddr >= eaddr: + print('wrong region [%d,%d)' % (saddr, eaddr)) + exit(1) + + max_nr_accesses = aint / sint + if nr_accesses > max_nr_accesses: + if err_percent(nr_accesses, max_nr_accesses) > 15: + print('too high nr_access: expected %d but %d' % + (max_nr_accesses, nr_accesses)) + exit(1) + if nr_gaps != 2: + print('number of gaps are not two but %d' % nr_gaps) + exit(1) + +def parse_time_us(bindat): + sec = struct.unpack('l', bindat[0:8])[0] + nsec = struct.unpack('l', bindat[8:16])[0] + return (sec * 1000000000 + nsec) / 1000 + +def main(): + global sint + global aint + global min_nr + global max_nr_regions + + parser = argparse.ArgumentParser() + parser.add_argument('file', metavar='', + help='path to the record file') + parser.add_argument('--attrs', metavar='', + default='5000 100000 1000000 10 1000', + help='content of debugfs attrs file') + args = parser.parse_args() + file_path = args.file + attrs = [int(x) for x in args.attrs.split()] + sint, aint, rint, min_nr, max_nr_regions = attrs + + with open(file_path, 'rb') as f: + last_aggr_time = None + while True: + timebin = f.read(16) + if len(timebin) != 16: + break + + now = parse_time_us(timebin) + if not last_aggr_time: + last_aggr_time = now + else: + error = err_percent(now - last_aggr_time, aint) + if error > 15: + print('wrong aggr interval: expected %d, but %d' % + (aint, now - last_aggr_time)) + exit(1) + last_aggr_time = now + + nr_tasks = struct.unpack('I', f.read(4))[0] + for t in range(nr_tasks): + chk_task_info(f) + +if __name__ == '__main__': + main() diff --git a/tools/testing/selftests/damon/debugfs_attrs.sh b/tools/testing/selftests/damon/debugfs_attrs.sh new file mode 100755 index 000000000000..d5188b0f71b1 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_attrs.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source ./_chk_dependency.sh + +# Test attrs file +file="$DBGFS/attrs" + +ORIG_CONTENT=$(cat $file) + +echo 1 2 3 4 5 > $file +if [ $? -ne 0 ] +then + echo "$file write failed" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo 1 2 3 4 > $file +if [ $? -eq 0 ] +then + echo "$file write success (should failed)" + echo $ORIG_CONTENT > $file + exit 1 +fi + +CONTENT=$(cat $file) +if [ "$CONTENT" != "1 2 3 4 5" ] +then + echo "$file not written" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo $ORIG_CONTENT > $file + +# Test record file +file="$DBGFS/record" + +ORIG_CONTENT=$(cat $file) + +echo "4242 foo.bar" > $file +if [ $? -ne 0 ] +then + echo "$file writing sane input failed" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo abc 2 3 > $file +if [ $? -eq 0 ] +then + echo "$file writing insane input 1 success (should failed)" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo 123 > $file +if [ $? -eq 0 ] +then + echo "$file writing insane input 2 success (should failed)" + echo $ORIG_CONTENT > $file + exit 1 +fi + +CONTENT=$(cat $file) +if [ "$CONTENT" != "4242 foo.bar" ] +then + echo "$file not written" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo "0 null" > $file +if [ $? -ne 0 ] +then + echo "$file disabling write fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + +CONTENT=$(cat $file) +if [ "$CONTENT" != "0 null" ] +then + echo "$file not disabled" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo "4242 foo.bar" > $file +if [ $? -ne 0 ] +then + echo "$file writing sane data again fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo $ORIG_CONTENT > $file + +# Test pids file +file="$DBGFS/pids" + +ORIG_CONTENT=$(cat $file) + +echo "1 2 3 4" > $file +if [ $? -ne 0 ] +then + echo "$file write fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo "1 2 abc 4" > $file +if [ $? -ne 0 ] +then + echo "$file write fail" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo abc 2 3 > $file +if [ $? -eq 0 ] +then + echo "$file write success (should failed)" + echo $ORIG_CONTENT > $file + exit 1 +fi + +CONTENT=$(cat $file) +if [ "$CONTENT" != "1 2" ] +then + echo "$file not written" + echo $ORIG_CONTENT > $file + exit 1 +fi + +echo $ORIG_CONTENT > $file + +echo "PASS" diff --git a/tools/testing/selftests/damon/debugfs_record.sh b/tools/testing/selftests/damon/debugfs_record.sh new file mode 100755 index 000000000000..fa9e07eea258 --- /dev/null +++ b/tools/testing/selftests/damon/debugfs_record.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source ./_chk_dependency.sh + +restore_attrs() +{ + echo $ORIG_ATTRS > $DBGFS/attrs + echo $ORIG_PIDS > $DBGFS/pids + echo $ORIG_RECORD > $DBGFS/record +} + +ORIG_ATTRS=$(cat $DBGFS/attrs) +ORIG_PIDS=$(cat $DBGFS/pids) +ORIG_RECORD=$(cat $DBGFS/record) + +rfile=$pwd/damon.data + +rm -f $rfile +ATTRS="5000 100000 1000000 10 1000" +echo $ATTRS > $DBGFS/attrs +echo 4096 $rfile > $DBGFS/record +sleep 5 & +echo $(pidof sleep) > $DBGFS/pids +echo on > $DBGFS/monitor_on +sleep 0.5 +killall sleep +echo off > $DBGFS/monitor_on + +sync + +if [ ! -f $rfile ] +then + echo "record file not made" + restore_attrs + + exit 1 +fi + +python3 ./_chk_record.py $rfile --attrs "$ATTRS" +if [ $? -ne 0 ] +then + echo "record file is wrong" + restore_attrs + exit 1 +fi + +rm -f $rfile +restore_attrs +echo "PASS" -- 2.17.1