speedtest.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.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. return 0;
  62. }
  63. static int write_eraseblock(int ebnum)
  64. {
  65. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  66. return mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
  67. }
  68. static int write_eraseblock_by_page(int ebnum)
  69. {
  70. int i, err = 0;
  71. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  72. void *buf = iobuf;
  73. for (i = 0; i < pgcnt; i++) {
  74. err = mtdtest_write(mtd, addr, pgsize, buf);
  75. if (err)
  76. break;
  77. addr += pgsize;
  78. buf += pgsize;
  79. }
  80. return err;
  81. }
  82. static int write_eraseblock_by_2pages(int ebnum)
  83. {
  84. size_t sz = pgsize * 2;
  85. int i, n = pgcnt / 2, err = 0;
  86. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  87. void *buf = iobuf;
  88. for (i = 0; i < n; i++) {
  89. err = mtdtest_write(mtd, addr, sz, buf);
  90. if (err)
  91. return err;
  92. addr += sz;
  93. buf += sz;
  94. }
  95. if (pgcnt % 2)
  96. err = mtdtest_write(mtd, addr, pgsize, buf);
  97. return err;
  98. }
  99. static int read_eraseblock(int ebnum)
  100. {
  101. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  102. return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
  103. }
  104. static int read_eraseblock_by_page(int ebnum)
  105. {
  106. int i, err = 0;
  107. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  108. void *buf = iobuf;
  109. for (i = 0; i < pgcnt; i++) {
  110. err = mtdtest_read(mtd, addr, pgsize, buf);
  111. if (err)
  112. break;
  113. addr += pgsize;
  114. buf += pgsize;
  115. }
  116. return err;
  117. }
  118. static int read_eraseblock_by_2pages(int ebnum)
  119. {
  120. size_t sz = pgsize * 2;
  121. int i, n = pgcnt / 2, err = 0;
  122. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  123. void *buf = iobuf;
  124. for (i = 0; i < n; i++) {
  125. err = mtdtest_read(mtd, addr, sz, buf);
  126. if (err)
  127. return err;
  128. addr += sz;
  129. buf += sz;
  130. }
  131. if (pgcnt % 2)
  132. err = mtdtest_read(mtd, addr, pgsize, buf);
  133. return err;
  134. }
  135. static inline void start_timing(void)
  136. {
  137. start = ktime_get();
  138. }
  139. static inline void stop_timing(void)
  140. {
  141. finish = ktime_get();
  142. }
  143. static long calc_speed(void)
  144. {
  145. uint64_t k;
  146. long ms;
  147. ms = ktime_ms_delta(finish, start);
  148. if (ms == 0)
  149. return 0;
  150. k = (uint64_t)goodebcnt * (mtd->erasesize / 1024) * 1000;
  151. do_div(k, ms);
  152. return k;
  153. }
  154. static int __init mtd_speedtest_init(void)
  155. {
  156. int err, i, blocks, j, k;
  157. long speed;
  158. uint64_t tmp;
  159. printk(KERN_INFO "\n");
  160. printk(KERN_INFO "=================================================\n");
  161. if (dev < 0) {
  162. pr_info("Please specify a valid mtd-device via module parameter\n");
  163. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  164. return -EINVAL;
  165. }
  166. if (count)
  167. pr_info("MTD device: %d count: %d\n", dev, count);
  168. else
  169. pr_info("MTD device: %d\n", dev);
  170. mtd = get_mtd_device(NULL, dev);
  171. if (IS_ERR(mtd)) {
  172. err = PTR_ERR(mtd);
  173. pr_err("error: cannot get MTD device\n");
  174. return err;
  175. }
  176. if (mtd->writesize == 1) {
  177. pr_info("not NAND flash, assume page size is 512 "
  178. "bytes.\n");
  179. pgsize = 512;
  180. } else
  181. pgsize = mtd->writesize;
  182. tmp = mtd->size;
  183. do_div(tmp, mtd->erasesize);
  184. ebcnt = tmp;
  185. pgcnt = mtd->erasesize / pgsize;
  186. pr_info("MTD device size %llu, eraseblock size %u, "
  187. "page size %u, count of eraseblocks %u, pages per "
  188. "eraseblock %u, OOB size %u\n",
  189. (unsigned long long)mtd->size, mtd->erasesize,
  190. pgsize, ebcnt, pgcnt, mtd->oobsize);
  191. if (count > 0 && count < ebcnt)
  192. ebcnt = count;
  193. err = -ENOMEM;
  194. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  195. if (!iobuf)
  196. goto out;
  197. prandom_bytes(iobuf, mtd->erasesize);
  198. bbt = kzalloc(ebcnt, GFP_KERNEL);
  199. if (!bbt)
  200. goto out;
  201. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  202. if (err)
  203. goto out;
  204. for (i = 0; i < ebcnt; i++) {
  205. if (!bbt[i])
  206. goodebcnt++;
  207. }
  208. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  209. if (err)
  210. goto out;
  211. /* Write all eraseblocks, 1 eraseblock at a time */
  212. pr_info("testing eraseblock write speed\n");
  213. start_timing();
  214. for (i = 0; i < ebcnt; ++i) {
  215. if (bbt[i])
  216. continue;
  217. err = write_eraseblock(i);
  218. if (err)
  219. goto out;
  220. err = mtdtest_relax();
  221. if (err)
  222. goto out;
  223. }
  224. stop_timing();
  225. speed = calc_speed();
  226. pr_info("eraseblock write speed is %ld KiB/s\n", speed);
  227. /* Read all eraseblocks, 1 eraseblock at a time */
  228. pr_info("testing eraseblock read speed\n");
  229. start_timing();
  230. for (i = 0; i < ebcnt; ++i) {
  231. if (bbt[i])
  232. continue;
  233. err = read_eraseblock(i);
  234. if (err)
  235. goto out;
  236. err = mtdtest_relax();
  237. if (err)
  238. goto out;
  239. }
  240. stop_timing();
  241. speed = calc_speed();
  242. pr_info("eraseblock read speed is %ld KiB/s\n", speed);
  243. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  244. if (err)
  245. goto out;
  246. /* Write all eraseblocks, 1 page at a time */
  247. pr_info("testing page write speed\n");
  248. start_timing();
  249. for (i = 0; i < ebcnt; ++i) {
  250. if (bbt[i])
  251. continue;
  252. err = write_eraseblock_by_page(i);
  253. if (err)
  254. goto out;
  255. err = mtdtest_relax();
  256. if (err)
  257. goto out;
  258. }
  259. stop_timing();
  260. speed = calc_speed();
  261. pr_info("page write speed is %ld KiB/s\n", speed);
  262. /* Read all eraseblocks, 1 page at a time */
  263. pr_info("testing page read speed\n");
  264. start_timing();
  265. for (i = 0; i < ebcnt; ++i) {
  266. if (bbt[i])
  267. continue;
  268. err = read_eraseblock_by_page(i);
  269. if (err)
  270. goto out;
  271. err = mtdtest_relax();
  272. if (err)
  273. goto out;
  274. }
  275. stop_timing();
  276. speed = calc_speed();
  277. pr_info("page read speed is %ld KiB/s\n", speed);
  278. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  279. if (err)
  280. goto out;
  281. /* Write all eraseblocks, 2 pages at a time */
  282. pr_info("testing 2 page write speed\n");
  283. start_timing();
  284. for (i = 0; i < ebcnt; ++i) {
  285. if (bbt[i])
  286. continue;
  287. err = write_eraseblock_by_2pages(i);
  288. if (err)
  289. goto out;
  290. err = mtdtest_relax();
  291. if (err)
  292. goto out;
  293. }
  294. stop_timing();
  295. speed = calc_speed();
  296. pr_info("2 page write speed is %ld KiB/s\n", speed);
  297. /* Read all eraseblocks, 2 pages at a time */
  298. pr_info("testing 2 page read speed\n");
  299. start_timing();
  300. for (i = 0; i < ebcnt; ++i) {
  301. if (bbt[i])
  302. continue;
  303. err = read_eraseblock_by_2pages(i);
  304. if (err)
  305. goto out;
  306. err = mtdtest_relax();
  307. if (err)
  308. goto out;
  309. }
  310. stop_timing();
  311. speed = calc_speed();
  312. pr_info("2 page read speed is %ld KiB/s\n", speed);
  313. /* Erase all eraseblocks */
  314. pr_info("Testing erase speed\n");
  315. start_timing();
  316. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  317. if (err)
  318. goto out;
  319. stop_timing();
  320. speed = calc_speed();
  321. pr_info("erase speed is %ld KiB/s\n", speed);
  322. /* Multi-block erase all eraseblocks */
  323. for (k = 1; k < 7; k++) {
  324. blocks = 1 << k;
  325. pr_info("Testing %dx multi-block erase speed\n",
  326. blocks);
  327. start_timing();
  328. for (i = 0; i < ebcnt; ) {
  329. for (j = 0; j < blocks && (i + j) < ebcnt; j++)
  330. if (bbt[i + j])
  331. break;
  332. if (j < 1) {
  333. i++;
  334. continue;
  335. }
  336. err = multiblock_erase(i, j);
  337. if (err)
  338. goto out;
  339. err = mtdtest_relax();
  340. if (err)
  341. goto out;
  342. i += j;
  343. }
  344. stop_timing();
  345. speed = calc_speed();
  346. pr_info("%dx multi-block erase speed is %ld KiB/s\n",
  347. blocks, speed);
  348. }
  349. pr_info("finished\n");
  350. out:
  351. kfree(iobuf);
  352. kfree(bbt);
  353. put_mtd_device(mtd);
  354. if (err)
  355. pr_info("error %d occurred\n", err);
  356. printk(KERN_INFO "=================================================\n");
  357. return err;
  358. }
  359. module_init(mtd_speedtest_init);
  360. static void __exit mtd_speedtest_exit(void)
  361. {
  362. return;
  363. }
  364. module_exit(mtd_speedtest_exit);
  365. MODULE_DESCRIPTION("Speed test module");
  366. MODULE_AUTHOR("Adrian Hunter");
  367. MODULE_LICENSE("GPL");