fs_test.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-01-02 aozima the first version.
  9. * 2011-03-17 aozima fix some bug.
  10. * 2011-03-18 aozima to dynamic thread.
  11. */
  12. #include <rtthread.h>
  13. #include <dfs_posix.h>
  14. static rt_uint32_t stop_flag = 0;
  15. static rt_thread_t fsrw1_thread = RT_NULL;
  16. static rt_thread_t fsrw2_thread = RT_NULL;
  17. #define fsrw1_fn "/test1.dat"
  18. #define fsrw1_data_len 120 /* Less than 256 */
  19. static void fsrw1_thread_entry(void* parameter)
  20. {
  21. int fd;
  22. int index,length;
  23. rt_uint32_t round;
  24. rt_uint32_t tick_start,tick_end,read_speed,write_speed;
  25. static rt_uint8_t write_data1[fsrw1_data_len];
  26. static rt_uint8_t read_data1[fsrw1_data_len];
  27. round = 1;
  28. while(1)
  29. {
  30. if( stop_flag )
  31. {
  32. rt_kprintf("thread fsrw2 error,thread fsrw1 quit!\r\n");
  33. fsrw1_thread = RT_NULL;
  34. stop_flag = 0;
  35. return;
  36. }
  37. /* creat file */
  38. fd = open(fsrw1_fn, O_WRONLY | O_CREAT | O_TRUNC, 0);
  39. if (fd < 0)
  40. {
  41. rt_kprintf("fsrw1 open file for write failed\n");
  42. stop_flag = 1;
  43. fsrw1_thread = RT_NULL;
  44. return;
  45. }
  46. /* plan write data */
  47. for (index = 0; index < fsrw1_data_len; index ++)
  48. {
  49. write_data1[index] = index;
  50. }
  51. /* write 8000 times */
  52. tick_start = rt_tick_get();
  53. for(index=0; index<8000; index++)
  54. {
  55. length = write(fd, write_data1, fsrw1_data_len);
  56. if (length != fsrw1_data_len)
  57. {
  58. rt_kprintf("fsrw1 write data failed\n");
  59. close(fd);
  60. fsrw1_thread = RT_NULL;
  61. stop_flag = 1;
  62. return;
  63. }
  64. }
  65. tick_end = rt_tick_get();
  66. write_speed = fsrw1_data_len*8000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
  67. /* close file */
  68. close(fd);
  69. /* open file read only */
  70. fd = open(fsrw1_fn, O_RDONLY, 0);
  71. if (fd < 0)
  72. {
  73. rt_kprintf("fsrw1 open file for read failed\n");
  74. stop_flag = 1;
  75. fsrw1_thread = RT_NULL;
  76. return;
  77. }
  78. /* verify data */
  79. tick_start = rt_tick_get();
  80. for(index=0; index<8000; index++)
  81. {
  82. rt_uint32_t i;
  83. length = read(fd, read_data1, fsrw1_data_len);
  84. if (length != fsrw1_data_len)
  85. {
  86. rt_kprintf("fsrw1 read file failed\r\n");
  87. close(fd);
  88. stop_flag = 1;
  89. fsrw1_thread = RT_NULL;
  90. return;
  91. }
  92. for(i=0; i<fsrw1_data_len; i++)
  93. {
  94. if( read_data1[i] != write_data1[i] )
  95. {
  96. rt_kprintf("fsrw1 data error!\r\n");
  97. close(fd);
  98. stop_flag = 1;
  99. fsrw1_thread = RT_NULL;
  100. return;
  101. }
  102. }
  103. }
  104. tick_end = rt_tick_get();
  105. read_speed = fsrw1_data_len*8000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
  106. rt_kprintf("thread fsrw1 round %d ",round++);
  107. rt_kprintf("rd:%dbyte/s,wr:%dbyte/s\r\n",read_speed,write_speed);
  108. /* close file */
  109. close(fd);
  110. }
  111. }
  112. #define fsrw2_fn "/test2.dat"
  113. #define fsrw2_data_len 180 /* Less than 256 */
  114. static void fsrw2_thread_entry(void* parameter)
  115. {
  116. int fd;
  117. int index,length;
  118. rt_uint32_t round;
  119. rt_uint32_t tick_start,tick_end,read_speed,write_speed;
  120. static rt_uint8_t write_data2[fsrw2_data_len];
  121. static rt_uint8_t read_data2[fsrw2_data_len];
  122. round = 1;
  123. while(1)
  124. {
  125. if( stop_flag )
  126. {
  127. rt_kprintf("thread fsrw1 error,thread fsrw2 quit!\r\n");
  128. fsrw2_thread = RT_NULL;
  129. stop_flag = 0;
  130. return;
  131. }
  132. /* creat file */
  133. fd = open(fsrw2_fn, O_WRONLY | O_CREAT | O_TRUNC, 0);
  134. if (fd < 0)
  135. {
  136. rt_kprintf("fsrw2 open file for write failed\n");
  137. stop_flag = 1;
  138. fsrw2_thread = RT_NULL;
  139. return;
  140. }
  141. /* plan write data */
  142. for (index = 0; index < fsrw2_data_len; index ++)
  143. {
  144. write_data2[index] = index;
  145. }
  146. /* write 5000 times */
  147. tick_start = rt_tick_get();
  148. for(index=0; index<5000; index++)
  149. {
  150. length = write(fd, write_data2, fsrw2_data_len);
  151. if (length != fsrw2_data_len)
  152. {
  153. rt_kprintf("fsrw2 write data failed\n");
  154. close(fd);
  155. stop_flag = 1;
  156. fsrw2_thread = RT_NULL;
  157. return;
  158. }
  159. }
  160. tick_end = rt_tick_get();
  161. write_speed = fsrw2_data_len*5000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
  162. /* close file */
  163. close(fd);
  164. /* open file read only */
  165. fd = open(fsrw2_fn, O_RDONLY, 0);
  166. if (fd < 0)
  167. {
  168. rt_kprintf("fsrw2 open file for read failed\n");
  169. stop_flag = 1;
  170. fsrw2_thread = RT_NULL;
  171. return;
  172. }
  173. /* verify data */
  174. tick_start = rt_tick_get();
  175. for(index=0; index<5000; index++)
  176. {
  177. rt_uint32_t i;
  178. length = read(fd, read_data2, fsrw2_data_len);
  179. if (length != fsrw2_data_len)
  180. {
  181. rt_kprintf("fsrw2 read file failed\r\n");
  182. close(fd);
  183. stop_flag = 1;
  184. fsrw2_thread = RT_NULL;
  185. return;
  186. }
  187. for(i=0; i<fsrw2_data_len; i++)
  188. {
  189. if( read_data2[i] != write_data2[i] )
  190. {
  191. rt_kprintf("fsrw2 data error!\r\n");
  192. close(fd);
  193. stop_flag = 1;
  194. fsrw2_thread = RT_NULL;
  195. return;
  196. }
  197. }
  198. }
  199. tick_end = rt_tick_get();
  200. read_speed = fsrw2_data_len*5000UL*RT_TICK_PER_SECOND/(tick_end-tick_start);
  201. rt_kprintf("thread fsrw2 round %d ",round++);
  202. rt_kprintf("rd:%dbyte/s,wr:%dbyte/s\r\n",read_speed,write_speed);
  203. /* close file */
  204. close(fd);
  205. }
  206. }
  207. /** \brief startup filesystem read/write test(multi thread).
  208. *
  209. * \param arg rt_uint32_t [0]startup thread1,[1]startup thread2.
  210. * \return void
  211. *
  212. */
  213. void fs_test(rt_uint32_t arg)
  214. {
  215. rt_kprintf("arg is : 0x%02X ",arg);
  216. if(arg & 0x01)
  217. {
  218. if( fsrw1_thread != RT_NULL )
  219. {
  220. rt_kprintf("fsrw1_thread already exists!\r\n");
  221. }
  222. else
  223. {
  224. fsrw1_thread = rt_thread_create( "fsrw1",
  225. fsrw1_thread_entry,
  226. RT_NULL,
  227. 2048,
  228. RT_THREAD_PRIORITY_MAX-2,
  229. 1);
  230. if ( fsrw1_thread != RT_NULL)
  231. {
  232. rt_thread_startup(fsrw1_thread);
  233. }
  234. }
  235. }
  236. if( arg & 0x02)
  237. {
  238. if( fsrw2_thread != RT_NULL )
  239. {
  240. rt_kprintf("fsrw2_thread already exists!\r\n");
  241. }
  242. else
  243. {
  244. fsrw2_thread = rt_thread_create( "fsrw2",
  245. fsrw2_thread_entry,
  246. RT_NULL,
  247. 2048,
  248. RT_THREAD_PRIORITY_MAX-2,
  249. 1);
  250. if ( fsrw2_thread != RT_NULL)
  251. {
  252. rt_thread_startup(fsrw2_thread);
  253. }
  254. }
  255. }
  256. }
  257. #ifdef RT_USING_FINSH
  258. #include <finsh.h>
  259. FINSH_FUNCTION_EXPORT(fs_test, file system R/W test. e.g: fs_test(3));
  260. #endif