TestFs.Mod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. MODULE TestFs;
  2. IMPORT
  3. SYSTEM, Random, Files, Kernel, Commands, FoxBasic;
  4. TYPE
  5. Writer = OBJECT
  6. VAR
  7. id: LONGINT;
  8. file: Files.File;
  9. fw: Files.Writer;
  10. fileSize: LONGINT;
  11. byteCount: LONGINT;
  12. rndData, rndUpdate: Random.Generator;
  13. tStart: Kernel.MilliTimer;
  14. run := FALSE, exited := FALSE: BOOLEAN;
  15. PROCEDURE &InitWriter(id: LONGINT; CONST fileName: ARRAY OF CHAR; fileSize: LONGINT);
  16. BEGIN
  17. file := Files.New(fileName);
  18. ASSERT(file # NIL);
  19. Files.Register(file);
  20. Files.OpenWriter(fw,file,0);
  21. NEW(rndData);
  22. rndData.InitSeed(id);
  23. NEW(rndUpdate);
  24. rndUpdate.InitSeed(-id);
  25. SELF.id := id;
  26. SELF.fileSize := fileSize;
  27. run := TRUE;
  28. END InitWriter;
  29. PROCEDURE Exit;
  30. BEGIN{EXCLUSIVE}
  31. run := FALSE;
  32. AWAIT(exited);
  33. END Exit;
  34. PROCEDURE Start;
  35. BEGIN {EXCLUSIVE}
  36. run := TRUE
  37. END Start;
  38. PROCEDURE Pause;
  39. BEGIN {EXCLUSIVE}
  40. run := FALSE
  41. END Pause;
  42. PROCEDURE Running (): BOOLEAN;
  43. BEGIN {EXCLUSIVE}
  44. RETURN run
  45. END Running;
  46. PROCEDURE Exited (): BOOLEAN;
  47. BEGIN {EXCLUSIVE}
  48. RETURN exited
  49. END Exited;
  50. PROCEDURE Loop1;
  51. VAR
  52. v: LONGINT;
  53. BEGIN
  54. WHILE run & (byteCount < fileSize) DO
  55. v := rndData.Dice(256);
  56. fw.Char(CHR(v));
  57. INC(byteCount);
  58. IF rndUpdate.Dice(4096) = 0 THEN (* Files.DefaultWriterSize *)
  59. fw.Update;
  60. END;
  61. END;
  62. fw.Update;
  63. run := FALSE;
  64. END Loop1;
  65. PROCEDURE Loop2;
  66. TYPE
  67. Char4 = ARRAY 4 OF CHAR;
  68. VAR
  69. buf: ARRAY 4096 OF CHAR; (* Files.DefaultWriterSize *)
  70. v: LONGINT;
  71. i: LONGINT;
  72. BEGIN
  73. WHILE run & (byteCount < fileSize) & (fw.res = 0) DO
  74. FOR i := 0 TO LEN(buf)-1 BY 4 DO
  75. v := rndData.Integer();
  76. SYSTEM.MOVE(ADDRESSOF(v),ADDRESSOF(buf[i]),SIZEOF(LONGINT));
  77. END;
  78. fw.Bytes(buf,0,LEN(buf));
  79. fw.Update;
  80. INC(byteCount,LEN(buf));
  81. END;
  82. run := FALSE;
  83. IF fw.res # 0 THEN
  84. TRACE("TestFs writer",id,"stopped due to a writer error",fw.res);
  85. END;
  86. END Loop2;
  87. BEGIN{ACTIVE}
  88. Kernel.SetTimer(tStart,0);
  89. Loop1;
  90. FINALLY
  91. BEGIN{EXCLUSIVE}
  92. exited := TRUE;
  93. END;
  94. END Writer;
  95. (* High-level reader activity *)
  96. Reader = OBJECT
  97. VAR
  98. id: LONGINT;
  99. file: Files.File;
  100. reader: Files.Reader;
  101. rndData: Random.Generator;
  102. byteCount: LONGINT;
  103. run, exited: BOOLEAN;
  104. tStart: Kernel.MilliTimer;
  105. PROCEDURE & Init*(id: LONGINT; CONST filename: ARRAY OF CHAR);
  106. BEGIN
  107. SELF.id := id;
  108. file := Files.Old(filename);
  109. ASSERT(file # NIL);
  110. Files.OpenReader(reader, file, 0);
  111. ASSERT(reader # NIL);
  112. NEW(rndData);
  113. rndData.InitSeed(id);
  114. run := TRUE;
  115. END Init;
  116. PROCEDURE Exit;
  117. BEGIN {EXCLUSIVE}
  118. run := FALSE;
  119. AWAIT(exited);
  120. END Exit;
  121. PROCEDURE Start;
  122. BEGIN {EXCLUSIVE}
  123. run := TRUE
  124. END Start;
  125. PROCEDURE Pause;
  126. BEGIN {EXCLUSIVE}
  127. run := FALSE
  128. END Pause;
  129. PROCEDURE Running (): BOOLEAN;
  130. BEGIN {EXCLUSIVE}
  131. RETURN run
  132. END Running;
  133. PROCEDURE Exited (): BOOLEAN;
  134. BEGIN {EXCLUSIVE}
  135. RETURN exited
  136. END Exited;
  137. PROCEDURE Loop;
  138. VAR
  139. buf: ARRAY 4096 OF CHAR;
  140. i, len: LONGINT;
  141. BEGIN
  142. WHILE run & (reader.res = 0) DO
  143. reader.Bytes(buf, 0, LEN(buf), len);
  144. INC(byteCount, len);
  145. FOR i := 0 TO len - 1 DO ASSERT(ORD(buf[i]) = rndData.Dice(256)); END;
  146. END
  147. END Loop;
  148. BEGIN {ACTIVE}
  149. Kernel.SetTimer(tStart, 0);
  150. Loop;
  151. FINALLY
  152. BEGIN {EXCLUSIVE}
  153. exited := TRUE;
  154. END;
  155. END Reader;
  156. VAR
  157. writers: FoxBasic.List;
  158. readers: FoxBasic.List;
  159. (**
  160. Start id fileName fileSize ~
  161. *)
  162. PROCEDURE StartWriter*(ctx: Commands.Context);
  163. VAR
  164. id: LONGINT;
  165. fileName: Files.FileName;
  166. fileSize: LONGINT;
  167. w: Writer;
  168. i: LONGINT;
  169. BEGIN{EXCLUSIVE}
  170. IF ~ctx.arg.GetInteger(id,FALSE) THEN ctx.result := 1; RETURN; END;
  171. IF ~ctx.arg.GetString(fileName) THEN ctx.result := 1; RETURN;END;
  172. IF ~ctx.arg.GetInteger(fileSize,FALSE) THEN ctx.result := 1; RETURN; END;
  173. i := 0;
  174. WHILE (i < writers.Length()) & (writers.Get(i)(Writer).id # id) DO
  175. INC(i);
  176. END;
  177. IF i < writers.Length() THEN
  178. ctx.error.String("TestFs writer with ID "); ctx.error.Int(id,0); ctx.error.String(" already running"); ctx.error.Ln;
  179. ctx.result := 1;
  180. RETURN;
  181. END;
  182. NEW(w,id,fileName,fileSize);
  183. writers.Add(w);
  184. ctx.out.String("Started TestFs writer with ID "); ctx.out.Int(id,0);
  185. ctx.out.String(", fileName='"); ctx.out.String(fileName);
  186. ctx.out.String("', fileSize="); ctx.out.Int(fileSize,0); ctx.out.Ln;
  187. END StartWriter;
  188. (* ID *)
  189. PROCEDURE StopWriter*(ctx: Commands.Context);
  190. VAR
  191. id: LONGINT;
  192. i: LONGINT;
  193. BEGIN{EXCLUSIVE}
  194. IF ~ctx.arg.GetInteger(id,FALSE) THEN ctx.result := 1; RETURN; END;
  195. i := 0;
  196. WHILE (i < writers.Length()) & (writers.Get(i)(Writer).id # id) DO
  197. INC(i);
  198. END;
  199. IF i < writers.Length() THEN
  200. writers.Get(i)(Writer).Exit;
  201. writers.RemoveByIndex(i);
  202. ctx.out.String("Stopped TestFs writer with ID "); ctx.out.Int(id,0); ctx.out.Ln;
  203. ELSE
  204. ctx.error.String("TestFs writer with ID "); ctx.error.Int(id,0); ctx.error.String(" not found"); ctx.error.Ln;
  205. ctx.result := 1;
  206. RETURN;
  207. END;
  208. END StopWriter;
  209. (**
  210. ReportWriter id ~
  211. *)
  212. PROCEDURE ReportWriter*(ctx: Commands.Context);
  213. VAR
  214. id: LONGINT;
  215. i: LONGINT;
  216. w: Writer;
  217. speed: REAL;
  218. BEGIN{EXCLUSIVE}
  219. IF ~ctx.arg.GetInteger(id,FALSE) THEN ctx.result := 1; RETURN; END;
  220. i := 0;
  221. WHILE (i < writers.Length()) & (writers.Get(i)(Writer).id # id) DO
  222. INC(i);
  223. END;
  224. IF i < writers.Length() THEN
  225. w := writers.Get(i)(Writer);
  226. ctx.out.String("byteCount="); ctx.out.Int(w.byteCount,0); ctx.out.Ln;
  227. speed := 1000.0 * REAL(w.byteCount) / Kernel.Elapsed(w.tStart);
  228. ctx.out.String("overall speed="); ctx.out.FloatFix(speed,0,5,0); ctx.out.String(" bytes/s"); ctx.out.Ln;
  229. ctx.out.String("status: ");
  230. IF w.Exited() THEN
  231. ctx.out.String("Finished");
  232. ELSIF w.Running() THEN
  233. ctx.out.String("Running");
  234. ELSE
  235. ctx.out.String("Paused");
  236. END;
  237. ctx.out.Ln;
  238. ELSE
  239. ctx.error.String("TestFs writer with ID "); ctx.error.Int(id,0); ctx.error.String(" not found"); ctx.error.Ln;
  240. ctx.result := 1;
  241. RETURN;
  242. END;
  243. END ReportWriter;
  244. PROCEDURE StartReader*(context : Commands.Context);
  245. VAR
  246. name: Files.FileName;
  247. r: Reader;
  248. id: LONGINT;
  249. i: LONGINT;
  250. BEGIN {EXCLUSIVE}
  251. IF ~context.arg.GetInteger(id, FALSE) THEN context.result := 1; RETURN; END;
  252. IF ~context.arg.GetString(name) THEN context.result := 1; RETURN; END;
  253. WHILE (i < readers.Length()) & (readers.Get(i)(Reader).id # id) DO INC(i); END;
  254. IF i < readers.Length() THEN
  255. context.error.String("TestFs reader with ID ");
  256. context.error.Int(id, 0);
  257. context.error.String(" already running.");
  258. context.error.Ln;
  259. context.result := 1;
  260. RETURN;
  261. END;
  262. NEW(r, id, name);
  263. readers.Add(r);
  264. context.out.String("Added TestFs reader with ID ");
  265. context.out.Int(id, 0);
  266. context.out.String(", filename='");
  267. context.out.String(name);
  268. context.out.String("'");
  269. context.out.Ln;
  270. END StartReader;
  271. PROCEDURE StopReader*(context : Commands.Context);
  272. VAR
  273. r: Reader;
  274. id: LONGINT;
  275. i: LONGINT;
  276. BEGIN {EXCLUSIVE}
  277. IF ~context.arg.GetInteger(id, FALSE) THEN context.result := 1; RETURN; END;
  278. WHILE (i < readers.Length()) & (readers.Get(i)(Reader).id # id) DO INC(i); END;
  279. IF i = readers.Length() THEN
  280. context.error.String("TestFs reader with ID ");
  281. context.error.Int(id, 0);
  282. context.error.String(" is not running");
  283. context.error.Ln;
  284. context.result := 1;
  285. RETURN;
  286. END;
  287. r := readers.Get(i)(Reader);
  288. r.Exit;
  289. readers.RemoveByIndex(i);
  290. context.out.String("TestFs reader ");
  291. context.out.Int(id, 0);
  292. context.out.String(" stopped");
  293. context.out.Ln;
  294. END StopReader;
  295. PROCEDURE ReportReader*(context : Commands.Context);
  296. VAR
  297. r: Reader;
  298. id: LONGINT;
  299. i: LONGINT;
  300. bytes: LONGINT;
  301. speed: LONGREAL;
  302. BEGIN {EXCLUSIVE}
  303. IF ~context.arg.GetInteger(id, FALSE) THEN context.result := 1; RETURN; END;
  304. WHILE (i < readers.Length()) & (readers.Get(i)(Reader).id # id) DO INC(i); END;
  305. IF i = readers.Length() THEN
  306. context.error.String("TestFs reader with ID ");
  307. context.error.Int(id, 0);
  308. context.error.String(" is not running");
  309. context.error.Ln;
  310. context.result := 1;
  311. RETURN;
  312. END;
  313. r := readers.Get(i)(Reader);
  314. bytes := r.byteCount;
  315. speed := 1000.0 * bytes / Kernel.Elapsed(r.tStart);
  316. context.out.String("byte count="); context.out.Int(r.byteCount, 0); context.out.Ln;
  317. context.out.String("overall speed="); context.out.FloatFix(speed,0,5,0); context.out.Ln;
  318. context.out.String("status: ");
  319. IF r.Exited() THEN
  320. context.out.String("Finished");
  321. ELSIF r.Running() THEN
  322. context.out.String("Running");
  323. ELSE
  324. context.out.String("Paused");
  325. END;
  326. context.out.Ln;
  327. END ReportReader;
  328. BEGIN
  329. NEW(writers,8);
  330. NEW(readers, 8);
  331. END TestFs.
  332. SystemTools.DoCommands
  333. TFTPServer.Start ~
  334. OEB.NewServer --type=Auto
  335. PLAN
  336. setsource TFTP 10.3.34.188
  337. mount A2 SD0 3
  338. load A2.Bin a2
  339. load build/TestFs.Gof t
  340. deploy t file A2:TestFs.Gof
  341. setinput auto.txt
  342. END
  343. ~
  344. (*OEB.NewServer --type=Interactive ~*)
  345. OEB.NewInterface --type=Udp --server=0 ~
  346. OEB.Server --start --all ~
  347. OEB.Interface --start --all --server=0 ~
  348. ~
  349. SystemTools.DoCommands
  350. TFTPServer.Start ~
  351. OEB.NewServer --type=Interactive ~
  352. OEB.NewInterface --type=Udp --server=0 ~
  353. OEB.Server --start --all ~
  354. OEB.Interface --start --all --server=0 ~
  355. ~
  356. StaticLinker.Link --fileName=A2.Bin --displacement=100000H -a --path=build/
  357. Runtime Initializer Platform FPE64 ARMRuntime Trace BootConfig Uart Machine Heaps Modules Objects Kernel KernelLog Plugins
  358. Streams Pipes Commands Reals Clock Dates Strings Files Disks Reflection TrapWriters Traps Locks Options PsConfig SdEnvironment
  359. Sd SdDisks SdControllers Caches DiskVolumes DiskFS BitSets StringPool ObjectFile Diagnostics GenericLinker GenericLoader
  360. BootConsole
  361. ~
  362. OEB.Command --server=0 --id=0
  363. setsource TFTP 10.3.34.188
  364. mount A2 SD0 3
  365. load A2.Bin a2
  366. save a2
  367. setinput auto.txt
  368. ~
  369. save a2
  370. ~
  371. OEB.Command --server=0 --id=0
  372. setsource TFTP 10.3.34.188
  373. mount A2 SD0 3
  374. deployfile build/TestFs.Gof A2:TestFs.Gof
  375. ~
  376. OEB.Command --server=0 --id=0
  377. start
  378. ~
  379. OEB.Command --server=0 --id=0
  380. setinput auto.txt
  381. ~
  382. OEB.Command --server=0 --id=0
  383. reset
  384. ~
  385. OEB.Interface --show --all --server=0 ~
  386. OEB.Server --show --all ~
  387. OEB.Session --show --all ~
  388. OEB.Server --kill --all ~
  389. SystemTools.FreeDownTo OEBInterfaces ~
  390. SystemTools.FreeDownTo UDP ~
  391. mount DATA AosFS SD0#4 '|' N
  392. TestFs.StartWriter 1234567 DATA:TestFs-123457.dat 1000000000
  393. TestFs.StartWriter 7654321 DATA:TestFs-7654321.dat 1000000000
  394. TestFs.StartReader 1234567 DATA:TestFs-123457.dat 1000000000
  395. TestFs.StartReader 7654321 DATA:TestFs-7654321.dat
  396. TestFs.ReportWriter 1234567
  397. TestFs.ReportWriter 7654321
  398. TestFs.ReportReader 1234567
  399. TestFs.ReportReader 1234567
  400. asd