Skip to content

Commit 74d0579

Browse files
committed
[smart] fix console setup and CPU usage tracing
This patch introduces following features: - Updated default console setup to dynamically select an appropriate tty device based on the configured console device name. - Added CPU usage tracing functionality, enabled by default, for applications like 'top'. Signed-off-by: Shell <[email protected]>
1 parent 78bdf67 commit 74d0579

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

components/lwp/Kconfig

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ if RT_USING_LWP
1212

1313
if LWP_DEBUG
1414
config LWP_DEBUG_INIT
15-
select RT_USING_HOOKLIST
16-
bool "Enable debug mode of init process"
17-
default n
15+
select RT_USING_HOOKLIST
16+
bool "Enable debug mode of init process"
17+
default n
1818
endif
1919

2020
config RT_LWP_MAX_NR
@@ -33,6 +33,13 @@ if RT_USING_LWP
3333
int "The input buffer size of lwp console device"
3434
default 1024
3535

36+
config LWP_TRACING_CPU_USAGE
37+
select RT_USING_HOOK
38+
bool "Enable cpu usage tracing"
39+
help
40+
Enable cpu usage tracing for application like top.
41+
default y
42+
3643
config LWP_TID_MAX_NR
3744
int "The maximum number of lwp thread id"
3845
default 64

components/lwp/lwp.c

+50-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
#define DBG_TAG "lwp"
20-
#define DBG_LVL DBG_WARNING
20+
#define DBG_LVL DBG_INFO
2121
#include <rtdbg.h>
2222

2323
#include <rthw.h>
@@ -69,11 +69,41 @@ extern char working_directory[];
6969
*/
7070
static rt_err_t lwp_default_console_setup(void)
7171
{
72-
rt_device_t bakdev = rt_device_find("ttyS0");
72+
rt_device_t bakdev;
7373
rt_err_t rc;
74+
char *ttyname;
75+
char *bakdev_name = RT_CONSOLE_DEVICE_NAME;
76+
77+
if (bakdev_name)
78+
{
79+
int uart_id;
80+
size_t tail_len = 0;
81+
if (sscanf(bakdev_name, "uart%d", &uart_id))
82+
{
83+
while (uart_id > 0)
84+
{
85+
tail_len += 2;
86+
uart_id >>= 4;
87+
}
88+
tail_len += sizeof("ttyS");
89+
ttyname = rt_malloc(tail_len);
90+
snprintf(ttyname, tail_len, "ttyS%d", uart_id);
91+
bakdev = rt_device_find(ttyname);
92+
}
93+
}
94+
95+
if (!bakdev)
96+
{
97+
LOG_W("No compatible tty device found for /dev/%s", bakdev_name);
98+
99+
/* fall back */
100+
ttyname = "ttyS0";
101+
bakdev = rt_device_find(ttyname);
102+
}
74103

75104
if (bakdev)
76105
{
106+
LOG_I("Using /dev/%s as default console", ttyname);
77107
lwp_console_register_backend(bakdev, LWP_CONSOLE_LOWEST_PRIOR);
78108
rc = RT_EOK;
79109
}
@@ -85,6 +115,15 @@ static rt_err_t lwp_default_console_setup(void)
85115
return rc;
86116
}
87117

118+
static void _update_process_times(void);
119+
static int _set_usage_hook(void)
120+
{
121+
#ifdef LWP_TRACING_CPU_USAGE
122+
rt_tick_sethook(_update_process_times);
123+
#endif /* LWP_TRACING_CPU_USAGE */
124+
return RT_EOK;
125+
}
126+
88127
static int lwp_component_init(void)
89128
{
90129
int rc;
@@ -104,6 +143,10 @@ static int lwp_component_init(void)
104143
{
105144
LOG_E("%s: lwp_futex_init() failed", __func__);
106145
}
146+
else if ((rc = _set_usage_hook()) != RT_EOK)
147+
{
148+
LOG_E("%s: _set_usage_hook() failed", __func__);
149+
}
107150
else if ((rc = lwp_default_console_setup()) != RT_EOK)
108151
{
109152
LOG_E("%s: lwp_default_console_setup() failed", __func__);
@@ -1583,7 +1626,9 @@ rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *
15831626
return rc;
15841627
}
15851628

1586-
void rt_update_process_times(void)
1629+
#ifdef LWP_TRACING_CPU_USAGE
1630+
1631+
static void _update_process_times(void)
15871632
{
15881633
struct rt_thread *thread;
15891634
#ifdef RT_USING_SMP
@@ -1616,3 +1661,5 @@ void rt_update_process_times(void)
16161661
#endif
16171662
}
16181663
}
1664+
1665+
#endif /* LWP_TRACING_CPU_USAGE */

components/lwp/terminal/terminal.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ void tty_rel_gone(struct lwp_tty *tp);
212212
#define tty_lock_notrecused(tp) (rt_mutex_get_hold(tty_getlock(tp)) == 1)
213213
#define tty_assert_locked(tp) RT_ASSERT(tty_lock_owned(tp))
214214
#define tty_lock_assert(tp, option) \
215-
(((option) == (MA_OWNED | MA_NOTRECURSED)) \
216-
? (tty_lock_owned(tp) && tty_lock_notrecused(tp)) \
217-
: rt_assert_handler("Operation not allowed", __func__, __LINE__))
215+
RT_ASSERT(((option) == (MA_OWNED | MA_NOTRECURSED)) && \
216+
(tty_lock_owned(tp) && tty_lock_notrecused(tp)))
218217

219218
/* System messages. */
220219
int tty_checkoutq(struct lwp_tty *tp);

components/lwp/terminal/tty_device.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static char *alloc_device_name(const char *name)
2525
{
2626
char *tty_dev_name;
2727
long name_buf_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
28-
+ rt_strlen(name) /* custom name */
28+
+ rt_strlen(name) /* custom name */
2929
+ 1; /* tailing \0 */
3030

3131
tty_dev_name = rt_malloc(name_buf_len);

0 commit comments

Comments
 (0)