Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BAC9C433B4 for ; Tue, 6 Apr 2021 12:02:38 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9CE9461019 for ; Tue, 6 Apr 2021 12:02:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9CE9461019 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id B53961657; Tue, 6 Apr 2021 14:01:45 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz B53961657 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1617710555; bh=+9AifJGlQOuZV+7b/zpKK470bFwqzXJ5eMjZOLPh+dY=; h=Date:From:To:Subject:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=aSzJLKTMSsGA+p0QpYUrKDBifs1i3235bTfYcIB3lbl28S5OJDG9Ew6dRZQEms+FH zhjCHTn+PnY4VEZMg6iYaFvB7P+JK1G9rl5eZYEoozt68gcaEV4xjZs84uthZiWhVq u1lyDHjot+UX6FaJ3isyM4iwioOkpcXxa9R51nZ8= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 37479F80169; Tue, 6 Apr 2021 14:01:45 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 2DEC1F8020B; Tue, 6 Apr 2021 14:01:43 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 3DD90F800E3 for ; Tue, 6 Apr 2021 14:01:29 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 3DD90F800E3 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A2851B166; Tue, 6 Apr 2021 12:01:29 +0000 (UTC) Date: Tue, 06 Apr 2021 14:01:28 +0200 Message-ID: From: Takashi Iwai To: David Henningsson Subject: Re: [PATCH v2] sound: rawmidi: Add framing mode In-Reply-To: References: <20210324054253.34642-1-coding@diwic.se> <20210324124430.GA3711@workstation> <057ef387-9ee1-2678-29ce-d644f2a3a90a@diwic.se> <20210326044615.GA51246@workstation> <2ca71809-9872-bfee-c19d-76b6ce143212@diwic.se> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Cc: alsa-devel@alsa-project.org X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" On Mon, 05 Apr 2021 14:13:27 +0200, David Henningsson wrote: > > > On 2021-03-31 09:40, Takashi Iwai wrote: > > On Tue, 30 Mar 2021 21:35:11 +0200, > > David Henningsson wrote: > >> > >> Well, I believe that rawmidi provides less jitter than seq is not a > >> theoretical problem but a known fact (see e g [1]), so I haven't tried > >> to "prove" it myself. And I cannot read your mind well enough to know > >> what you would consider a sufficient proof - are you expecting to see > >> differences on a default or RT kernel, on a Threadripper or a > >> Beaglebone, idle system or while running RT torture tests? Etc. > > There is certainly difference, and it might be interesting to see the > > dependency on the hardware or on the configuration. But, again, my > > primary question is: have you measured how *your patch* really > > provides the improvement? If yes, please show the numbers in the > > patch description. > > As you requested, I have now performed such testing. > > Results: > > Seq - idle: 5.0 ms > > Seq - hackbench: 1.3 s (yes, above one second) > > Raw + framing - idle: 2.8 ms > > Raw + framing - hackbench: 2.8 ms > > Setup / test description: > > I had an external midi sequencer connected through USB. The system > under test was a Celeron N3150 with internal graphics. The sequencer > was set to generate note on/note off commands exactly 10 times per > second. > > For the seq tests I used "arecordmidi" and analyzed the delta values > of resulting midi file. For the raw + framing tests I used a home-made > application to write a midi file. The monotonic clock option was used > to rule out differences between monotonic and monotonic_raw. The > result shown above is the maximum amount of delta value, converted to > milliseconds, minus the expected 100 ms between notes. Each test was > run for a minute or two. > > For the "idle" test, the machine was idle (running a normal desktop), > and for the "hackbench" test, "chrt -r 10 hackbench" was run a few > times in parallel with the midi recording application (which was run > with "chrt -r 15"). > > I also tried a few other stress tests but hackbench was the one that > stood out as totally destroying the timestamps of seq midi. (E g, > running "rt-migrate-test" in parallel with "arecordmidi" gave a max > jitter value of 13 ms.) > > Conclusion: > > I still believe the proposed raw + framing mode is a valuable > improvement in the normal/idle case, but even more so because it is > more stable in stressed conditions. Do you agree? Thanks for the tests. Yes, that's an interesting and convincing result. Could you do a couple of favors in addition? 1) Check the other workqueue It's interesting to see whether the hiprio system workqueue may give a better latency. A oneliner patch is like below. -- 8< -- --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c @@ -1028,7 +1028,7 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, } if (result > 0) { if (runtime->event) - schedule_work(&runtime->event_work); + queue_work(system_highpri_wq, &runtime->event_work); else if (__snd_rawmidi_ready(runtime)) wake_up(&runtime->sleep); } -- 8< -- Also, system_unbound_wq can be another interesting test case instead of system_highpri_wq. 2) Direct sequencer event process If a chance of workqueue doesn't give significant improvement, we might need to check the direct invocation of the sequencer dispatcher. A totally untested patch is like below. -- 8< -- --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c @@ -979,6 +979,7 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, unsigned long flags; int result = 0, count1; struct snd_rawmidi_runtime *runtime = substream->runtime; + bool call_event = false; if (!substream->opened) return -EBADFD; @@ -1028,11 +1029,13 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, } if (result > 0) { if (runtime->event) - schedule_work(&runtime->event_work); + call_event = true; else if (__snd_rawmidi_ready(runtime)) wake_up(&runtime->sleep); } spin_unlock_irqrestore(&runtime->lock, flags); + if (call_event) + runtime->event(runtime->substream); return result; } EXPORT_SYMBOL(snd_rawmidi_receive); -- 8< -- In theory, this should bring to the same level of latency as the rawmidi timestamping. Of course, this doesn't mean we can go straight to this way, but it's some material for consideration. Takashi