Skip to content

Commit e5bf043

Browse files
system/nxinit: Warning for long commands
If the command of an action takes too long (greater than CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW milliseconds, 50 ms by default), a warning log will be output for analysis and debugging. For example: a. sleep 1 [ 0.340000] [ 3] [ 0] init_main: executing NSH command 'sleep 1' [ 1.360000] [ 3] [ 0] init_main: NSH command 'sleep 1' exited 0 > [ 1.360000] [ 3] [ 0] init_main: command 'sleep' took 1020 ms b. hello (add sleep(1) to examples/hello) [ 1.390000] [ 3] [ 0] init_main: executed command 'hello' pid 14 [ 1.390000] [ 3] [ 0] init_main: waiting 'hello' pid 14 Hello, World!! > [ 2.400000] [ 3] [ 0] init_main: command 'hello' pid 14 took 1010 ms [ 2.400000] [ 3] [ 0] init_main: command 'hello' pid 14 exited status 0 Signed-off-by: wangjianyu3 <[email protected]>
1 parent a361f63 commit e5bf043

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

system/nxinit/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ config SYSTEM_NXINIT_ACTION_CMD_ARGS_MAX
5959
...
6060
```
6161

62+
config SYSTEM_NXINIT_ACTION_WARN_SLOW
63+
int "Warn if command takes too long"
64+
default 50
65+
depends on SYSTEM_NXINIT_WARN
66+
---help---
67+
Warning if command took more than `SYSTEM_NXINIT_ACTION_WARN_SLOW` ms.
68+
6269
config SYSTEM_NXINIT_ACTION_MANAGER_EVENT_MAX
6370
int "Max number of action manager events"
6471
default 32

system/nxinit/action.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ int init_action_run_command(FAR struct action_manager_s *am)
210210
struct action_cmd_s, node);
211211
}
212212

213+
#if defined(CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW) && \
214+
CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW > 0
215+
clock_gettime(CLOCK_MONOTONIC, &am->time_run);
216+
#endif
213217
ret = init_builtin_run(am, am->running->argc, am->running->argv);
214218
if (ret > 0)
215219
{
@@ -229,6 +233,28 @@ void init_action_reap_command(FAR struct action_manager_s *am)
229233
struct action_s,
230234
ready_node);
231235

236+
#if defined(CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW) && \
237+
CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW > 0
238+
struct timespec time;
239+
int ms;
240+
241+
clock_gettime(CLOCK_MONOTONIC, &time);
242+
clock_timespec_subtract(&time, &am->time_run, &time);
243+
ms = TIMESPEC2MS(time);
244+
if (ms > CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW)
245+
{
246+
if (am->pid_running <= 0)
247+
{
248+
init_warn("Command '%s' took %d ms", am->running->argv[0], ms);
249+
}
250+
else
251+
{
252+
init_warn("Command '%s' pid %d took %d ms", am->running->argv[0],
253+
am->pid_running, ms);
254+
}
255+
}
256+
#endif
257+
232258
am->pid_running = -1;
233259
if (list_is_tail(&ready->cmds, &am->running->node))
234260
{

system/nxinit/action.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ struct action_manager_s
6464

6565
FAR struct action_cmd_s *running;
6666
int pid_running;
67+
#if defined(CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW) && \
68+
CONFIG_SYSTEM_NXINIT_ACTION_WARN_SLOW > 0
69+
struct timespec time_run;
70+
#endif
6771
FAR struct service_manager_s *sm;
6872
};
6973

system/nxinit/init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* Pre-processor Definitions
3434
****************************************************************************/
3535

36+
#define TIMESPEC2MS(t) (((t).tv_sec * 1000) + (t).tv_nsec / 1000000)
37+
3638
#ifdef CONFIG_SYSTEM_NXINIT_DEBUG
3739
#define init_debug(...) syslog(LOG_DEBUG, ##__VA_ARGS__)
3840
#define init_dump_args(argc, argv) \

system/nxinit/service.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
****************************************************************************/
4545

4646
#define SYSTEM_NXINIT_SERVICE_GENTLE_KILL_TIMEOUT 200
47-
#define TIMESPEC2MS(t) (((t).tv_sec * 1000) + (t).tv_nsec / 1000000)
4847

4948
#define check_flags(s, f) ((s)->flags & (f))
5049

0 commit comments

Comments
 (0)