Skip to content

Commit

Permalink
apps/system/utils: remove config dependency to fix ps command issue
Browse files Browse the repository at this point in the history
Due to config dependency, ps command doesn't give correct output.
So, this patch fix the issue by removing config dependency for
CONFIG_SMP and CONFIG_SMP_NCPUS.

Signed-off-by: neel-samsung <[email protected]>
  • Loading branch information
neel-samsung committed Jan 16, 2025
1 parent 9334452 commit a25867b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
43 changes: 15 additions & 28 deletions apps/system/utils/utils_cpuload.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ static void cpuload_stop(void)

static bool has_cpuload(char *load_info)
{
#ifdef CONFIG_SMP
int len = strlen(load_info);
int ncpus = sched_getcpucount();

if (cpu == CONFIG_SMP_NCPUS) {
if (cpu == ncpus) {
/* This is the case when user has not specified cpu idx.
* In this case we check the avg value. If avg is zero, then
* all cpu load values will be zero and hence we return false.
Expand All @@ -133,14 +133,11 @@ static bool has_cpuload(char *load_info)
return (strncmp(load_info, "0.0", 3) != 0);
} else {
char *str = load_info;
for (int i = 1; i < CONFIG_SMP_NCPUS && i <= cpu; i++) {
for (int i = 1; i < ncpus && i <= cpu; i++) {
str = strchr(str, '-') + 1;
}
return (strncmp(str, "0.0", 3) != 0);
}
#else
return (strncmp(load_info, "0.0", 3) != 0);
#endif
}

static void cpuload_print_pid_value(char *buf, void *arg)
Expand All @@ -150,6 +147,7 @@ static void cpuload_print_pid_value(char *buf, void *arg)
int pid_hash;
double load_ratio;
stat_data stat_info[PROC_STAT_MAX];
int ncpus = sched_getcpucount();

stat_info[0] = buf;

Expand Down Expand Up @@ -179,32 +177,24 @@ static void cpuload_print_pid_value(char *buf, void *arg)
} else {
#ifdef CONFIG_SCHED_MULTI_CPULOAD
for (i = PROC_STAT_CPULOAD_SHORT; i <= PROC_STAT_CPULOAD_LONG; i++) {
#ifdef CONFIG_SMP
char * avgload[CONFIG_SMP_NCPUS + 1];
char * avgload[ncpus + 1];
avgload[0] = stat_info[i];
for (int j = 0; j < CONFIG_SMP_NCPUS; j++) {
for (int j = 0; j < ncpus; j++) {
avgload[j] = strtok_r(avgload[j], "-", &avgload[j + 1]);
if (cpu >= CONFIG_SMP_NCPUS || cpu == j) {
if (cpu >= ncpus || cpu == j) {
printf(" %5s |", avgload[j]);
}
}
#else
printf(" %5s |", stat_info[i]);
#endif
}
#else
#ifdef CONFIG_SMP
char * avgload[CONFIG_SMP_NCPUS + 1];
char * avgload[ncpus + 1];
avgload[0] = stat_info[PROC_STAT_CPULOAD];
for (i = 0; i < CONFIG_SMP_NCPUS; i++) {
for (i = 0; i < ncpus; i++) {
avgload[i] = strtok_r(avgload[i], "-", &avgload[i + 1]);
if (cpu >= CONFIG_SMP_NCPUS || cpu == i) {
if (cpu >= ncpus || cpu == i) {
printf(" %5s |", avgload[i]);
}
}
#else
printf(" %5s |", stat_info[PROC_STAT_CPULOAD]);
#endif
#endif
}
#if (CONFIG_TASK_NAME_SIZE > 0)
Expand Down Expand Up @@ -244,15 +234,11 @@ static void cpuload_print_normal(void)
for (int i = PROC_STAT_CPULOAD_SHORT; i <= PROC_STAT_CPULOAD_LONG; i++)
#endif
{
#ifdef CONFIG_SMP
for (int j = 0; j < CONFIG_SMP_NCPUS; j++) {
if (cpu >= CONFIG_SMP_NCPUS || cpu == j) {
for (int j = 0; j < ncpus; j++) {
if (cpu >= ncpus || cpu == j) {
printf(" CPU%d |", j);
}
}
#else
printf(" CPU |");
#endif
printf(" Task Name ");
}

Expand Down Expand Up @@ -443,6 +429,7 @@ int utils_cpuload(int argc, char **args)
int opt;
int ret;
long value;
int ncpus = sched_getcpucount();

if (argc >= 2) {
if (!strncmp(args[1], "--help", strlen("--help") + 1)) {
Expand All @@ -464,7 +451,7 @@ int utils_cpuload(int argc, char **args)
cpuload_mode = CPULOAD_NORMAL;
cpuload_interval = CONFIG_CPULOADMONITOR_INTERVAL;
cpuload_count = CPULOADMON_RUNNING_FOREVER;
cpu = CONFIG_SMP_NCPUS;
cpu = ncpus;

if (argc > 1) {
/*
Expand Down Expand Up @@ -508,7 +495,7 @@ int utils_cpuload(int argc, char **args)
case 'c':
/* set count of iterations */
value = atoi(optarg);
if (value < 0 || value >= CONFIG_SMP_NCPUS) {
if (value < 0 || value >= ncpus) {
printf("Invalid input for -c option");
goto show_usage;
}
Expand Down
2 changes: 0 additions & 2 deletions apps/system/utils/utils_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ enum proc_stat_data_e {
PROC_STAT_PEAKSTACK,
PROC_STAT_CURRHEAP,
PROC_STAT_PEAKHEAP,
#ifdef CONFIG_SMP
PROC_STAT_CPU,
#endif
#ifdef CONFIG_SCHED_CPULOAD
#ifdef CONFIG_SCHED_MULTI_CPULOAD
PROC_STAT_CPULOAD_SHORT,
Expand Down
24 changes: 6 additions & 18 deletions apps/system/utils/utils_ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ static const char *utils_statenames[] = {
"INVALID ",
"PENDING ",
"READY ",
#ifdef CONFIG_SMP
"ASSIGNED",
#endif
"ASSIGNED",
"RUNNING ",
"INACTIVE",
"WAITSEM ",
Expand All @@ -98,30 +96,27 @@ static const char *utils_ttypenames[4] = {
"--?-- "
};

#ifdef CONFIG_SMP
static uint32_t cpu;
#endif

static void ps_print_values(char *buf, void *arg)
{
int i;
int flags;
int state;
stat_data stat_info[PROC_STAT_MAX];
int ncpus = sched_getcpucount();

stat_info[0] = buf;

for (i = 0; i < PROC_STAT_MAX - 1; i++) {
stat_info[i] = strtok_r(stat_info[i], " ", &stat_info[i + 1]);
}

#ifdef CONFIG_SMP
int cpu_idx;
cpu_idx = atoi(stat_info[PROC_STAT_CPU]);
if (cpu_idx != cpu && cpu != CONFIG_SMP_NCPUS) {
if (cpu_idx != cpu && cpu != ncpus) {
return;
}
#endif

flags = atoi(stat_info[PROC_STAT_FLAG]);
if (flags <= 0) {
Expand All @@ -138,11 +133,7 @@ static void ps_print_values(char *buf, void *arg)
flags & TCB_FLAG_NONCANCELABLE ? 'N' : ' ', flags & TCB_FLAG_CANCEL_PENDING ? 'P' : ' ', \
utils_statenames[state]);

#ifdef CONFIG_SMP
printf(" | %3s", stat_info[PROC_STAT_CPU]);
#else
printf(" | NA ");
#endif

#if (CONFIG_TASK_NAME_SIZE > 0)
printf(" | %s\n", stat_info[PROC_STAT_NAME]);
Expand Down Expand Up @@ -171,6 +162,7 @@ static int ps_read_proc(FAR struct dirent *entryp, FAR void *arg)

int utils_ps(int argc, char **args)
{
int ncpus = sched_getcpucount();
#if !defined(CONFIG_FS_AUTOMOUNT_PROCFS)
int ret;
bool is_mounted;
Expand All @@ -190,9 +182,8 @@ int utils_ps(int argc, char **args)

#endif

#ifdef CONFIG_SMP
int opt;
cpu = CONFIG_SMP_NCPUS;
cpu = ncpus;
if (argc > 1) {
/*
* -c [cpu idx] : only display processes running on given cpu
Expand All @@ -204,7 +195,7 @@ int utils_ps(int argc, char **args)
switch (opt) {
case 'c':
cpu = atoi(optarg);
if (cpu < 0 || cpu >= CONFIG_SMP_NCPUS) {
if (cpu < 0 || cpu >= ncpus) {
printf("Invalid input for -c option\n");
goto out;
}
Expand All @@ -216,17 +207,14 @@ int utils_ps(int argc, char **args)
}
}
}
#endif

printf("\n");
printf(" PID | PRIO | FLAG | TYPE | NP | STATUS | CPU | NAME\n");
printf("------|------|------|---------|----|----------|-----|----\n");
/* Print information for each task/thread */
utils_proc_pid_foreach(ps_read_proc, NULL);

#ifdef CONFIG_SMP
out:
#endif
#if !defined(CONFIG_FS_AUTOMOUNT_PROCFS)
if (!is_mounted) {
/* Detach mounted Procfs */
Expand Down

0 comments on commit a25867b

Please sign in to comment.