xref: /linux/tools/testing/selftests/damon/sysfs.sh (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest frmework requirement - SKIP code is 4.
5ksft_skip=4
6
7ensure_write_succ()
8{
9	file=$1
10	content=$2
11	reason=$3
12
13	if ! echo "$content" > "$file"
14	then
15		echo "writing $content to $file failed"
16		echo "expected success because $reason"
17		exit 1
18	fi
19}
20
21ensure_write_fail()
22{
23	file=$1
24	content=$2
25	reason=$3
26
27	if echo "$content" > "$file"
28	then
29		echo "writing $content to $file succeed ($fail_reason)"
30		echo "expected failure because $reason"
31		exit 1
32	fi
33}
34
35ensure_dir()
36{
37	dir=$1
38	to_ensure=$2
39	if [ "$to_ensure" = "exist" ] && [ ! -d "$dir" ]
40	then
41		echo "$dir dir is expected but not found"
42		exit 1
43	elif [ "$to_ensure" = "not_exist" ] && [ -d "$dir" ]
44	then
45		echo "$dir dir is not expected but found"
46		exit 1
47	fi
48}
49
50ensure_file()
51{
52	file=$1
53	to_ensure=$2
54	permission=$3
55	if [ "$to_ensure" = "exist" ]
56	then
57		if [ ! -f "$file" ]
58		then
59			echo "$file is expected but not found"
60			exit 1
61		fi
62		perm=$(stat -c "%a" "$file")
63		if [ ! "$perm" = "$permission" ]
64		then
65			echo "$file permission: expected $permission but $perm"
66			exit 1
67		fi
68	elif [ "$to_ensure" = "not_exist" ] && [ -f "$dir" ]
69	then
70		echo "$file is not expected but found"
71		exit 1
72	fi
73}
74
75test_range()
76{
77	range_dir=$1
78	ensure_dir "$range_dir" "exist"
79	ensure_file "$range_dir/min" "exist" 600
80	ensure_file "$range_dir/max" "exist" 600
81}
82
83test_stats()
84{
85	stats_dir=$1
86	ensure_dir "$stats_dir" "exist"
87	for f in nr_tried sz_tried nr_applied sz_applied qt_exceeds
88	do
89		ensure_file "$stats_dir/$f" "exist" "400"
90	done
91}
92
93test_watermarks()
94{
95	watermarks_dir=$1
96	ensure_dir "$watermarks_dir" "exist"
97	ensure_file "$watermarks_dir/metric" "exist" "600"
98	ensure_file "$watermarks_dir/interval_us" "exist" "600"
99	ensure_file "$watermarks_dir/high" "exist" "600"
100	ensure_file "$watermarks_dir/mid" "exist" "600"
101	ensure_file "$watermarks_dir/low" "exist" "600"
102}
103
104test_weights()
105{
106	weights_dir=$1
107	ensure_dir "$weights_dir" "exist"
108	ensure_file "$weights_dir/sz_permil" "exist" "600"
109	ensure_file "$weights_dir/nr_accesses_permil" "exist" "600"
110	ensure_file "$weights_dir/age_permil" "exist" "600"
111}
112
113test_quotas()
114{
115	quotas_dir=$1
116	ensure_dir "$quotas_dir" "exist"
117	ensure_file "$quotas_dir/ms" "exist" 600
118	ensure_file "$quotas_dir/bytes" "exist" 600
119	ensure_file "$quotas_dir/reset_interval_ms" "exist" 600
120	test_weights "$quotas_dir/weights"
121}
122
123test_access_pattern()
124{
125	access_pattern_dir=$1
126	ensure_dir "$access_pattern_dir" "exist"
127	test_range "$access_pattern_dir/age"
128	test_range "$access_pattern_dir/nr_accesses"
129	test_range "$access_pattern_dir/sz"
130}
131
132test_scheme()
133{
134	scheme_dir=$1
135	ensure_dir "$scheme_dir" "exist"
136	ensure_file "$scheme_dir/action" "exist" "600"
137	test_access_pattern "$scheme_dir/access_pattern"
138	test_quotas "$scheme_dir/quotas"
139	test_watermarks "$scheme_dir/watermarks"
140	test_stats "$scheme_dir/stats"
141}
142
143test_schemes()
144{
145	schemes_dir=$1
146	ensure_dir "$schemes_dir" "exist"
147	ensure_file "$schemes_dir/nr_schemes" "exist" 600
148
149	ensure_write_succ  "$schemes_dir/nr_schemes" "1" "valid input"
150	test_scheme "$schemes_dir/0"
151
152	ensure_write_succ  "$schemes_dir/nr_schemes" "2" "valid input"
153	test_scheme "$schemes_dir/0"
154	test_scheme "$schemes_dir/1"
155
156	ensure_write_succ "$schemes_dir/nr_schemes" "0" "valid input"
157	ensure_dir "$schemes_dir/0" "not_exist"
158	ensure_dir "$schemes_dir/1" "not_exist"
159}
160
161test_region()
162{
163	region_dir=$1
164	ensure_dir "$region_dir" "exist"
165	ensure_file "$region_dir/start" "exist" 600
166	ensure_file "$region_dir/end" "exist" 600
167}
168
169test_regions()
170{
171	regions_dir=$1
172	ensure_dir "$regions_dir" "exist"
173	ensure_file "$regions_dir/nr_regions" "exist" 600
174
175	ensure_write_succ  "$regions_dir/nr_regions" "1" "valid input"
176	test_region "$regions_dir/0"
177
178	ensure_write_succ  "$regions_dir/nr_regions" "2" "valid input"
179	test_region "$regions_dir/0"
180	test_region "$regions_dir/1"
181
182	ensure_write_succ "$regions_dir/nr_regions" "0" "valid input"
183	ensure_dir "$regions_dir/0" "not_exist"
184	ensure_dir "$regions_dir/1" "not_exist"
185}
186
187test_target()
188{
189	target_dir=$1
190	ensure_dir "$target_dir" "exist"
191	ensure_file "$target_dir/pid_target" "exist" "600"
192	test_regions "$target_dir/regions"
193}
194
195test_targets()
196{
197	targets_dir=$1
198	ensure_dir "$targets_dir" "exist"
199	ensure_file "$targets_dir/nr_targets" "exist" 600
200
201	ensure_write_succ  "$targets_dir/nr_targets" "1" "valid input"
202	test_target "$targets_dir/0"
203
204	ensure_write_succ  "$targets_dir/nr_targets" "2" "valid input"
205	test_target "$targets_dir/0"
206	test_target "$targets_dir/1"
207
208	ensure_write_succ "$targets_dir/nr_targets" "0" "valid input"
209	ensure_dir "$targets_dir/0" "not_exist"
210	ensure_dir "$targets_dir/1" "not_exist"
211}
212
213test_intervals()
214{
215	intervals_dir=$1
216	ensure_dir "$intervals_dir" "exist"
217	ensure_file "$intervals_dir/aggr_us" "exist" "600"
218	ensure_file "$intervals_dir/sample_us" "exist" "600"
219	ensure_file "$intervals_dir/update_us" "exist" "600"
220}
221
222test_monitoring_attrs()
223{
224	monitoring_attrs_dir=$1
225	ensure_dir "$monitoring_attrs_dir" "exist"
226	test_intervals "$monitoring_attrs_dir/intervals"
227	test_range "$monitoring_attrs_dir/nr_regions"
228}
229
230test_context()
231{
232	context_dir=$1
233	ensure_dir "$context_dir" "exist"
234	ensure_file "$context_dir/avail_operations" "exit" 400
235	ensure_file "$context_dir/operations" "exist" 600
236	test_monitoring_attrs "$context_dir/monitoring_attrs"
237	test_targets "$context_dir/targets"
238	test_schemes "$context_dir/schemes"
239}
240
241test_contexts()
242{
243	contexts_dir=$1
244	ensure_dir "$contexts_dir" "exist"
245	ensure_file "$contexts_dir/nr_contexts" "exist" 600
246
247	ensure_write_succ  "$contexts_dir/nr_contexts" "1" "valid input"
248	test_context "$contexts_dir/0"
249
250	ensure_write_fail "$contexts_dir/nr_contexts" "2" "only 0/1 are supported"
251	test_context "$contexts_dir/0"
252
253	ensure_write_succ "$contexts_dir/nr_contexts" "0" "valid input"
254	ensure_dir "$contexts_dir/0" "not_exist"
255}
256
257test_kdamond()
258{
259	kdamond_dir=$1
260	ensure_dir "$kdamond_dir" "exist"
261	ensure_file "$kdamond_dir/state" "exist" "600"
262	ensure_file "$kdamond_dir/pid" "exist" 400
263	test_contexts "$kdamond_dir/contexts"
264}
265
266test_kdamonds()
267{
268	kdamonds_dir=$1
269	ensure_dir "$kdamonds_dir" "exist"
270
271	ensure_file "$kdamonds_dir/nr_kdamonds" "exist" "600"
272
273	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "1" "valid input"
274	test_kdamond "$kdamonds_dir/0"
275
276	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "2" "valid input"
277	test_kdamond "$kdamonds_dir/0"
278	test_kdamond "$kdamonds_dir/1"
279
280	ensure_write_succ "$kdamonds_dir/nr_kdamonds" "0" "valid input"
281	ensure_dir "$kdamonds_dir/0" "not_exist"
282	ensure_dir "$kdamonds_dir/1" "not_exist"
283}
284
285test_damon_sysfs()
286{
287	damon_sysfs=$1
288	if [ ! -d "$damon_sysfs" ]
289	then
290		echo "$damon_sysfs not found"
291		exit $ksft_skip
292	fi
293
294	test_kdamonds "$damon_sysfs/kdamonds"
295}
296
297check_dependencies()
298{
299	if [ $EUID -ne 0 ]
300	then
301		echo "Run as root"
302		exit $ksft_skip
303	fi
304}
305
306check_dependencies
307test_damon_sysfs "/sys/kernel/mm/damon/admin"
308