Skip to content

Conversation

@VijsharQC
Copy link
Contributor

Introduce lifecycle observer callbacks (init, start, stop, expiry) for k_timer using Zephyr's iterable sections pattern. This enables external modules to extend timer functionality without modifying kernel internals.

@github-actions
Copy link

Hello @VijsharQC, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

Copy link
Contributor

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

Can't comment on the usefulness of the feature, but I do have some implementation suggestions.

@VijsharQC
Copy link
Contributor Author

Hi @pdgendt, Thank you for the suggestions. Made changes as per those.
The idea behind these changes are to extend the timer functionality to allow user-driven customization and flexibility.

Copy link
Contributor

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

I think this needs tests.

@VijsharQC VijsharQC force-pushed the timerObserver branch 2 times, most recently from 83c5b1b to 4b649fe Compare December 23, 2025 15:05
@npitre
Copy link

npitre commented Dec 23, 2025

Can you provide examples of what this is useful for?

@VijsharQC
Copy link
Contributor Author

There are few cases where timers may need to operate only in specific modes. This approach allows vendors to add custom functionality to manage those timers without making significant changes to Zephyr’s core timer implementation.

Copy link
Contributor

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

Still missing some sample/test, it's currently not being built in CI.

Ideally a sample is added with a use case to prove the usefulness of the feature.

@zephyrbot zephyrbot added the area: Tests Issues related to a particular existing or missing test label Dec 26, 2025
@VijsharQC VijsharQC force-pushed the timerObserver branch 2 times, most recently from 100b8bc to 0c9c1cd Compare December 26, 2025 07:48
@VijsharQC VijsharQC force-pushed the timerObserver branch 5 times, most recently from c6ebddd to d04c8c1 Compare December 26, 2025 11:33
@VijsharQC
Copy link
Contributor Author

VijsharQC commented Dec 28, 2025

After adding the test code, I see that it is failing with below error. The failure is specific to the qemu_x86 board.
Can you please suggest a way to resolve this.

INFO - 962/962 qemu_x86_tiny/atom kernel.timer.timer_observer FAILED Timeout (qemu 119.955s )
INFO - /__w/zephyr/zephyr/twister-out/qemu_x86_tiny_atom/zephyr/tests/kernel/timer/timer_observer/kernel.timer.timer_observer/handler.log
ERROR - SeaBIOS (version zephyr-v1.16.3-2-0-gf8da0cc-zephyr)
Booting from ROM..
ASSERTION FAIL [page_frames_initialized] @ WEST_TOPDIR/zephyr/kernel/mmu.c:1625
page fault at 0x4011b015 happened too early
E: EAX: 0x4010a805, EBX: 0x4011b015, ECX: 0x4010a805, EDX: 0x40117ee0
E: ESI: 0x4011b015, EDI: 0x00000000, EBP: 0x40117f08, ESP: 0x40117ed4
E: EFLAGS: 0x00000012 CS: 0x0008 CR3: 0x00110000
E: EIP: 0x40105bcd
E: call trace:
E: 0: 0x40104818
E: 1: 0x40105add
E: 2: 0x4010070e
E: 3: 0x4010404a
E: 4: 0x40100411
E: 5: 0x40106c94
E: >>> ZEPHYR FATAL ERROR 4: Kernel panic on CPU 0
E: Current thread: 0x40111000 (main)
E: Halting system

k_busy_wait(10 * 1000);

/* Verify timer on_init was called once */
zassert_equal(obs.init_cnt, 1, "obs init count mismatch");
Copy link
Contributor

Choose a reason for hiding this comment

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

This test assumes that there will only be a single timer in the system--an assumption that is unlikely to hold for all boards. A quick grep in the drivers/ directory shows that there are many drivers that use k_timer; without delving deep into their use cases, it seems reasonable that they may interfere with the expected counts. I'm not sure what the best way to resolve that would be. (It might be to just document it as a comment here noting that those boards should be excluded from the test as they show up).

Copy link
Contributor

Choose a reason for hiding this comment

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

Or only increment the counters if the timer instance matches the test timer

static void obs_on_expiry(struct k_timer *timer)
{
	if (timer == &test_periodic_timer) {
		obs.expiry_cnt++;
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @pdgendt , @peter-mitsis - thank you for the suggestions.

I did try updating the observer callbacks so that they only increment counts when the timer instance matches test_periodic_timer. Unfortunately, the tests are still failing with the same issue.

As mentioned by @peter-mitsis, I’ve excluded qemu_x86_tiny for this test.

Introduce lifecycle observer callbacks (init, start, stop, expiry)
for k_timer using Zephyr's iterable sections pattern. This enables
external modules to extend timer functionality without modifying
kernel internals.

Signed-off-by: Vijay Sharma <[email protected]>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 6, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Kernel area: Linker Scripts area: Tests Issues related to a particular existing or missing test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants