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/operations" "exist" 600
235	test_monitoring_attrs "$context_dir/monitoring_attrs"
236	test_targets "$context_dir/targets"
237	test_schemes "$context_dir/schemes"
238}
239
240test_contexts()
241{
242	contexts_dir=$1
243	ensure_dir "$contexts_dir" "exist"
244	ensure_file "$contexts_dir/nr_contexts" "exist" 600
245
246	ensure_write_succ  "$contexts_dir/nr_contexts" "1" "valid input"
247	test_context "$contexts_dir/0"
248
249	ensure_write_fail "$contexts_dir/nr_contexts" "2" "only 0/1 are supported"
250	test_context "$contexts_dir/0"
251
252	ensure_write_succ "$contexts_dir/nr_contexts" "0" "valid input"
253	ensure_dir "$contexts_dir/0" "not_exist"
254}
255
256test_kdamond()
257{
258	kdamond_dir=$1
259	ensure_dir "$kdamond_dir" "exist"
260	ensure_file "$kdamond_dir/state" "exist" "600"
261	ensure_file "$kdamond_dir/pid" "exist" 400
262	test_contexts "$kdamond_dir/contexts"
263}
264
265test_kdamonds()
266{
267	kdamonds_dir=$1
268	ensure_dir "$kdamonds_dir" "exist"
269
270	ensure_file "$kdamonds_dir/nr_kdamonds" "exist" "600"
271
272	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "1" "valid input"
273	test_kdamond "$kdamonds_dir/0"
274
275	ensure_write_succ  "$kdamonds_dir/nr_kdamonds" "2" "valid input"
276	test_kdamond "$kdamonds_dir/0"
277	test_kdamond "$kdamonds_dir/1"
278
279	ensure_write_succ "$kdamonds_dir/nr_kdamonds" "0" "valid input"
280	ensure_dir "$kdamonds_dir/0" "not_exist"
281	ensure_dir "$kdamonds_dir/1" "not_exist"
282}
283
284test_damon_sysfs()
285{
286	damon_sysfs=$1
287	if [ ! -d "$damon_sysfs" ]
288	then
289		echo "$damon_sysfs not found"
290		exit $ksft_skip
291	fi
292
293	test_kdamonds "$damon_sysfs/kdamonds"
294}
295
296check_dependencies()
297{
298	if [ $EUID -ne 0 ]
299	then
300		echo "Run as root"
301		exit $ksft_skip
302	fi
303}
304
305check_dependencies
306test_damon_sysfs "/sys/kernel/mm/damon/admin"
307