Skip to content

lib: MCPS budget manager - #6649

Merged
lgirdwood merged 2 commits into
thesofproject:mainfrom
kfrydryx:mcps_pr
Jan 9, 2023
Merged

lgirdwood merged 2 commits into
thesofproject:mainfrom
kfrydryx:mcps_pr

Conversation

@kfrydryx

Copy link
Copy Markdown
Contributor

Add initial implementation of mcps budget management.

Ideally it should be used to control cpu clock, so that it runs on the lowest frequency suitable for performed tasks.
Different parts of FW can request/free some budget using core_cps_adjust and clock should be changed accordingly. (i.e. when setting pipeline to run we should request budget required by its modules).

Currently there is no proper performance data to be used, so it's advised to request some artificial, high budget on platform init to keep the clock running fast and avoid performance issues.

Requires zephyrproject-rtos/zephyr#52205 to work on mtl

@kfrydryx
kfrydryx force-pushed the mcps_pr branch 5 times, most recently from 4091080 to d99810d Compare November 23, 2022 08:24

@abonislawski abonislawski 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.

Why there are no users of this manager? Even not supporting IPC to set CoreCpsAdjust from IPC data?

Comment thread src/platform/intel/ace/lib/clk.c Outdated

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.

CONFIG_ZEPHYR_NATIVE_DRIVERS not needed in this file

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.

right

@kfrydryx
kfrydryx force-pushed the mcps_pr branch 2 times, most recently from 77dfed5 to e6436e4 Compare November 23, 2022 10:38

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.

This is not recommended on multicore as the array[CORE_COUNT] can easily be clobbered. This structure should either use coherent API or be uncache only (with the correct SPARSE types).

Comment thread src/lib/cps-budget-manager.c Outdated

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.

Needs to be a for loop with an array size end condition.

Comment thread src/lib/cps-budget-manager.c Outdated

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.

Given my previous statement, its worth saying in a comment that this should be in uncache (which it should be on Zephyr) so no one is tempted to move it to cache.

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.

is a comment there sufficient or do we have sth else (sparse?) for uncache specifically?

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.

@lyakh can sparse be used to say static variables are uncache atm ?

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.

@lgirdwood don't think so. An address space has to be specified explicitly for sparse to check it, all objects, that don't specify one explicitly are assumed to belong to the default address space - regardless whether static or automatic.

Comment thread app/boards/intel_adsp_ace15_mtpm.conf Outdated

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.

newline

Comment thread src/platform/intel/ace/lib/clk.c Outdated

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.

would be nice to have something in the logs here.

@abonislawski abonislawski 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.

Please add basic usage for this driver/manager in base_fw (set&get kcps data)

@btian1
btian1 requested review from btian1 and ranj063 November 25, 2022 02:11
@btian1

btian1 commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

Add initial implementation of mcps budget management.

Ideally it should be used to control cpu clock, so that it runs on the lowest frequency suitable for performed tasks. Different parts of FW can request/free some budget using core_cps_adjust and clock should be changed accordingly. (i.e. when setting pipeline to run we should request budget required by its modules).

Currently there is no proper performance data to be used, so it's advised to request some artificial, high budget on platform init to keep the clock running fast and avoid performance issues.

Requires zephyrproject-rtos/zephyr#52205 to work on mtl

For clock adjustment, discuss with single core first, if I remember correctly, ADSP have 38.4/100/400 Mhz to choose, 38.4 is not possible for now, since a single playback is 40-50 mcps, and peak may go to 60, 38.4 may be possible for capture, I did not test it, later CI will have data on capture.

So our strategy may be simple: whether need set clock to 100Mhz, a simple way is depend on pipeline and component num, we do have those data, please check implementation from app/perf_overlay.conf, later CI will have a report for different case performance data.

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.

What's the full name for cps and kcps? anything special inside?

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.

cycles per second and kilo cycles per second. cps = 1000 * kcps. Nothing special inside, though I did unify the naming to use kcps where possible.

@btian1 btian1 left a comment

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.

whether need a extra function for estimate current workload performance based on pipeline num and component name/num? then consolidate these data and set freq accordingy?

@kfrydryx
kfrydryx force-pushed the mcps_pr branch 8 times, most recently from 15a918d to 7dbef99 Compare November 28, 2022 13:26
Comment thread src/audio/base_fw.c Resolved
@kfrydryx
kfrydryx force-pushed the mcps_pr branch 4 times, most recently from 657ce00 to acc9e9e Compare November 28, 2022 15:25
@kfrydryx

Copy link
Copy Markdown
Contributor Author

@btian1 The performance numbers can come in multiple forms. We definitely need some base usage for basefw. For audio modules their usage should come in ipc4 message (the values there are unmeasured yet afaik). Maybe some other parts of fw will have to request budget for themselves. That'll come with future PRs.

Comment thread src/audio/base_fw.c Resolved
@lgirdwood

Copy link
Copy Markdown
Member

@kfrydryx btw, is there any CI test that uses code in this PR or is this currently locally tested ? i.e will this code be invoked today if CI runs a low MCPS pipeline ?

Comment thread src/lib/kcps-budget-manager.c Resolved
Comment thread src/lib/kcps-budget-manager.c Resolved
Comment thread src/audio/base_fw.c
return ipc4_logging_enable_logs(first_block, last_block, data_offset, data);
case IPC4_REGISTER_KCPS:
return basefw_register_kcps(first_block, last_block, data_offset, data);
case IPC4_RESOURCE_ALLOCATION_REQUEST:

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.

@kfrydryx did you try to clarify with ACE FW/Windows SW why do we have two IPC's doing actually the same?

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 did not. register_kcps is not used in internal testing, though the call is implemented. Both are handled by closed source fw.

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.

is 1 mcps == 1000 kcps?

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.

yes, exactly. Just like in metric system. 1 MCPS = 1000 KCPS = 1000 000 CPS.

@kfrydryx

kfrydryx commented Dec 2, 2022

Copy link
Copy Markdown
Contributor Author

@lgirdwood This code is not used yet anywhere else, other than in those IPC requests. There are tests in internal CI calling them (though they are not made exclusively to test this functionality).

@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.

@kfrydryx I assume this should be our single way/API to control CPU frequency/source (when it's enabled to avoid collisions with the manual API) ? If so, I would recommend

  1. We check current CPU frequency/source is correct before we set new frequency/source.
  2. I would call it cpu-clk-manager.c/.h, the word "budget" suggest scheduling and this PR is about clock frequency/source.

Comment thread src/include/ipc4/base_fw.h Outdated

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.

can we prefix to avoid any naming collisions/confusion ?

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.

Sure
Also:
File name changed.
I was thinking about having a pause/resume mechanism for overriding the managers clock choice, but according to @mmaka1 it is currently sufficient. This can be easily worked around by requesting artificial kcps budget. We were also considering an approach of holding a reference of who requested the clock and decide on the change on clock driver level, but left that topic for later.

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.

We need to add doxygen for these new APIs.

@kfrydryx
kfrydryx force-pushed the mcps_pr branch 2 times, most recently from 76d2a24 to cf08404 Compare December 7, 2022 11:00
Comment thread src/include/sof/lib/cpu-clk-manager.h Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pls update the pre-proc symbols to reflect the new (nice) name.

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.

Sorry for the late answer, sure. Missed that.

Comment thread src/lib/cpu-clk-manager.c Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using core and core_id as symbols is a bit confusing, especially that the first variable contains a core id too. core could be renamed as adjusted_core_id or sth similar.

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.

agree, changed

Comment thread src/lib/cpu-clk-manager.c Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not sure why core is of signed int type (also in many other places in the code) while this piece of the code (as many others) works only for sequential values in 0..CONFIG_CORE_COUNT-1 range. Note that cpu_get_id() is now mapped to Zephyr's arch_proc_id() that returns uint32_t. Note that the latter does not guarantee the condition mentioned above (see https://docs.zephyrproject.org/latest/hardware/porting/arch.html#c.arch_proc_id), so indexing kcps_consumption with core might not be safe, unless there is some integration note but I did not find anything in place the cpu_get_id() is declared/defined.

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.

That's the way it's done in other parts of code. I also couldn't find any spec guaranteeing core ids to be continues and starting at 0. I can imagine something like platform specific iterator for enumerating cores, but that is probably not the place for such discussion. And it would require refactoring other code too.
I did change the iterators to be unsigned, just for the sake of it :)

Comment thread src/lib/cpu-clk-manager.c Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Defining multiple exit points and requiring serious cleanup like unlock in all that places might be less readable that setting error code to the local variable and jump to the label near the end of hte function.

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.

ok

Comment thread src/audio/base_fw.c Outdated

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.

This needs to be POSIX and do the IPC4 translation in the IPC layer.

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 see, in base_fw.c they are mixed up. I suppose that's just lack of cleanup?

@btian1

btian1 commented Jan 3, 2023

Copy link
Copy Markdown
Contributor

Is there a way to adjust DSP clock at runtime instead of at beginning? this is because beginning performance is poor at the first 1-3 ms running.

Initial implementation of MCPS budget manager.
FW can request/free MCPS usage on enabling certain FW elements. Caution
should be taken when processing on different cores, as kcps_budget_data
is uncache only. The manager will change clock source to match requested
MCPS consumption. Budget manager may interfere with manually setting
clock values!

Signed-off-by: Krzysztof Frydryk <krzysztofx.frydryk@intel.com>
Handle register_kcps, resource_allocation_request for kcps,
power_state_info_get.

Signed-off-by: Krzysztof Frydryk <krzysztofx.frydryk@intel.com>
@kfrydryx

kfrydryx commented Jan 4, 2023

Copy link
Copy Markdown
Contributor Author

@btian1 Well... that's what this is for. The desired model is to register everything that is running on the fw and change clock accordingly at runtime. At the moment there's not enough performance data for this to make sense, so that PR consists only of base mechanism and some IPC handling as usage.
Also, at least for MTL, I believe the clock is hardcode selected to fastest at boot.

@btian1

btian1 commented Jan 4, 2023

Copy link
Copy Markdown
Contributor

ok,we will have mcps data later, Q1 will happen, for one single playback, expectation is change clock one time except there are new pipe join, since change clock also takes time.

Also I would echo Liam's suggestion, title and file name better come up with: cpu-clk-manager.c/.h or adsp-clk-manager.c/h

@lgirdwood

Copy link
Copy Markdown
Member

@pblaszko good for you now ?

@lgirdwood
lgirdwood dismissed pblaszko’s stale review January 9, 2023 15:11

Review request changes have been made.

@lgirdwood
lgirdwood merged commit e584701 into thesofproject:main Jan 9, 2023
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