speedtest.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test read and write speed of a MTD device.
  18. *
  19. * Author: Adrian Hunter <adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/ktime.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/err.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/slab.h>
  29. #include <linux/sched.h>
  30. #include <linux/random.h>
  31. #include "mtd_test.h"
  32. static int dev = -EINVAL;
  33. module_param(dev, int, S_IRUGO);
  34. MODULE_PARM_DESC(dev, "MTD device number to use");
  35. static int count;
  36. module_param(count, int, S_IRUGO);
  37. MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
  38. "(0 means use all)");
  39. static struct mtd_info *mtd;
  40. static unsigned char *iobuf;
  41. static unsigned char *bbt;
  42. static int pgsize;
  43. static int ebcnt;
  44. static int pgcnt;
  45. static int goodebcnt;
  46. static ktime_t start, finish;
  47. static int multiblock_erase(int ebnum, int blocks)
  48. {
  49. int err;
  50. struct erase_info ei;
  51. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  52. memset(&ei, 0, sizeof(struct erase_info));
  53. ei.mtd = mtd;
  54. ei.addr = addr;
  55. ei.len = mtd->erasesize * blocks;
  56. err = mtd_erase(mtd, &ei);
  57. if (err) {
  58. pr_err("error %d while erasing EB %d, blocks %d\n",
  59. err, ebnum, blocks);
  60. return err;
  61. }
  62. if (ei.state == MTD_ERASE_FAILED) {
  63. pr_err("some erase error occurred at EB %d,"
  64. "blocks %d\n", ebnum, blocks);
  65. return -EIO;
  66. }
  67. return 0;
  68. }
  69. static int write_eraseblock(int ebnum)
  70. {
  71. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  72. return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
  73. }
  74. static int write_eraseblock_by_page(int ebnum)
  75. {
  76. int i, err = 0;
  77. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  78. void *buf = iobuf;
  79. for (i = 0; i < pgcnt; i++) {
  80. err = mtdtest_write(mtd, addr, pgsize, buf);
  81. if (err)
  82. break;
  83. addr += pgsize;
  84. buf += pgsize;
  85. }
  86. return err;
  87. }
  88. static int write_eraseblock_by_2pages(int ebnum)
  89. {
  90. size_t sz = pgsize * 2;
  91. int i, n = pgcnt / 2, err = 0;
  92. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  93. 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;
  100. }
  101. if (pgcnt % 2)
  102. err = mtdtest_write(mtd, addr, pgsize, buf);
  103. return err;
  104. }
  105. static int read_eraseblock(int ebnum)
  106. {
  107. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  108. return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
  109. }
  110. static int read_eraseblock_by_page(int ebnum)
  111. {
  112. int i, err = 0;
  113. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  114. void *buf = iobuf;
  115. for (i = 0; i < pgcnt; i++) {
  116. err = mtdtest_read(mtd, addr, pgsize, buf);
  117. if (err)
  118. break;
  119. addr += pgsize;
  120. buf += pgsize;
  121. }
  122. return err;
  123. }
  124. static int read_eraseblock_by_2pages(int ebnum)
  125. {
  126. size_t sz = pgsize * 2;
  127. int i, n = pgcnt / 2, err = 0;
  128. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  129. void *buf = iobuf;
  130. for (i = 0; i < n; i++) {
  131. err = mtdtest_read(mtd, addr, sz, buf);
  132. if (err)
  133. return err;
  134. addr += sz;
  135. buf += sz;
  136. }
  137. if (pgcnt % 2)
  138. err = mtdtest_read(mtd, addr, pgsize, buf);
  139. return err;
  140. }
  141. static inline void start_timing(void)
  142. {
  143. start = ktime_get();
  144. }
  145. static inline void stop_timing(void)
  146. {
  147. finish = ktime_get();
  148. }
  149. static long calc_speed(void)
  150. {
  151. uint64_t k;
  152. long ms;
  153. ms = ktime_ms_delta(finish, start);
  154. if (ms == 0)
  155. return 0;
  156. k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
  157. do_div(k, ms);
  158. return k;
  159. }
  160. static int __init mtd_speedtest_init(void)
  161. {
  162. int err, i, blocks, j, k;
  163. long speed;
  164. uint64_t tmp;
  165. printk(KERN_INFO "\n");
  166. printk(KERN_INFO "=================================================\n");
  167. if (dev < 0) {
  168. pr_info("Please specify a valid mtd-device via module parameter\n");
  169. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  170. return -EINVAL;
  171. }
  172. if (count)
  173. pr_info("MTD device: %d count: %d\n", dev, count);
  174. else
  175. pr_info("MTD device: %d\n", dev);
  176. mtd = get_mtd_device(NULL, dev);
  177. if (IS_ERR(mtd)) {
  178. err = PTR_ERR(mtd);
  179. pr_err("error: cannot get MTD device\n");
  180. return err;
  181. }
  182. if (mtd->writesize == 1) {
  183. pr_info("not NAND flash, assume page size is 512 "
  184. "bytes.\n");
  185. pgsize = 512;
  186. } else
  187. pgsize = mtd->writesize;
  188. tmp = mtd->size;
  189. do_div(tmp, mtd->erasesize);
  190. ebcnt = tmp;
  191. pgcnt = mtd->erasesize / pgsize;
  192. pr_info("MTD device size %llu, eraseblock size %u, "
  193. "page size %u, count of eraseblocks %u, pages per "
  194. "eraseblock %u, OOB size %u\n",
  195. (unsigned long long)mtd->size, mtd->erasesize,
  196. pgsize, ebcnt, pgcnt, mtd->oobsize);
  197. if (count > 0 && count < ebcnt)
  198. ebcnt = count;
  199. err = -ENOMEM;
  200. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  201. if (!iobuf)
  202. goto out;
  203. prandom_bytes(iobuf, mtd->erasesize);
  204. bbt = kzalloc(ebcnt, GFP_KERNEL);
  205. if (!bbt)
  206. goto out;
  207. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  208. if (err)
  209. goto out;
  210. for (i = 0; i < ebcnt; i++) {
  211. if (!bbt[i])
  212. goodebcnt++;
  213. }
  214. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  215. if (err)
  216. goto out;
  217. /* Write all eraseblocks, 1 eraseblock at a time */
  218. pr_info("testing eraseblock write speed\n");
  219. start_timing();
  220. for (i = 0; i < ebcnt; ++i) {
  221. if (bbt[i])
  222. continue;
  223. err = write_eraseblock(i);
  224. if (err)
  225. goto out;
  226. err = mtdtest_relax();
  227. if (err)
  228. goto out;
  229. }
  230. stop_timing();
  231. speed = calc_speed();
  232. pr_info("eraseblock write speed is %ld KiB/s\n", speed);
  233. /* Read all eraseblocks, 1 eraseblock at a time */
  234. pr_info("testing eraseblock read speed\n");
  235. start_timing();
  236. for (i = 0; i < ebcnt; ++i) {
  237. if (bbt[i])
  238. continue;
  239. err = read_eraseblock(i);
  240. if (err)
  241. goto out;
  242. err = mtdtest_relax();
  243. if (err)
  244. goto out;
  245. }
  246. stop_timing();
  247. speed = calc_speed();
  248. pr_info("eraseblock read speed is %ld KiB/s\n", speed);
  249. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  250. if (err)
  251. goto out;
  252. /* Write all eraseblocks, 1 page at a time */
  253. pr_info("testing page write speed\n");
  254. start_timing();
  255. for (i = 0; i < ebcnt; ++i) {
  256. if (bbt[i])
  257. continue;
  258. err = write_eraseblock_by_page(i);
  259. if (err)
  260. goto out;
  261. err = mtdtest_relax();
  262. if (err)
  263. goto out;
  264. }
  265. stop_timing();
  266. speed = calc_speed();
  267. pr_info("page write speed is %ld KiB/s\n", speed);
  268. /* Read all eraseblocks, 1 page at a time */
  269. pr_info("testing page read speed\n");
  270. start_timing();
  271. for (i = 0; i < ebcnt; ++i) {
  272. if (bbt[i])
  273. continue;
  274. err = read_eraseblock_by_page(i);
  275. if (err)
  276. goto out;
  277. err = mtdtest_relax();
  278. if (err)
  279. goto out;
  280. }
  281. stop_timing();
  282. speed = calc_speed();
  283. pr_info("page read speed is %ld KiB/s\n", speed);
  284. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  285. if (err)
  286. goto out;
  287. /* Write all eraseblocks, 2 pages at a time */
  288. pr_info("testing 2 page write speed\n");
  289. start_timing();
  290. for (i = 0; i < ebcnt; ++i) {
  291. if (bbt[i])
  292. continue;
  293. err = write_eraseblock_by_2pages(i);
  294. if (err)
  295. goto out;
  296. err = mtdtest_relax();
  297. if (err)
  298. goto out;
  299. }
  300. stop_timing();
  301. speed = calc_speed();
  302. pr_info("2 page write speed is %ld KiB/s\n", speed);
  303. /* Read all eraseblocks, 2 pages at a time */
  304. pr_info("testing 2 page read speed\n");
  305. start_timing();
  306. for (i = 0; i < ebcnt; ++i) {
  307. if (bbt[i])
  308. continue;
  309. err = read_eraseblock_by_2pages(i);
  310. if (err)
  311. goto out;
  312. err = mtdtest_relax();
  313. if (err)
  314. goto out;
  315. }
  316. stop_timing();
  317. speed = calc_speed();
  318. pr_info("2 page read speed is %ld KiB/s\n", speed);
  319. /* Erase all eraseblocks */
  320. pr_info("Testing erase speed\n");
  321. start_timing();
  322. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  323. if (err)
  324. goto out;
  325. stop_timing();
  326. speed = calc_speed();
  327. pr_info("erase speed is %ld KiB/s\n", speed);
  328. /* Multi-block erase all eraseblocks */
  329. for (k = 1; k < 7; k++) {
  330. blocks = 1 << k;
  331. pr_info("Testing %dx multi-block erase speed\n",
  332. blocks);
  333. start_timing();
  334. for (i = 0; i < ebcnt; ) {
  335. for (j = 0; j < blocks && (i + j) < ebcnt; j++)
  336. if (bbt[i + j])
  337. break;
  338. if (j < 1) {
  339. i++;
  340. continue;
  341. }
  342. err = multiblock_erase(i, j);
  343. if (err)
  344. goto out;
  345. err = mtdtest_relax();
  346. if (err)
  347. goto out;
  348. i += j;
  349. }
  350. stop_timing();
  351. speed = calc_speed();
  352. pr_info("%dx multi-block erase speed is %ld KiB/s\n",
  353. blocks, speed);
  354. }
  355. pr_info("finished\n");
  356. out:
  357. kfree(iobuf);
  358. kfree(bbt);
  359. put_mtd_device(mtd);
  360. if (err)
  361. pr_info("error %d occurred\n", err);
  362. printk(KERN_INFO "=================================================\n");
  363. return err;
  364. }
  365. module_init(mtd_speedtest_init);
  366. static void __exit mtd_speedtest_exit(void)
  367. {
  368. return;
  369. }
  370. module_exit(mtd_speedtest_exit);
  371. MODULE_DESCRIPTION("Speed test module");
  372. MODULE_AUTHOR("Adrian Hunter");
  373. MODULE_LICENSE("GPL");