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