Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#7113 decode cache: Add analyzer library for decode_cache_t #7114

Open
wants to merge 47 commits into
base: master
Choose a base branch
from

Conversation

abhinav92003
Copy link
Contributor

@abhinav92003 abhinav92003 commented Dec 9, 2024

Adds a new drmemtrace_decode_cache library to cache information about decoded instructions using decode_cache_t. This can be used by analysis tools that need to decode the instr encodings in the trace, to avoid overhead of redundant decodings which can get expensive.

The library allows the tools to specify what information they need to cache. Also, it uses instr_noalloc_t when possible to reduce heap usage and allocation/deallocation overhead.

If the trace does not include embedded encodings or if the user wants to get encodings from the app binaries using module_mapper_t instead, they can provide the module file path to the init API on the decode_cache_t object. decode_cache_t keeps a single initialized module_mapper_t at any time, which is shared between all decode_cache_t objects (even the ones of different template types); this is done by tracking the count of active objects using the module mapper.

decode_cache_t provides the clear_cache() API which can be used in parallel_shard_exit() to keep memory consumption in check by free-ing up cached decoding info that may not be needed for result computation in later print_results() which has to wait until all shards are done.

Refactors the invariant checker and opcode mix tools to use this library.

Modifies add_encodings_to_memrefs to support a mode where encodings are not set in the generated test memref but only the instr addr and size fields are set.

Makes the opcode cache in opcode_mix_t per-shard instead of per-worker. Decodings must not be cached per-worker as that may cause stale encodings for non-first shards processed by the worker. This means the worker init and worker exit APIs can be removed now from opcode_mix_t.

Adds decode_cache_test and opcode_mix_test unit tests that verify operation of the decode_cache_t.

Issue: #7113

Adds a new library to cache information about decoded instructions. This can be
used by analysis tools that need to decode the instr encodings in the trace.

The library allows the tools to specify what information they need to cache.

Refactors the invariant checker tool to use this library.

Issue: #7113
@abhinav92003 abhinav92003 changed the title i#7113: Add library to cache information about decoded instructions i#7113: Add analyzer library to cache instr decode info Dec 10, 2024
clients/drcachesim/tools/instr_decode_cache.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tests/instr_decode_cache_test.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tests/instr_decode_cache_test.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/instr_decode_cache.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/instr_decode_cache.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/invariant_checker.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/invariant_checker.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/invariant_checker.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/invariant_checker.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/instr_decode_cache.h Outdated Show resolved Hide resolved
@abhinav92003
Copy link
Contributor Author

Decided to try out an alternate way to support module-mapper-decoding in instr_decode_cache_t that came out of offline discussion. Okay to hold off on the re-review until then (Cannot undo re-request review)

Copy link
Contributor

@derekbruening derekbruening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank review to reset the requested review state.

@abhinav92003 abhinav92003 changed the title i#7113: Add analyzer library to cache instr decode info i#7113 decode cache: Add analyzer library for decode_cache_t Dec 16, 2024
@abhinav92003
Copy link
Contributor Author

Almost all concerns from the review and offline discussions have been addressed, so this is ready for a re-review. Added a TODO for a couple items: dr_set_isa_mode for regdeps, and the Windows test-only i#5960. PTAL.

Copy link
Contributor

@derekbruening derekbruening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a large diff. Did not make it through all the files yet but sending comments so far. May be delayed on finishing as have other work to get to.

clients/drcachesim/CMakeLists.txt Show resolved Hide resolved
clients/drcachesim/tests/decode_cache_test.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tests/decode_cache_test.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tests/decode_cache_test.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/common/decode_cache.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/view.cpp Show resolved Hide resolved
clients/drcachesim/tools/common/decode_cache.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/common/decode_cache.h Outdated Show resolved Hide resolved
* An \p decode_cache_t for testing which uses a \p test_module_mapper_t.
*/
template <class DecodeInfo>
class test_decode_cache_t : public decode_cache_t<DecodeInfo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be in this header? Shouldn't this be in the test file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed convenient to have the test class available at a common location. But sure, it'll be better to find a new test-only common location.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually test_module_mapper_t also lives in raw2trace_shared.h. Do we want to update all such test classes to be in their own separate header?

clients/drcachesim/tools/common/decode_cache.h Outdated Show resolved Hide resolved
Copy link
Contributor

@derekbruening derekbruening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest of files. Maybe main points are that optimizations in opcode_mix (worker instead of shard data; further caching) are being thrown out: seems like we want to keep at least the worker data.

pair.memref.instr.encoding_is_new = true;
if (!set_only_instr_addr) {
memcpy(pair.memref.instr.encoding, &decode_buf[offset], instr_size);
pair.memref.instr.encoding_is_new = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your code assuming encoding_is_new is always initialized? E.g., "if (!use_module_mapper_ && memref_instr.encoding_is_new) {" in decode_cache.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that condition, encoding_is_new will be read only if the user didn't ask to use the module mapper, in which case the init() time check will ensure the trace filetype supports embedded-encodings, in which case encoding_is_new will be set.

clients/drcachesim/tools/opcode_mix.cpp Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.cpp Show resolved Hide resolved
clients/drcachesim/tools/common/decode_cache.h Outdated Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.cpp Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.cpp Outdated Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.h Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.h Show resolved Hide resolved
clients/drcachesim/tools/opcode_mix.cpp Show resolved Hide resolved
// XXX: We could potentially use instr_decode_cache_t here (i#7113) and avoid the
// repeated instr decoding logic. However, we want to preserve the legacy view
// tool output format which uses disassemble_to_buffer, and disassemble_to_buffer
// does the decoding on its own internally, while adding a bunch of non-trival
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to update the view tool separately (this PR is already large), so long as we know it won't require interface changes to this library. If it used instr_disassemle(), and the library sets ISA regdeps, will it all work out?

@abhinav92003
Copy link
Contributor Author

Location for test_decode_cache_t and providing raw instr encoding for view_t need more discussion; resolved other threads.

Ready for re-review. Though no rush because of the holidays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants