test_lwext4.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "../common/test_lwext4.h"
  29. #include <ext4.h>
  30. #include <stdio.h>
  31. #include <inttypes.h>
  32. #include <string.h>
  33. /**@brief Block device handle.*/
  34. static struct ext4_blockdev *bd;
  35. /**@brief Block cache handle.*/
  36. static struct ext4_bcache *bc;
  37. static char *entry_to_str(uint8_t type)
  38. {
  39. switch (type) {
  40. case EXT4_DE_UNKNOWN:
  41. return "[unk] ";
  42. case EXT4_DE_REG_FILE:
  43. return "[fil] ";
  44. case EXT4_DE_DIR:
  45. return "[dir] ";
  46. case EXT4_DE_CHRDEV:
  47. return "[cha] ";
  48. case EXT4_DE_BLKDEV:
  49. return "[blk] ";
  50. case EXT4_DE_FIFO:
  51. return "[fif] ";
  52. case EXT4_DE_SOCK:
  53. return "[soc] ";
  54. case EXT4_DE_SYMLINK:
  55. return "[sym] ";
  56. default:
  57. break;
  58. }
  59. return "[???]";
  60. }
  61. static long int get_ms(void) { return tim_get_ms(); }
  62. static void printf_io_timings(long int diff)
  63. {
  64. const struct ext4_io_stats *stats = io_timings_get(diff);
  65. if (!stats)
  66. return;
  67. printf("io_timings:\n");
  68. printf(" io_read: %.3f%%\n", (double)stats->io_read);
  69. printf(" io_write: %.3f%%\n", (double)stats->io_write);
  70. printf(" io_cpu: %.3f%%\n", (double)stats->cpu);
  71. }
  72. void test_lwext4_dir_ls(const char *path)
  73. {
  74. char sss[255];
  75. ext4_dir d;
  76. const ext4_direntry *de;
  77. printf("ls %s\n", path);
  78. ext4_dir_open(&d, path);
  79. de = ext4_dir_entry_next(&d);
  80. while (de) {
  81. memcpy(sss, de->name, de->name_length);
  82. sss[de->name_length] = 0;
  83. printf(" %s%s\n", entry_to_str(de->inode_type), sss);
  84. de = ext4_dir_entry_next(&d);
  85. }
  86. ext4_dir_close(&d);
  87. }
  88. void test_lwext4_mp_stats(void)
  89. {
  90. struct ext4_mount_stats stats;
  91. ext4_mount_point_stats("/mp/", &stats);
  92. printf("********************\n");
  93. printf("ext4_mount_point_stats\n");
  94. printf("inodes_count = %" PRIu32 "\n", stats.inodes_count);
  95. printf("free_inodes_count = %" PRIu32 "\n", stats.free_inodes_count);
  96. printf("blocks_count = %" PRIu32 "\n", (uint32_t)stats.blocks_count);
  97. printf("free_blocks_count = %" PRIu32 "\n",
  98. (uint32_t)stats.free_blocks_count);
  99. printf("block_size = %" PRIu32 "\n", stats.block_size);
  100. printf("block_group_count = %" PRIu32 "\n", stats.block_group_count);
  101. printf("blocks_per_group= %" PRIu32 "\n", stats.blocks_per_group);
  102. printf("inodes_per_group = %" PRIu32 "\n", stats.inodes_per_group);
  103. printf("volume_name = %s\n", stats.volume_name);
  104. printf("********************\n");
  105. }
  106. void test_lwext4_block_stats(void)
  107. {
  108. if (!bd)
  109. return;
  110. printf("********************\n");
  111. printf("ext4 blockdev stats\n");
  112. printf("bdev->bread_ctr = %" PRIu32 "\n", bd->bdif->bread_ctr);
  113. printf("bdev->bwrite_ctr = %" PRIu32 "\n", bd->bdif->bwrite_ctr);
  114. printf("bcache->ref_blocks = %" PRIu32 "\n", bd->bc->ref_blocks);
  115. printf("bcache->max_ref_blocks = %" PRIu32 "\n", bd->bc->max_ref_blocks);
  116. printf("bcache->lru_ctr = %" PRIu32 "\n", bd->bc->lru_ctr);
  117. printf("\n");
  118. printf("********************\n");
  119. }
  120. bool test_lwext4_dir_test(int len)
  121. {
  122. ext4_file f;
  123. int r;
  124. int i;
  125. char path[64];
  126. long int diff;
  127. long int stop;
  128. long int start;
  129. printf("test_lwext4_dir_test: %d\n", len);
  130. io_timings_clear();
  131. start = get_ms();
  132. printf("directory create: /mp/dir1\n");
  133. r = ext4_dir_mk("/mp/dir1");
  134. if (r != EOK) {
  135. printf("ext4_dir_mk: rc = %d\n", r);
  136. return false;
  137. }
  138. printf("add files to: /mp/dir1\n");
  139. for (i = 0; i < len; ++i) {
  140. sprintf(path, "/mp/dir1/f%d", i);
  141. r = ext4_fopen(&f, path, "wb");
  142. if (r != EOK) {
  143. printf("ext4_fopen: rc = %d\n", r);
  144. return false;
  145. }
  146. }
  147. stop = get_ms();
  148. diff = stop - start;
  149. test_lwext4_dir_ls("/mp/dir1");
  150. printf("test_lwext4_dir_test: time: %d ms\n", (int)diff);
  151. printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1));
  152. printf_io_timings(diff);
  153. return true;
  154. }
  155. static int verify_buf(const unsigned char *b, size_t len, unsigned char c)
  156. {
  157. size_t i;
  158. for (i = 0; i < len; ++i) {
  159. if (b[i] != c)
  160. return c - b[i];
  161. }
  162. return 0;
  163. }
  164. bool test_lwext4_file_test(uint8_t *rw_buff, uint32_t rw_size, uint32_t rw_count)
  165. {
  166. int r;
  167. size_t size;
  168. uint32_t i;
  169. long int start;
  170. long int stop;
  171. long int diff;
  172. uint32_t kbps;
  173. uint64_t size_bytes;
  174. ext4_file f;
  175. printf("file_test:\n");
  176. printf(" rw size: %" PRIu32 "\n", rw_size);
  177. printf(" rw count: %" PRIu32 "\n", rw_count);
  178. /*Add hello world file.*/
  179. r = ext4_fopen(&f, "/mp/hello.txt", "wb");
  180. r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
  181. r = ext4_fclose(&f);
  182. io_timings_clear();
  183. start = get_ms();
  184. r = ext4_fopen(&f, "/mp/test1", "wb");
  185. if (r != EOK) {
  186. printf("ext4_fopen ERROR = %d\n", r);
  187. return false;
  188. }
  189. printf("ext4_write: %" PRIu32 " * %" PRIu32 " ...\n", rw_size,
  190. rw_count);
  191. for (i = 0; i < rw_count; ++i) {
  192. memset(rw_buff, i % 10 + '0', rw_size);
  193. r = ext4_fwrite(&f, rw_buff, rw_size, &size);
  194. if ((r != EOK) || (size != rw_size))
  195. break;
  196. }
  197. if (i != rw_count) {
  198. printf(" file_test: rw_count = %" PRIu32 "\n", i);
  199. return false;
  200. }
  201. stop = get_ms();
  202. diff = stop - start;
  203. size_bytes = rw_size * rw_count;
  204. size_bytes = (size_bytes * 1000) / 1024;
  205. kbps = (size_bytes) / (diff + 1);
  206. printf(" write time: %d ms\n", (int)diff);
  207. printf(" write speed: %" PRIu32 " KB/s\n", kbps);
  208. printf_io_timings(diff);
  209. r = ext4_fclose(&f);
  210. io_timings_clear();
  211. start = get_ms();
  212. r = ext4_fopen(&f, "/mp/test1", "r+");
  213. if (r != EOK) {
  214. printf("ext4_fopen ERROR = %d\n", r);
  215. return false;
  216. }
  217. printf("ext4_read: %" PRIu32 " * %" PRIu32 " ...\n", rw_size, rw_count);
  218. for (i = 0; i < rw_count; ++i) {
  219. r = ext4_fread(&f, rw_buff, rw_size, &size);
  220. if ((r != EOK) || (size != rw_size))
  221. break;
  222. if (verify_buf(rw_buff, rw_size, i % 10 + '0'))
  223. break;
  224. }
  225. if (i != rw_count) {
  226. printf(" file_test: rw_count = %" PRIu32 "\n", i);
  227. return false;
  228. }
  229. stop = get_ms();
  230. diff = stop - start;
  231. size_bytes = rw_size * rw_count;
  232. size_bytes = (size_bytes * 1000) / 1024;
  233. kbps = (size_bytes) / (diff + 1);
  234. printf(" read time: %d ms\n", (int)diff);
  235. printf(" read speed: %d KB/s\n", (int)kbps);
  236. printf_io_timings(diff);
  237. r = ext4_fclose(&f);
  238. return true;
  239. }
  240. void test_lwext4_cleanup(void)
  241. {
  242. long int start;
  243. long int stop;
  244. long int diff;
  245. int r;
  246. printf("\ncleanup:\n");
  247. r = ext4_fremove("/mp/hello.txt");
  248. if (r != EOK && r != ENOENT) {
  249. printf("ext4_fremove error: rc = %d\n", r);
  250. }
  251. printf("remove /mp/test1\n");
  252. r = ext4_fremove("/mp/test1");
  253. if (r != EOK && r != ENOENT) {
  254. printf("ext4_fremove error: rc = %d\n", r);
  255. }
  256. printf("remove /mp/dir1\n");
  257. io_timings_clear();
  258. start = get_ms();
  259. r = ext4_dir_rm("/mp/dir1");
  260. if (r != EOK && r != ENOENT) {
  261. printf("ext4_fremove ext4_dir_rm: rc = %d\n", r);
  262. }
  263. stop = get_ms();
  264. diff = stop - start;
  265. printf("cleanup: time: %d ms\n", (int)diff);
  266. printf_io_timings(diff);
  267. }
  268. bool test_lwext4_mount(struct ext4_blockdev *bdev, struct ext4_bcache *bcache)
  269. {
  270. int r;
  271. bc = bcache;
  272. bd = bdev;
  273. if (!bd) {
  274. printf("test_lwext4_mount: no block device\n");
  275. return false;
  276. }
  277. ext4_dmask_set(DEBUG_ALL);
  278. r = ext4_device_register(bd, "ext4_fs");
  279. if (r != EOK) {
  280. printf("ext4_device_register: rc = %d\n", r);
  281. return false;
  282. }
  283. r = ext4_mount("ext4_fs", "/mp/", false);
  284. if (r != EOK) {
  285. printf("ext4_mount: rc = %d\n", r);
  286. return false;
  287. }
  288. r = ext4_recover("/mp/");
  289. if (r != EOK && r != ENOTSUP) {
  290. printf("ext4_recover: rc = %d\n", r);
  291. return false;
  292. }
  293. r = ext4_journal_start("/mp/");
  294. if (r != EOK) {
  295. printf("ext4_journal_start: rc = %d\n", r);
  296. return false;
  297. }
  298. ext4_cache_write_back("/mp/", 1);
  299. return true;
  300. }
  301. bool test_lwext4_umount(void)
  302. {
  303. int r;
  304. ext4_cache_write_back("/mp/", 0);
  305. r = ext4_journal_stop("/mp/");
  306. if (r != EOK) {
  307. printf("ext4_journal_stop: fail %d", r);
  308. return false;
  309. }
  310. r = ext4_umount("/mp/");
  311. if (r != EOK) {
  312. printf("ext4_umount: fail %d", r);
  313. return false;
  314. }
  315. return true;
  316. }