commit 3da1267680504222bfe604c976332c2359d094b5
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Mon Jan 20 12:10:15 2020 +0100

    Introduced a new system shell command 'module:spuload' to read out CPU load

diff --git a/core/src/aos_system.c b/core/src/aos_system.c
index 67f20af..c04c1cd 100644
--- a/core/src/aos_system.c
+++ b/core/src/aos_system.c
@@ -88,6 +88,9 @@ aos_system_t aos;
 static int _shellcmd_configcb(BaseSequentialStream* stream, int argc, char* argv[]);
 static int _shellcmd_infocb(BaseSequentialStream* stream, int argc, char* argv[]);
 static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* argv[]);
+#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
+static int _shellcmd_cpuloadcb(BaseSequentialStream* stream, int argc, char* argv[]);
+#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
 static int _shellcmd_kerneltestcb(BaseSequentialStream* stream, int argc, char* argv[]);
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
@@ -111,6 +114,11 @@ static systime_t _synctime;
 #if (AMIROOS_CFG_SHELL_ENABLE == true) || defined(__DOXYGEN__)
 
 /**
+ * @brief   System shell name.
+ */
+static const char _shell_name[] = "system shell";
+
+/**
  * @brief   Shell thread working area.
  */
 static THD_WORKING_AREA(_shell_wa, AMIROOS_CFG_SHELL_STACKSIZE);
@@ -139,10 +147,19 @@ static AOS_SHELL_COMMAND(_shellcmd_shutdown, "system:shutdown", _shellcmd_shutdo
 static AOS_SHELL_COMMAND(_shellcmd_shutdown, "module:shutdown", _shellcmd_shutdowncb);
 #endif /* (AMIROOS_CFG_SSSP_ENABLE == true) */
 
+#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
+
+/**
+ * @brief   Shell command to read out CPU load.
+ */
+static AOS_SHELL_COMMAND(_shellcmd_cpuload, "module:cpuload", _shellcmd_cpuloadcb);
+
+#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */
+
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
 
 /**
- * @brief   Shell kommand to run a test of the ChibiOS/RT kernel.
+ * @brief   Shell command to run a test of the ChibiOS/RT kernel.
  */
 static AOS_SHELL_COMMAND(_shellcmd_kerneltest, "kernel:test", _shellcmd_kerneltestcb);
 
@@ -238,6 +255,7 @@ static void _printSystemInfo(BaseSequentialStream* stream)
   _printSystemInfoLine(stream, "Core Variant", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_CORE_VARIANT_NAME);
 #endif /* defined(PORT_CORE_VARIANT_NAME) */
   _printSystemInfoLine(stream, "Architecture", SYSTEM_INFO_NAMEWIDTH, "%s", PORT_ARCHITECTURE_NAME);
+  _printSystemInfoLine(stream, "Core Frequency", SYSTEM_INFO_NAMEWIDTH, "%u MHz", SystemCoreClock / 1000000);
   _printSystemInfoSeparator(stream, '-', SYSTEM_INFO_WIDTH);
 #if (AMIROOS_CFG_SSSP_ENABLE == true)
   _printSystemInfoLine(stream, "AMiRo-OS" , SYSTEM_INFO_NAMEWIDTH, "%u.%u.%u %s (SSSP %u.%u)", AMIROOS_VERSION_MAJOR, AMIROOS_VERSION_MINOR, AMIROOS_VERSION_PATCH, AMIROOS_RELEASE_TYPE, AOS_SSSP_VERSION_MAJOR, AOS_SSSP_VERSION_MINOR);
@@ -622,6 +640,67 @@ static int _shellcmd_shutdowncb(BaseSequentialStream* stream, int argc, char* ar
 #endif /* (AMIROOS_CFG_BOOTLOADER == X) */
 }
 
+#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
+
+/**
+ * @brief   Callback function for the module:cpuload shell command.
+ *
+ * @param[in] stream    The I/O stream to use.
+ * @param[in] argc      Number of arguments.
+ * @param[in] argv      List of pointers to arguments.
+ *
+ * @return      An exit status.
+ */
+static int _shellcmd_cpuloadcb(BaseSequentialStream* stream, int argc, char* argv[])
+{
+  aosDbgCheck(stream != NULL);
+
+  (void)argc;
+  (void)argv;
+
+  // local variables
+  rttime_t sum = 0;
+  thread_t* thd = chRegFirstThread();
+
+  // accumulate system load
+  do {
+    sum += thd->stats.cumulative;
+    thd = chRegNextThread(thd);
+  } while (thd);
+  sum += ch.kernel_stats.m_crit_thd.cumulative + ch.kernel_stats.m_crit_isr.cumulative;
+
+  // calculate and retreive cpu load per thread
+  chprintf(stream, "threads & critical zones:\n");
+  thd = chRegFirstThread();
+  do {
+    chprintf(stream, "\t%22s: %6.2f%%\n",
+             (thd->name != NULL) ? thd->name : "<unnamed thread>",
+             (float)thd->stats.cumulative / (float)sum * 100.f);
+    thd = chRegNextThread(thd);
+  } while (thd);
+  chprintf(stream, "\t%22s: %6.2f%%\n",
+           "thread critical zones",
+           (float)ch.kernel_stats.m_crit_thd.cumulative / (float)sum * 100.f);
+  chprintf(stream, "\t%22s: %6.2f%%\n",
+           "ISR critical zones",
+           (float)ch.kernel_stats.m_crit_isr.cumulative / (float)sum * 100.f);
+
+  // retreive further real-time statistics
+  chprintf(stream, "\nworst critical zones:\n");
+  chprintf(stream, "\tthreads: %uus (%u clocks @ %uMHz)\n",
+           RTC2US(SystemCoreClock, ch.kernel_stats.m_crit_thd.worst),
+           ch.kernel_stats.m_crit_thd.worst,
+           SystemCoreClock / 1000000);
+  chprintf(stream, "\t   ISRs: %uus (%u clocks @ %uMHz)\n",
+           RTC2US(SystemCoreClock, ch.kernel_stats.m_crit_isr.worst),
+           ch.kernel_stats.m_crit_isr.worst,
+           SystemCoreClock / 1000000);
+
+  return 0;
+}
+
+#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */
+
 #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
 
 /**
@@ -750,6 +829,7 @@ void aosSysInit(const char* shellPrompt)
 
   /* init shell */
   aosShellInit(&aos.shell,
+               _shell_name,
                shellPrompt,
                _shell_buffer,
                SYSTEM_SHELL_BUFFERENTRIES,
@@ -759,6 +839,9 @@ void aosSysInit(const char* shellPrompt)
   aosShellAddCommand(&aos.shell, &_shellcmd_config);
   aosShellAddCommand(&aos.shell, &_shellcmd_info);
   aosShellAddCommand(&aos.shell, &_shellcmd_shutdown);
+#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE)) || defined(__DOXYGEN__)
+  aosShellAddCommand(&aos.shell, &_shellcmd_cpuload);
+#endif /* (CH_CFG_USE_REGISTRY == TRUE) && (CH_DBG_STATISTICS == TRUE) */
 #if (AMIROOS_CFG_TESTS_ENABLE == true)
   aosShellAddCommand(&aos.shell, &_shellcmd_kerneltest);
 #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
