diff --git a/apps/include/system/utils.h b/apps/include/system/utils.h index 1cb87920ec..fc16b1bf47 100644 --- a/apps/include/system/utils.h +++ b/apps/include/system/utils.h @@ -32,7 +32,10 @@ void fs_register_utilcmds(void); void net_register_utilcmds(void); void net_register_appcmds(void); #endif - +#ifdef CONFIG_PM_CMDS +void pm_register_utilcmds(void); +void pm_register_appcmds(void); +#endif #ifdef CONFIG_ENABLE_STACKMONITOR_CMD void stkmon_logging(struct tcb_s *); #endif diff --git a/apps/system/init/init.c b/apps/system/init/init.c index 3415c37048..d8a6c4520c 100644 --- a/apps/system/init/init.c +++ b/apps/system/init/init.c @@ -92,6 +92,11 @@ static void tash_register_cmds(void) net_register_appcmds(); #endif +#ifdef CONFIG_PM_CMDS +pm_register_utilcmds(); +pm_register_appcmds(); +#endif + #ifdef CONFIG_BUILTIN_APPS register_examples_cmds(); #endif diff --git a/apps/system/utils/Kconfig b/apps/system/utils/Kconfig index 81b5715f45..d7624a81e8 100644 --- a/apps/system/utils/Kconfig +++ b/apps/system/utils/Kconfig @@ -69,6 +69,14 @@ config NET_PING_CMD_ICOUNT endif #NET_CMDS +config PM_CMDS + bool "PM shell commands" + default n + depends on TASH && PM + ---help--- + Enable Power Management System command in TASH. + Command including pm_start, pm_suspend, pm_resume, and so on. + config ENABLE_CPULOAD bool "cpuload monitor" default y diff --git a/apps/system/utils/Makefile b/apps/system/utils/Makefile index 06adcec7e1..d78776e9ae 100644 --- a/apps/system/utils/Makefile +++ b/apps/system/utils/Makefile @@ -103,6 +103,10 @@ ifeq ($(CONFIG_NETUTILS_TFTPC),y) CSRCS += netcmd_tftpc.c endif +ifeq ($(CONFIG_PM_CMDS),y) +CSRCS += pmcmd.c +endif + ifeq ($(CONFIG_ENABLE_CPULOAD),y) CSRCS += utils_cpuload.c endif diff --git a/apps/system/utils/pmcmd.c b/apps/system/utils/pmcmd.c new file mode 100644 index 0000000000..ccde3067b3 --- /dev/null +++ b/apps/system/utils/pmcmd.c @@ -0,0 +1,242 @@ +/**************************************************************************** + * + * Copyright 2024 Samsung Electronics All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + * + ****************************************************************************/ +/**************************************************************************** + * apps/system/utils/pmcmd.c + * + * Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ +/// @file pmcmd.c +/// @brief Cursor functions. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_TASH +#include +#endif + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define WORK_TIME 50000000 +#define SLEEP_TIME 5000 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ +#ifdef CONFIG_AMP +static void* pm_smp_test_app(void *arg) { + int ret = -1; + struct timeval start, cur; + int work_time = WORK_TIME; + int sleep_time = SLEEP_TIME; + int delay = 0; + if (fd < 0) { + int pid = getpid(); + int fd = open(PM_DRVPATH, O_RDONLY); + printf("Cannot open %s file with fd = %d\n", PM_DRVPATH, fd); + return NULL; + } + while (1) { + printf("PID %d: I am pm_smp_test_app\n", pid); + printf("PID %d: Lets do non critical work\n", pid); + volatile count = work_time; + while (count) { + count--; + } + printf("PID %d: Lets sleep for %d ms\n", pid, sleep_time); + system_pm_sleep(sleep_time); + } + close(fd); + return NULL; +} + +static int tash_pm_smp_test(int args, char *argv[]) { + pthread_t threadid; + pthread_attr_t attr; + int ret; + ret = pthread_attr_init(&attr); + if (ret != OK) { + printf("Failed to init thread attributes\n"); + } + attr.affinity = 1<domain_metrics.stime[domain_id] = clock_systimer(); - g_pm_metrics->domain_metrics.suspend_ticks[domain_id] = 0; - } -} + clock_t tick; + pm_wakeup_reason_code_t wakeup_src; + int domain_id; + int index; -/**************************************************************************** - * Name: pm_metrics_update_suspend - * - * Description: - * This function is called inside pm_suspend. It note the timestamp (in ticks) of - * suspended domain. - * - * Input parameters: - * domain_id - the ID of domain registered with PM. - * - * Returned value: - * None - * - ****************************************************************************/ -void pm_metrics_update_suspend(int domain_id) -{ - if (g_pm_metrics_running && (g_pmglobals.suspend_count[domain_id] == 0)) { - g_pm_metrics->domain_metrics.stime[domain_id] = clock_systimer(); + if (!g_pm_metrics_running) { + return; } -} -/**************************************************************************** - * Name: pm_metrics_update_resume - * - * Description: - * This function is called inside pm_resume. Before resuming domain, it counts - * amount of time (in ticks) the given domain was suspended. - * - * Input parameters: - * domain_id - the ID of domain registered with PM. - * - * Returned value: - * None - * - ****************************************************************************/ -void pm_metrics_update_resume(int domain_id) -{ - if (g_pm_metrics_running && (g_pmglobals.suspend_count[domain_id] == 1)) { - g_pm_metrics->domain_metrics.suspend_ticks[domain_id] += clock_systimer() - g_pm_metrics->domain_metrics.stime[domain_id]; - } -} + switch (cmd) { -/**************************************************************************** - * Name: pm_metrics_update_idle - * - * Description: - * This function is called inside pm_idle. It counts the frequency of domain, which - * make board unable to go into sleep during idle cpu time. - * - * Input parameters: - * None - * - * Returned value: - * None - * - ****************************************************************************/ -void pm_metrics_update_idle(void) -{ - int index; - if (g_pm_metrics_running) { - g_pm_metrics->total_try_ticks++; - for (index = 0; index < CONFIG_PM_NDOMAINS; index++) { - if (g_pmglobals.suspend_count[index] != 0) { - g_pm_metrics->domain_metrics.blocking_board_sleep_ticks[index]++; + case PM_MET_DOMAIN: + domain_id = (int)arg; + g_pm_metrics->domain_metrics.stime[domain_id] = clock_systimer(); + g_pm_metrics->domain_metrics.suspend_ticks[domain_id] = 0; + break; + + case PM_MET_SUSPEND: + domain_id = (int)arg; + if (g_pmglobals.suspend_count[domain_id] == 0) { + g_pm_metrics->domain_metrics.stime[domain_id] = clock_systimer(); } - } - } -} + break; -/**************************************************************************** - * Name: pm_metrics_update_changestate - * - * Description: - * This function is called inside pm_changestate. Before changing state, it counts - * amount of time (in ticks) was in that state. - * - * Input parameters: - * None - * - * Returned value: - * None - * - ****************************************************************************/ -void pm_metrics_update_changestate(void) -{ - clock_t now; - if (g_pm_metrics_running) { - now = clock_systimer(); - g_pm_metrics->state_metrics.state_accum_ticks[g_pmglobals.state] += now - g_pm_metrics->state_metrics.stime; - g_pm_metrics->state_metrics.stime = now; - } -} + case PM_MET_RESUME: + domain_id = (int)arg; + if (g_pmglobals.suspend_count[domain_id] == 1) { + g_pm_metrics->domain_metrics.suspend_ticks[domain_id] += clock_systimer() - g_pm_metrics->domain_metrics.stime[domain_id]; + } + break; + + case PM_MET_IDLE: + g_pm_metrics->total_try_ticks++; + for (index = 0; index < CONFIG_PM_NDOMAINS; index++) { + if (g_pmglobals.suspend_count[index] != 0) { + g_pm_metrics->domain_metrics.blocking_board_sleep_ticks[index]++; + } + } + break; -/**************************************************************************** - * Name: pm_metrics_update_wakehandler - * - * Description: - * This function is called inside pm_wakehandler. It counts the frequency of wakeup - * sources, which are waking up the board. It also checks the amount of time board - * was in sleep. - * - * Input parameters: - * missing_tick - the amount of time the board was in sleep. - * wakeup_src - the wakeup reason code. - * - * Returned value: - * None - * - ****************************************************************************/ -void pm_metrics_update_wakehandler(clock_t missing_tick, pm_wakeup_reason_code_t wakeup_src) -{ - if (g_pm_metrics_running) { - g_pm_metrics->wakeup_src_counts[wakeup_src]++; - g_pm_metrics->board_sleep_ticks += missing_tick; + case PM_MET_CHANGESTATE: + tick = clock_systimer(); + g_pm_metrics->state_metrics.state_accum_ticks[g_pmglobals.state] += tick - g_pm_metrics->state_metrics.stime; + g_pm_metrics->state_metrics.stime = tick; + break; + + case PM_MET_WAKE: + tick = ((pm_metrics_wake_t *)arg)->ticks; + wakeup_src = ((pm_metrics_wake_t *)arg)->wakeup_src; + g_pm_metrics->wakeup_src_counts[wakeup_src]++; + g_pm_metrics->board_sleep_ticks += tick; + break; + + default: + pmdbg("Unknown CMD: %d\n", (int)cmd); + break; } } @@ -259,8 +180,6 @@ int pm_metrics(int milliseconds) irqstate_t flags; int index; int n_domains; - int pm_suspended = -1; - int pm_resumed = -1; /* If PM Metrics already running then notify other thread */ if (g_pm_metrics) { pmdbg("PM Metrics already running\n"); @@ -273,8 +192,6 @@ int pm_metrics(int milliseconds) } /* Lock PM so that no two thread can run PM Metrics simultaneously */ pm_lock(); - /* Avoid board sleep during PM Metrics initialization */ - pm_suspended = pm_suspend(PM_IDLE_DOMAIN); /* Allocate memory for initializing PM Metrics measurements */ g_pm_metrics = (pm_metric_t *)pm_alloc(1, sizeof(pm_metric_t)); if (g_pm_metrics == NULL) { @@ -284,34 +201,16 @@ int pm_metrics(int milliseconds) return ERROR; } /* PM Metrics Initialization */ - for (index = 0; index < PM_COUNT; index++) { - g_pm_metrics->state_metrics.state_accum_ticks[index] = 0; - } flags = enter_critical_section(); start_time = clock_systimer(); g_pm_metrics->state_metrics.stime = start_time; for (index = 0; (index < CONFIG_PM_NDOMAINS) && pm_domain_map[index]; index++) { - pm_metrics_update_domain(index); g_pm_metrics->domain_metrics.stime[index] = start_time; } g_pm_metrics_running = true; leave_critical_section(flags); - /* Resume Board Sleep */ - if (pm_suspended == OK) { - pm_resumed = pm_resume(PM_IDLE_DOMAIN); - } else { - pm_resumed = -1; - pmdbg("Unable to resume IDLE Domain\n"); - } /* Suspend for given time interval */ pm_sleep(TICK2MSEC(MSEC2TICK(milliseconds) - (clock_systimer() - start_time))); - /* Avoid board sleep during PM Metrics post processing */ - if (pm_resumed == OK) { - pm_suspended = pm_suspend(PM_IDLE_DOMAIN); - } else { - pm_suspended = -1; - pmdbg("Unable to suspend IDLE Domain\n"); - } /* PM Metrics post calculations for consistent result */ flags = enter_critical_section(); g_pm_metrics_running = false; @@ -329,13 +228,6 @@ int pm_metrics(int milliseconds) /* Free allocated memory */ free(g_pm_metrics); g_pm_metrics = NULL; - /* Resume Board Sleep */ - if (pm_suspended == OK) { - pm_resumed = pm_resume(PM_IDLE_DOMAIN); - } else { - pm_resumed = -1; - pmdbg("Unable to resume IDLE Domain\n"); - } /* Unlock PM Metrics for other threads */ pm_unlock(); return OK; diff --git a/os/pm/pm_resume.c b/os/pm/pm_resume.c index 628c6d880d..caede85527 100644 --- a/os/pm/pm_resume.c +++ b/os/pm/pm_resume.c @@ -108,7 +108,7 @@ int pm_resume(int domain_id) goto errout; } #ifdef CONFIG_PM_METRICS - pm_metrics_update_resume(domain_id); + pm_metrics_update(PM_MET_RESUME, domain_id); #endif g_pmglobals.suspend_count[domain_id]--; errout: diff --git a/os/pm/pm_suspend.c b/os/pm/pm_suspend.c index aa70a79f67..5e84ef2176 100644 --- a/os/pm/pm_suspend.c +++ b/os/pm/pm_suspend.c @@ -111,7 +111,7 @@ int pm_suspend(int domain_id) goto errout; } #ifdef CONFIG_PM_METRICS - pm_metrics_update_suspend(domain_id); + pm_metrics_update(PM_MET_SUSPEND, domain_id); #endif g_pmglobals.suspend_count[domain_id]++; errout: diff --git a/os/pm/pm_wakehandler.c b/os/pm/pm_wakehandler.c index 857aa55be3..081889491b 100644 --- a/os/pm/pm_wakehandler.c +++ b/os/pm/pm_wakehandler.c @@ -56,7 +56,10 @@ void pm_wakehandler(clock_t missing_tick, pm_wakeup_reason_code_t wakeup_src) { irqstate_t flags = enter_critical_section(); #ifdef CONFIG_PM_METRICS - pm_metrics_update_wakehandler(missing_tick, wakeup_src); + pm_metrics_wake_t wake_arg; + wake_arg.ticks = missing_tick; + wake_arg.wakeup_src = wakeup_src; + pm_metrics_update(PM_MET_WAKE, (unsigned long)(&wake_arg)); #endif pmllvdbg("wakeup source code = %d\n", wakeup_src); pmllvdbg("missing_tick: %llu\n", missing_tick);