From nobody Thu Mar 30 19:05:01 2023 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PnXtN5tx0z42BgG for ; Thu, 30 Mar 2023 19:05:16 +0000 (UTC) (envelope-from kevin.bowling@kev009.com) Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PnXtN2ScMz3LSS for ; Thu, 30 Mar 2023 19:05:16 +0000 (UTC) (envelope-from kevin.bowling@kev009.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-pl1-x62c.google.com with SMTP id o2so19031121plg.4 for ; Thu, 30 Mar 2023 12:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kev009.com; s=google; t=1680203115; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=E1GimjrYwah0l7RBDHGlb8FHPgwgtcqsnvNK3pTYEqw=; b=eOmf3fboHhJsjs+qMB0IqBVziRlnR+R83T1nxYN9vOJiEMIZ3ZtLzIObN0O6mXnT0W wvyyokEu8W0205MSKXom4DNkerWQ6Fdw8o6kXRvexJPTlwbFJiRMzJNp8bELZB5/iMtx 2K3Yx7L0IQ+NzoUVX+nFVsazwUGuqE4TaJfbU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680203115; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=E1GimjrYwah0l7RBDHGlb8FHPgwgtcqsnvNK3pTYEqw=; b=06aW0NKd6L7VliYgZMnHyyyEhUMIaRU33pVQTjZ2I4/ws5xo6J8Rej9dc+PWbSXezw CTMJUEI6vZjlx4D27rICVjelV7arfBaVwgFR3zz1VL4ExzdsyVdYPxwpY32PggMdt5K2 zVBKLbuHM0zcOfixjSqgBVpMPCbpIHOf2XX8bJi1ERxWHBtE0MbXjAokWMGPGZqaPXo9 uRKlkLfbEDh4Ivpbfi7qsN8qoCAP1QeeqmoBEM9VOEsjUUV3/6c+J2pgBcaAyb1X8EtJ xXPBvhBYrsWJtSUeBuxBpIYX1WO+7J3S11BEORRDVqR/c4RApqZWKTQvoyiUnZZiy0Ff AXkg== X-Gm-Message-State: AAQBX9faKDNw5yTrCrREylQViRDNsq2OL1q4d5VUnVWJ0dRB+Mif3+Ic PnLoNLVatSe3Zi04NeGWq8lYadMjusyOsfIDqzhOvw== X-Google-Smtp-Source: AKy350YoMxAe08CkGIrO2m7NEwKZe23wXIhu+G4RlZfC8k3mkAlPkao41m67SLJk3sZN/IBPd7ou0FpygRGyR85XjXs= X-Received: by 2002:a17:902:dace:b0:1a0:41ea:b9ba with SMTP id q14-20020a170902dace00b001a041eab9bamr9376502plx.8.1680203115035; Thu, 30 Mar 2023 12:05:15 -0700 (PDT) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 References: <8173cc7e-e934-dd5c-312a-1dfa886941aa@FreeBSD.org> <8cfdb951-9b1f-ecd3-2291-7a528e1b042c@m5p.com> In-Reply-To: From: Kevin Bowling Date: Thu, 30 Mar 2023 12:05:01 -0700 Message-ID: Subject: Re: Periodic rant about SCHED_ULE To: Mateusz Guzik Cc: freebsd-hackers@freebsd.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4PnXtN2ScMz3LSS X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Thu, Mar 30, 2023 at 11:50=E2=80=AFAM Mateusz Guzik = wrote: > > On 3/30/23, Kevin Bowling wrote: > > On Thu, Mar 30, 2023 at 11:29=E2=80=AFAM Kevin Bowling > > wrote: > >> > >> On Thu, Mar 30, 2023 at 8:37=E2=80=AFAM Mateusz Guzik wrote: > >> > > >> > I looked into it a little more, below you can find summary and steps > >> > forward. > >> > > >> > First a general statement: while ULE does have performance bugs, it > >> > has better basis than 4BSD to make scheduling decisions. Most notabl= y > >> > it understands CPU topology, at least for cases which don't involve > >> > big.LITTLE. For any non-freak case where 4BSD performs better, it is= a > >> > bug in ULE if this is for any reason other than a tradeoff which can > >> > be tweaked to line them up. Or more to the point, there should not b= e > >> > any legitimate reason to use 4BSD these days and modulo the bugs > >> > below, you are probably losing on performance for doing so. > >> > >> An elided simple algorithm for big.LITTLE, from Larry McVoy.. if you > >> run for an entire quantum, flag preference for big core. If you run > >> for less or get punted off, flag for little core preference. > >> > >> > Bugs reported in this thread by others and confirmed by me: > >> > 1. failure to load-balance when having n CPUs and n + 1 workers -- t= he > >> > excess one stays on one the same CPU thread continuously penalizing > >> > the same victim. as a result total real time to execute a finite > >> > computation is longer than in the case of 4BSD > >> > 2. unfairness of nice -n 20 threads vs threads going frequently off > >> > CPU (e.g., due to I/O) -- after using only a fraction of the slice t= he > >> > victim has to wait for the cpu hog to use up its entire slice, rinse > >> > and repeat. This extends a 7+ minute buildkernel to over 67 minutes, > >> > not an issue on 4BSD > >> > > >> > I did not put almost any effort into investigating no 1. There is co= de > >> > which is supposed to rebalance load across CPUs, someone(tm) will ha= ve > >> > to sit through it -- for all I know the fix is trivial. > >> > > >> > Fixing number 2 makes *another* bug more acute and it complicates th= e > >> > whole ordeal. > >> > > >> > Thus, bug reported by me: > >> > 3. interactivity scoring is bogus -- originally introduced to detect > >> > "interactive" behavior by equating being off CPU with waiting for us= er > >> > input. One part of the problem is that it puts *all* non-preempted o= ff > >> > CPU time into one bag: a voluntary sleep. This includes suffering fr= om > >> > lock contention in the kernel, lock contention in the program itself= , > >> > file I/O and so on, none of which has bearing on how interactive or > >> > not the program might happen to be. A bigger part of the problem is > >> > that at least today, the graphical programs don't even act this way = to > >> > begin with -- they stay on CPU *a lot*. > >> > > >> > I asked people to provide me with the output of: dtrace -n > >> > 'sched:::on-cpu { @[execname] =3D lquantize(curthread->td_priority, = 0, > >> > 224, 1); }' from their laptops/desktops. > >> > > >> > One finding is that most people (at least those who reported) use > >> > firefox. > >> > > >> > Another finding is that the browser is above the threshold which wou= ld > >> > be considered "interactive" for vast majority of the time in all > >> > reported cases. > >> > > >> > I booted a 2 thread vm with xfce and decided to click around. Spawne= d > >> > firefox, opened a file manager (Thunar) and from there I opened a > >> > movie to play with mpv. As root I spawned make -j 2 buildkernel. it > >> > was not particularly good :) > >> > > >> > I found that mpv spawns a bunch of threads, most notably 2 distinct > >> > threads for audio and video output. The one for video got a priority > >> > of 175, while the rest had either 88 or 89 -- the lowest for > >> > timesharing not considered interactive [note lower is considered > >> > better]. > >> > > >> > At the same time the file manager who was left in the background kep= t > >> > doing evil syscall usage, which as a result bouncing between a regul= ar > >> > timesharing priority and one which made it "interactive", even thoug= h > >> > the program was not touched for minutes. > >> > > >> > Or to put it differently, the scheduler failed to recognize that mpv > >> > is the program to prioritize, all while thinking the background time > >> > waster is the thing to look after (so to speak). > >> > > >> > This brings us to fixing problem 2: currently, due to the existence = of > >> > said problem, the interactivity scoring woes are less acute -- the > >> > venerable make -j example is struggling to get CPU time, as a result > >> > messing with real interactive programs to a lesser extent. If that > >> > gets fixed, we are in a different boat altogether. > >> > > >> > I don't see a clean solution. > > > > One other random anecdote. Windows 11 uses window focus to highly > > boost scheduling priority in an obviously effective way. I have no > > idea how difficult something like that would be to fit into the unix > > world. > > > > I thought about doing something like that, but I consider it dodgy. > Imagine you play some crap from youtube while messing around in a text > editor -- I'm pretty sure the former is more prone to disturbance from > scheduling changes. > > Anyhow after sending the above e-mail an actual solution hit me: the X > server can tell the kernel what processes connect to it over the unix > socket, which again very well may be good enough. > > In the reports I got I found pulseaudio, this one may need to be > patched in a similar manner. Yeah that seems like an easier problem, IMO something like a userspace audio server (or its init script) should be in charge of setting it to RT. > -- > Mateusz Guzik