Use ring buffer for the SimProfiler
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.
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.
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 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 toUpdate()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.,headadvances from 0 to 1) - Calling
Update()max_frames + 1 times wraps head back via % max_frames (i.e.,headgoes from max_frames to 1) - Calling
Clear()resetsheadto 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, withdim_bodyvalues)
Which scope would you prefer?
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 ?
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.
This seems fine to me. Thanks for doing it!
@okmatija @haroonq I added the test. Please let me know when you want to adjust something
@okmatija Thank you for the review! I've addressed all your comments except this one, which is still under discussion.
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).
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!
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.