filex_media_cache_invalidate_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* This FileX test concentrates on the media cache invalidate operation. */
  2. #ifndef FX_STANDALONE_ENABLE
  3. #include "tx_api.h"
  4. #endif
  5. #include "fx_api.h"
  6. #include <stdio.h>
  7. #include "fx_ram_driver_test.h"
  8. void test_control_return(UINT status);
  9. #ifndef FX_DISABLE_CACHE
  10. #define DEMO_STACK_SIZE 4096
  11. #define CACHE_SIZE 16*128
  12. /* Define the ThreadX and FileX object control blocks... */
  13. #ifndef FX_STANDALONE_ENABLE
  14. static TX_THREAD ftest_0;
  15. #endif
  16. static FX_MEDIA ram_disk;
  17. static FX_FILE my_file;
  18. /* Define the counters used in the test application... */
  19. #ifndef FX_STANDALONE_ENABLE
  20. static UCHAR *ram_disk_memory;
  21. static UCHAR *cache_buffer;
  22. #else
  23. static UCHAR cache_buffer[CACHE_SIZE];
  24. #endif
  25. /* Define thread prototypes. */
  26. void filex_media_cache_invalidate_application_define(void *first_unused_memory);
  27. static void ftest_0_entry(ULONG thread_input);
  28. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  29. /* Define what the initial system looks like. */
  30. #ifdef CTEST
  31. void test_application_define(void *first_unused_memory)
  32. #else
  33. void filex_media_cache_invalidate_application_define(void *first_unused_memory)
  34. #endif
  35. {
  36. #ifndef FX_STANDALONE_ENABLE
  37. UCHAR *pointer;
  38. /* Setup the working pointer. */
  39. pointer = (UCHAR *) first_unused_memory;
  40. /* Create the main thread. */
  41. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  42. pointer, DEMO_STACK_SIZE,
  43. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  44. pointer = pointer + DEMO_STACK_SIZE;
  45. /* Setup memory for the RAM disk and the sector cache. */
  46. cache_buffer = pointer;
  47. pointer = pointer + CACHE_SIZE;
  48. ram_disk_memory = pointer;
  49. #endif
  50. /* Initialize the FileX system. */
  51. fx_system_initialize();
  52. #ifdef FX_STANDALONE_ENABLE
  53. ftest_0_entry(0);
  54. #endif
  55. }
  56. /* Define the test threads. */
  57. static void ftest_0_entry(ULONG thread_input)
  58. {
  59. UINT status;
  60. ULONG actual;
  61. ULONG read_value;
  62. ULONG write_value;
  63. ULONG available_bytes;
  64. ULONG i;
  65. FX_PARAMETER_NOT_USED(thread_input);
  66. /* Print out some test information banners. */
  67. printf("FileX Test: Media cache invalidate test............................");
  68. /* Format the media. This needs to be done before opening it! */
  69. status = fx_media_format(&ram_disk,
  70. _fx_ram_driver, // Driver entry
  71. ram_disk_memory, // RAM disk memory pointer
  72. cache_buffer, // Media buffer pointer
  73. CACHE_SIZE, // Media buffer size
  74. "MY_RAM_DISK", // Volume Name
  75. 1, // Number of FATs
  76. 32, // Directory Entries
  77. 0, // Hidden sectors
  78. 256, // Total sectors
  79. 128, // Sector size
  80. 1, // Sectors per cluster
  81. 1, // Heads
  82. 1); // Sectors per track
  83. /* Determine if the format had an error. */
  84. if (status)
  85. {
  86. printf("ERROR!\n");
  87. test_control_return(2);
  88. }
  89. /* Open the ram_disk. */
  90. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  91. /* Check the status. */
  92. if (status != FX_SUCCESS)
  93. {
  94. /* Error, return error code. */
  95. printf("ERROR!\n");
  96. test_control_return(21);
  97. }
  98. #ifndef FX_DISABLE_ERROR_CHECKING
  99. /* send a null pointer to generate an error */
  100. status = fx_media_cache_invalidate(FX_NULL);
  101. if (status != FX_PTR_ERROR)
  102. {
  103. printf("ERROR!\n");
  104. test_control_return(8);
  105. }
  106. #endif
  107. /* Create a file called TEST.TXT in the root directory. */
  108. status = fx_file_create(&ram_disk, "TEST.TXT");
  109. /* Check the create status. */
  110. if (status != FX_SUCCESS)
  111. {
  112. printf("ERROR!\n");
  113. test_control_return(3);
  114. }
  115. /* Open the test file. */
  116. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  117. /* Check the file open status. */
  118. if (status != FX_SUCCESS)
  119. {
  120. printf("ERROR!\n");
  121. test_control_return(4);
  122. }
  123. /* Pickup the available bytes in the media. */
  124. status = fx_media_space_available(&ram_disk, &available_bytes);
  125. /* Check for available bytes error. */
  126. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  127. {
  128. printf("ERROR!\n");
  129. test_control_return(5);
  130. }
  131. /* Loop to write successive bytes out to the file.... to fill the media! */
  132. i = 0;
  133. write_value = 0;
  134. while (i < available_bytes)
  135. {
  136. /* Write 4 bytes to the file. */
  137. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  138. /* Check the file write status. */
  139. if (status != FX_SUCCESS)
  140. {
  141. printf("ERROR!\n");
  142. test_control_return(6);
  143. }
  144. /* Increment byte count. */
  145. i = i + sizeof(ULONG);
  146. /* Increment write value. */
  147. write_value++;
  148. }
  149. /* Pickup the available bytes in the media again. */
  150. status = fx_media_space_available(&ram_disk, &i);
  151. /* Check for available bytes error. */
  152. if ((status != FX_SUCCESS) || (i != 0))
  153. {
  154. printf("ERROR!\n");
  155. test_control_return(7);
  156. }
  157. /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
  158. dirty sectors are written. */
  159. status = fx_media_cache_invalidate(&ram_disk);
  160. /* Check for flush errors. */
  161. if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
  162. {
  163. printf("ERROR!\n");
  164. test_control_return(8);
  165. }
  166. /* See if any sectors are still valid in the cache. */
  167. for (i = 0; i < FX_MAX_SECTOR_CACHE; i++)
  168. {
  169. /* Determine if this cache entry is still valid. */
  170. if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
  171. {
  172. printf("ERROR!\n");
  173. test_control_return(81);
  174. }
  175. }
  176. /* Seek to the beginning of the test file. */
  177. status = fx_file_seek(&my_file, 0);
  178. /* Check the file seek status. */
  179. if (status != FX_SUCCESS)
  180. {
  181. printf("ERROR!\n");
  182. test_control_return(9);
  183. }
  184. /* Now read in all the bytes again to make sure the file contents are really there. */
  185. i = 0;
  186. read_value = 0;
  187. while (i < available_bytes)
  188. {
  189. /* Read 4 bytes from the file. */
  190. status = fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
  191. /* Check the file read status. */
  192. if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
  193. {
  194. printf("ERROR!\n");
  195. test_control_return(10);
  196. }
  197. /* Increment byte count. */
  198. i = i + sizeof(ULONG);
  199. }
  200. /* Close the test file. */
  201. status = fx_file_close(&my_file);
  202. /* Check the file close status. */
  203. if (status != FX_SUCCESS)
  204. {
  205. printf("ERROR!\n");
  206. test_control_return(11);
  207. }
  208. /* Close the media. */
  209. status = fx_media_close(&ram_disk);
  210. /* Check the media close status. */
  211. if (status != FX_SUCCESS)
  212. {
  213. printf("ERROR!\n");
  214. test_control_return(12);
  215. }
  216. /* Reformat the media. This needs to be done before opening it! */
  217. status = fx_media_format(&ram_disk,
  218. _fx_ram_driver, // Driver entry
  219. ram_disk_memory, // RAM disk memory pointer
  220. cache_buffer, // Media buffer pointer
  221. CACHE_SIZE, // Media buffer size
  222. "MY_RAM_DISK", // Volume Name
  223. 1, // Number of FATs
  224. 32, // Directory Entries
  225. 0, // Hidden sectors
  226. 256, // Total sectors
  227. 128, // Sector size
  228. 1, // Sectors per cluster
  229. 1, // Heads
  230. 1); // Sectors per track
  231. /* Determine if the format had an error. */
  232. if (status)
  233. {
  234. printf("ERROR!\n");
  235. test_control_return(13);
  236. }
  237. /* Open the ram_disk, but do so to ensure non-hashed algorithm is used by supplying CACHE_SIZE-1. */
  238. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
  239. /* Check the status. */
  240. if (status != FX_SUCCESS)
  241. {
  242. /* Error, return error code. */
  243. printf("ERROR!\n");
  244. test_control_return(14);
  245. }
  246. /* Create a file called TEST.TXT in the root directory. */
  247. status = fx_file_create(&ram_disk, "TEST.TXT");
  248. /* Check the create status. */
  249. if (status != FX_SUCCESS)
  250. {
  251. printf("ERROR!\n");
  252. test_control_return(15);
  253. }
  254. /* Open the test file. */
  255. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  256. /* Check the file open status. */
  257. if (status != FX_SUCCESS)
  258. {
  259. printf("ERROR!\n");
  260. test_control_return(16);
  261. }
  262. /* Pickup the available bytes in the media. */
  263. status = fx_media_space_available(&ram_disk, &available_bytes);
  264. /* Check for available bytes error. */
  265. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  266. {
  267. printf("ERROR!\n");
  268. test_control_return(17);
  269. }
  270. /* Loop to write successive bytes out to the file.... to fill the media! */
  271. i = 0;
  272. write_value = 0;
  273. while (i < available_bytes)
  274. {
  275. /* Write 4 bytes to the file. */
  276. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  277. /* Check the file write status. */
  278. if (status != FX_SUCCESS)
  279. {
  280. printf("ERROR!\n");
  281. test_control_return(18);
  282. }
  283. /* Increment byte count. */
  284. i = i + sizeof(ULONG);
  285. /* Increment write value. */
  286. write_value++;
  287. }
  288. /* Pickup the available bytes in the media again. */
  289. status = fx_media_space_available(&ram_disk, &i);
  290. /* Check for available bytes error. */
  291. if ((status != FX_SUCCESS) || (i != 0))
  292. {
  293. printf("ERROR!\n");
  294. test_control_return(19);
  295. }
  296. /* At this point, we should invalidate the media to ensure that all
  297. dirty sectors are written. */
  298. status = fx_media_cache_invalidate(&ram_disk);
  299. /* Check for flush errors. */
  300. if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
  301. {
  302. printf("ERROR!\n");
  303. test_control_return(20);
  304. }
  305. /* See if any sectors are still valid in the cache. */
  306. for (i = 0; i < ram_disk.fx_media_sector_cache_size; i++)
  307. {
  308. /* Determine if this cache entry is still valid. */
  309. if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
  310. {
  311. printf("ERROR!\n");
  312. test_control_return(81);
  313. }
  314. }
  315. /* Seek to the beginning of the test file. */
  316. status = fx_file_seek(&my_file, 0);
  317. /* Check the file seek status. */
  318. if (status != FX_SUCCESS)
  319. {
  320. printf("ERROR!\n");
  321. test_control_return(22);
  322. }
  323. /* Now read in all the bytes again to make sure the file contents are really there. */
  324. i = 0;
  325. read_value = 0;
  326. while (i < available_bytes)
  327. {
  328. /* Read 4 bytes from the file. */
  329. status = fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
  330. /* Check the file read status. */
  331. if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
  332. {
  333. printf("ERROR!\n");
  334. test_control_return(23);
  335. }
  336. /* Increment byte count. */
  337. i = i + sizeof(ULONG);
  338. }
  339. /* Close the test file. */
  340. status = fx_file_close(&my_file);
  341. /* Check the file close status. */
  342. if (status != FX_SUCCESS)
  343. {
  344. printf("ERROR!\n");
  345. test_control_return(24);
  346. }
  347. /* Close the media. */
  348. status = fx_media_close(&ram_disk);
  349. /* Check the media close status. */
  350. if (status != FX_SUCCESS)
  351. {
  352. printf("ERROR!\n");
  353. test_control_return(25);
  354. }
  355. /* Determine if the test was successful. */
  356. if (status != FX_SUCCESS)
  357. {
  358. printf("ERROR!\n");
  359. test_control_return(26);
  360. }
  361. else
  362. {
  363. printf("SUCCESS!\n");
  364. test_control_return(0);
  365. }
  366. }
  367. #else
  368. #ifdef CTEST
  369. void test_application_define(void *first_unused_memory)
  370. #else
  371. void filex_media_cache_invalidate_application_define(void *first_unused_memory)
  372. #endif
  373. {
  374. FX_PARAMETER_NOT_USED(first_unused_memory);
  375. /* Print out some test information banners. */
  376. printf("FileX Test: Media cache invalidate test............................N/A\n");
  377. test_control_return(255);
  378. }
  379. #endif