Reducing self-modifying code / single-use JIT performance penalty

By: Nyan (nyan.delete@this.mailinator.com), June 26, 2020 9:06 pm
Room: Moderated Discussions
Suppose you have the following code:

page = allocate_memory_page(permissions = read+write+execute)
begin loop
  write_1kb_function_code(page)
  page() ; execute written code
end loop


This is essentially a JIT where the code is only used once.

On many processors, this seems to cause it to detect self-modifying code behaviour, and act accordingly, which causes severe performance penalties.

I've been looking into seeing how one can reduce this penalty. As it turns out, there does seem to be things which can be done, however, I haven't seen them documented anywhere.

Observations made:

  • On modern Intel processors, it seems that each write can trigger a machine clear. This leads to the effect that first writing the code to non-executable memory, then copying it to the executable page can be faster, if this results in fewer write operations hitting the executable page. Interestingly, clearing the executable page before writing also works. Trying to clear the instruction cache (and maybe uOp cache?) by jumping across 32KB of instructions also seems to help. Issuing CLFLUSHOPT over the executable page before writing to it, also seems to be effective, but not CLFLUSH.

  • On AMD Piledriver, it seems to have similar benefits when copying to the executable page, as well as clearing it beforehand, as above. Interestingly, issuing PREFETCHW also seems to help.

  • On Silvermont, copying/clearing seems to help.

  • On AMD K10, copying with MOVNTDQ seems to be the most efficient. Other methods don't seem to help much.
  • On Intel Core2, none of the methods seem to help much.


My code, with some explanation as to what's being tested, and some test results can be found here.

I'm not particularly familiar with the exact workings of this, most of this is based on assumptions and guesses, so any advice, suggestions or insight into what's going on is welcome. I'm particularly interested in any ways to further reduce the penalty, so if you know something, please do tell! Testing on other micro-architectures is also helpful.
 Next Post in Thread >
TopicPosted ByDate
Reducing self-modifying code / single-use JIT performance penaltyNyan2020/06/26 09:06 PM
  Reducing self-modifying code / single-use JIT performance penaltyJon Masters2020/06/26 10:53 PM
    Reducing self-modifying code / single-use JIT performance penaltyNyan2020/06/27 12:44 AM
      Reducing self-modifying code / single-use JIT performance penaltyTravis Downs2020/07/06 07:50 PM
        Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/08 03:58 AM
          Reducing self-modifying code / single-use JIT performance penaltyTravis Downs2020/07/08 10:41 AM
            Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/09 04:35 AM
              Reducing self-modifying code / single-use JIT performance penaltyTravis Downs2020/07/09 06:54 PM
                Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/10 05:08 AM
  Reducing self-modifying code / single-use JIT performance penaltyGabriele Svelto2020/06/28 12:35 AM
    Reducing self-modifying code / single-use JIT performance penaltyNyan2020/06/28 02:28 AM
      Reducing self-modifying code / single-use JIT performance penaltyLinus Torvalds2020/06/28 10:14 AM
        Reducing self-modifying code / single-use JIT performance penaltyJon Masters2020/06/28 08:47 PM
        Reducing self-modifying code / single-use JIT performance penaltyNyan2020/06/29 01:01 AM
          Reducing self-modifying code / single-use JIT performance penaltyLinus Torvalds2020/07/03 02:43 PM
            Reducing self-modifying code / single-use JIT performance penaltyuseruser2020/07/05 09:42 AM
            Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/06 05:04 AM
  Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/02 01:58 AM
    Reducing self-modifying code / single-use JIT performance penaltyanon2020/07/02 03:54 PM
      Reducing self-modifying code / single-use JIT performance penaltyNyan2020/07/03 04:03 AM
Reply to this Topic
Name:
Email:
Topic:
Body: No Text
How do you spell purple?