Skip to content

!threads: split cpuTime into user and system time#813

Open
oI0ck wants to merge 1 commit into
masterfrom
michal.lach/system_time
Open

!threads: split cpuTime into user and system time#813
oI0ck wants to merge 1 commit into
masterfrom
michal.lach/system_time

Conversation

@oI0ck

@oI0ck oI0ck commented Jul 20, 2026

Copy link
Copy Markdown
Member

TASK: RTOS-1382

Description

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request splits the single thread CPU time tracking into separate user and system CPU times, updating the thread structures, syscall dispatching, and thread iteration logic accordingly. Feedback on the changes highlights a critical bug in the newly added threads_updateCpuTime function, where an inverted NULL check will cause a NULL pointer dereference. Additionally, it is recommended to hold the spinlock for the entire duration of the time update to prevent data races, and to replace the runtime NULL check with a debug assertion.

Comment thread proc/threads.c Outdated
Comment thread proc/threads.c Fixed
Comment thread proc/threads.c Fixed
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown

Unit Test Results

11 305 tests   - 2   10 597 ✅  - 2   1h 0m 37s ⏱️ + 8m 10s
   690 suites ±0      707 💤 ±0 
     1 files   ±0        1 ❌ ±0 

For more details on these failures, see this check.

Results for commit ef90a2b. ± Comparison against base commit fb914a8.

This pull request removes 3 and adds 1 tests. Note that renamed tests count towards both.
phoenix-rtos-tests/mem/mprotect ‑ armv7a9-zynq7000-zedboard:phoenix-rtos-tests/mem/mprotect.test_mprotect.pages_in_child_copied
phoenix-rtos-tests/mem/mprotect ‑ armv7a9-zynq7000-zedboard:phoenix-rtos-tests/mem/mprotect.test_mprotect.pages_in_parent_copied
phoenix-rtos-tests/mem/mprotect ‑ armv7a9-zynq7000-zedboard:phoenix-rtos-tests/mem/mprotect.test_mprotect.test_mprotect_singlecore
phoenix-rtos-tests/mem/mprotect ‑ armv7a9-zynq7000-zedboard:phoenix-rtos-tests/mem/mprotect

♻️ This comment has been updated with latest results.

@oI0ck
oI0ck force-pushed the michal.lach/system_time branch from 7402e53 to 9859532 Compare July 20, 2026 19:52
@oI0ck
oI0ck marked this pull request as ready for review July 20, 2026 20:39
@oI0ck
oI0ck requested a review from a team July 20, 2026 20:39
Comment thread proc/threads.c Outdated
Comment on lines +320 to +322
hal_spinlockSet(&threads_common.spinlock, &sc);
now = _proc_gettimeRaw();
hal_spinlockClear(&threads_common.spinlock, &sc);

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.

Since thread_t already tracks lastTime, you may take now directly from hal_timerGetUs() and detect non-monotonicity in DEBUG by comparing lastTime with now, instead of calling _proc_gettimeRaw(). That way you wouldn't need a spinlock.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Then I'm essentially reimplementing what _proc_gettimeRaw does, without writing to threads_common.prev.
I don't really get the point of it; sure, it will save some time, but then, what is this function for?

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 take spinlock twice for each syscall when you can simply not?

_proc_gettimeRaw() checks non-monotonicity in threads.c from a common prev, since there is no other place to store this information. In your case however, you already store lastTime in the thread, so you can track it from there. You are concerned about reimplementing a two line function.

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.

ok, @etiaro (and the tin man) have a point that systemTime and userTime should be synchronized due to use in threadsinfo, so this becomes irrelevant

Comment thread include/sysinfo.h
Comment thread proc/threads.h Outdated
Comment on lines +94 to +97
typedef enum threadTimeKind {
kSystem,
kUser,
} threadTimeKind_t;

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.

Not sure about the naming of this enum. Usually named kernel enums follow xyz_t -> values: xyz_a, xyz_b, xyz_c etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are two conventions, one is the one you mentioned, the other one is the one I used.
I don't think we have guidelines regarding enum definition styles.

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.

New enums follow the style I have described, look in the code. Your convention is not used anywhere except for message types, which are exported. Besides, kSystem is simply cryptic.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Corrected

@oI0ck
oI0ck force-pushed the michal.lach/system_time branch from 9859532 to 34a9bd6 Compare July 21, 2026 18:47
@oI0ck oI0ck changed the title threads: split cpuTime into user and system time !threads: split cpuTime into user and system time Jul 21, 2026
@oI0ck
oI0ck force-pushed the michal.lach/system_time branch from 34a9bd6 to ef90a2b Compare July 22, 2026 15:19
@oI0ck
oI0ck requested review from adamgreloch and etiaro July 22, 2026 15:19

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

Please ensure all kernel/userspace boundaries are included, as we don't always pass through a syscall return, e.g. through a few calls of hal_jmp.

Consider what should happen on execve syscalls (currently the time will go over to the exec'd process, but maybe that's fine? Is it standardized in any way?).

Another boundaries are user interrupts and exceptions (especially pageFault, as we pretend to support lazy-allocations which would make it happen in regular execution).
Not sure what to do about these, maybe nothing, maybe leave a memo in a comment at least.

@oI0ck

oI0ck commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@etiaro Linux differentiates between 'user time' vs 'system time' and 'irq time', we can do that too I think.

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.

4 participants