drivers/note: Optimize code logic.

Remove duplicate code

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16
2025-12-09 18:08:48 +08:00
committed by Xiang Xiao
parent 0750e417c9
commit ddad4fa16e
3 changed files with 25 additions and 30 deletions
+10 -13
View File
@@ -2097,11 +2097,11 @@ void sched_note_filter_tag(FAR struct note_filter_named_tag_s *oldf,
* len - The length of the buffer * len - The length of the buffer
* *
* Returned Value: * Returned Value:
* None * Return name if task name can be retrieved, otherwise "<noname>"
* *
****************************************************************************/ ****************************************************************************/
void note_get_taskname(pid_t pid, FAR char *buf, size_t len) FAR char *note_get_taskname(pid_t pid, FAR char *buf, size_t len)
{ {
#if CONFIG_TASK_NAME_SIZE > 0 #if CONFIG_TASK_NAME_SIZE > 0
FAR struct tcb_s *tcb = nxsched_get_tcb(pid); FAR struct tcb_s *tcb = nxsched_get_tcb(pid);
@@ -2109,28 +2109,25 @@ void note_get_taskname(pid_t pid, FAR char *buf, size_t len)
if (tcb != NULL) if (tcb != NULL)
{ {
strlcpy(buf, tcb->name, len); strlcpy(buf, tcb->name, len);
return buf;
} }
else
{
# if defined(CONFIG_SCHED_INSTRUMENTATION_SWITCH) && \ # if defined(CONFIG_SCHED_INSTRUMENTATION_SWITCH) && \
(CONFIG_DRIVERS_NOTE_TASKNAME_BUFSIZE > 0) (CONFIG_DRIVERS_NOTE_TASKNAME_BUFSIZE > 0)
else
{
FAR struct note_taskname_info_s *ti = note_find_taskname(pid); FAR struct note_taskname_info_s *ti = note_find_taskname(pid);
if (ti != NULL) if (ti != NULL)
{ {
strlcpy(buf, ti->name, len); strlcpy(buf, ti->name, len);
return buf;
} }
else
{
strlcpy(buf, "<noname>", len);
}
# else
strlcpy(buf, "<noname>", len);
# endif
} }
#else # endif
strlcpy(buf, "<noname>", len);
#endif #endif
return "<noname>";
} }
/**************************************************************************** /****************************************************************************
+13 -15
View File
@@ -790,11 +790,9 @@ static int noteram_dump_header(FAR struct lib_outstream_s *s,
int cpu = 0; int cpu = 0;
#endif #endif
note_get_taskname(pid, buf, TASK_NAME_SIZE);
ret = lib_sprintf(s, "%8s-%-3u [%d] %3" PRIu64 ".%09lu: ", ret = lib_sprintf(s, "%8s-%-3u [%d] %3" PRIu64 ".%09lu: ",
buf, get_pid(pid), cpu, note_get_taskname(pid, buf, TASK_NAME_SIZE),
(uint64_t)ts.tv_sec, ts.tv_nsec); get_pid(pid), cpu, (uint64_t)ts.tv_sec, ts.tv_nsec);
return ret; return ret;
} }
@@ -830,16 +828,15 @@ static int noteram_dump_sched_switch(FAR struct lib_outstream_s *s,
current_priority = cctx->current_priority; current_priority = cctx->current_priority;
next_priority = cctx->next_priority; next_priority = cctx->next_priority;
note_get_taskname(current_pid, current_buf, TASK_NAME_SIZE);
note_get_taskname(next_pid, next_buf, TASK_NAME_SIZE);
ret = lib_sprintf(s, "sched_switch: prev_comm=%s prev_pid=%u " ret = lib_sprintf(s, "sched_switch: prev_comm=%s prev_pid=%u "
"prev_prio=%u prev_state=%c ==> " "prev_prio=%u prev_state=%c ==> "
"next_comm=%s next_pid=%u next_prio=%u\n", "next_comm=%s next_pid=%u next_prio=%u\n",
current_buf, get_pid(current_pid), note_get_taskname(current_pid, current_buf,
current_priority, get_task_state(cctx->current_state), TASK_NAME_SIZE),
next_buf, get_pid(next_pid), get_pid(current_pid), current_priority,
next_priority); get_task_state(cctx->current_state),
note_get_taskname(next_pid, next_buf, TASK_NAME_SIZE),
get_pid(next_pid), next_priority);
cctx->current_pid = cctx->next_pid; cctx->current_pid = cctx->next_pid;
cctx->current_priority = cctx->next_priority; cctx->current_priority = cctx->next_priority;
@@ -942,11 +939,11 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s,
#ifdef CONFIG_SCHED_INSTRUMENTATION_SWITCH #ifdef CONFIG_SCHED_INSTRUMENTATION_SWITCH
case NOTE_START: case NOTE_START:
{ {
note_get_taskname(pid, buf, TASK_NAME_SIZE);
ret += noteram_dump_header(s, note, ctx); ret += noteram_dump_header(s, note, ctx);
ret += lib_sprintf(s, "sched_wakeup_new: comm=%s pid=%d " ret += lib_sprintf(s, "sched_wakeup_new: comm=%s pid=%d "
"target_cpu=%d\n", "target_cpu=%d\n",
buf, get_pid(pid), cpu); note_get_taskname(pid, buf, TASK_NAME_SIZE),
get_pid(pid), cpu);
} }
break; break;
@@ -996,11 +993,12 @@ static int noteram_dump_one(FAR uint8_t *p, FAR struct lib_outstream_s *s,
* until leaving the interrupt handler. * until leaving the interrupt handler.
*/ */
note_get_taskname(cctx->next_pid, buf, TASK_NAME_SIZE);
ret += noteram_dump_header(s, note, ctx); ret += noteram_dump_header(s, note, ctx);
ret += lib_sprintf(s, "sched_waking: comm=%s " ret += lib_sprintf(s, "sched_waking: comm=%s "
"pid=%d target_cpu=%d\n", "pid=%d target_cpu=%d\n",
buf, get_pid(cctx->next_pid), cpu); note_get_taskname(cctx->next_pid, buf,
TASK_NAME_SIZE),
get_pid(cctx->next_pid), cpu);
cctx->pendingswitch = true; cctx->pendingswitch = true;
} }
} }
+2 -2
View File
@@ -191,11 +191,11 @@ int note_initialize(void);
* len - The length of the buffer * len - The length of the buffer
* *
* Returned Value: * Returned Value:
* None * Return name if task name can be retrieved, otherwise "<noname>"
* *
****************************************************************************/ ****************************************************************************/
void note_get_taskname(pid_t pid, FAR char *buf, size_t len); FAR char *note_get_taskname(pid_t pid, FAR char *buf, size_t len);
/**************************************************************************** /****************************************************************************
* Name: note_driver_register * Name: note_driver_register