Skip to content

volume: do not schedule ramping as separate task - #2619

Merged
tlauda merged 2 commits into
thesofproject:masterfrom
tlauda:topic/volume-start-task-on-trigger
Apr 28, 2020
Merged

tlauda merged 2 commits into
thesofproject:masterfrom
tlauda:topic/volume-start-task-on-trigger

Conversation

@tlauda

@tlauda tlauda commented Mar 24, 2020 •

Copy link
Copy Markdown
Contributor

We should not be scheduling ramping as a separate task.
This patch makes it a part of copy function, which allows
for removal of entire schedule related code.

Signed-off-by: Tomasz Lauda tomasz.lauda@linux.intel.com

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from 9ac0b60 to 9981ee3 Compare March 24, 2020 16:38

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have data ? Are we spinning on a lock or actually consuming too many cycles here ?

@tlauda

tlauda commented Mar 25, 2020

Copy link
Copy Markdown
Contributor Author

Do you have data ? Are we spinning on a lock or actually consuming too many cycles here ?

For standard format volume_copy uses ~6 MCPS and this task scheduling added additional ~4 MCPS, so it almost doubled time of first copy. I suspect it's due to the fact that ll scheduler's timer domain is synchronized among all the cores, so it uses good portion of uncached memory. Nevertheless, there is no reason to not move it to trigger instead of copy.

@lgirdwood

Copy link
Copy Markdown
Member

@tlauda ok, I'm glad you have the data ~4MPCS seems high for scheduling but we can address that later. Maybe it's better here to do the ramp change calculation as part of copy() ? i.e. no scheduling for volume ramp, we just re-calc the ramp on every copy().
It may also be beneficial to add a pipeline timestamp in struct pipeline that is updated on every pipeline_copy(), this way all components could use this time to work in sync and allow ramping etc. Also saves looking up HW timestamps with locking etc.

@tlauda

tlauda commented Mar 25, 2020 •

Copy link
Copy Markdown
Contributor Author

@lgirdwood It's a good idea. I will update this PR to do the ramping in the copy itself and drop the entire task scheduling.
However I didn't get the second part. Why the rest of the pipeline needs to know about ramping at all?

My thinking here is that if we sample the timestamp every time the pipeline is scheduled (and subtract the pipeline start time) then components can use this to get the "pipeline time" in order to help with any sync or for items like ramping. Not essential though, just thinking aloud as it may be something useful for future features.

@paulstelian97

Copy link
Copy Markdown
Collaborator

@lgirdwood I assume you are aware edits don't generate notifications for anyone.

@lgirdwood

Copy link
Copy Markdown
Member

@paulstelian97 edit was done about 10 secs after posting so nothing lost.

@mmaka1

mmaka1 commented Mar 26, 2020

Copy link
Copy Markdown

This is a good catch. A call to schedule() should not be a part of copy() since it is probably not expected to be speed optimized. There are for example trace() calls inside (important ones, so rather should not go to dbg level), many O(n) loops inside that could be optimized if necessary (I believe it is not).

Btw schedule_task() behavior looks a bit strange, as it quits silently if there is no schedule_task handler for the specified type. It also keeps on iterating once called the handler - is this intended behavior? I am not sure how to explain that in the API documentation.

@tlauda

tlauda commented Mar 26, 2020

Copy link
Copy Markdown
Contributor Author

This is a good catch. A call to schedule() should not be a part of copy() since it is probably not expected to be speed optimized. There are for example trace() calls inside (important ones, so rather should not go to dbg level), many O(n) loops inside that could be optimized if necessary (I believe it is not).

Btw schedule_task() behavior looks a bit strange, as it quits silently if there is no schedule_task handler for the specified type. It also keeps on iterating once called the handler - is this intended behavior? I am not sure how to explain that in the API documentation.

I believe both of your observations are actually bugs in the API. Every schedule call should fail if there is no scheduler available or handler is mandatory (and I believe schedule_task handler is). Also the loop should return after first handler execution, unless we expect multiple schedulers per same type.

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from 9981ee3 to 389a52a Compare March 26, 2020 16:56
@tlauda tlauda changed the title volume: start ramping task on trigger volume: do not schedule ramping as separate task Mar 26, 2020
@tlauda

tlauda commented Mar 26, 2020

Copy link
Copy Markdown
Contributor Author

@lgirdwood Updated.

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, what's the new MCPS now ? Might also be worth sharing your MPCS data on next Tuesdays call so we can address any other problem areas like demux ? @lbetlej ?

@singalsu

Copy link
Copy Markdown
Collaborator

This makes volume ramp step equals copy scheduling. If the volume is scheduled e.g. every 10 ms the zipper noise from discontinuous audio waveform would be very audible and annoying.

The copy() could be of course split to reasonable length parts to avoid zipper noise. Now the macro VOL_RAMP_UPDATE_US becomes ignored.

Also the topology defined volume ramp length would go wrong if scheduling is changed from every 1 ms.

@singalsu singalsu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to include component schedule rate to ramp step calculation. VOL_RAMP_STEP_CONS need to be a variable that's calculated when the scheduling is known.

The volume copy() split to 1 ms parts for ramp update need to be done later if volume is used in lower schedule rate pipelines.

@ranj063

ranj063 commented Mar 26, 2020

Copy link
Copy Markdown
Collaborator

@tlauda I maybe missing something here. But when you get a new volume set cmd, volume_ramp() should ramp up/down in increments until the target volume is reached. I do not see a while loop to continue ramping or a reccursive call to volume_ramp() in volume_ctrl_set_cmd(). What am I missing?

@tlauda

tlauda commented Mar 26, 2020

Copy link
Copy Markdown
Contributor Author

@ranj063 No, you're right. I've missed this case. ramp_finished should be set to false, so the ramp will continue in next copies.

@lgirdwood @singalsu I didn't think about such scheduling differences. Maybe we should go back to the original idea with starting task on trigger start? It will be easier that way than trying to modify this solution and hoping no mistakes will be done. MCPS are also measured for whole system ticks, so no data will be lost.

@lgirdwood

Copy link
Copy Markdown
Member

@singalsu I see what you are saying, if ramp step time is < period time then we can get zipper. We need to check this at the start of copy so we can scale it correctly during the copy.

Comment thread src/audio/volume/volume.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you are removing the ramping actually? we did several cycles to ramp up to the target volume, now it will only do the step 1 and stop if it doesn't achieve to the target volume.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I get your point now, we will continue the ramping in the next .copy() iteration, this is good idea, even we can't finish the ramping target before the pipeline is trigger stopped, we still keep the target volume there so it should be fine.

@singalsu

Copy link
Copy Markdown
Collaborator

@singalsu I see what you are saying, if ramp step time is < period time then we can get zipper. We need to check this at the start of copy so we can scale it correctly during the copy.

I adjusted the vol_work() schedule rate a while ago to fix Github error about reported by @plbossart annoying noise when volume is ramped (e.g. press keys 0 - 9 on ASCII alsamixer slider). The value of 1000 us is where with the current volume scale takes some effort to hear the effect. A more dense grid would have been better but I tried to find a good trade-off for subjective volume change quality vs. resources consumed.

We can schedule components/pipelies freely. I think 1 ms is max. feasible rate for a low latency system. The schedule could be also lower rate to improve cache performance etc. If volume schedule would be changed to e.g. every 2 ms or more the zipper noise issue would arrive again. I like this idea (the scheduling jitter impacts ramp) but the volume copy() should be split to max. 1ms parts so we can maintain the volume ramp update grid.

Also the ramp step computation needs to take into account stream rate and component scheduling. With the independent vol_work() there was no need.

The volume in the intrinsic version could be updated with good code even per sample without much overhead to completely make zipper noise issue vanish but it might be overkill.

This change by the way has the benefit that without vol_work() the testbench can fully simulate volume component. There's now script volume_run.sh. Convert you wav to raw with sox and run the sound clip with that script... easy to listen zipper sounds!

@tlauda

tlauda commented Mar 27, 2020

Copy link
Copy Markdown
Contributor Author

@singalsu So you're saying that the volume_copy will always need to execute every 1 ms, no matter what is the scheduling period for pipeline? With current pipeline scheduling implementation it's not possible to achieve. Pipelines are treated as a whole and we cannot schedule individual components separately.

@singalsu

Copy link
Copy Markdown
Collaborator

@tlauda Not that way! I meant that it's simple in your patch to alter the copy() function is such way that it processes a fixed amount between volume ramp updates. E.g. 3 ms scheduling.

copy() {
update ramp
process 1 ms
update ramp
process 1ms
update ramp
process 1ms
}

With timer based scheduling there's a bit jitter in number of frames to process in each copy() but this doesn not need to be perfect. E.g. the last block can be a bit longer or shorter.

You need to calculate how many frames to call the process function to not exceed the max ramp update time so that a steady ramp slope is achieved. The N to process depends on stream sample rate.

@lgirdwood

Copy link
Copy Markdown
Member

@tlauda @singalsu how are we in terms of MCPS ? if ZC is disabled there should be no impact.

  1. If ZC is enabled and period size is small 1ms
  2. as 1 but with period size 10ms ?
    There is extra effort in finding the ZC samples for each channel but we only need to do this minimally and always revert to standard copy() function when we can.

@tlauda

tlauda commented Apr 23, 2020

Copy link
Copy Markdown
Contributor Author

@tlauda @singalsu how are we in terms of MCPS ? if ZC is disabled there should be no impact.

  1. If ZC is enabled and period size is small 1ms
  2. as 1 but with period size 10ms ?
    There is extra effort in finding the ZC samples for each channel but we only need to do this minimally and always revert to standard copy() function when we can.

Rough numbers for 48 kHz, 32 bit, 2 channels:

  1. ZC disabled with period 1 ms: ~6 MCPS.
  2. ZC enabled with period 1 ms: up to ~9 MCPS.
  3. ZC disabled with period 10 ms: ~50 MCPS.
  4. ZC enabled with period 10 ms: ~80 MCPS.

@lgirdwood

Copy link
Copy Markdown
Member

Ok, @singalsu @tlauda do these numbers look ok for you. Btw, what happens when the ramp is complete ? do the ZC numbers go back to non ZC values ?

Rough numbers for 48 kHz, 32 bit, 2 channels:

  1. ZC disabled with period 1 ms: ~6 MCPS.
  2. ZC enabled with period 1 ms: up to ~9 MCPS.
  3. ZC disabled with period 10 ms: ~50 MCPS.
  4. ZC enabled with period 10 ms: ~80 MCPS.

Dividing 3 & 4 by 10 (for period size) looks reasonable to be and inline with 1 ms period..

@tlauda

tlauda commented Apr 23, 2020 •

Copy link
Copy Markdown
Contributor Author

Ok, @singalsu @tlauda do these numbers look ok for you. Btw, what happens when the ramp is complete ? do the ZC numbers go back to non ZC values ?

Rough numbers for 48 kHz, 32 bit, 2 channels:

  1. ZC disabled with period 1 ms: ~6 MCPS.
  2. ZC enabled with period 1 ms: up to ~9 MCPS.
  3. ZC disabled with period 10 ms: ~50 MCPS.
  4. ZC enabled with period 10 ms: ~80 MCPS.

Dividing 3 & 4 by 10 (for period size) looks reasonable to be and inline with 1 ms period..

Yes, they go back to non ZC values.

@lgirdwood

Copy link
Copy Markdown
Member

Jenkins has known issues. Internal CI seems to be unrelated WHL failures. Retry.

@lgirdwood

Copy link
Copy Markdown
Member

SOFCI TEST

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from d1dcc7c to c936880 Compare April 23, 2020 11:39
@tlauda

tlauda commented Apr 23, 2020

Copy link
Copy Markdown
Contributor Author

@singalsu Changes applied.

@dbaluta

dbaluta commented Apr 23, 2020

Copy link
Copy Markdown
Collaborator

@tlauda out of contest question:

  • what does MCPS stands for? M Cycles per Seconds?
  • how do you guys measure that?

@singalsu

Copy link
Copy Markdown
Collaborator

After merge can you do a separate PR to enable ZC volume mode in some topology to enable to keep it tested in CI? E.g. @fredoh9 and @xiulipan could suggest platforms with possibility to test such. We also need tests to check audio level envelope while volume is being controlled to ensure the ramps and mute/unmute happens.

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from c936880 to 20133a7 Compare April 23, 2020 14:16
@tlauda

tlauda commented Apr 23, 2020

Copy link
Copy Markdown
Contributor Author

@dbaluta Million Cycles Per Second.
@mmaka1 has added some measurements some time ago in comp_copy. You can check in log processing peak of every running component and see how many cycles it took.

@singalsu Will do. @xiulipan @fredoh9 Any suggestions?

@singalsu

singalsu commented Apr 23, 2020 •

Copy link
Copy Markdown
Collaborator

There's some difference in overall ramp time in the two modes. Without ZC correct 250 ms and in this case with ZC (997 Hz sine) about 220 ms. The step logic has some flaw likely.

Screenshot from 2020-04-23 17-23-20
Screenshot from 2020-04-23 17-22-46

With testbench (need #2824)

$ sox sine_997hz_3db_1s_stereo_16b.wav sine_997hz_3db_1s_stereo_16b.raw
$ ./volume_run.sh  16 16 48000 sine_997hz_3db_1s_stereo_16b.raw out_withzc.raw
$ raw2wav16b2c48k.sh out_withzc.raw out_withzc.wav
$ cat ~/bin/raw2wav16b2c48k.sh
#!/bin/sh
sox --encoding signed-integer -L -r 48000 -c 2 -b 16 "$1" "$2"

Edit: Both sound OK. In spectral view there's slightly less high-frequency content with ZC and from the irregularity in noise spectra it can be seen that ZC is indeed selecting the gain update position.

@tlauda

tlauda commented Apr 23, 2020

Copy link
Copy Markdown
Contributor Author

@singalsu Isn't it because we are always assuming ramp increment step of 1 ms? But with ZC we can get slightly lesser granularity, which ends up in faster ramp time.

@singalsu

Copy link
Copy Markdown
Collaborator

The step value should be recomputed for actual update if you don't add a no-updates allowed wait time.

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from 20133a7 to 97343d3 Compare April 24, 2020 09:36
@tlauda

tlauda commented Apr 24, 2020

Copy link
Copy Markdown
Contributor Author

@singalsu Added ramp step recalculation. It looks like ZC has the right ramp time now.

@xiulipan

Copy link
Copy Markdown
Contributor

@tlauda @singalsu will try to work with @ClarexZhou @keqiaozhang to create some new test case for this feature.

@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch 2 times, most recently from a670fd2 to ea9f6c2 Compare April 24, 2020 10:56
Comment thread src/include/sof/audio/volume.h Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about this since the pre-processor can't compute this. Does this emit emulated float code?

Comment thread src/audio/volume/volume.c Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the float code likely appears here. Can you check it and convert to all-integer fractional math.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't fully understand. The macro expands to Q_CONVERT_FLOAT, which explicitly casts to float. What do you mean by converting to "all-integer fractional math"?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The divide by 1000.0 becomes a run-time float divide if the argument to VOL_RAMP_STEP_CONST(x) is not a constant number calculated by pre-processor. Did you look into disassembly how this got handled?

Fractional arithmetic uses mostly fixed multiplication, and more costly fixed division if integer multiply by inverse is not available. I'm not sure this change emitted C library float code but since there was need to adjust memory I wonder if it happened.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if things get complicated instead of step size compute (where error does accumulate a bit) the actual gain could be computed from time since volume transition start.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@singalsu Yes, unfortunately it looks like float code is generated.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's off-topic but I wonder if it would be possible create compile error if argument for Q_CONVERT_FLOAT() is not a constant value. It's a useful macro but hides this risk.

@singalsu singalsu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that there's no floats math generated.

tlauda added 2 commits April 28, 2020 11:38
Implements functions for getting nearest zero crossing frame.
They will be used to reduce zipper noise during volume ramping
by updating ramp only on such frames. In the current implementation
average value of all channels is used to check sample sign change.

Signed-off-by: Tomasz Lauda <tomasz.lauda@linux.intel.com>
We should not be scheduling ramping as a separate task.
This patch makes it a part of copy function, which allows
for removal of entire schedule related code.
Ramping is done in maximum of 1 ms chunks by using functions
for retrieval of nearest zero crossing frame.

Signed-off-by: Tomasz Lauda <tomasz.lauda@linux.intel.com>
@tlauda
tlauda force-pushed the topic/volume-start-task-on-trigger branch from ea9f6c2 to 5486b2f Compare April 28, 2020 10:00
@tlauda
tlauda requested a review from singalsu April 28, 2020 10:02
@tlauda
tlauda merged commit 1b57609 into thesofproject:master Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants