xref: /linux/tools/testing/selftests/damon/sysfs.sh (revision 06d07429858317ded2db7986113a9e0129cd599b)
140184e48SSeongJae Park#!/bin/bash
240184e48SSeongJae Park# SPDX-License-Identifier: GPL-2.0
340184e48SSeongJae Park
440184e48SSeongJae Park# Kselftest frmework requirement - SKIP code is 4.
540184e48SSeongJae Parkksft_skip=4
640184e48SSeongJae Park
740184e48SSeongJae Parkensure_write_succ()
840184e48SSeongJae Park{
940184e48SSeongJae Park	file=$1
1040184e48SSeongJae Park	content=$2
1140184e48SSeongJae Park	reason=$3
1240184e48SSeongJae Park
1340184e48SSeongJae Park	if ! echo "$content" > "$file"
1440184e48SSeongJae Park	then
1540184e48SSeongJae Park		echo "writing $content to $file failed"
1640184e48SSeongJae Park		echo "expected success because $reason"
1740184e48SSeongJae Park		exit 1
1840184e48SSeongJae Park	fi
1940184e48SSeongJae Park}
2040184e48SSeongJae Park
2140184e48SSeongJae Parkensure_write_fail()
2240184e48SSeongJae Park{
2340184e48SSeongJae Park	file=$1
2440184e48SSeongJae Park	content=$2
2540184e48SSeongJae Park	reason=$3
2640184e48SSeongJae Park
2716ddcb15SSeongJae Park	if (echo "$content" > "$file") 2> /dev/null
2840184e48SSeongJae Park	then
2940184e48SSeongJae Park		echo "writing $content to $file succeed ($fail_reason)"
3040184e48SSeongJae Park		echo "expected failure because $reason"
3140184e48SSeongJae Park		exit 1
3240184e48SSeongJae Park	fi
3340184e48SSeongJae Park}
3440184e48SSeongJae Park
3540184e48SSeongJae Parkensure_dir()
3640184e48SSeongJae Park{
3740184e48SSeongJae Park	dir=$1
3840184e48SSeongJae Park	to_ensure=$2
3940184e48SSeongJae Park	if [ "$to_ensure" = "exist" ] && [ ! -d "$dir" ]
4040184e48SSeongJae Park	then
4140184e48SSeongJae Park		echo "$dir dir is expected but not found"
4240184e48SSeongJae Park		exit 1
4340184e48SSeongJae Park	elif [ "$to_ensure" = "not_exist" ] && [ -d "$dir" ]
4440184e48SSeongJae Park	then
4540184e48SSeongJae Park		echo "$dir dir is not expected but found"
4640184e48SSeongJae Park		exit 1
4740184e48SSeongJae Park	fi
4840184e48SSeongJae Park}
4940184e48SSeongJae Park
5040184e48SSeongJae Parkensure_file()
5140184e48SSeongJae Park{
5240184e48SSeongJae Park	file=$1
5340184e48SSeongJae Park	to_ensure=$2
5440184e48SSeongJae Park	permission=$3
5540184e48SSeongJae Park	if [ "$to_ensure" = "exist" ]
5640184e48SSeongJae Park	then
5740184e48SSeongJae Park		if [ ! -f "$file" ]
5840184e48SSeongJae Park		then
5940184e48SSeongJae Park			echo "$file is expected but not found"
6040184e48SSeongJae Park			exit 1
6140184e48SSeongJae Park		fi
6240184e48SSeongJae Park		perm=$(stat -c "%a" "$file")
6340184e48SSeongJae Park		if [ ! "$perm" = "$permission" ]
6440184e48SSeongJae Park		then
6540184e48SSeongJae Park			echo "$file permission: expected $permission but $perm"
6640184e48SSeongJae Park			exit 1
6740184e48SSeongJae Park		fi
6840184e48SSeongJae Park	elif [ "$to_ensure" = "not_exist" ] && [ -f "$dir" ]
6940184e48SSeongJae Park	then
7040184e48SSeongJae Park		echo "$file is not expected but found"
7140184e48SSeongJae Park		exit 1
7240184e48SSeongJae Park	fi
7340184e48SSeongJae Park}
7440184e48SSeongJae Park
7540184e48SSeongJae Parktest_range()
7640184e48SSeongJae Park{
7740184e48SSeongJae Park	range_dir=$1
7840184e48SSeongJae Park	ensure_dir "$range_dir" "exist"
7940184e48SSeongJae Park	ensure_file "$range_dir/min" "exist" 600
8040184e48SSeongJae Park	ensure_file "$range_dir/max" "exist" 600
8140184e48SSeongJae Park}
8240184e48SSeongJae Park
832b3ee3f6SSeongJae Parktest_tried_regions()
842b3ee3f6SSeongJae Park{
852b3ee3f6SSeongJae Park	tried_regions_dir=$1
862b3ee3f6SSeongJae Park	ensure_dir "$tried_regions_dir" "exist"
87b823cb08SSeongJae Park	ensure_file "$tried_regions_dir/total_bytes" "exist" "400"
882b3ee3f6SSeongJae Park}
892b3ee3f6SSeongJae Park
9040184e48SSeongJae Parktest_stats()
9140184e48SSeongJae Park{
9240184e48SSeongJae Park	stats_dir=$1
9340184e48SSeongJae Park	ensure_dir "$stats_dir" "exist"
9440184e48SSeongJae Park	for f in nr_tried sz_tried nr_applied sz_applied qt_exceeds
9540184e48SSeongJae Park	do
9640184e48SSeongJae Park		ensure_file "$stats_dir/$f" "exist" "400"
9740184e48SSeongJae Park	done
9840184e48SSeongJae Park}
9940184e48SSeongJae Park
100553b0142SSeongJae Parktest_filter()
101553b0142SSeongJae Park{
102553b0142SSeongJae Park	filter_dir=$1
103553b0142SSeongJae Park	ensure_file "$filter_dir/type" "exist" "600"
104553b0142SSeongJae Park	ensure_write_succ "$filter_dir/type" "anon" "valid input"
105553b0142SSeongJae Park	ensure_write_succ "$filter_dir/type" "memcg" "valid input"
1064c45c20dSSeongJae Park	ensure_write_succ "$filter_dir/type" "addr" "valid input"
1079628ace8SSeongJae Park	ensure_write_succ "$filter_dir/type" "target" "valid input"
108553b0142SSeongJae Park	ensure_write_fail "$filter_dir/type" "foo" "invalid input"
109553b0142SSeongJae Park	ensure_file "$filter_dir/matching" "exist" "600"
110553b0142SSeongJae Park	ensure_file "$filter_dir/memcg_path" "exist" "600"
1114c45c20dSSeongJae Park	ensure_file "$filter_dir/addr_start" "exist" "600"
1124c45c20dSSeongJae Park	ensure_file "$filter_dir/addr_end" "exist" "600"
1139628ace8SSeongJae Park	ensure_file "$filter_dir/damon_target_idx" "exist" "600"
114553b0142SSeongJae Park}
115553b0142SSeongJae Park
116553b0142SSeongJae Parktest_filters()
117553b0142SSeongJae Park{
118553b0142SSeongJae Park	filters_dir=$1
119553b0142SSeongJae Park	ensure_dir "$filters_dir" "exist"
120553b0142SSeongJae Park	ensure_file "$filters_dir/nr_filters" "exist" "600"
121553b0142SSeongJae Park	ensure_write_succ  "$filters_dir/nr_filters" "1" "valid input"
122553b0142SSeongJae Park	test_filter "$filters_dir/0"
123553b0142SSeongJae Park
124553b0142SSeongJae Park	ensure_write_succ  "$filters_dir/nr_filters" "2" "valid input"
125553b0142SSeongJae Park	test_filter "$filters_dir/0"
126553b0142SSeongJae Park	test_filter "$filters_dir/1"
127553b0142SSeongJae Park
128553b0142SSeongJae Park	ensure_write_succ "$filters_dir/nr_filters" "0" "valid input"
129553b0142SSeongJae Park	ensure_dir "$filters_dir/0" "not_exist"
130553b0142SSeongJae Park	ensure_dir "$filters_dir/1" "not_exist"
131553b0142SSeongJae Park}
132553b0142SSeongJae Park
13340184e48SSeongJae Parktest_watermarks()
13440184e48SSeongJae Park{
13540184e48SSeongJae Park	watermarks_dir=$1
13640184e48SSeongJae Park	ensure_dir "$watermarks_dir" "exist"
13740184e48SSeongJae Park	ensure_file "$watermarks_dir/metric" "exist" "600"
13840184e48SSeongJae Park	ensure_file "$watermarks_dir/interval_us" "exist" "600"
13940184e48SSeongJae Park	ensure_file "$watermarks_dir/high" "exist" "600"
14040184e48SSeongJae Park	ensure_file "$watermarks_dir/mid" "exist" "600"
14140184e48SSeongJae Park	ensure_file "$watermarks_dir/low" "exist" "600"
14240184e48SSeongJae Park}
14340184e48SSeongJae Park
14440184e48SSeongJae Parktest_weights()
14540184e48SSeongJae Park{
14640184e48SSeongJae Park	weights_dir=$1
14740184e48SSeongJae Park	ensure_dir "$weights_dir" "exist"
14840184e48SSeongJae Park	ensure_file "$weights_dir/sz_permil" "exist" "600"
14940184e48SSeongJae Park	ensure_file "$weights_dir/nr_accesses_permil" "exist" "600"
15040184e48SSeongJae Park	ensure_file "$weights_dir/age_permil" "exist" "600"
15140184e48SSeongJae Park}
15240184e48SSeongJae Park
153*3649caedSSeongJae Parktest_goal()
154*3649caedSSeongJae Park{
155*3649caedSSeongJae Park	goal_dir=$1
156*3649caedSSeongJae Park	ensure_dir "$goal_dir" "exist"
157*3649caedSSeongJae Park	ensure_file "$goal_dir/target_value" "exist" "600"
158*3649caedSSeongJae Park	ensure_file "$goal_dir/current_value" "exist" "600"
159*3649caedSSeongJae Park}
160*3649caedSSeongJae Park
161*3649caedSSeongJae Parktest_goals()
162*3649caedSSeongJae Park{
163*3649caedSSeongJae Park	goals_dir=$1
164*3649caedSSeongJae Park	ensure_dir "$goals_dir" "exist"
165*3649caedSSeongJae Park	ensure_file "$goals_dir/nr_goals" "exist" "600"
166*3649caedSSeongJae Park
167*3649caedSSeongJae Park	ensure_write_succ  "$goals_dir/nr_goals" "1" "valid input"
168*3649caedSSeongJae Park	test_goal "$goals_dir/0"
169*3649caedSSeongJae Park
170*3649caedSSeongJae Park	ensure_write_succ  "$goals_dir/nr_goals" "2" "valid input"
171*3649caedSSeongJae Park	test_goal "$goals_dir/0"
172*3649caedSSeongJae Park	test_goal "$goals_dir/1"
173*3649caedSSeongJae Park
174*3649caedSSeongJae Park	ensure_write_succ  "$goals_dir/nr_goals" "0" "valid input"
175*3649caedSSeongJae Park	ensure_dir "$goals_dir/0" "not_exist"
176*3649caedSSeongJae Park	ensure_dir "$goals_dir/1" "not_exist"
177*3649caedSSeongJae Park}
178*3649caedSSeongJae Park
17940184e48SSeongJae Parktest_quotas()
18040184e48SSeongJae Park{
18140184e48SSeongJae Park	quotas_dir=$1
18240184e48SSeongJae Park	ensure_dir "$quotas_dir" "exist"
18340184e48SSeongJae Park	ensure_file "$quotas_dir/ms" "exist" 600
18440184e48SSeongJae Park	ensure_file "$quotas_dir/bytes" "exist" 600
18540184e48SSeongJae Park	ensure_file "$quotas_dir/reset_interval_ms" "exist" 600
18640184e48SSeongJae Park	test_weights "$quotas_dir/weights"
187*3649caedSSeongJae Park	test_goals "$quotas_dir/goals"
18840184e48SSeongJae Park}
18940184e48SSeongJae Park
19040184e48SSeongJae Parktest_access_pattern()
19140184e48SSeongJae Park{
19240184e48SSeongJae Park	access_pattern_dir=$1
19340184e48SSeongJae Park	ensure_dir "$access_pattern_dir" "exist"
19440184e48SSeongJae Park	test_range "$access_pattern_dir/age"
19540184e48SSeongJae Park	test_range "$access_pattern_dir/nr_accesses"
19640184e48SSeongJae Park	test_range "$access_pattern_dir/sz"
19740184e48SSeongJae Park}
19840184e48SSeongJae Park
19940184e48SSeongJae Parktest_scheme()
20040184e48SSeongJae Park{
20140184e48SSeongJae Park	scheme_dir=$1
20240184e48SSeongJae Park	ensure_dir "$scheme_dir" "exist"
20340184e48SSeongJae Park	ensure_file "$scheme_dir/action" "exist" "600"
20440184e48SSeongJae Park	test_access_pattern "$scheme_dir/access_pattern"
20565ded14eSSeongJae Park	ensure_file "$scheme_dir/apply_interval_us" "exist" "600"
20640184e48SSeongJae Park	test_quotas "$scheme_dir/quotas"
20740184e48SSeongJae Park	test_watermarks "$scheme_dir/watermarks"
208553b0142SSeongJae Park	test_filters "$scheme_dir/filters"
20940184e48SSeongJae Park	test_stats "$scheme_dir/stats"
2102b3ee3f6SSeongJae Park	test_tried_regions "$scheme_dir/tried_regions"
21140184e48SSeongJae Park}
21240184e48SSeongJae Park
21340184e48SSeongJae Parktest_schemes()
21440184e48SSeongJae Park{
21540184e48SSeongJae Park	schemes_dir=$1
21640184e48SSeongJae Park	ensure_dir "$schemes_dir" "exist"
21740184e48SSeongJae Park	ensure_file "$schemes_dir/nr_schemes" "exist" 600
21840184e48SSeongJae Park
21940184e48SSeongJae Park	ensure_write_succ  "$schemes_dir/nr_schemes" "1" "valid input"
22040184e48SSeongJae Park	test_scheme "$schemes_dir/0"
22140184e48SSeongJae Park
22240184e48SSeongJae Park	ensure_write_succ  "$schemes_dir/nr_schemes" "2" "valid input"
22340184e48SSeongJae Park	test_scheme "$schemes_dir/0"
22440184e48SSeongJae Park	test_scheme "$schemes_dir/1"
22540184e48SSeongJae Park
22640184e48SSeongJae Park	ensure_write_succ "$schemes_dir/nr_schemes" "0" "valid input"
22740184e48SSeongJae Park	ensure_dir "$schemes_dir/0" "not_exist"
22840184e48SSeongJae Park	ensure_dir "$schemes_dir/1" "not_exist"
22940184e48SSeongJae Park}
23040184e48SSeongJae Park
23140184e48SSeongJae Parktest_region()
23240184e48SSeongJae Park{
23340184e48SSeongJae Park	region_dir=$1
23440184e48SSeongJae Park	ensure_dir "$region_dir" "exist"
23540184e48SSeongJae Park	ensure_file "$region_dir/start" "exist" 600
23640184e48SSeongJae Park	ensure_file "$region_dir/end" "exist" 600
23740184e48SSeongJae Park}
23840184e48SSeongJae Park
23940184e48SSeongJae Parktest_regions()
24040184e48SSeongJae Park{
24140184e48SSeongJae Park	regions_dir=$1
24240184e48SSeongJae Park	ensure_dir "$regions_dir" "exist"
24340184e48SSeongJae Park	ensure_file "$regions_dir/nr_regions" "exist" 600
24440184e48SSeongJae Park
24540184e48SSeongJae Park	ensure_write_succ  "$regions_dir/nr_regions" "1" "valid input"
24640184e48SSeongJae Park	test_region "$regions_dir/0"
24740184e48SSeongJae Park
24840184e48SSeongJae Park	ensure_write_succ  "$regions_dir/nr_regions" "2" "valid input"
24940184e48SSeongJae Park	test_region "$regions_dir/0"
25040184e48SSeongJae Park	test_region "$regions_dir/1"
25140184e48SSeongJae Park
25240184e48SSeongJae Park	ensure_write_succ "$regions_dir/nr_regions" "0" "valid input"
25340184e48SSeongJae Park	ensure_dir "$regions_dir/0" "not_exist"
25440184e48SSeongJae Park	ensure_dir "$regions_dir/1" "not_exist"
25540184e48SSeongJae Park}
25640184e48SSeongJae Park
25740184e48SSeongJae Parktest_target()
25840184e48SSeongJae Park{
25940184e48SSeongJae Park	target_dir=$1
26040184e48SSeongJae Park	ensure_dir "$target_dir" "exist"
26140184e48SSeongJae Park	ensure_file "$target_dir/pid_target" "exist" "600"
26240184e48SSeongJae Park	test_regions "$target_dir/regions"
26340184e48SSeongJae Park}
26440184e48SSeongJae Park
26540184e48SSeongJae Parktest_targets()
26640184e48SSeongJae Park{
26740184e48SSeongJae Park	targets_dir=$1
26840184e48SSeongJae Park	ensure_dir "$targets_dir" "exist"
26940184e48SSeongJae Park	ensure_file "$targets_dir/nr_targets" "exist" 600
27040184e48SSeongJae Park
27140184e48SSeongJae Park	ensure_write_succ  "$targets_dir/nr_targets" "1" "valid input"
27240184e48SSeongJae Park	test_target "$targets_dir/0"
27340184e48SSeongJae Park
27440184e48SSeongJae Park	ensure_write_succ  "$targets_dir/nr_targets" "2" "valid input"
27540184e48SSeongJae Park	test_target "$targets_dir/0"
27640184e48SSeongJae Park	test_target "$targets_dir/1"
27740184e48SSeongJae Park
27840184e48SSeongJae Park	ensure_write_succ "$targets_dir/nr_targets" "0" "valid input"
27940184e48SSeongJae Park	ensure_dir "$targets_dir/0" "not_exist"
28040184e48SSeongJae Park	ensure_dir "$targets_dir/1" "not_exist"
28140184e48SSeongJae Park}
28240184e48SSeongJae Park
28340184e48SSeongJae Parktest_intervals()
28440184e48SSeongJae Park{
28540184e48SSeongJae Park	intervals_dir=$1
28640184e48SSeongJae Park	ensure_dir "$intervals_dir" "exist"
28740184e48SSeongJae Park	ensure_file "$intervals_dir/aggr_us" "exist" "600"
28840184e48SSeongJae Park	ensure_file "$intervals_dir/sample_us" "exist" "600"
28940184e48SSeongJae Park	ensure_file "$intervals_dir/update_us" "exist" "600"
29040184e48SSeongJae Park}
29140184e48SSeongJae Park
29240184e48SSeongJae Parktest_monitoring_attrs()
29340184e48SSeongJae Park{
29440184e48SSeongJae Park	monitoring_attrs_dir=$1
29540184e48SSeongJae Park	ensure_dir "$monitoring_attrs_dir" "exist"
29640184e48SSeongJae Park	test_intervals "$monitoring_attrs_dir/intervals"
29740184e48SSeongJae Park	test_range "$monitoring_attrs_dir/nr_regions"
29840184e48SSeongJae Park}
29940184e48SSeongJae Park
30040184e48SSeongJae Parktest_context()
30140184e48SSeongJae Park{
30240184e48SSeongJae Park	context_dir=$1
30340184e48SSeongJae Park	ensure_dir "$context_dir" "exist"
304f893abbdSSeongJae Park	ensure_file "$context_dir/avail_operations" "exit" 400
30540184e48SSeongJae Park	ensure_file "$context_dir/operations" "exist" 600
30640184e48SSeongJae Park	test_monitoring_attrs "$context_dir/monitoring_attrs"
30740184e48SSeongJae Park	test_targets "$context_dir/targets"
30840184e48SSeongJae Park	test_schemes "$context_dir/schemes"
30940184e48SSeongJae Park}
31040184e48SSeongJae Park
31140184e48SSeongJae Parktest_contexts()
31240184e48SSeongJae Park{
31340184e48SSeongJae Park	contexts_dir=$1
31440184e48SSeongJae Park	ensure_dir "$contexts_dir" "exist"
31540184e48SSeongJae Park	ensure_file "$contexts_dir/nr_contexts" "exist" 600
31640184e48SSeongJae Park
31740184e48SSeongJae Park	ensure_write_succ  "$contexts_dir/nr_contexts" "1" "valid input"
31840184e48SSeongJae Park	test_context "$contexts_dir/0"
31940184e48SSeongJae Park
32040184e48SSeongJae Park	ensure_write_fail "$contexts_dir/nr_contexts" "2" "only 0/1 are supported"
32140184e48SSeongJae Park	test_context "$contexts_dir/0"
32240184e48SSeongJae Park
32340184e48SSeongJae Park	ensure_write_succ "$contexts_dir/nr_contexts" "0" "valid input"
32440184e48SSeongJae Park	ensure_dir "$contexts_dir/0" "not_exist"
32540184e48SSeongJae Park}
32640184e48SSeongJae Park
32740184e48SSeongJae Parktest_kdamond()
32840184e48SSeongJae Park{
32940184e48SSeongJae Park	kdamond_dir=$1
33040184e48SSeongJae Park	ensure_dir "$kdamond_dir" "exist"
33140184e48SSeongJae Park	ensure_file "$kdamond_dir/state" "exist" "600"
33240184e48SSeongJae Park	ensure_file "$kdamond_dir/pid" "exist" 400
33340184e48SSeongJae Park	test_contexts "$kdamond_dir/contexts"
33440184e48SSeongJae Park}
33540184e48SSeongJae Park
33640184e48SSeongJae Parktest_kdamonds()
33740184e48SSeongJae Park{
33840184e48SSeongJae Park	kdamonds_dir=$1
33940184e48SSeongJae Park	ensure_dir "$kdamonds_dir" "exist"
34040184e48SSeongJae Park
34140184e48SSeongJae Park	ensure_file "$kdamonds_dir/nr_kdamonds" "exist" "600"
34240184e48SSeongJae Park
34340184e48SSeongJae Park	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "1" "valid input"
34440184e48SSeongJae Park	test_kdamond "$kdamonds_dir/0"
34540184e48SSeongJae Park
34640184e48SSeongJae Park	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "2" "valid input"
34740184e48SSeongJae Park	test_kdamond "$kdamonds_dir/0"
34840184e48SSeongJae Park	test_kdamond "$kdamonds_dir/1"
34940184e48SSeongJae Park
35040184e48SSeongJae Park	ensure_write_succ "$kdamonds_dir/nr_kdamonds" "0" "valid input"
35140184e48SSeongJae Park	ensure_dir "$kdamonds_dir/0" "not_exist"
35240184e48SSeongJae Park	ensure_dir "$kdamonds_dir/1" "not_exist"
35340184e48SSeongJae Park}
35440184e48SSeongJae Park
35540184e48SSeongJae Parktest_damon_sysfs()
35640184e48SSeongJae Park{
35740184e48SSeongJae Park	damon_sysfs=$1
35840184e48SSeongJae Park	if [ ! -d "$damon_sysfs" ]
35940184e48SSeongJae Park	then
36040184e48SSeongJae Park		echo "$damon_sysfs not found"
36140184e48SSeongJae Park		exit $ksft_skip
36240184e48SSeongJae Park	fi
36340184e48SSeongJae Park
36440184e48SSeongJae Park	test_kdamonds "$damon_sysfs/kdamonds"
36540184e48SSeongJae Park}
36640184e48SSeongJae Park
36740184e48SSeongJae Parkcheck_dependencies()
36840184e48SSeongJae Park{
36940184e48SSeongJae Park	if [ $EUID -ne 0 ]
37040184e48SSeongJae Park	then
37140184e48SSeongJae Park		echo "Run as root"
37240184e48SSeongJae Park		exit $ksft_skip
37340184e48SSeongJae Park	fi
37440184e48SSeongJae Park}
37540184e48SSeongJae Park
37640184e48SSeongJae Parkcheck_dependencies
37740184e48SSeongJae Parktest_damon_sysfs "/sys/kernel/mm/damon/admin"
378