flash_speed.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation
  3. * Copyright (C) 2015 sigma star gmbh
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; see the file COPYING. If not, write to the Free Software
  16. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Test read and write speed of a MTD device.
  19. *
  20. * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  21. *
  22. * Based on linux flash_speed.c
  23. * Author: Adrian Hunter <adrian.hunter@nokia.com>
  24. */
  25. #define DESTRUCTIVE 0x01
  26. #define PROGRAM_NAME "flash_speed"
  27. #include <mtd/mtd-user.h>
  28. #include <sys/ioctl.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <libmtd.h>
  33. #include <getopt.h>
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include <time.h>
  37. #include "common.h"
  38. static struct mtd_dev_info mtd;
  39. static unsigned char *iobuf;
  40. static unsigned char *bbt;
  41. static const char *mtddev;
  42. static libmtd_t mtd_desc;
  43. static int fd;
  44. static int peb=-1, count=-1, skip=-1, flags=0;
  45. static struct timespec start, finish;
  46. static int pgsize, pgcnt;
  47. static int goodebcnt;
  48. static const struct option options[] = {
  49. { "help", no_argument, NULL, 'h' },
  50. { "destructive", no_argument, NULL, 'd' },
  51. { "peb", required_argument, NULL, 'b' },
  52. { "count", required_argument, NULL, 'c' },
  53. { "skip", required_argument, NULL, 's' },
  54. { NULL, 0, NULL, 0 },
  55. };
  56. static NORETURN void usage(int status)
  57. {
  58. fputs(
  59. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  60. "Common options:\n"
  61. " -h, --help Display this help output\n"
  62. " -b, --peb <num> Start from this physical erase block\n"
  63. " -c, --count <num> Number of erase blocks to use (default: all)\n"
  64. " -s, --skip <num> Number of blocks to skip\n"
  65. " -d, --destructive Run destructive (erase and write speed) tests\n",
  66. status==EXIT_SUCCESS ? stdout : stderr);
  67. exit(status);
  68. }
  69. static long read_num(int opt, const char *arg)
  70. {
  71. char *end;
  72. long num;
  73. num = strtol(arg, &end, 0);
  74. if (!end || *end != '\0') {
  75. fprintf(stderr, "-%c: expected integer argument\n", opt);
  76. exit(EXIT_FAILURE);
  77. }
  78. return num;
  79. }
  80. static void process_options(int argc, char **argv)
  81. {
  82. int c;
  83. while (1) {
  84. c = getopt_long(argc, argv, "hb:c:s:d", options, NULL);
  85. if (c == -1)
  86. break;
  87. switch (c) {
  88. case 'h':
  89. usage(EXIT_SUCCESS);
  90. case 'b':
  91. if (peb >= 0)
  92. goto failmulti;
  93. peb = read_num(c, optarg);
  94. if (peb < 0)
  95. goto failarg;
  96. break;
  97. case 'c':
  98. if (count > 0)
  99. goto failmulti;
  100. count = read_num(c, optarg);
  101. if (count <= 0)
  102. goto failarg;
  103. break;
  104. case 's':
  105. if (skip >= 0)
  106. goto failmulti;
  107. skip = read_num(c, optarg);
  108. if (skip < 0)
  109. goto failarg;
  110. break;
  111. case 'd':
  112. if (flags & DESTRUCTIVE)
  113. goto failmulti;
  114. flags |= DESTRUCTIVE;
  115. break;
  116. default:
  117. exit(EXIT_FAILURE);
  118. }
  119. }
  120. if (optind < argc)
  121. mtddev = argv[optind++];
  122. else
  123. errmsg_die("No device specified!\n");
  124. if (optind < argc)
  125. usage(EXIT_FAILURE);
  126. if (peb < 0)
  127. peb = 0;
  128. if (skip < 0)
  129. skip = 0;
  130. if (count < 0)
  131. count = 1;
  132. return;
  133. failmulti:
  134. errmsg_die("'-%c' specified more than once!\n", c);
  135. failarg:
  136. errmsg_die("Invalid argument for '-%c'!\n", c);
  137. }
  138. static int write_eraseblock(int ebnum)
  139. {
  140. int err = mtd_write(mtd_desc, &mtd, fd, ebnum, 0,
  141. iobuf, mtd.eb_size, NULL, 0, 0);
  142. if (err)
  143. fprintf(stderr, "Error writing block %d!\n", ebnum);
  144. return err;
  145. }
  146. static int read_eraseblock(int ebnum)
  147. {
  148. int err = mtd_read(&mtd, fd, ebnum, 0, iobuf, mtd.eb_size);
  149. if (err)
  150. fprintf(stderr, "Error writing block %d!\n", ebnum);
  151. return err;
  152. }
  153. static int write_eraseblock_by_page(int ebnum)
  154. {
  155. void *buf = iobuf;
  156. int i, err = 0;
  157. for (i = 0; i < pgcnt; ++i) {
  158. err = mtd_write(mtd_desc, &mtd, fd, ebnum, i * pgsize,
  159. buf, pgsize, NULL, 0, 0);
  160. if (err) {
  161. fprintf(stderr, "Error writing block %d, page %d!\n",
  162. ebnum, i);
  163. break;
  164. }
  165. buf += pgsize;
  166. }
  167. return err;
  168. }
  169. static int write_eraseblock_by_2pages(int ebnum)
  170. {
  171. int i, n = pgcnt / 2, err = 0;
  172. size_t sz = pgsize * 2;
  173. void *buf = iobuf;
  174. for (i = 0; i < n; ++i) {
  175. err = mtd_write(mtd_desc, &mtd, fd, ebnum, i * sz,
  176. buf, sz, NULL, 0, 0);
  177. if (err) {
  178. fprintf(stderr, "Error writing block %d, page %d + %d!\n",
  179. ebnum, i*2, i*2+1);
  180. return err;
  181. }
  182. buf += sz;
  183. }
  184. if (pgcnt % 2) {
  185. err = mtd_write(mtd_desc, &mtd, fd, ebnum, i * sz,
  186. buf, pgsize, NULL, 0, 0);
  187. if (err) {
  188. fprintf(stderr, "Error reading block %d, page %d!\n",
  189. ebnum, i*2);
  190. }
  191. }
  192. return err;
  193. }
  194. static int read_eraseblock_by_page(int ebnum)
  195. {
  196. void *buf = iobuf;
  197. int i, err = 0;
  198. for (i = 0; i < pgcnt; ++i) {
  199. err = mtd_read(&mtd, fd, ebnum, i * pgsize, iobuf, pgsize);
  200. if (err) {
  201. fprintf(stderr, "Error reading block %d, page %d!\n",
  202. ebnum, i);
  203. break;
  204. }
  205. buf += pgsize;
  206. }
  207. return err;
  208. }
  209. static int read_eraseblock_by_2pages(int ebnum)
  210. {
  211. int i, n = pgcnt / 2, err = 0;
  212. size_t sz = pgsize * 2;
  213. void *buf = iobuf;
  214. for (i = 0; i < n; ++i) {
  215. err = mtd_read(&mtd, fd, ebnum, i * sz, iobuf, sz);
  216. if (err) {
  217. fprintf(stderr, "Error reading block %d, page %d + %d!\n",
  218. ebnum, i*2, i*2+1);
  219. return err;
  220. }
  221. buf += sz;
  222. }
  223. if (pgcnt % 2) {
  224. err = mtd_read(&mtd, fd, ebnum, i * sz, iobuf, pgsize);
  225. if (err) {
  226. fprintf(stderr, "Error reading block %d, page %d!\n",
  227. ebnum, i*2);
  228. }
  229. }
  230. return err;
  231. }
  232. static void start_timing(void)
  233. {
  234. clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  235. }
  236. static void stop_timing(void)
  237. {
  238. clock_gettime(CLOCK_MONOTONIC_RAW, &finish);
  239. }
  240. static long calc_speed(void)
  241. {
  242. long ms;
  243. ms = (finish.tv_sec - start.tv_sec) * 1000L;
  244. ms += (finish.tv_nsec - start.tv_nsec) / 1000000L;
  245. if (ms <= 0)
  246. return 0;
  247. return ((long)goodebcnt * (mtd.eb_size / 1024L) * 1000L) / ms;
  248. }
  249. static void scan_for_bad_eraseblocks(unsigned int eb, int ebcnt, int ebskip)
  250. {
  251. int i, bad = 0;
  252. puts("scanning for bad eraseblocks");
  253. for (i = 0; i < ebcnt; ++i) {
  254. bbt[i] = mtd_is_bad(&mtd, fd, eb + i*(ebskip+1)) ? 1 : 0;
  255. if (bbt[i])
  256. bad += 1;
  257. }
  258. printf("scanned %d eraseblocks, %d are bad\n", ebcnt, bad);
  259. }
  260. static int erase_good_eraseblocks(unsigned int eb, int ebcnt, int ebskip)
  261. {
  262. int err = 0, block;
  263. unsigned int i;
  264. for (i = 0; i < ebcnt; ++i) {
  265. if (bbt[i])
  266. continue;
  267. block = eb + i*(ebskip+1);
  268. err = mtd_erase(mtd_desc, &mtd, fd, block);
  269. if (err)
  270. fprintf(stderr, "Error erasing block %d!\n", block);
  271. }
  272. return err;
  273. }
  274. #define TIME_OP_PER_PEB( op )\
  275. start_timing();\
  276. for (i = 0; i < count; ++i) {\
  277. if (bbt[i])\
  278. continue;\
  279. err = op(peb + i*(skip+1));\
  280. if (err)\
  281. goto out;\
  282. }\
  283. stop_timing();\
  284. speed = calc_speed()
  285. int main(int argc, char **argv)
  286. {
  287. int err, i, blocks, j, k, status = EXIT_FAILURE;
  288. long speed;
  289. process_options(argc, argv);
  290. mtd_desc = libmtd_open();
  291. if (!mtd_desc)
  292. return errmsg("can't initialize libmtd");
  293. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  294. return errmsg("mtd_get_dev_info failed");
  295. if (mtd.subpage_size == 1) {
  296. puts("not NAND flash, assume page size is 512 bytes.");
  297. pgsize = 512;
  298. } else {
  299. pgsize = mtd.subpage_size;
  300. }
  301. pgcnt = mtd.eb_size / pgsize;
  302. if (count < 0)
  303. count = mtd.eb_size;
  304. if (peb >= mtd.eb_cnt)
  305. return errmsg("Physical erase block %d is out of range!\n", peb);
  306. if ((peb + (count - 1)*(skip + 1)) >= mtd.eb_cnt) {
  307. return errmsg("Given block range exceeds block count of %d!\n",
  308. mtd.eb_cnt);
  309. }
  310. iobuf = xmalloc(mtd.eb_size);
  311. bbt = xzalloc(count);
  312. if ((fd = open(mtddev, O_RDWR)) == -1) {
  313. perror(mtddev);
  314. goto outfree;
  315. }
  316. for (i = 0; i < mtd.eb_size; ++i)
  317. iobuf[i] = rand();
  318. scan_for_bad_eraseblocks(peb, count, skip);
  319. for (i = 0; i < count; ++i) {
  320. if (!bbt[i])
  321. goodebcnt++;
  322. }
  323. /* Write all eraseblocks, 1 eraseblock at a time */
  324. if (flags & DESTRUCTIVE) {
  325. err = erase_good_eraseblocks(peb, count, skip);
  326. if (err)
  327. goto out;
  328. puts("testing eraseblock write speed");
  329. TIME_OP_PER_PEB(write_eraseblock);
  330. printf("eraseblock write speed is %ld KiB/s\n", speed);
  331. }
  332. /* Read all eraseblocks, 1 eraseblock at a time */
  333. puts("testing eraseblock read speed");
  334. TIME_OP_PER_PEB(read_eraseblock);
  335. printf("eraseblock read speed is %ld KiB/s\n", speed);
  336. /* Write all eraseblocks, 1 page at a time */
  337. if (flags & DESTRUCTIVE) {
  338. err = erase_good_eraseblocks(peb, count, skip);
  339. if (err)
  340. goto out;
  341. puts("testing page write speed");
  342. TIME_OP_PER_PEB(write_eraseblock_by_page);
  343. printf("page write speed is %ld KiB/s\n", speed);
  344. }
  345. /* Read all eraseblocks, 1 page at a time */
  346. puts("testing page read speed");
  347. TIME_OP_PER_PEB(read_eraseblock_by_page);
  348. printf("page read speed is %ld KiB/s\n", speed);
  349. /* Write all eraseblocks, 2 pages at a time */
  350. if (flags & DESTRUCTIVE) {
  351. err = erase_good_eraseblocks(peb, count, skip);
  352. if (err)
  353. goto out;
  354. puts("testing 2 page write speed");
  355. TIME_OP_PER_PEB(write_eraseblock_by_2pages);
  356. printf("2 page write speed is %ld KiB/s\n", speed);
  357. }
  358. /* Read all eraseblocks, 2 pages at a time */
  359. puts("testing 2 page read speed");
  360. TIME_OP_PER_PEB(read_eraseblock_by_2pages);
  361. printf("2 page read speed is %ld KiB/s\n", speed);
  362. /* Erase all eraseblocks */
  363. if (flags & DESTRUCTIVE) {
  364. puts("Testing erase speed");
  365. start_timing();
  366. err = erase_good_eraseblocks(peb, count, skip);
  367. if (err)
  368. goto out;
  369. stop_timing();
  370. speed = calc_speed();
  371. printf("erase speed is %ld KiB/s\n", speed);
  372. }
  373. /* Multi-block erase all eraseblocks */
  374. if (!skip) {
  375. for (k = 1; k < 7; ++k) {
  376. blocks = 1 << k;
  377. printf("Testing %dx multi-block erase speed\n", blocks);
  378. start_timing();
  379. for (i = 0; i < count; ) {
  380. for (j = 0; j < blocks && (i + j) < count; ++j)
  381. if (bbt[i + j])
  382. break;
  383. if (j < 1) {
  384. ++i;
  385. continue;
  386. }
  387. err = mtd_erase_multi(mtd_desc, &mtd, fd, i, j);
  388. if (err)
  389. goto out;
  390. i += j;
  391. }
  392. stop_timing();
  393. speed = calc_speed();
  394. printf("%dx multi-block erase speed is %ld KiB/s\n",
  395. blocks, speed);
  396. }
  397. }
  398. puts("finished");
  399. status = EXIT_SUCCESS;
  400. out:
  401. close(fd);
  402. outfree:
  403. free(iobuf);
  404. free(bbt);
  405. return status;
  406. }