xref: /aosp_15_r20/external/ltp/testcases/kernel/lib/ksm_helper.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author(s): Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "ksm_helper.h"
12*49cdfc7eSAndroid Build Coastguard Worker 
wait_ksmd_full_scan(void)13*49cdfc7eSAndroid Build Coastguard Worker void wait_ksmd_full_scan(void)
14*49cdfc7eSAndroid Build Coastguard Worker {
15*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long full_scans, at_least_one_full_scan;
16*49cdfc7eSAndroid Build Coastguard Worker 	int count = 0;
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(PATH_KSM "full_scans", "%lu", &full_scans);
19*49cdfc7eSAndroid Build Coastguard Worker 	/*
20*49cdfc7eSAndroid Build Coastguard Worker 	 * The current scan is already in progress so we can't guarantee that
21*49cdfc7eSAndroid Build Coastguard Worker 	 * the get_user_pages() is called on every existing rmap_item if we
22*49cdfc7eSAndroid Build Coastguard Worker 	 * only waited for the remaining part of the scan.
23*49cdfc7eSAndroid Build Coastguard Worker 	 *
24*49cdfc7eSAndroid Build Coastguard Worker 	 * The actual merging happens after the unstable tree has been built so
25*49cdfc7eSAndroid Build Coastguard Worker 	 * we need to wait at least two full scans to guarantee merging, hence
26*49cdfc7eSAndroid Build Coastguard Worker 	 * wait full_scans to increment by 3 so that at least two full scans
27*49cdfc7eSAndroid Build Coastguard Worker 	 * will run.
28*49cdfc7eSAndroid Build Coastguard Worker 	 */
29*49cdfc7eSAndroid Build Coastguard Worker 	at_least_one_full_scan = full_scans + 3;
30*49cdfc7eSAndroid Build Coastguard Worker 	while (full_scans < at_least_one_full_scan) {
31*49cdfc7eSAndroid Build Coastguard Worker 		sleep(1);
32*49cdfc7eSAndroid Build Coastguard Worker 		count++;
33*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_SCANF(PATH_KSM "full_scans", "%lu", &full_scans);
34*49cdfc7eSAndroid Build Coastguard Worker 	}
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "ksm daemon takes %ds to run two full scans", count);
37*49cdfc7eSAndroid Build Coastguard Worker }
38