Skip to content

Commit 3b1428c

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 3b1428c

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
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

+49-2
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,45 @@ 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+
int uart_id;
7373
rt_err_t rc;
74+
char *ttyname;
75+
rt_device_t bakdev;
76+
char *bakdev_name;
77+
size_t tail_len = 1;
78+
79+
bakdev = rt_console_get_device();
80+
if (!bakdev)
81+
{
82+
return -RT_ENOENT;
83+
}
84+
85+
bakdev_name = bakdev->parent.name;
86+
if (sscanf(bakdev_name, "uart%d", &uart_id))
87+
{
88+
while (uart_id > 0)
89+
{
90+
tail_len += 2;
91+
uart_id >>= 4;
92+
}
93+
tail_len += sizeof("ttyS");
94+
ttyname = rt_malloc(tail_len);
95+
snprintf(ttyname, tail_len, "ttyS%d", uart_id);
96+
bakdev = rt_device_find(ttyname);
97+
}
98+
99+
if (!bakdev)
100+
{
101+
LOG_W("No compatible tty device found for /dev/%s", bakdev_name);
102+
103+
/* fall back */
104+
ttyname = "ttyS0";
105+
bakdev = rt_device_find(ttyname);
106+
}
74107

75108
if (bakdev)
76109
{
110+
LOG_I("Using /dev/%s as default console", ttyname);
77111
lwp_console_register_backend(bakdev, LWP_CONSOLE_LOWEST_PRIOR);
78112
rc = RT_EOK;
79113
}
@@ -85,6 +119,15 @@ static rt_err_t lwp_default_console_setup(void)
85119
return rc;
86120
}
87121

122+
void rt_update_process_times(void);
123+
static int _set_usage_hook(void)
124+
{
125+
#ifdef LWP_TRACING_CPU_USAGE
126+
rt_tick_sethook(rt_update_process_times);
127+
#endif /* LWP_TRACING_CPU_USAGE */
128+
return RT_EOK;
129+
}
130+
88131
static int lwp_component_init(void)
89132
{
90133
int rc;
@@ -104,6 +147,10 @@ static int lwp_component_init(void)
104147
{
105148
LOG_E("%s: lwp_futex_init() failed", __func__);
106149
}
150+
else if ((rc = _set_usage_hook()) != RT_EOK)
151+
{
152+
LOG_E("%s: _set_usage_hook() failed", __func__);
153+
}
107154
else if ((rc = lwp_default_console_setup()) != RT_EOK)
108155
{
109156
LOG_E("%s: lwp_default_console_setup() failed", __func__);

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)