<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>204871</bug_id>
          
          <creation_ts>2019-12-04 20:07:20 -0800</creation_ts>
          <short_desc>sched_yield can cause calling thread to be preempted for 10 ms on Darwin</short_desc>
          <delta_ts>2020-08-08 17:30:27 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>215248</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ben Nham">nham</reporter>
          <assigned_to name="Ben Nham">nham</assigned_to>
          <cc>benjamin</cc>
    
    <cc>cdumez</cc>
    
    <cc>cmarcelo</cc>
    
    <cc>dbates</cc>
    
    <cc>ews-watchlist</cc>
    
    <cc>fpizlo</cc>
    
    <cc>ggaren</cc>
    
    <cc>nham</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1595681</commentid>
    <comment_count>0</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:07:20 -0800</bug_when>
    <thetext>We currently use sched_yield in both bmalloc and WTF::Lock. This can lead to the calling thread being preempted for up to 10 ms even if the lock was relinquished much earlier than that because of the way this is implemented on Darwin:

 - sched_yield calls the swtch_pri syscall
 - swtch_pri depresses the calling thread&apos;s priority to 0 for thread_depress_time milliseconds
 - thread_depress_time is set to one scheduling quantum in sched_*.c
 - most of our devices run with a timer tick of 10 ms, so thread_depress_time is 10 ms
 - if the system is busy with enough &gt;0 pri work for 10 ms to saturate all possible CPUs, then the calling thread blocks for 10 ms

We actually see exactly this in a number of MobileSafari launch tests (this originally came up as a launch test variability issue from the perf team).

An alternative is to use thread_switch, which can depress the priority of the calling thread for a configurable amount of time with millisecond granularity.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595683</commentid>
    <comment_count>1</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:09:08 -0800</bug_when>
    <thetext>&lt;rdar://problem/57645816&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595684</commentid>
    <comment_count>2</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:10:42 -0800</bug_when>
    <thetext>I ran this by Daniel in Core OS (who is one of the scheduler maintainers) and he said that thread_switch(MACH_PORT_NULL, SWITCH_OPTION_DEPRESS, 1) would do basically what sched_yield does, except only depress the priority for 1 ms instead of 10 ms.

Of course the kernel folks would rather we use os_unfair_lock rather than rolling our own locks but I think that&apos;s probably a separate discussion.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595689</commentid>
    <comment_count>3</comment_count>
    <who name="Filip Pizlo">fpizlo</who>
    <bug_when>2019-12-04 20:14:59 -0800</bug_when>
    <thetext>(In reply to Ben Nham from comment #2)
&gt; I ran this by Daniel in Core OS (who is one of the scheduler maintainers)
&gt; and he said that thread_switch(MACH_PORT_NULL, SWITCH_OPTION_DEPRESS, 1)
&gt; would do basically what sched_yield does, except only depress the priority
&gt; for 1 ms instead of 10 ms.
&gt; 
&gt; Of course the kernel folks would rather we use os_unfair_lock rather than
&gt; rolling our own locks but I think that&apos;s probably a separate discussion.

We use sched_yield because it’s faster than what Daniel suggests. We have tested this many times. 

We use our own locks because they are faster for WebKit and because they can fit into 2 bits. We use that fact in JSCell. Even WTF::Lock is only one byte, which is 4x smaller than os_unfair_lock.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595690</commentid>
    <comment_count>4</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:15:27 -0800</bug_when>
    <thetext>Reopening to attach new patch.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595691</commentid>
    <comment_count>5</comment_count>
      <attachid>384875</attachid>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:15:28 -0800</bug_when>
    <thetext>Created attachment 384875
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595692</commentid>
    <comment_count>6</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:19:11 -0800</bug_when>
    <thetext>How are we testing thread_switch vs sched_yield? There is some difference in using a Mach trap vs. a BSD syscall but I am somewhat surprised it&apos;s that large.

I have brought up lowering the amount of time that swtch_pri depresses the priority as well, but didn&apos;t get any response about that.

In any case, we are definitely seeing 10 ms yields on phones (see the radar); whether the calling thread could have woken up earlier is harder to determine from the profile that I was looking at. When the calling thread is the main thread then those 10 ms can count.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595693</commentid>
    <comment_count>7</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2019-12-04 20:26:27 -0800</bug_when>
    <thetext>Actually both swtch_pri and thread_switch seem to be Mach traps. There is a bit more logic in the latter call on the kernel side, but I am somewhat surprised that there would be that much of a difference. But if you have a benchmark in mind that I can try out I would be happy to do that.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595701</commentid>
    <comment_count>8</comment_count>
    <who name="Filip Pizlo">fpizlo</who>
    <bug_when>2019-12-04 20:42:43 -0800</bug_when>
    <thetext>(In reply to Ben Nham from comment #6)
&gt; How are we testing thread_switch vs sched_yield? There is some difference in
&gt; using a Mach trap vs. a BSD syscall but I am somewhat surprised it&apos;s that
&gt; large.
&gt; 
&gt; I have brought up lowering the amount of time that swtch_pri depresses the
&gt; priority as well, but didn&apos;t get any response about that.
&gt; 
&gt; In any case, we are definitely seeing 10 ms yields on phones (see the
&gt; radar); whether the calling thread could have woken up earlier is harder to
&gt; determine from the profile that I was looking at. When the calling thread is
&gt; the main thread then those 10 ms can count.

I don’t think you’re understanding me. Your patch is going to be a performance regression.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595702</commentid>
    <comment_count>9</comment_count>
      <attachid>384875</attachid>
    <who name="Filip Pizlo">fpizlo</who>
    <bug_when>2019-12-04 20:43:25 -0800</bug_when>
    <thetext>Comment on attachment 384875
Patch

We have tried this change before. It was bad for perf. Unless you’ve got overwhelming perf data, this ain’t a change we are going to land.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595706</commentid>
    <comment_count>10</comment_count>
    <who name="Filip Pizlo">fpizlo</who>
    <bug_when>2019-12-04 20:46:19 -0800</bug_when>
    <thetext>(In reply to Ben Nham from comment #7)
&gt; Actually both swtch_pri and thread_switch seem to be Mach traps. There is a
&gt; bit more logic in the latter call on the kernel side, but I am somewhat
&gt; surprised that there would be that much of a difference. But if you have a
&gt; benchmark in mind that I can try out I would be happy to do that.

Any benchmark?

We have tried your exact change about 10 times throughout the history of our locks. We have run all the benchmarks. We know it’s a bad idea.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1678932</commentid>
    <comment_count>11</comment_count>
    <who name="Ben Nham">nham</who>
    <bug_when>2020-08-08 17:30:27 -0700</bug_when>
    <thetext>We hadn&apos;t re-run the benchmarks again since xnu made another change to thread_switch to more closely match the behavior for swtch_pri a couple of years ago. I wasn&apos;t able to complete running the benchmarks due to some automation issues, but it looks like Saam was able to do it last week.

Duping to Saam&apos;s bug, which is the exact same patch.

*** This bug has been marked as a duplicate of bug 215248 ***</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>384875</attachid>
            <date>2019-12-04 20:15:28 -0800</date>
            <delta_ts>2019-12-04 20:43:25 -0800</delta_ts>
            <desc>Patch</desc>
            <filename>bug-204871-20191204201527.patch</filename>
            <type>text/plain</type>
            <size>3820</size>
            <attacher name="Ben Nham">nham</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMjUyOTgyCmRpZmYgLS1naXQgYS9Tb3VyY2UvV1RGL0NoYW5n
ZUxvZyBiL1NvdXJjZS9XVEYvQ2hhbmdlTG9nCmluZGV4IDZjOTMxZjFkMzIyNjVjNTI2ODU0OWVl
MTcyZDJlMWQyYjBlYzc1MGQuLmY1OGFiNGFkY2IyYzAyZDdmMDMzYzYxYmZkMGU1ZTRhOWNkMTY0
ZGQgMTAwNjQ0Ci0tLSBhL1NvdXJjZS9XVEYvQ2hhbmdlTG9nCisrKyBiL1NvdXJjZS9XVEYvQ2hh
bmdlTG9nCkBAIC0xLDMgKzEsMjIgQEAKKzIwMTktMTItMDQgIEJlbiBOaGFtICA8bmhhbUBhcHBs
ZS5jb20+CisKKyAgICAgICAgc2NoZWRfeWllbGQgY2FuIGNhdXNlIGNhbGxpbmcgdGhyZWFkIHRv
IGJlIHByZWVtcHRlZCBmb3IgMTAgbXMgb24gRGFyd2luCisgICAgICAgIGh0dHBzOi8vYnVncy53
ZWJraXQub3JnL3Nob3dfYnVnLmNnaT9pZD0yMDQ4NzEKKyAgICAgICAgPHJkYXI6Ly9wcm9ibGVt
LzU3NjQ1ODE2PgorCisgICAgICAgIFJldmlld2VkIGJ5IE5PQk9EWSAoT09QUyEpLgorCisgICAg
ICAgIEN1cnJlbnRseSB3ZSB1c2Ugc2NoZWRfeWllbGQgdG8gYWxsb3cgb3RoZXIgdGhyZWFkcyB0
byBydW4uIFRoaXMgd29ya3MKKyAgICAgICAgYnkgZGVwcmVzc2luZyB0aGUgY2FsbGluZyB0aHJl
YWQncyBwcmlvcml0eSB0byAwIGZvciAxIHRpbWVyIHRpY2ssIHdoaWNoCisgICAgICAgIGlzIDEw
IG1zIG9uIG1vc3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2
NWNlNzc4YTQ3YmMwNjc1NTI3MTcxMTI0YzIxZGNkMjc3MTYwNWNmLi5hODJjOTYyZTVkMTFmY2Yw
ZjI1NTQyZTYzNWRkNTQ1NGEzNDZlNDg3IDEwMDY0NAotLS0gYS9Tb3VyY2UvYm1hbGxvYy9ibWFs
bG9jL011dGV4LmNwcAorKysgYi9Tb3VyY2UvYm1hbGxvYy9ibWFsbG9jL011dGV4LmNwcApAQCAt
MjksOCArMjksMjEgQEAKICNpbmNsdWRlICJTY29wZUV4aXQuaCIKICNpbmNsdWRlIDx0aHJlYWQ+
CiAKKyNpZiBCT1MoREFSV0lOKQorI2luY2x1ZGUgPG1hY2gvbWFjaC5oPgorI2VuZGlmCisKIG5h
bWVzcGFjZSBibWFsbG9jIHsKIAorc3RhdGljIHZvaWQgeWllbGRJbXBsKCkKK3sKKyNpZiBCT1Mo
REFSV0lOKQorICAgIHRocmVhZF9zd2l0Y2goTUFDSF9QT1JUX05VTEwsIFNXSVRDSF9PUFRJT05f
REVQUkVTUywgMSk7CisjZWxzZQorICAgIHNjaGVkX3lpZWxkKCk7CisjZW5kaWYKK30KKwogdm9p
ZCBNdXRleDo6bG9ja1Nsb3dDYXNlKCkKIHsKICAgICAvLyBUaGUgbG9uZ2VzdCBjcml0aWNhbCBz
ZWN0aW9uIGluIGJtYWxsb2MgaXMgbXVjaCBzaG9ydGVyIHRoYW4gdGhlCkBAIC00OSw3ICs2Miw3
IEBAIHZvaWQgTXV0ZXg6OmxvY2tTbG93Q2FzZSgpCiAKICAgICAvLyBBdm9pZCBzcGlubmluZyBw
YXRob2xvZ2ljYWxseS4KICAgICB3aGlsZSAoIXRyeV9sb2NrKCkpCi0gICAgICAgIHNjaGVkX3lp
ZWxkKCk7CisgICAgICAgIHlpZWxkSW1wbCgpOwogfQogCiB9IC8vIG5hbWVzcGFjZSBibWFsbG9j
Cg==
</data>
<flag name="review"
          id="400709"
          type_id="1"
          status="-"
          setter="fpizlo"
    />
          </attachment>
      

    </bug>

</bugzilla>