1 /*
2  * Copyright (C) 2017-2021 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * This code is a complete clean re-write of the stress tool by
19  * Colin Ian King <colin.king@canonical.com> and attempts to be
20  * backwardly compatible with the stress tool by Amos Waterland
21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
22  * functionality.
23  *
24  */
25 #include "stress-ng.h"
26 
27 /*
28  *  stress_mmap_set()
29  *	set mmap'd data, touching pages in
30  *	a specific pattern - check with
31  *	mmap_check().
32  */
stress_mmap_set(uint8_t * buf,const size_t sz,const size_t page_size)33 void stress_mmap_set(
34 	uint8_t *buf,
35 	const size_t sz,
36 	const size_t page_size)
37 {
38 	size_t i, j;
39 	uint8_t val = 0;
40 	uint8_t *ptr = buf;
41 
42 	for (i = 0; i < sz; i += page_size) {
43 		if (!keep_stressing_flag())
44 			break;
45 		for (j = 0; j < page_size; j++)
46 			*ptr++ = val++;
47 		val++;
48 	}
49 }
50 
51 /*
52  *  stress_mmap_check()
53  *	check if mmap'd data is sane
54  */
stress_mmap_check(uint8_t * buf,const size_t sz,const size_t page_size)55 int stress_mmap_check(
56 	uint8_t *buf,
57 	const size_t sz,
58 	const size_t page_size)
59 {
60 	size_t i, j;
61 	uint8_t val = 0;
62 	uint8_t *ptr = buf;
63 
64 	for (i = 0; i < sz; i += page_size) {
65 		if (!keep_stressing_flag())
66 			break;
67 		for (j = 0; j < page_size; j++)
68 			if (*ptr++ != val++)
69 				return -1;
70 		val++;
71 	}
72 	return 0;
73 }
74