speedtest.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/random.h>
  30. #include "mtd_test.h"
  31. static int dev = -EINVAL;
  32. module_param(dev, int, S_IRUGO);
  33. MODULE_PARM_DESC(dev, "MTD device number to use");
  34. static int count;
  35. module_param(count, int, S_IRUGO);
  36. MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
  37. "(0 means use all)");
  38. static struct mtd_info *mtd;
  39. static unsigned char *iobuf;
  40. static unsigned char *bbt;
  41. static int pgsize;
  42. static int ebcnt;
  43. static int pgcnt;
  44. static int goodebcnt;
  45. static struct timeval start, finish;
  46. static int multiblock_erase(int ebnum, int blocks)
  47. {
  48. int err;
  49. struct erase_info ei;
  50. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  51. memset(&ei, 0, sizeof(struct erase_info));
  52. ei.mtd = mtd;
  53. ei.addr = addr;
  54. ei.len = mtd->erasesize * blocks;
  55. err = mtd_erase(mtd, &ei);
  56. if (err) {
  57. pr_err("error %d while erasing EB %d, blocks %d\n",
  58. err, ebnum, blocks);
  59. return err;
  60. }
  61. if (ei.state == MTD_ERASE_FAILED) {
  62. pr_err("some erase error occurred at EB %d,"
  63. "blocks %d\n", ebnum, blocks);
  64. return -EIO;
  65. }
  66. return 0;
  67. }
  68. static int write_eraseblock(int ebnum)
  69. {
  70. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  71. return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
  72. }
  73. static int write_eraseblock_by_page(int ebnum)
  74. {
  75. int i, err = 0;
  76. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  77. void *buf = iobuf;
  78. for (i = 0; i < pgcnt; i++) {
  79. err = mtdtest_write(mtd, addr, pgsize, buf);
  80. if (err)
  81. break;
  82. addr += pgsize;
  83. buf += pgsize;
  84. }
  85. return err;
  86. }
  87. static int write_eraseblock_by_2pages(int ebnum)
  88. {
  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;
  93. for (i = 0; i < n; i++) {
  94. err = mtdtest_write(mtd, addr, sz, buf);
  95. if (err)
  96. return err;
  97. addr += sz;
  98. buf += sz;
  99. }
  100. if (pgcnt % 2)
  101. err = mtdtest_write(mtd, addr, pgsize, buf);
  102. return err;
  103. }
  104. static int read_eraseblock(int ebnum)
  105. {
  106. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  107. return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
  108. }
  109. static int read_eraseblock_by_page(int ebnum)
  110. {
  111. int i, err = 0;
  112. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  113. void *buf = iobuf;
  114. for (i = 0; i < pgcnt; i++) {
  115. err = mtdtest_read(mtd, addr, pgsize, buf);
  116. if (err)
  117. break;
  118. addr += pgsize;
  119. buf += pgsize;
  120. }
  121. return err;
  122. }
  123. static int read_eraseblock_by_2pages(int ebnum)
  124. {
  125. size_t sz = pgsize * 2;
  126. int i, n = pgcnt / 2, err = 0;
  127. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  128. void *buf = iobuf;
  129. for (i = 0; i < n; i++) {
  130. err = mtdtest_read(mtd, addr, sz, buf);
  131. if (err)
  132. return err;
  133. addr += sz;
  134. buf += sz;
  135. }
  136. if (pgcnt % 2)
  137. err = mtdtest_read(mtd, addr, pgsize, buf);
  138. return err;
  139. }
  140. static inline void start_timing(void)
  141. {
  142. do_gettimeofday(&start);
  143. }
  144. static inline void stop_timing(void)
  145. {
  146. do_gettimeofday(&finish);
  147. }
  148. static long calc_speed(void)
  149. {
  150. uint64_t k;
  151. long ms;
  152. ms = (finish.tv_sec - start.tv_sec) * 1000 +
  153. (finish.tv_usec - start.tv_usec) / 1000;
  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");