Instrument a single thread

33 views
Skip to first unread message

Sergey Egorov

unread,
Jun 6, 2023, 3:35:04 PM6/6/23
to DynamoRIO Users

Hello, I would like to implement a client that instruments a single thread from the target multithreaded application. The goal is to eliminate/minimise the overhead of instrumentation on the remaining threads. At the moment I check this using  dr_get_thread_id for every basic block that is being instrumented and return in case if the id of the thread is different. I get ~15% overhead on the entire application on this check alone. Performance of the application is critical for this project. Is there any better/faster way to isolate instrumentation of the single thread? 

Many thanks!

Sergey

Abhinav Sharma

unread,
Jun 6, 2023, 4:04:42 PM6/6/23
to DynamoRIO Users
Hi,
Note that DynamoRIO's code cache is shared between threads by default. This means that a code fragment may be created by one thread but used also by others, which I think is not what you want.

Are you explicitly specifying "-thread_private" (https://dynamorio.org/page_deploy.html#sec_options)? With -thread_private each thread has its own instrumented code fragments; it is the intended way to add thread-specific instrumentation. However, note that it may increase memory overhead.

Separate idea for future: we could perhaps use the drbbdup extension to add better support for thread-specific instrumentation where we want the same instrumentation for most threads. I created https://github.com/DynamoRIO/dynamorio/issues/6117 for that. For now, see if -thread_private helps.

Abhinav

Sergey Egorov

unread,
Jun 8, 2023, 6:41:51 AM6/8/23
to DynamoRIO Users

Thank you for the reply Abhinav, 
Yes, I am already using thread private option and it does improve the performance. 

Thanks,
Sergey

Dan O'Keeffe

unread,
Jun 8, 2023, 8:23:52 AM6/8/23
to DynamoRIO Users
Hi. As an alternative, is it possible to use the dr_app_start/stop API calls (https://dynamorio.org/dr__app_8h.html#afb7b7aed03dd6f0ed8d48b8a8796e55e) but to only enable/disable instrumentation dynamically for the current thread?
Dan

Dan O'Keeffe

unread,
Jun 8, 2023, 8:33:50 AM6/8/23
to DynamoRIO Users
To clarify, we have an application where at specific parts of the code a thread should switch from native execution to executing under dynamorio. This code may be executed by different threads at different times. On return the thread should switch back to native execution. The dr_app_start/stop APIs look relevant (since we know the switching point statically and are able to add those calls in the code), but they appear to cause every thread to switch to execution under dynamorio, which is not what we want. 

The dr_app_handle_mbr_target()/native_exec call also looks relevant, but we can't find many examples of it being used. It is unclear whether it allows per-thread switching, and also whether it can handle cases where the same code (e.g. for utility/helper functions) may need to execute both natively and under dynamorio. 

Sergey's suggestion of instrumenting only a single-thread would also work, but would force us to rearchitect our application to use a dedicated instrumented thread.
Dan

Derek Bruening

unread,
Jun 8, 2023, 10:47:33 AM6/8/23
to Dan O'Keeffe, DynamoRIO Users
Mixing managed threads and native threads is generally unsafe as other threads can take actions that cause incorrect execution by the managed thread (just about anything from running stale code to completely losing control) without much recourse.  When in full control these would be detected and handled.  (On Windows with ntdll hooks some things can attempt to be monitored while native.) 

That said, for known applications with known behavior this "mixed mode" can work successfully.  There are various features that support types of mixed mode developed in the history of DynamoRIO.  Mostly these are not fully supported and maintained by current developers however, or are labeled "experimental" or "risky".  I think the most recent native feature set is DR_EMIT_GO_NATIVE, dr_suspend_all_other_threads_ex(),, dr_retakeover_suspended_native_thread(), and dr_is_thread_native(), added for drcov's -native_until_thread on Windows.

--
You received this message because you are subscribed to the Google Groups "DynamoRIO Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dynamorio-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dynamorio-users/74fa120b-9f03-4588-8e49-8eae4280999fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages