| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- // Round 190sp (0.1.33 perf block E, QUEUE item 13): the undo history shares the song's big containers (core/CowVector.h).
- // Included in the app class of Main.cpp.
- // - undo190spCheck: --selftest "undo190sp" / --undo190sptest <report>: random edits through a real engine, then undo
- // all of them (each state the project's exact text, ProjectFile::toJsonExact, byte for byte), then redo all;
- // - runUndoMemory190sp: --undomem190sp <project> <report> [--steps N]: the project opened, N edits of the usual
- // kinds, the history's bytes and the process' heap / private memory before and after (QUEUE item 13's numbers).
- static juce::String undoState190sp (const lynx::Song& song)
- {
- auto copy = song;
- copy.info.workSeconds = 0; // (the project's clock is not part of the history, round 190re)
- copy.info.created = 0;
- return lynx::ProjectFile::toJsonExact (copy);
- }
- // round 190sz: the kinds of undoEdit190sp (the first 7 are 190sp's: the same random sequence with `kinds` = 7)
- static constexpr int undoKinds190sz = 12;
- // one edit of the usual kinds, through the engine (one undo step each); `kindOut`: which one it was. `kinds` (round
- // 190sz): 7 = 190sp's kinds; 12 adds a mixer slot's knob, an effect slot's saved state, a channel's plugin state, an
- // effect slot added / removed and a channel renamed
- static void undoEdit190sp (lynx::AudioEngine& engine, std::mt19937& rng, int* kindOut = nullptr, int kinds = 7)
- {
- using namespace lynx;
- const auto& s = engine.getSong();
- const auto pick = [&rng] (std::size_t n) { return n == 0 ? (std::size_t) 0 : (std::size_t) (rng() % n); };
- const auto kind = rng() % (unsigned) juce::jlimit (1, undoKinds190sz, kinds);
- if (kindOut != nullptr)
- *kindOut = (int) kind;
- const auto p = pick (s.patterns.size());
- const auto c = pick (s.channels.size());
- const auto k = pick (s.playlist.size());
- const auto value = (float) (rng() % 1000) / 1000.0f;
- engine.editSong ([&] (Song& song) {
- switch (kind)
- {
- case 0: // a note moved up a semitone
- if (p < song.patterns.size() && ! song.patterns[p].notes.empty())
- {
- auto& n = song.patterns[p].notes[pick (song.patterns[p].notes.size())];
- n.key = juce::jlimit (0, 127, n.key + 1);
- }
- break;
- case 1: // a note added at the start
- if (p < song.patterns.size())
- {
- Note n;
- n.channel = (int) c;
- n.key = 60 + (int) (rng() % 12);
- song.patterns[p].notes.insert (song.patterns[p].notes.cbegin(), n);
- }
- break;
- case 2: // a clip moved
- if (k < song.playlist.size())
- song.playlist[k].start += ppq;
- break;
- case 3: // a channel level
- if (c < song.channels.size())
- song.channels[c].volume = value;
- break;
- case 4: // a mixer track level
- if (! song.mixer.empty())
- song.mixer[pick (song.mixer.size())].volume = value;
- break;
- case 5: // a Patcher node moved (a Live rack's device)
- {
- // (round 190sz: found through the const view, so only the channel that is changed is copied)
- for (std::size_t i = 0; i < song.channels.size(); ++i)
- if (const auto& nodes = std::as_const (song.channels)[i].patcher.nodes; ! nodes.empty())
- {
- auto& ch = song.channels[i];
- ch.patcher.nodes[pick (nodes.size())].x += 5.0f;
- return;
- }
- break;
- }
- // ---- round 190sz (QUEUE item 54) ----
- case 7: // a mixer slot's knob
- case 8: // an effect slot's saved state (FL's raw state, or a hosted plugin's)
- {
- std::vector<std::pair<std::size_t, std::size_t>> slots;
- for (std::size_t t = 0; t < song.mixer.size(); ++t)
- for (std::size_t e = 0; e < std::as_const (song.mixer)[t].effects.size(); ++e)
- {
- const auto& slot = std::as_const (song.mixer)[t].effects[e];
- if (kind == 7 || ! slot.state.empty() || ! slot.plugin.state.empty())
- slots.emplace_back (t, e);
- }
- if (slots.empty())
- break;
- const auto [t, e] = slots[pick (slots.size())];
- auto& slot = song.mixer[t].effects[e];
- if (kind == 7)
- slot.params[pick (8)] = value;
- else if (! std::as_const (slot).state.empty())
- slot.state[pick (slot.state.size())] ^= 0x01;
- else
- {
- auto text = slot.plugin.state.str();
- auto& c = text[pick (text.size())];
- c = c == 'A' ? 'B' : 'A';
- slot.plugin.state = std::move (text);
- }
- break;
- }
- case 9: // a channel's plugin state (a native FL engine's saved state, or a hosted plugin's)
- {
- std::vector<std::size_t> withState;
- for (std::size_t i = 0; i < song.channels.size(); ++i)
- if (const auto& ch = std::as_const (song.channels)[i]; ! ch.pluginState.empty() || ! ch.plugin.state.empty())
- withState.push_back (i);
- if (withState.empty())
- break;
- auto& ch = song.channels[withState[pick (withState.size())]];
- if (! std::as_const (ch).pluginState.empty())
- {
- // (its last byte flipped: another state, which the engine reads back)
- auto& last = ch.pluginState[ch.pluginState.size() - 1];
- last = (std::uint8_t) (last ^ 0x01);
- }
- else
- {
- auto text = ch.plugin.state.str();
- auto& c = text[pick (text.size())];
- c = c == 'A' ? 'B' : 'A';
- ch.plugin.state = std::move (text);
- }
- break;
- }
- case 10: // an effect slot added to an insert (or its last one removed when it is full)
- {
- if (song.mixer.size() < 2)
- break;
- auto& track = song.mixer[1 + pick (song.mixer.size() - 1)];
- if (track.effects.size() >= 8)
- track.effects.pop_back();
- else
- {
- EffectSlot slot;
- slot.kind = EffectKind::eq;
- slot.params[0] = value;
- track.effects.push_back (slot);
- }
- break;
- }
- case 11: // a channel renamed
- if (c < song.channels.size())
- song.channels[c].name += "*";
- break;
- default: // a note deleted
- if (p < song.patterns.size() && ! song.patterns[p].notes.empty())
- song.patterns[p].notes.erase (song.patterns[p].notes.cbegin() + (std::ptrdiff_t) pick (song.patterns[p].notes.size()));
- break;
- }
- });
- }
- juce::String undo190spCheck (juce::String& log)
- {
- using namespace lynx;
- juce::String result = "OK";
- const auto check = [&] (const juce::String& what, bool ok) {
- log << (ok ? "ok " : "FAIL ") << what << "\n";
- if (! ok && result == "OK")
- result = "ERROR " + what;
- };
- for (unsigned seed = 1; seed <= 3; ++seed)
- {
- AudioEngine engine (false);
- auto song = makeShowcaseSong (0);
- if (! song.channels.empty()) // (a Patcher graph whose nodes the history shares)
- song.channels[0].patcher.nodes.resize (3);
- // round 190sz: saved states the history shares too (an effect slot's FL state and a hosted plugin's text, a
- // channel's native engine state and a hosted instrument's text), for the mixer and plugin state edits
- if (song.mixer.size() < 3)
- song.mixer.resize (3);
- for (std::size_t t = 1; t < 3; ++t)
- {
- EffectSlot slot;
- slot.kind = EffectKind::eq;
- for (int b = 0; b < 64; ++b)
- slot.state.push_back ((std::uint8_t) (b * 7 + (int) t));
- song.mixer[t].effects.push_back (slot);
- EffectSlot hosted;
- hosted.kind = EffectKind::eq;
- hosted.plugin.state = std::string (96, 'A') + "==";
- song.mixer[t].effects.push_back (hosted);
- }
- if (song.channels.size() > 2)
- {
- for (int b = 0; b < 48; ++b)
- song.channels[1].pluginState.push_back ((std::uint8_t) b);
- song.channels[2].plugin.state = std::string (80, 'A');
- }
- engine.setSong (std::move (song));
- engine.setUndoLevels (200);
- std::mt19937 rng (seed);
- std::vector<juce::String> states;
- constexpr int edits = 80;
- for (int i = 0; i < edits; ++i)
- {
- states.push_back (undoState190sp (engine.getSong()));
- undoEdit190sp (engine, rng, nullptr, undoKinds190sz);
- }
- const auto end = undoState190sp (engine.getSong());
- check ("seed " + juce::String (seed) + ": " + juce::String (edits) + " edits, " + juce::String (engine.getHistoryPosition()) + " undo steps",
- engine.getHistoryPosition() == edits);
- auto exact = true;
- int firstBad = -1;
- for (int i = edits; i-- > 0;)
- {
- engine.undo();
- if (undoState190sp (engine.getSong()) != states[(std::size_t) i] && firstBad < 0)
- firstBad = i;
- exact = exact && firstBad < 0;
- }
- check ("seed " + juce::String (seed) + ": undo all, every state byte for byte" + (firstBad >= 0 ? " (first difference at step " + juce::String (firstBad) + ")" : juce::String()),
- exact && ! engine.canUndo());
- auto forward = true;
- for (int i = 1; i <= edits; ++i)
- {
- engine.redo();
- forward = forward && undoState190sp (engine.getSong()) == (i == edits ? end : states[(std::size_t) i]);
- }
- check ("seed " + juce::String (seed) + ": redo all, every state byte for byte, the end as it was", forward && ! engine.canRedo());
- // jumps across the history (History panel), then an edit: the history before it stays as it was
- engine.jumpToHistory (edits / 2);
- const auto middle = undoState190sp (engine.getSong());
- check ("seed " + juce::String (seed) + ": a jump to the middle", middle == states[(std::size_t) edits / 2]);
- undoEdit190sp (engine, rng, nullptr, undoKinds190sz);
- engine.undo();
- check ("seed " + juce::String (seed) + ": an edit there, undone: the middle state again", undoState190sp (engine.getSong()) == middle);
- engine.jumpToHistory (0);
- check ("seed " + juce::String (seed) + ": the first state at the start of the history", undoState190sp (engine.getSong()) == states.front());
- }
- return result;
- }
- // the process' memory in MB: Linux the heap in use (mallinfo2), Windows the private bytes
- static double processMemoryMb190sp()
- {
- #if JUCE_LINUX
- const auto m = mallinfo2();
- return (double) (m.uordblks + m.hblkhd) / 1048576.0;
- #elif JUCE_WINDOWS
- return (double) lynx::toolbar::PerformanceMeter::processPrivateBytes() / 1048576.0;
- #else
- return -1.0;
- #endif
- }
- #if JUCE_LINUX && __has_include(<execinfo.h>)
- // round 190sz: --trace, where each shared container was copied (the non-const reads the audit looks for): the
- // return addresses, for addr2line -f -C -e LYNX (the binary's offsets: the load address is subtracted)
- static void traceSharedCopy190sz (const char* what, std::size_t bytes)
- {
- void* frames[24];
- const auto n = backtrace (frames, 24);
- Dl_info self {};
- dladdr ((void*) &traceSharedCopy190sz, &self);
- std::fprintf (stderr, "COPY %s %zu bytes:", what, bytes);
- for (int f = 1; f < n; ++f)
- std::fprintf (stderr, " 0x%zx", (std::size_t) ((const char*) frames[f] - (const char*) self.dli_fbase));
- std::fprintf (stderr, "\n");
- }
- #endif
- void runUndoMemory190sp (const juce::File& project, const juce::File& report, int steps, int kinds = 7, bool trace = false)
- {
- using namespace lynx;
- juce::String text;
- const auto line = [&] (const juce::String& s) { text << s << "\n"; report.replaceWithText (text); };
- engine = std::make_unique<AudioEngine> (false);
- engine->setUndoLevels (juce::jmax (steps, 100));
- juce::String error;
- if (! engine->importFlpFile (project, error))
- {
- line ("FAIL open " + project.getFullPathName() + ": " + error);
- setApplicationReturnValue (1);
- engine.reset();
- quit();
- return;
- }
- line ("project " + project.getFileName() + ": a copy of its song " + juce::String ((double) songbytes::song (engine->getSong()) / 1048576.0, 2) + " MB (estimate)");
- const auto before = processMemoryMb190sp();
- #if __has_include("core/CowVector.h") // (the same check also builds on 0.1.32, for the numbers before)
- const auto copiesBefore = cowdetail::sharedCopies().load();
- #endif
- std::mt19937 rng (190);
- if (kinds != 7)
- line ("edit kinds: " + juce::String (kinds) + " (round 190sz)");
- #if __has_include("core/CowVector.h") && JUCE_LINUX && __has_include(<execinfo.h>)
- if (trace)
- cowdetail::sharedCopyHook().store (&traceSharedCopy190sz);
- #else
- (void) trace;
- #endif
- #if __has_include("core/CowVector.h")
- const auto bytesBefore = cowdetail::sharedCopyBytes().load();
- #endif
- const auto t0 = juce::Time::getMillisecondCounterHiRes();
- std::array<std::uint64_t, undoKinds190sz> kindCopies {}, kindCount {}, kindBytes {};
- for (int i = 0; i < steps; ++i)
- {
- int kind = 0;
- #if __has_include("core/CowVector.h")
- const auto c0 = cowdetail::sharedCopies().load();
- const auto b0 = cowdetail::sharedCopyBytes().load();
- #endif
- undoEdit190sp (*engine, rng, &kind, kinds);
- if (trace)
- std::fprintf (stderr, "EDIT %d was kind %d\n", i, kind);
- ++kindCount[(std::size_t) kind];
- #if __has_include("core/CowVector.h")
- kindCopies[(std::size_t) kind] += cowdetail::sharedCopies().load() - c0;
- kindBytes[(std::size_t) kind] += cowdetail::sharedCopyBytes().load() - b0;
- #endif
- }
- #if __has_include("core/CowVector.h")
- cowdetail::sharedCopyHook().store (nullptr);
- #endif
- const auto editMs = juce::Time::getMillisecondCounterHiRes() - t0;
- // (round 190sz: the racks the edits replaced are freed first, as the app's message loop frees them: since 190sz a
- // rack shares the song's lists, so a retired one still held here would keep the history's old buffers alive and
- // hide them from the count below)
- engine->freeRetiredRacks();
- {
- juce::String perKind;
- const char* names[] = { "note up", "note added", "clip moved", "channel level", "mixer level", "Patcher node", "note deleted",
- "mixer slot knob", "effect slot state", "channel plugin state", "effect slot added / removed", "channel renamed" };
- for (std::size_t k = 0; k < (std::size_t) kinds; ++k)
- perKind << names[k] << " " << (juce::int64) kindCount[k] << "x/" << (juce::int64) kindCopies[k] << " copies ("
- << juce::String ((double) kindBytes[k] / 1024.0, 0) << " KB); ";
- line ("edits by kind (count / shared containers copied (their bytes)): " + perKind);
- }
- const auto after = processMemoryMb190sp();
- line ("steps " + juce::String (engine->getHistoryPosition()) + " of " + juce::String (steps) + " edits, in " + juce::String (editMs, 0) + " ms ("
- + juce::String (editMs / juce::jmax (1, steps), 2) + " ms an edit)");
- #if __has_include("core/CowVector.h")
- line ("shared containers copied by the edits: " + juce::String ((juce::int64) (cowdetail::sharedCopies().load() - copiesBefore)) + " ("
- + juce::String ((double) (cowdetail::sharedCopies().load() - copiesBefore) / juce::jmax (1, steps), 1) + " an edit, "
- + juce::String ((double) (cowdetail::sharedCopyBytes().load() - bytesBefore) / 1048576.0, 2) + " MB)");
- #endif
- line ("history bytes (the cap's count): " + juce::String ((double) engine->getUndoBytes() / 1048576.0, 2) + " MB");
- line ("process memory: " + juce::String (before, 1) + " MB before, " + juce::String (after, 1) + " MB after: +" + juce::String (after - before, 1) + " MB");
- const auto u0 = juce::Time::getMillisecondCounterHiRes();
- for (int i = 0; i < 10; ++i)
- engine->undo();
- const auto undoMs = (juce::Time::getMillisecondCounterHiRes() - u0) / 10.0;
- const auto r0 = juce::Time::getMillisecondCounterHiRes();
- for (int i = 0; i < 10; ++i)
- engine->redo();
- const auto redoMs = (juce::Time::getMillisecondCounterHiRes() - r0) / 10.0;
- line ("undo " + juce::String (undoMs, 2) + " ms, redo " + juce::String (redoMs, 2) + " ms a step (10 each)");
- // what the history itself holds: the memory it gives back when it is cut to one step (the published states the
- // edits left behind stay: no audio device takes them in this run, so the process' growth above counts them too)
- engine->freeRetiredRacks();
- const auto full = processMemoryMb190sp();
- engine->setUndoBytesForTest (1);
- engine->freeRetiredRacks();
- const auto trimmed = processMemoryMb190sp();
- line ("history memory: " + juce::String (full - trimmed, 1) + " MB (" + juce::String (full, 1) + " MB with it, " + juce::String (trimmed, 1)
- + " MB with one step left; " + juce::String ((full - trimmed) / juce::jmax (1, steps - 1), 2) + " MB a step)");
- line ("result=OK");
- setApplicationReturnValue (0);
- engine.reset();
- quit();
- }
|