staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / mtd / tests / speedtest.c
blob93e76648f67676c9351bbcf44155af34a68d6525
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2007 Nokia Corporation
5 * Test read and write speed of a MTD device.
7 * Author: Adrian Hunter <adrian.hunter@nokia.com>
8 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/ktime.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/err.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/random.h>
22 #include "mtd_test.h"
24 static int dev = -EINVAL;
25 module_param(dev, int, S_IRUGO);
26 MODULE_PARM_DESC(dev, "MTD device number to use");
28 static int count;
29 module_param(count, int, S_IRUGO);
30 MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
31 "(0 means use all)");
33 static struct mtd_info *mtd;
34 static unsigned char *iobuf;
35 static unsigned char *bbt;
37 static int pgsize;
38 static int ebcnt;
39 static int pgcnt;
40 static int goodebcnt;
41 static ktime_t start, finish;
43 static int multiblock_erase(int ebnum, int blocks)
45 int err;
46 struct erase_info ei;
47 loff_t addr = (loff_t)ebnum * mtd->erasesize;
49 memset(&ei, 0, sizeof(struct erase_info));
50 ei.addr = addr;
51 ei.len = mtd->erasesize * blocks;
53 err = mtd_erase(mtd, &ei);
54 if (err) {
55 pr_err("error %d while erasing EB %d, blocks %d\n",
56 err, ebnum, blocks);
57 return err;
60 return 0;
63 static int write_eraseblock(int ebnum)
65 loff_t addr = (loff_t)ebnum * mtd->erasesize;
67 return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
70 static int write_eraseblock_by_page(int ebnum)
72 int i, err = 0;
73 loff_t addr = (loff_t)ebnum * mtd->erasesize;
74 void *buf = iobuf;
76 for (i = 0; i < pgcnt; i++) {
77 err = mtdtest_write(mtd, addr, pgsize, buf);
78 if (err)
79 break;
80 addr += pgsize;
81 buf += pgsize;
84 return err;
87 static int write_eraseblock_by_2pages(int ebnum)
89 size_t sz = pgsize * 2;
90 int i, n = pgcnt / 2, err = 0;
91 loff_t addr = (loff_t)ebnum * mtd->erasesize;
92 void *buf = iobuf;
94 for (i = 0; i < n; i++) {
95 err = mtdtest_write(mtd, addr, sz, buf);
96 if (err)
97 return err;
98 addr += sz;
99 buf += sz;
101 if (pgcnt % 2)
102 err = mtdtest_write(mtd, addr, pgsize, buf);
104 return err;
107 static int read_eraseblock(int ebnum)
109 loff_t addr = (loff_t)ebnum * mtd->erasesize;
111 return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
114 static int read_eraseblock_by_page(int ebnum)
116 int i, err = 0;
117 loff_t addr = (loff_t)ebnum * mtd->erasesize;
118 void *buf = iobuf;
120 for (i = 0; i < pgcnt; i++) {
121 err = mtdtest_read(mtd, addr, pgsize, buf);
122 if (err)
123 break;
124 addr += pgsize;
125 buf += pgsize;
128 return err;
131 static int read_eraseblock_by_2pages(int ebnum)
133 size_t sz = pgsize * 2;
134 int i, n = pgcnt / 2, err = 0;
135 loff_t addr = (loff_t)ebnum * mtd->erasesize;
136 void *buf = iobuf;
138 for (i = 0; i < n; i++) {
139 err = mtdtest_read(mtd, addr, sz, buf);
140 if (err)
141 return err;
142 addr += sz;
143 buf += sz;
145 if (pgcnt % 2)
146 err = mtdtest_read(mtd, addr, pgsize, buf);
148 return err;
151 static inline void start_timing(void)
153 start = ktime_get();
156 static inline void stop_timing(void)
158 finish = ktime_get();
161 static long calc_speed(void)
163 uint64_t k;
164 long ms;
166 ms = ktime_ms_delta(finish, start);
167 if (ms == 0)
168 return 0;
169 k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
170 do_div(k, ms);
171 return k;
174 static int __init mtd_speedtest_init(void)
176 int err, i, blocks, j, k;
177 long speed;
178 uint64_t tmp;
180 printk(KERN_INFO "\n");
181 printk(KERN_INFO "=================================================\n");
183 if (dev < 0) {
184 pr_info("Please specify a valid mtd-device via module parameter\n");
185 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
186 return -EINVAL;
189 if (count)
190 pr_info("MTD device: %d count: %d\n", dev, count);
191 else
192 pr_info("MTD device: %d\n", dev);
194 mtd = get_mtd_device(NULL, dev);
195 if (IS_ERR(mtd)) {
196 err = PTR_ERR(mtd);
197 pr_err("error: cannot get MTD device\n");
198 return err;
201 if (mtd->writesize == 1) {
202 pr_info("not NAND flash, assume page size is 512 "
203 "bytes.\n");
204 pgsize = 512;
205 } else
206 pgsize = mtd->writesize;
208 tmp = mtd->size;
209 do_div(tmp, mtd->erasesize);
210 ebcnt = tmp;
211 pgcnt = mtd->erasesize / pgsize;
213 pr_info("MTD device size %llu, eraseblock size %u, "
214 "page size %u, count of eraseblocks %u, pages per "
215 "eraseblock %u, OOB size %u\n",
216 (unsigned long long)mtd->size, mtd->erasesize,
217 pgsize, ebcnt, pgcnt, mtd->oobsize);
219 if (count > 0 && count < ebcnt)
220 ebcnt = count;
222 err = -ENOMEM;
223 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
224 if (!iobuf)
225 goto out;
227 prandom_bytes(iobuf, mtd->erasesize);
229 bbt = kzalloc(ebcnt, GFP_KERNEL);
230 if (!bbt)
231 goto out;
232 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
233 if (err)
234 goto out;
235 for (i = 0; i < ebcnt; i++) {
236 if (!bbt[i])
237 goodebcnt++;
240 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
241 if (err)
242 goto out;
244 /* Write all eraseblocks, 1 eraseblock at a time */
245 pr_info("testing eraseblock write speed\n");
246 start_timing();
247 for (i = 0; i < ebcnt; ++i) {
248 if (bbt[i])
249 continue;
250 err = write_eraseblock(i);
251 if (err)
252 goto out;
254 err = mtdtest_relax();
255 if (err)
256 goto out;
258 stop_timing();
259 speed = calc_speed();
260 pr_info("eraseblock write speed is %ld KiB/s\n", speed);
262 /* Read all eraseblocks, 1 eraseblock at a time */
263 pr_info("testing eraseblock read speed\n");
264 start_timing();
265 for (i = 0; i < ebcnt; ++i) {
266 if (bbt[i])
267 continue;
268 err = read_eraseblock(i);
269 if (err)
270 goto out;
272 err = mtdtest_relax();
273 if (err)
274 goto out;
276 stop_timing();
277 speed = calc_speed();
278 pr_info("eraseblock read speed is %ld KiB/s\n", speed);
280 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
281 if (err)
282 goto out;
284 /* Write all eraseblocks, 1 page at a time */
285 pr_info("testing page write speed\n");
286 start_timing();
287 for (i = 0; i < ebcnt; ++i) {
288 if (bbt[i])
289 continue;
290 err = write_eraseblock_by_page(i);
291 if (err)
292 goto out;
294 err = mtdtest_relax();
295 if (err)
296 goto out;
298 stop_timing();
299 speed = calc_speed();
300 pr_info("page write speed is %ld KiB/s\n", speed);
302 /* Read all eraseblocks, 1 page at a time */
303 pr_info("testing page read speed\n");
304 start_timing();
305 for (i = 0; i < ebcnt; ++i) {
306 if (bbt[i])
307 continue;
308 err = read_eraseblock_by_page(i);
309 if (err)
310 goto out;
312 err = mtdtest_relax();
313 if (err)
314 goto out;
316 stop_timing();
317 speed = calc_speed();
318 pr_info("page read speed is %ld KiB/s\n", speed);
320 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
321 if (err)
322 goto out;
324 /* Write all eraseblocks, 2 pages at a time */
325 pr_info("testing 2 page write speed\n");
326 start_timing();
327 for (i = 0; i < ebcnt; ++i) {
328 if (bbt[i])
329 continue;
330 err = write_eraseblock_by_2pages(i);
331 if (err)
332 goto out;
334 err = mtdtest_relax();
335 if (err)
336 goto out;
338 stop_timing();
339 speed = calc_speed();
340 pr_info("2 page write speed is %ld KiB/s\n", speed);
342 /* Read all eraseblocks, 2 pages at a time */
343 pr_info("testing 2 page read speed\n");
344 start_timing();
345 for (i = 0; i < ebcnt; ++i) {
346 if (bbt[i])
347 continue;
348 err = read_eraseblock_by_2pages(i);
349 if (err)
350 goto out;
352 err = mtdtest_relax();
353 if (err)
354 goto out;
356 stop_timing();
357 speed = calc_speed();
358 pr_info("2 page read speed is %ld KiB/s\n", speed);
360 /* Erase all eraseblocks */
361 pr_info("Testing erase speed\n");
362 start_timing();
363 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
364 if (err)
365 goto out;
366 stop_timing();
367 speed = calc_speed();
368 pr_info("erase speed is %ld KiB/s\n", speed);
370 /* Multi-block erase all eraseblocks */
371 for (k = 1; k < 7; k++) {
372 blocks = 1 << k;
373 pr_info("Testing %dx multi-block erase speed\n",
374 blocks);
375 start_timing();
376 for (i = 0; i < ebcnt; ) {
377 for (j = 0; j < blocks && (i + j) < ebcnt; j++)
378 if (bbt[i + j])
379 break;
380 if (j < 1) {
381 i++;
382 continue;
384 err = multiblock_erase(i, j);
385 if (err)
386 goto out;
388 err = mtdtest_relax();
389 if (err)
390 goto out;
392 i += j;
394 stop_timing();
395 speed = calc_speed();
396 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
397 blocks, speed);
399 pr_info("finished\n");
400 out:
401 kfree(iobuf);
402 kfree(bbt);
403 put_mtd_device(mtd);
404 if (err)
405 pr_info("error %d occurred\n", err);
406 printk(KERN_INFO "=================================================\n");
407 return err;
409 module_init(mtd_speedtest_init);
411 static void __exit mtd_speedtest_exit(void)
413 return;
415 module_exit(mtd_speedtest_exit);
417 MODULE_DESCRIPTION("Speed test module");
418 MODULE_AUTHOR("Adrian Hunter");
419 MODULE_LICENSE("GPL");