!syscalls/threadsinfo: return more granular thread information#720
!syscalls/threadsinfo: return more granular thread information#720
Conversation
Summary of ChangesHello @oI0ck, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
55881ff to
c647754
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a more flexible threadsinfo syscall, which is a great improvement for querying thread information. The refactoring of proc_threadsList into smaller helper functions is also a positive change. However, I've identified a few critical issues that must be addressed. These include a typo that will cause a compilation error, and incomplete validation of user-provided arguments in the syscall handler, which poses a security risk. Please see my detailed comments for each issue.
c647754 to
5104b56
Compare
3c6ba52 to
93985e7
Compare
93985e7 to
fbb7eaf
Compare
Unit Test Results9 522 tests - 1 8 895 ✅ - 36 52m 13s ⏱️ - 3m 7s For more details on these failures, see this check. Results for commit 80c5dc5. ± Comparison against base commit 0efd690. This pull request removes 2 and adds 1 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
fbb7eaf to
e3f8392
Compare
|
CI test fails are a result of the fact that this PR breaks compatibility and it requires correspondent changes in |
e3f8392 to
36b7461
Compare
| } | ||
|
|
||
|
|
||
| static inline int _proc_calculateVmem(thread_t *thread) |
There was a problem hiding this comment.
prefixed with _ - does it require any external sync? there is a spinlock inside, so I guess not?
There was a problem hiding this comment.
It uses a thread_t *, so we have to guarantee that it won't be deallocated, therefore it should be called with threads_common.lock set, as all the other functions here.
There was a problem hiding this comment.
I would make it explicit in the comment then, as the use of thread_t * alone doesn't imply that - usually you just up the refcnt on a thread, guaranteeing that it won't get deallocated when you need it and threadsinfo is an exception.
There was a problem hiding this comment.
Please add a comment similar to one preceeding _proc_threadInfo
proc/threads.c
Outdated
|
|
||
|
|
||
| int proc_threadsList(int n, threadinfo_t *info) | ||
| static inline void _proc_makeProcessName(thread_t *thread, char *name, int sz) |
There was a problem hiding this comment.
prefixed with _ - does it require any external sync?
proc/threads.c
Outdated
| i++; | ||
| t = lib_idtreeof(thread_t, idlinkage, lib_idtreeNext(&t->idlinkage.linkage)); |
There was a problem hiding this comment.
I wonder if we shouldn't just maintain a global thread count instead
There was a problem hiding this comment.
Might be, though I'd say that's good enough for the amount of changes here. We'd have to guarantee that this counter will be kept in sync with node count in tree, which I'd say is besides the point in this PR.
There was a problem hiding this comment.
Yeah, you're right. Maybe add a TODO/REVISIT then for now?
36b7461 to
bad1fa0
Compare
julianuziemblo
left a comment
There was a problem hiding this comment.
MISRA nits (for a better (worse) evening)
proc/threads.c
Outdated
| int i = 0, argc; | ||
| unsigned int len, space; | ||
| thread_t *t; | ||
| int i, len, left = sz; |
There was a problem hiding this comment.
I'd leave left as int (or maybe long?) and change type of i and len and sz to size_t or unsigned int, as sz is always passed from sizeof.
Then of course remember to cast to int when assiging to left as for MISRA...
There was a problem hiding this comment.
Changed all of them to size_t as well as parameter sz, since we always pass sizeof
There was a problem hiding this comment.
Now the test in line 2103 (left <= 0) is pointless because left cannot be < 0. You should change back the type as this probably introduces an error
There was a problem hiding this comment.
eh, right, was tired when commiting that, i'm changing left to long
bad1fa0 to
ee5ddc6
Compare
julianuziemblo
left a comment
There was a problem hiding this comment.
More comments to threadsinfo_t data structure. Some of its members seem to have improper types with no explanation and we could fix it while at it.
|
|
||
| typedef struct _threadinfo_t { | ||
| pid_t pid; | ||
| unsigned int tid; |
There was a problem hiding this comment.
tid seems to sometimes be signed, and sometimes unsigned. Broader question here - do you happen to know if there's a reason for this? Even proc_getTid() returns int. @adamgreloch
There was a problem hiding this comment.
Internally tid is an idtree key which is signed to support POSIX handles
There was a problem hiding this comment.
A signed thread identifier makes little sense as a key in a tree structure. By convention, values such as PID are unique ID based on a counter, so making it signed is seemingly pointless, unless to signal a possibility of returning an error within the negative domain.
There was a problem hiding this comment.
Yes, I agree. My (vague) comment's intention was to explain why tid sometimes happens to be an int and AFAIK it's purely a consequence of idtree implementation.
We should probably clean this up, make it consistently unsigned int across the kernel or even introduce tid_t to control the types even further (outside of this PR).
| int priority; | ||
| int state; |
There was a problem hiding this comment.
state and priority seem to be bitmasks too, could probably be changed to unsigned int
There was a problem hiding this comment.
priority is an index into thread_common.ready, which is an array of arrays containing threads divided by their priority. Signedness of this value is more engraved in the system, as apparent by proc_threadPriority and the priority syscall, changing this seems unnecessary, at least as a part of this PR.
state is an enum with 3 possible values, READY (0U), SLEEP (1U), GHOST (2U), so while, sure, we could change this type here, it is not only breaking, but it might be, that in the future, someone may want to introduce a state signalling an error as a negative value, which would the require changing it back to int, so further breaking changes.
I'd personally stray from making further breaking changes within this PR, as it will be harder to merge it, reason about implications of it, and so on and so forth. The fact that it is already breaking should not be a signal to break even more, without a proper discussion. Review comments are a place to talk through changes made in a specific PR, to probably make it better, not introduce more changes unrelated to the PR.
This should be talked through in-person or on our issue tracker, so if you are keen making those changes, please create a new ticket, I'll be glad to participate in the discussion.
There was a problem hiding this comment.
Valid. From our perspective however, there isn't really ever better time for this discussions than these PRs. But it's hard to tell sometimes how much the discussion is connected with the topic. The recent MISRA changes also affect this mindset somewhat.
These points were brought up here because you're already changing the interface and fixing it in other subrepos, so I wouldn't agree these changes are hard to reason about as there are little users to change.
But, if you think that this is too much of a breaking change, for me it can stay as is for now - the types here seem really weird though, that's what my comments are about.
There was a problem hiding this comment.
@oI0ck makes a good point. I invite you both to RTOS-1196 :)
ee5ddc6 to
e2d308f
Compare
|
threadsinfo was already tinkered with. See: https://github.com/phoenix-rtos/phoenix-rtos-kernel/pull/661/changes/BASE..c74eaa2757cedadb6b6d42e33fcfff97209a563e ( The referenced change will be reviewed and merged first and then this PR should be changed accordingly |
e2d308f to
80c5dc5
Compare
threadsinfo is not very flexible, takes a long time and causes unnecessary overhead in programs using it. This commit means to make it more flexible, by being able to query a thread identified by tid and query for specific thread attributes as specified in flags bitmask. Moreover, previously there was no was of inexpensively checking amount of present threads in the kernel, therefore the buffer passed to threadinfo may have been often reallocated to check all threads. Now threadsinfo, if passed a PH_THREADINFO_OPT_THREADCOUNT flag will yield no information in passed buffer, but will return amount of currently present threads. JIRA: RTOS-1187
JIRA: RTOS-1088
b482830 to
bd88e9f
Compare
julianuziemblo
left a comment
There was a problem hiding this comment.
nits, otherwise LGTM
| now = _proc_gettimeRaw(); | ||
| if (thread->process != NULL) { | ||
| info->pid = process_getPid(thread->process); | ||
| info->ppid = 0; |
There was a problem hiding this comment.
Nit: set info->ppid to 0 outside of if - else as it's always set
|
|
||
|
|
||
| typedef void (*proc_threadsListCb_t)(void *arg, int i, threadinfo_t *info); | ||
| typedef void (*proc_threadsListCb_t)(void *arg, threadinfo_t *info); |
There was a problem hiding this comment.
Nit: new coding standard recommends Fn_t suffix for function types
Description
This commit means to make
threadinfomore flexible, by being able to query a thread identified bytidand query for specific thread attributes as specified inflagsbitmask.Motivation and Context
threadsinfois not very flexible and causes unnecessary overhead in programs using it.Moreover, previously there was no was of inexpensively checking amount of present threads in the kernel, therefore the buffer passed to
threadsinfomay have been often reallocated to check all threads. Nowthreadsinfo, if passed aPH_THREADINFO_OPT_THREADCOUNTflag will yield no information in passed buffer, but will return amount of currently present threads.JIRA: RTOS-1187
Types of changes
How Has This Been Tested?
ia32-generic-qemu,riscv64-generic-qemu,sparcv8leon-generic-qemu,armv7a9-zynq7000-qemu,armv7r5f-zynqmp-qemu.Checklist:
Special treatment