[PATCH v2] sound: rawmidi: Add framing mode

Takashi Sakamoto o-takashi at sakamocchi.jp
Sat Mar 27 04:44:31 CET 2021


Hi,

>From Takashi (not maintainer),

On Fri, Mar 26, 2021 at 05:29:04PM +0100, David Henningsson wrote:
> On 2021-03-26 08:55, Takashi Iwai wrote:
> > On Fri, 26 Mar 2021 05:46:15 +0100, Takashi Sakamoto wrote:
> > > On Wed, Mar 24, 2021 at 04:57:31PM +0100, David Henningsson wrote:
> > > > > However, the timing jitter of IRQ handler invocation is issued in this
> > > > > case, as well as PCM interface, even if the data rate of MIDI physical
> > > > > layer is quite low nowadays (31.25 Kbit / sec ~= 3906.25 byte / sec).
> > > > > As long as I experienced, in actual running Linux system, the invocation
> > > > > of IRQ handler has no guarantee for timing jitter, mainly due to CPU level
> > > > > IRQ mask (like spin_lock). Therefore the interval of each invocation is not
> > > > > so precise to decide event timestamp, at least for time slot comes from
> > > > > MIDI physical layer.
> > > > > 
> > > > > Nevertheless, I think your idea is enough interesting, with conditions to
> > > > > deliver information from driver (or driver developer) to applications
> > > > > (ALSA Sequencer or userspace applications). Even if we have some
> > > > > limitation and restriction to precise timestamp, it's worth to work for
> > > > > it. It seems to be required that improvements at interface level and
> > > > > documentation about how to use the frame timestamp you implemented.
> > > > Right, so first, I believe the most common way to transport midi these days
> > > > is through USB, where the 31.25 Kbit/sec limit does not apply: instead we
> > > > have a granularity of 1/8 ms but many messages can fit in each one. So that
> > > > limit is - for many if not most cases - gone.
> > > > 
> > > > Second; you're probably right in that there is still some jitter w r t when
> > > > the IRQ fires. That is regrettable, but the earlier we get that timestamp
> > > > the better, so a timestamp in IRQ context would at least be better than in a
> > > > workqueue that fires after the IRQ, or in userspace that possibly has
> > > > scheduling delays.
> > > > 
> > > > Btw, I checked the "struct urb" and there was no timestamp in there, so I
> > > > don't know how to get a better timestamp than in snd_rawmidi_receive. But
> > > > maybe other interfaces (PCI, Firewire etc) offers something better.
> > > Hm. Regardless of type of hardware; e.g. OHCI/EHCI/xHCI, 1394 OHCI, or
> > > PCI-e extension card, for software to process sampled data, it's always
> > > issue that the jitter between triggering IRQ (hardware side) and invoking
> > > IRQ handler (processor side). As an actual example, let me share my
> > > experience in 1394 OHCI.
> > > 
> > > 
> > > 1394 OHCI controller is governed by 24.576 Mhz clock (or double depending
> > > on vendors). In IEEE 1394, ishcornous service is 8,000 times per second.
> > > 1394 OHCI specification allows software to schedule hardware IRQ per
> > > isochronous cycle.
> > > 
> > > Once ALSA firewire stack is programmed to schedule the hardwar IRQ evenry
> > > 16 isochronous cycle. The corresponding IRQ handler is expected to invoke
> > > every 2 milli second. As long as I tested in usual desktop environment[2],
> > > the jitter is between 150 micro second and 4.7 milli second. In the worst
> > > case, it's 6.0 milli seconds. The above is also the same wnen using
> > > 'threadirqs'.
> > > 
> > > When using 'isolcpus' and arranging 'smp_affinity' to reduce load from one
> > > of processor core to invoke the IRQ handler, the interval is 2 milli
> > > second with +- several nano seconds, therefore the 1394 OHCI controller
> > > can trigger hardware IRQ so precise. The jitter comes from processor side.
> > > I think many running contexts on the same processor core masks IRQ so
> > > often and the jitter is not deterministic.
> > > 
> > > Here, what I'd like to tell you is that we can not handle the system time
> > > as is as timestamp of received MIDI messages, as long as relying on IRQ
> > > context. We need some arrangements to construct better timestamp with some
> > > compensations. In this point, the 3rd version of patch is not enough[3],
> > > in my opinion.
> 
> Interesting measurements. While several ms of jitter is not ideal, I have a
> few counter arguments why I still believe we should merge this patch:
> 
>  1) I don't think we should let the perfect be the enemy of the good here,
> just because we cannot eliminate *all* jitter does not mean we should not
> try to eliminate as much jitter as we can.
 
Exposing something to userspace is easy engineering, but find appropriate
role to it is not as easy for userspace applications.

>  2) an unprivileged process (that cannot get RT_PRIO scheduling) might have
> a wakeup jitter of a lot more than a few ms, so for that type of process it
> would be a big improvement. And sometimes even RT_PRIO processes have big
> delays due to preempt_disable etc.

I agree with the point. When comparing to current implementation of ALSA
rawmidi stack, the patch makes it near true time somwhow to estimate read
MIDI messages since task scheduling includes larger resolution and jitter
than the ones of invocation of IRQ handler.

I reported some issues as a next step. At least, the system time to
invoke IRQ handler needs to be compensated with supplemental
information. Current patch forces the task to userspace applications
silently without any information from kernel space. This results in failure
to set incentive for userspace developers to use the new functionality, in
my opinion.

I guess it better to deliver below information:

 * interval of burstness
  * drivers for packet-based protocol handles several packets at once
 * minimal timestamp gap between successive frames
  * however actually fuzzed by the jitter of invocation of IRQ handler
 * maximum size of frame
  * the target hardware transfer batch of MIDI messages or single midi
    byte at once

The above items is an idea from my side. I need more time and discussion
for better set.

Again, I have no opposition to the idea itself. However in a view of
application developer, it's not necessarily useful against timeslot of
3906.25 bytes/sec.

>  3) The jitter will depend on the hardware, and other hardware might have
> better (or worse) IRQ jitter.
> 
>  4) If this patch gets accepted, it might show other kernel developers that
> IRQ jitter matters to us, and that in turn might improve the chances of
> getting IRQ jitter improvement patches in, in case someone else wants to
> help solving that problem.


Thanks

Takashi Sakamoto


More information about the Alsa-devel mailing list