mujoco icon indicating copy to clipboard operation
mujoco copied to clipboard

Use ring buffer for the SimProfiler

Open ttktjmt opened this issue 6 months ago • 11 comments

Update() called erase(begin()) + push_back() on 11 vectors every frame, O(N) per buffer. Replace with a head_ write cursor and direct indexed writes (buf[head_] = val), advancing the cursor modulo 200 at the end of each update. Pass head_ as ImPlot's offset parameter so the circular buffer renders correctly without data movement.

ttktjmt avatar Jun 01 '26 05:06 ttktjmt

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Jun 01 '26 05:06 google-cla[bot]

Thanks for the PR! We have not been very good with writing tests for the studio components, perhaps you could kick this off by adding a simple one for the SimProfiler. You could make sure it passes with and without your change e.g., by implementing the test in a first commit and doing the refactor in the next one.

okmatija avatar Jun 01 '26 10:06 okmatija

@okmatija Thank you for the review! Sure, I can add a simple test for SimProfiler as a first step. The design of the test contracts depends on whether they need to cover behavior both before and after this change, or only after.

Contracts covering both before and after the change:

  • Calling Update() N times does not crash
  • If N ≤ max_frames, exactly N frames are recorded
  • If N > max_frames, it does not crash
  • The most recently recorded values are retrievable
  • After Clear(), subsequent calls to Update() work correctly

Contracts focused only on the behavior after this change:

  • A single call to Update() increments the recorded frame count by 1 (i.e., head advances from 0 to 1)
  • Calling Update() max_frames + 1 times wraps head back via % max_frames (i.e., head goes from max_frames to 1)
  • Calling Clear() resets head to 0
  • The N-th call to Update() writes to slot N-1 (e.g., the 1st call writes to slot 0, the 2nd to slot 1, with dim_body values)

Which scope would you prefer?

ttktjmt avatar Jun 02 '26 03:06 ttktjmt

We don't want to maintain detailed tests I think, just some basic sanity checking is enough IMO. Also, looking at the API again I think we don't really have a useful public function we can use to implement the tests (we don't really want to check private implmeentation details like where head_ is), perhaps we could add a Summary() function which returns a Summary struct some useful info e.g., counts and averages? With that data we could implement one test that does something like:

  • Record N < max_frames and check summary.
  • Record one more frame and check summary.
  • Record one more frame (so N > max_frames) and check summary.
  • Clear() and check summary is all zero or whatever makes sense.

The summary data may be useful in the UI somewhere. WDYT @haroonq ?

okmatija avatar Jun 02 '26 15:06 okmatija

Sounds good. I'll keep it to one basic sanity test src/experimental/platform/sim/sim_profiler_test.cc built on a new Summary accessor.

Concrete shape I'm planning:

struct MetricStats { float average = 0, min = 0, max = 0; };
struct Summary {
  int num_frames = 0;  // <= capacity, recorded frames
  int capacity = 0;    // == kProfilerMaxFrames
  MetricStats cpu_total, cpu_collision, cpu_prepare, cpu_solve, cpu_other;
  MetricStats dim_dof, dim_body, dim_constraint, dim_sqrt_nnz, dim_contact, dim_iteration;
};
Summary GetSummary() const; // nested method with same name is not allowed

Note: the ring buffer pre-fills every slot with 0 and head_ wraps, so right now there's no way to distinguish a partially-filled buffer from a full one, I think. I can add a small counter num_frames_ (reset in Clear(), incremented in Update(), saturating at max frames) so the counts and averages are computed only over real frames.

The single test follows your four steps:

  • record capacity - 1
  • → +1 (== capacity)
  • → +1 (wrap, num_frames saturates)
  • → Clear() (back to zero)

feeding deterministic mjData timer values so the averages are exact rather than depending on wall-clock timing.

@haroonq Happy to adjust if you'd prefer something else.

ttktjmt avatar Jun 06 '26 05:06 ttktjmt

This seems fine to me. Thanks for doing it!

haroonq avatar Jun 06 '26 09:06 haroonq

@okmatija @haroonq I added the test. Please let me know when you want to adjust something

ttktjmt avatar Jun 09 '26 06:06 ttktjmt

@okmatija Thank you for the review! I've addressed all your comments except this one, which is still under discussion.

ttktjmt avatar Jun 09 '26 15:06 ttktjmt

LGTM, just one more suggestion related to record_frame(). Also something to think about but not change... the summary function could be called every frame and would compute the metrics again each frame by iterating over all the data, which is fine for now, if it becomes a problem later we coudl change to an incremental implementation. Right now we're just calling it in a test so its fine (no need to comment either IMO).

okmatija avatar Jun 11 '26 07:06 okmatija

Hi @ttktjmt, can you sync this to the latest main, this should fix the CI and then I can this over the line!

I would do this myself, but don't know how (maybe this is because you didn't enable maintainer edits when you opened the PR?). Thanks for the contribution!

okmatija avatar Jun 22 '26 09:06 okmatija

Sure, I can merge the current remote main into my feature branch. I think you can also directly commit to my source branch because I enabled it.

ttktjmt avatar Jun 22 '26 10:06 ttktjmt