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_tried_regions()
84{
85	tried_regions_dir=$1
86	ensure_dir "$tried_regions_dir" "exist"
87}
88
89test_stats()
90{
91	stats_dir=$1
92	ensure_dir "$stats_dir" "exist"
93	for f in nr_tried sz_tried nr_applied sz_applied qt_exceeds
94	do
95		ensure_file "$stats_dir/$f" "exist" "400"
96	done
97}
98
99test_watermarks()
100{
101	watermarks_dir=$1
102	ensure_dir "$watermarks_dir" "exist"
103	ensure_file "$watermarks_dir/metric" "exist" "600"
104	ensure_file "$watermarks_dir/interval_us" "exist" "600"
105	ensure_file "$watermarks_dir/high" "exist" "600"
106	ensure_file "$watermarks_dir/mid" "exist" "600"
107	ensure_file "$watermarks_dir/low" "exist" "600"
108}
109
110test_weights()
111{
112	weights_dir=$1
113	ensure_dir "$weights_dir" "exist"
114	ensure_file "$weights_dir/sz_permil" "exist" "600"
115	ensure_file "$weights_dir/nr_accesses_permil" "exist" "600"
116	ensure_file "$weights_dir/age_permil" "exist" "600"
117}
118
119test_quotas()
120{
121	quotas_dir=$1
122	ensure_dir "$quotas_dir" "exist"
123	ensure_file "$quotas_dir/ms" "exist" 600
124	ensure_file "$quotas_dir/bytes" "exist" 600
125	ensure_file "$quotas_dir/reset_interval_ms" "exist" 600
126	test_weights "$quotas_dir/weights"
127}
128
129test_access_pattern()
130{
131	access_pattern_dir=$1
132	ensure_dir "$access_pattern_dir" "exist"
133	test_range "$access_pattern_dir/age"
134	test_range "$access_pattern_dir/nr_accesses"
135	test_range "$access_pattern_dir/sz"
136}
137
138test_scheme()
139{
140	scheme_dir=$1
141	ensure_dir "$scheme_dir" "exist"
142	ensure_file "$scheme_dir/action" "exist" "600"
143	test_access_pattern "$scheme_dir/access_pattern"
144	test_quotas "$scheme_dir/quotas"
145	test_watermarks "$scheme_dir/watermarks"
146	test_stats "$scheme_dir/stats"
147	test_tried_regions "$scheme_dir/tried_regions"
148}
149
150test_schemes()
151{
152	schemes_dir=$1
153	ensure_dir "$schemes_dir" "exist"
154	ensure_file "$schemes_dir/nr_schemes" "exist" 600
155
156	ensure_write_succ  "$schemes_dir/nr_schemes" "1" "valid input"
157	test_scheme "$schemes_dir/0"
158
159	ensure_write_succ  "$schemes_dir/nr_schemes" "2" "valid input"
160	test_scheme "$schemes_dir/0"
161	test_scheme "$schemes_dir/1"
162
163	ensure_write_succ "$schemes_dir/nr_schemes" "0" "valid input"
164	ensure_dir "$schemes_dir/0" "not_exist"
165	ensure_dir "$schemes_dir/1" "not_exist"
166}
167
168test_region()
169{
170	region_dir=$1
171	ensure_dir "$region_dir" "exist"
172	ensure_file "$region_dir/start" "exist" 600
173	ensure_file "$region_dir/end" "exist" 600
174}
175
176test_regions()
177{
178	regions_dir=$1
179	ensure_dir "$regions_dir" "exist"
180	ensure_file "$regions_dir/nr_regions" "exist" 600
181
182	ensure_write_succ  "$regions_dir/nr_regions" "1" "valid input"
183	test_region "$regions_dir/0"
184
185	ensure_write_succ  "$regions_dir/nr_regions" "2" "valid input"
186	test_region "$regions_dir/0"
187	test_region "$regions_dir/1"
188
189	ensure_write_succ "$regions_dir/nr_regions" "0" "valid input"
190	ensure_dir "$regions_dir/0" "not_exist"
191	ensure_dir "$regions_dir/1" "not_exist"
192}
193
194test_target()
195{
196	target_dir=$1
197	ensure_dir "$target_dir" "exist"
198	ensure_file "$target_dir/pid_target" "exist" "600"
199	test_regions "$target_dir/regions"
200}
201
202test_targets()
203{
204	targets_dir=$1
205	ensure_dir "$targets_dir" "exist"
206	ensure_file "$targets_dir/nr_targets" "exist" 600
207
208	ensure_write_succ  "$targets_dir/nr_targets" "1" "valid input"
209	test_target "$targets_dir/0"
210
211	ensure_write_succ  "$targets_dir/nr_targets" "2" "valid input"
212	test_target "$targets_dir/0"
213	test_target "$targets_dir/1"
214
215	ensure_write_succ "$targets_dir/nr_targets" "0" "valid input"
216	ensure_dir "$targets_dir/0" "not_exist"
217	ensure_dir "$targets_dir/1" "not_exist"
218}
219
220test_intervals()
221{
222	intervals_dir=$1
223	ensure_dir "$intervals_dir" "exist"
224	ensure_file "$intervals_dir/aggr_us" "exist" "600"
225	ensure_file "$intervals_dir/sample_us" "exist" "600"
226	ensure_file "$intervals_dir/update_us" "exist" "600"
227}
228
229test_monitoring_attrs()
230{
231	monitoring_attrs_dir=$1
232	ensure_dir "$monitoring_attrs_dir" "exist"
233	test_intervals "$monitoring_attrs_dir/intervals"
234	test_range "$monitoring_attrs_dir/nr_regions"
235}
236
237test_context()
238{
239	context_dir=$1
240	ensure_dir "$context_dir" "exist"
241	ensure_file "$context_dir/avail_operations" "exit" 400
242	ensure_file "$context_dir/operations" "exist" 600
243	test_monitoring_attrs "$context_dir/monitoring_attrs"
244	test_targets "$context_dir/targets"
245	test_schemes "$context_dir/schemes"
246}
247
248test_contexts()
249{
250	contexts_dir=$1
251	ensure_dir "$contexts_dir" "exist"
252	ensure_file "$contexts_dir/nr_contexts" "exist" 600
253
254	ensure_write_succ  "$contexts_dir/nr_contexts" "1" "valid input"
255	test_context "$contexts_dir/0"
256
257	ensure_write_fail "$contexts_dir/nr_contexts" "2" "only 0/1 are supported"
258	test_context "$contexts_dir/0"
259
260	ensure_write_succ "$contexts_dir/nr_contexts" "0" "valid input"
261	ensure_dir "$contexts_dir/0" "not_exist"
262}
263
264test_kdamond()
265{
266	kdamond_dir=$1
267	ensure_dir "$kdamond_dir" "exist"
268	ensure_file "$kdamond_dir/state" "exist" "600"
269	ensure_file "$kdamond_dir/pid" "exist" 400
270	test_contexts "$kdamond_dir/contexts"
271}
272
273test_kdamonds()
274{
275	kdamonds_dir=$1
276	ensure_dir "$kdamonds_dir" "exist"
277
278	ensure_file "$kdamonds_dir/nr_kdamonds" "exist" "600"
279
280	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "1" "valid input"
281	test_kdamond "$kdamonds_dir/0"
282
283	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "2" "valid input"
284	test_kdamond "$kdamonds_dir/0"
285	test_kdamond "$kdamonds_dir/1"
286
287	ensure_write_succ "$kdamonds_dir/nr_kdamonds" "0" "valid input"
288	ensure_dir "$kdamonds_dir/0" "not_exist"
289	ensure_dir "$kdamonds_dir/1" "not_exist"
290}
291
292test_damon_sysfs()
293{
294	damon_sysfs=$1
295	if [ ! -d "$damon_sysfs" ]
296	then
297		echo "$damon_sysfs not found"
298		exit $ksft_skip
299	fi
300
301	test_kdamonds "$damon_sysfs/kdamonds"
302}
303
304check_dependencies()
305{
306	if [ $EUID -ne 0 ]
307	then
308		echo "Run as root"
309		exit $ksft_skip
310	fi
311}
312
313check_dependencies
314test_damon_sysfs "/sys/kernel/mm/damon/admin"
315