Undo190spCheck.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Round 190sp (0.1.33 perf block E, QUEUE item 13): the undo history shares the song's big containers (core/CowVector.h).
  2. // Included in the app class of Main.cpp.
  3. // - undo190spCheck: --selftest "undo190sp" / --undo190sptest <report>: random edits through a real engine, then undo
  4. // all of them (each state the project's exact text, ProjectFile::toJsonExact, byte for byte), then redo all;
  5. // - runUndoMemory190sp: --undomem190sp <project> <report> [--steps N]: the project opened, N edits of the usual
  6. // kinds, the history's bytes and the process' heap / private memory before and after (QUEUE item 13's numbers).
  7. static juce::String undoState190sp (const lynx::Song& song)
  8. {
  9. auto copy = song;
  10. copy.info.workSeconds = 0; // (the project's clock is not part of the history, round 190re)
  11. copy.info.created = 0;
  12. return lynx::ProjectFile::toJsonExact (copy);
  13. }
  14. // round 190sz: the kinds of undoEdit190sp (the first 7 are 190sp's: the same random sequence with `kinds` = 7)
  15. static constexpr int undoKinds190sz = 12;
  16. // one edit of the usual kinds, through the engine (one undo step each); `kindOut`: which one it was. `kinds` (round
  17. // 190sz): 7 = 190sp's kinds; 12 adds a mixer slot's knob, an effect slot's saved state, a channel's plugin state, an
  18. // effect slot added / removed and a channel renamed
  19. static void undoEdit190sp (lynx::AudioEngine& engine, std::mt19937& rng, int* kindOut = nullptr, int kinds = 7)
  20. {
  21. using namespace lynx;
  22. const auto& s = engine.getSong();
  23. const auto pick = [&rng] (std::size_t n) { return n == 0 ? (std::size_t) 0 : (std::size_t) (rng() % n); };
  24. const auto kind = rng() % (unsigned) juce::jlimit (1, undoKinds190sz, kinds);
  25. if (kindOut != nullptr)
  26. *kindOut = (int) kind;
  27. const auto p = pick (s.patterns.size());
  28. const auto c = pick (s.channels.size());
  29. const auto k = pick (s.playlist.size());
  30. const auto value = (float) (rng() % 1000) / 1000.0f;
  31. engine.editSong ([&] (Song& song) {
  32. switch (kind)
  33. {
  34. case 0: // a note moved up a semitone
  35. if (p < song.patterns.size() && ! song.patterns[p].notes.empty())
  36. {
  37. auto& n = song.patterns[p].notes[pick (song.patterns[p].notes.size())];
  38. n.key = juce::jlimit (0, 127, n.key + 1);
  39. }
  40. break;
  41. case 1: // a note added at the start
  42. if (p < song.patterns.size())
  43. {
  44. Note n;
  45. n.channel = (int) c;
  46. n.key = 60 + (int) (rng() % 12);
  47. song.patterns[p].notes.insert (song.patterns[p].notes.cbegin(), n);
  48. }
  49. break;
  50. case 2: // a clip moved
  51. if (k < song.playlist.size())
  52. song.playlist[k].start += ppq;
  53. break;
  54. case 3: // a channel level
  55. if (c < song.channels.size())
  56. song.channels[c].volume = value;
  57. break;
  58. case 4: // a mixer track level
  59. if (! song.mixer.empty())
  60. song.mixer[pick (song.mixer.size())].volume = value;
  61. break;
  62. case 5: // a Patcher node moved (a Live rack's device)
  63. {
  64. // (round 190sz: found through the const view, so only the channel that is changed is copied)
  65. for (std::size_t i = 0; i < song.channels.size(); ++i)
  66. if (const auto& nodes = std::as_const (song.channels)[i].patcher.nodes; ! nodes.empty())
  67. {
  68. auto& ch = song.channels[i];
  69. ch.patcher.nodes[pick (nodes.size())].x += 5.0f;
  70. return;
  71. }
  72. break;
  73. }
  74. // ---- round 190sz (QUEUE item 54) ----
  75. case 7: // a mixer slot's knob
  76. case 8: // an effect slot's saved state (FL's raw state, or a hosted plugin's)
  77. {
  78. std::vector<std::pair<std::size_t, std::size_t>> slots;
  79. for (std::size_t t = 0; t < song.mixer.size(); ++t)
  80. for (std::size_t e = 0; e < std::as_const (song.mixer)[t].effects.size(); ++e)
  81. {
  82. const auto& slot = std::as_const (song.mixer)[t].effects[e];
  83. if (kind == 7 || ! slot.state.empty() || ! slot.plugin.state.empty())
  84. slots.emplace_back (t, e);
  85. }
  86. if (slots.empty())
  87. break;
  88. const auto [t, e] = slots[pick (slots.size())];
  89. auto& slot = song.mixer[t].effects[e];
  90. if (kind == 7)
  91. slot.params[pick (8)] = value;
  92. else if (! std::as_const (slot).state.empty())
  93. slot.state[pick (slot.state.size())] ^= 0x01;
  94. else
  95. {
  96. auto text = slot.plugin.state.str();
  97. auto& c = text[pick (text.size())];
  98. c = c == 'A' ? 'B' : 'A';
  99. slot.plugin.state = std::move (text);
  100. }
  101. break;
  102. }
  103. case 9: // a channel's plugin state (a native FL engine's saved state, or a hosted plugin's)
  104. {
  105. std::vector<std::size_t> withState;
  106. for (std::size_t i = 0; i < song.channels.size(); ++i)
  107. if (const auto& ch = std::as_const (song.channels)[i]; ! ch.pluginState.empty() || ! ch.plugin.state.empty())
  108. withState.push_back (i);
  109. if (withState.empty())
  110. break;
  111. auto& ch = song.channels[withState[pick (withState.size())]];
  112. if (! std::as_const (ch).pluginState.empty())
  113. {
  114. // (its last byte flipped: another state, which the engine reads back)
  115. auto& last = ch.pluginState[ch.pluginState.size() - 1];
  116. last = (std::uint8_t) (last ^ 0x01);
  117. }
  118. else
  119. {
  120. auto text = ch.plugin.state.str();
  121. auto& c = text[pick (text.size())];
  122. c = c == 'A' ? 'B' : 'A';
  123. ch.plugin.state = std::move (text);
  124. }
  125. break;
  126. }
  127. case 10: // an effect slot added to an insert (or its last one removed when it is full)
  128. {
  129. if (song.mixer.size() < 2)
  130. break;
  131. auto& track = song.mixer[1 + pick (song.mixer.size() - 1)];
  132. if (track.effects.size() >= 8)
  133. track.effects.pop_back();
  134. else
  135. {
  136. EffectSlot slot;
  137. slot.kind = EffectKind::eq;
  138. slot.params[0] = value;
  139. track.effects.push_back (slot);
  140. }
  141. break;
  142. }
  143. case 11: // a channel renamed
  144. if (c < song.channels.size())
  145. song.channels[c].name += "*";
  146. break;
  147. default: // a note deleted
  148. if (p < song.patterns.size() && ! song.patterns[p].notes.empty())
  149. song.patterns[p].notes.erase (song.patterns[p].notes.cbegin() + (std::ptrdiff_t) pick (song.patterns[p].notes.size()));
  150. break;
  151. }
  152. });
  153. }
  154. juce::String undo190spCheck (juce::String& log)
  155. {
  156. using namespace lynx;
  157. juce::String result = "OK";
  158. const auto check = [&] (const juce::String& what, bool ok) {
  159. log << (ok ? "ok " : "FAIL ") << what << "\n";
  160. if (! ok && result == "OK")
  161. result = "ERROR " + what;
  162. };
  163. for (unsigned seed = 1; seed <= 3; ++seed)
  164. {
  165. AudioEngine engine (false);
  166. auto song = makeShowcaseSong (0);
  167. if (! song.channels.empty()) // (a Patcher graph whose nodes the history shares)
  168. song.channels[0].patcher.nodes.resize (3);
  169. // round 190sz: saved states the history shares too (an effect slot's FL state and a hosted plugin's text, a
  170. // channel's native engine state and a hosted instrument's text), for the mixer and plugin state edits
  171. if (song.mixer.size() < 3)
  172. song.mixer.resize (3);
  173. for (std::size_t t = 1; t < 3; ++t)
  174. {
  175. EffectSlot slot;
  176. slot.kind = EffectKind::eq;
  177. for (int b = 0; b < 64; ++b)
  178. slot.state.push_back ((std::uint8_t) (b * 7 + (int) t));
  179. song.mixer[t].effects.push_back (slot);
  180. EffectSlot hosted;
  181. hosted.kind = EffectKind::eq;
  182. hosted.plugin.state = std::string (96, 'A') + "==";
  183. song.mixer[t].effects.push_back (hosted);
  184. }
  185. if (song.channels.size() > 2)
  186. {
  187. for (int b = 0; b < 48; ++b)
  188. song.channels[1].pluginState.push_back ((std::uint8_t) b);
  189. song.channels[2].plugin.state = std::string (80, 'A');
  190. }
  191. engine.setSong (std::move (song));
  192. engine.setUndoLevels (200);
  193. std::mt19937 rng (seed);
  194. std::vector<juce::String> states;
  195. constexpr int edits = 80;
  196. for (int i = 0; i < edits; ++i)
  197. {
  198. states.push_back (undoState190sp (engine.getSong()));
  199. undoEdit190sp (engine, rng, nullptr, undoKinds190sz);
  200. }
  201. const auto end = undoState190sp (engine.getSong());
  202. check ("seed " + juce::String (seed) + ": " + juce::String (edits) + " edits, " + juce::String (engine.getHistoryPosition()) + " undo steps",
  203. engine.getHistoryPosition() == edits);
  204. auto exact = true;
  205. int firstBad = -1;
  206. for (int i = edits; i-- > 0;)
  207. {
  208. engine.undo();
  209. if (undoState190sp (engine.getSong()) != states[(std::size_t) i] && firstBad < 0)
  210. firstBad = i;
  211. exact = exact && firstBad < 0;
  212. }
  213. check ("seed " + juce::String (seed) + ": undo all, every state byte for byte" + (firstBad >= 0 ? " (first difference at step " + juce::String (firstBad) + ")" : juce::String()),
  214. exact && ! engine.canUndo());
  215. auto forward = true;
  216. for (int i = 1; i <= edits; ++i)
  217. {
  218. engine.redo();
  219. forward = forward && undoState190sp (engine.getSong()) == (i == edits ? end : states[(std::size_t) i]);
  220. }
  221. check ("seed " + juce::String (seed) + ": redo all, every state byte for byte, the end as it was", forward && ! engine.canRedo());
  222. // jumps across the history (History panel), then an edit: the history before it stays as it was
  223. engine.jumpToHistory (edits / 2);
  224. const auto middle = undoState190sp (engine.getSong());
  225. check ("seed " + juce::String (seed) + ": a jump to the middle", middle == states[(std::size_t) edits / 2]);
  226. undoEdit190sp (engine, rng, nullptr, undoKinds190sz);
  227. engine.undo();
  228. check ("seed " + juce::String (seed) + ": an edit there, undone: the middle state again", undoState190sp (engine.getSong()) == middle);
  229. engine.jumpToHistory (0);
  230. check ("seed " + juce::String (seed) + ": the first state at the start of the history", undoState190sp (engine.getSong()) == states.front());
  231. }
  232. return result;
  233. }
  234. // the process' memory in MB: Linux the heap in use (mallinfo2), Windows the private bytes
  235. static double processMemoryMb190sp()
  236. {
  237. #if JUCE_LINUX
  238. const auto m = mallinfo2();
  239. return (double) (m.uordblks + m.hblkhd) / 1048576.0;
  240. #elif JUCE_WINDOWS
  241. return (double) lynx::toolbar::PerformanceMeter::processPrivateBytes() / 1048576.0;
  242. #else
  243. return -1.0;
  244. #endif
  245. }
  246. #if JUCE_LINUX && __has_include(<execinfo.h>)
  247. // round 190sz: --trace, where each shared container was copied (the non-const reads the audit looks for): the
  248. // return addresses, for addr2line -f -C -e LYNX (the binary's offsets: the load address is subtracted)
  249. static void traceSharedCopy190sz (const char* what, std::size_t bytes)
  250. {
  251. void* frames[24];
  252. const auto n = backtrace (frames, 24);
  253. Dl_info self {};
  254. dladdr ((void*) &traceSharedCopy190sz, &self);
  255. std::fprintf (stderr, "COPY %s %zu bytes:", what, bytes);
  256. for (int f = 1; f < n; ++f)
  257. std::fprintf (stderr, " 0x%zx", (std::size_t) ((const char*) frames[f] - (const char*) self.dli_fbase));
  258. std::fprintf (stderr, "\n");
  259. }
  260. #endif
  261. void runUndoMemory190sp (const juce::File& project, const juce::File& report, int steps, int kinds = 7, bool trace = false)
  262. {
  263. using namespace lynx;
  264. juce::String text;
  265. const auto line = [&] (const juce::String& s) { text << s << "\n"; report.replaceWithText (text); };
  266. engine = std::make_unique<AudioEngine> (false);
  267. engine->setUndoLevels (juce::jmax (steps, 100));
  268. juce::String error;
  269. if (! engine->importFlpFile (project, error))
  270. {
  271. line ("FAIL open " + project.getFullPathName() + ": " + error);
  272. setApplicationReturnValue (1);
  273. engine.reset();
  274. quit();
  275. return;
  276. }
  277. line ("project " + project.getFileName() + ": a copy of its song " + juce::String ((double) songbytes::song (engine->getSong()) / 1048576.0, 2) + " MB (estimate)");
  278. const auto before = processMemoryMb190sp();
  279. #if __has_include("core/CowVector.h") // (the same check also builds on 0.1.32, for the numbers before)
  280. const auto copiesBefore = cowdetail::sharedCopies().load();
  281. #endif
  282. std::mt19937 rng (190);
  283. if (kinds != 7)
  284. line ("edit kinds: " + juce::String (kinds) + " (round 190sz)");
  285. #if __has_include("core/CowVector.h") && JUCE_LINUX && __has_include(<execinfo.h>)
  286. if (trace)
  287. cowdetail::sharedCopyHook().store (&traceSharedCopy190sz);
  288. #else
  289. (void) trace;
  290. #endif
  291. #if __has_include("core/CowVector.h")
  292. const auto bytesBefore = cowdetail::sharedCopyBytes().load();
  293. #endif
  294. const auto t0 = juce::Time::getMillisecondCounterHiRes();
  295. std::array<std::uint64_t, undoKinds190sz> kindCopies {}, kindCount {}, kindBytes {};
  296. for (int i = 0; i < steps; ++i)
  297. {
  298. int kind = 0;
  299. #if __has_include("core/CowVector.h")
  300. const auto c0 = cowdetail::sharedCopies().load();
  301. const auto b0 = cowdetail::sharedCopyBytes().load();
  302. #endif
  303. undoEdit190sp (*engine, rng, &kind, kinds);
  304. if (trace)
  305. std::fprintf (stderr, "EDIT %d was kind %d\n", i, kind);
  306. ++kindCount[(std::size_t) kind];
  307. #if __has_include("core/CowVector.h")
  308. kindCopies[(std::size_t) kind] += cowdetail::sharedCopies().load() - c0;
  309. kindBytes[(std::size_t) kind] += cowdetail::sharedCopyBytes().load() - b0;
  310. #endif
  311. }
  312. #if __has_include("core/CowVector.h")
  313. cowdetail::sharedCopyHook().store (nullptr);
  314. #endif
  315. const auto editMs = juce::Time::getMillisecondCounterHiRes() - t0;
  316. // (round 190sz: the racks the edits replaced are freed first, as the app's message loop frees them: since 190sz a
  317. // rack shares the song's lists, so a retired one still held here would keep the history's old buffers alive and
  318. // hide them from the count below)
  319. engine->freeRetiredRacks();
  320. {
  321. juce::String perKind;
  322. const char* names[] = { "note up", "note added", "clip moved", "channel level", "mixer level", "Patcher node", "note deleted",
  323. "mixer slot knob", "effect slot state", "channel plugin state", "effect slot added / removed", "channel renamed" };
  324. for (std::size_t k = 0; k < (std::size_t) kinds; ++k)
  325. perKind << names[k] << " " << (juce::int64) kindCount[k] << "x/" << (juce::int64) kindCopies[k] << " copies ("
  326. << juce::String ((double) kindBytes[k] / 1024.0, 0) << " KB); ";
  327. line ("edits by kind (count / shared containers copied (their bytes)): " + perKind);
  328. }
  329. const auto after = processMemoryMb190sp();
  330. line ("steps " + juce::String (engine->getHistoryPosition()) + " of " + juce::String (steps) + " edits, in " + juce::String (editMs, 0) + " ms ("
  331. + juce::String (editMs / juce::jmax (1, steps), 2) + " ms an edit)");
  332. #if __has_include("core/CowVector.h")
  333. line ("shared containers copied by the edits: " + juce::String ((juce::int64) (cowdetail::sharedCopies().load() - copiesBefore)) + " ("
  334. + juce::String ((double) (cowdetail::sharedCopies().load() - copiesBefore) / juce::jmax (1, steps), 1) + " an edit, "
  335. + juce::String ((double) (cowdetail::sharedCopyBytes().load() - bytesBefore) / 1048576.0, 2) + " MB)");
  336. #endif
  337. line ("history bytes (the cap's count): " + juce::String ((double) engine->getUndoBytes() / 1048576.0, 2) + " MB");
  338. line ("process memory: " + juce::String (before, 1) + " MB before, " + juce::String (after, 1) + " MB after: +" + juce::String (after - before, 1) + " MB");
  339. const auto u0 = juce::Time::getMillisecondCounterHiRes();
  340. for (int i = 0; i < 10; ++i)
  341. engine->undo();
  342. const auto undoMs = (juce::Time::getMillisecondCounterHiRes() - u0) / 10.0;
  343. const auto r0 = juce::Time::getMillisecondCounterHiRes();
  344. for (int i = 0; i < 10; ++i)
  345. engine->redo();
  346. const auto redoMs = (juce::Time::getMillisecondCounterHiRes() - r0) / 10.0;
  347. line ("undo " + juce::String (undoMs, 2) + " ms, redo " + juce::String (redoMs, 2) + " ms a step (10 each)");
  348. // what the history itself holds: the memory it gives back when it is cut to one step (the published states the
  349. // edits left behind stay: no audio device takes them in this run, so the process' growth above counts them too)
  350. engine->freeRetiredRacks();
  351. const auto full = processMemoryMb190sp();
  352. engine->setUndoBytesForTest (1);
  353. engine->freeRetiredRacks();
  354. const auto trimmed = processMemoryMb190sp();
  355. line ("history memory: " + juce::String (full - trimmed, 1) + " MB (" + juce::String (full, 1) + " MB with it, " + juce::String (trimmed, 1)
  356. + " MB with one step left; " + juce::String ((full - trimmed) / juce::jmax (1, steps - 1), 2) + " MB a step)");
  357. line ("result=OK");
  358. setApplicationReturnValue (0);
  359. engine.reset();
  360. quit();
  361. }