xiaoxiang781216 commented on code in PR #7844:
URL: https://github.com/apache/nuttx/pull/7844#discussion_r1045724766


##########
drivers/note/note_driver.c:
##########
@@ -22,57 +22,1288 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/note/note_driver.h>
-#include <nuttx/note/note_sysview.h>
-#include <nuttx/note/noteram_driver.h>
-#include <nuttx/note/notectl_driver.h>
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <time.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/sched.h>
+#include <nuttx/clock.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/sched_note.h>
+
+#include "sched/sched.h"
 
 /****************************************************************************
- * Public Functions
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+struct note_filter_s
+{
+  struct note_filter_mode_s mode;
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+  struct note_filter_irq_s irq_mask;
+#endif
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SYSCALL
+  struct note_filter_syscall_s syscall_mask;
+#endif
+};
+#endif
+
+struct note_startalloc_s
+{
+  struct note_common_s nsa_cmn; /* Common note parameters */
+#if CONFIG_TASK_NAME_SIZE > 0
+  char nsa_name[CONFIG_TASK_NAME_SIZE + 1];
+#endif
+};
+
+#if CONFIG_TASK_NAME_SIZE > 0
+#  define SIZEOF_NOTE_START(n) (sizeof(struct note_start_s) + (n) - 1)
+#else
+#  define SIZEOF_NOTE_START(n) (sizeof(struct note_start_s))
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+static struct note_filter_s g_note_filter =
+{
+  {
+     CONFIG_SCHED_INSTRUMENTATION_FILTER_DEFAULT_MODE
+#ifdef CONFIG_SMP
+     , CONFIG_SCHED_INSTRUMENTATION_CPUSET
+#endif
+  }
+};
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+static unsigned int g_note_disabled_irq_nest[CONFIG_SMP_NCPUS];
+#endif
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sched_note_flatten
+ *
+ * Description:
+ *   Copy the data in the little endian layout
+ *
+ ****************************************************************************/
+
+static inline void sched_note_flatten(FAR uint8_t *dst,
+                                      FAR void *src, size_t len)
+{
+#ifdef CONFIG_ENDIAN_BIG
+  FAR uint8_t *end = (FAR uint8_t *)src + len - 1;
+  while (len-- > 0)
+    {
+      *dst++ = *end--;
+    }
+#else
+  memcpy(dst, src, len);
+#endif
+}
+
+/****************************************************************************
+ * Name: note_common
+ *
+ * Description:
+ *   Fill in some of the common fields in the note structure.
+ *
+ * Input Parameters:
+ *   tcb    - The TCB containing the information
+ *   note   - The common note structure to use
+ *   length - The total lengthof the note structure
+ *   type   - The type of the note
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void note_common(FAR struct tcb_s *tcb,
+                        FAR struct note_common_s *note,
+                        uint8_t length, uint8_t type)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_HIRES
+  struct timespec ts;
+
+  clock_systime_timespec(&ts);
+#else
+  clock_t systime = clock_systime_ticks();
+#endif
+
+  /* Save all of the common fields */
+
+  note->nc_length   = length;
+  note->nc_type     = type;
+  note->nc_priority = tcb->sched_priority;
+#ifdef CONFIG_SMP
+  note->nc_cpu      = tcb->cpu;
+#endif
+  sched_note_flatten(note->nc_pid, &tcb->pid, sizeof(tcb->pid));
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_HIRES
+  sched_note_flatten(note->nc_systime_nsec, &ts.tv_nsec, sizeof(ts.tv_nsec));
+  sched_note_flatten(note->nc_systime_sec, &ts.tv_sec, sizeof(ts.tv_sec));
+#else
+  /* Save the LS 32-bits of the system timer in little endian order */
+
+  sched_note_flatten(note->nc_systime, &systime, sizeof(systime));
+#endif
+}
+
+/****************************************************************************
+ * Name: note_isenabled
+ *
+ * Description:
+ *   Check whether the instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+static inline int note_isenabled(void)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!(g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_ENABLE))
+    {
+      return false;
+    }
+
+#ifdef CONFIG_SMP
+  /* Ignore notes that are not in the set of monitored CPUs */
+
+  if ((g_note_filter.mode.cpuset & (1 << this_cpu())) == 0)
+    {
+      /* Not in the set of monitored CPUs.  Do not log the note. */
+
+      return false;
+    }
+#endif
+#endif
+
+  return true;
+}
+
+/****************************************************************************
+ * Name: note_isenabled_switch
+ *
+ * Description:
+ *   Check whether the switch instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SWITCH
+static inline int note_isenabled_switch(void)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!note_isenabled())
+    {
+      return false;
+    }
+
+  /* If the switch trace is disabled, do nothing. */
+
+  if ((g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_SWITCH) == 0)
+    {
+      return false;
+    }
+#endif
+
+  return true;
+}
+#endif
+
+/****************************************************************************
+ * Name: note_isenabled_syscall
+ *
+ * Description:
+ *   Check whether the syscall instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   nr - syscall number
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SYSCALL
+static inline int note_isenabled_syscall(int nr)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!note_isenabled())
+    {
+      return false;
+    }
+
+  /* Exclude the case of syscall called by the interrupt handler which is
+   * not traced.
+   */
+
+  if (up_interrupt_context())
+    {
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+#ifdef CONFIG_SMP
+      int cpu = this_cpu();
+#else
+      int cpu = 0;
+#endif

Review Comment:
   Done.



##########
drivers/note/note_driver.c:
##########
@@ -22,57 +22,1288 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/note/note_driver.h>
-#include <nuttx/note/note_sysview.h>
-#include <nuttx/note/noteram_driver.h>
-#include <nuttx/note/notectl_driver.h>
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <time.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/sched.h>
+#include <nuttx/clock.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/sched_note.h>
+
+#include "sched/sched.h"
 
 /****************************************************************************
- * Public Functions
+ * Private Types
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+struct note_filter_s
+{
+  struct note_filter_mode_s mode;
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+  struct note_filter_irq_s irq_mask;
+#endif
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SYSCALL
+  struct note_filter_syscall_s syscall_mask;
+#endif
+};
+#endif
+
+struct note_startalloc_s
+{
+  struct note_common_s nsa_cmn; /* Common note parameters */
+#if CONFIG_TASK_NAME_SIZE > 0
+  char nsa_name[CONFIG_TASK_NAME_SIZE + 1];
+#endif
+};
+
+#if CONFIG_TASK_NAME_SIZE > 0
+#  define SIZEOF_NOTE_START(n) (sizeof(struct note_start_s) + (n) - 1)
+#else
+#  define SIZEOF_NOTE_START(n) (sizeof(struct note_start_s))
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+static struct note_filter_s g_note_filter =
+{
+  {
+     CONFIG_SCHED_INSTRUMENTATION_FILTER_DEFAULT_MODE
+#ifdef CONFIG_SMP
+     , CONFIG_SCHED_INSTRUMENTATION_CPUSET
+#endif
+  }
+};
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+static unsigned int g_note_disabled_irq_nest[CONFIG_SMP_NCPUS];
+#endif
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sched_note_flatten
+ *
+ * Description:
+ *   Copy the data in the little endian layout
+ *
+ ****************************************************************************/
+
+static inline void sched_note_flatten(FAR uint8_t *dst,
+                                      FAR void *src, size_t len)
+{
+#ifdef CONFIG_ENDIAN_BIG
+  FAR uint8_t *end = (FAR uint8_t *)src + len - 1;
+  while (len-- > 0)
+    {
+      *dst++ = *end--;
+    }
+#else
+  memcpy(dst, src, len);
+#endif
+}
+
+/****************************************************************************
+ * Name: note_common
+ *
+ * Description:
+ *   Fill in some of the common fields in the note structure.
+ *
+ * Input Parameters:
+ *   tcb    - The TCB containing the information
+ *   note   - The common note structure to use
+ *   length - The total lengthof the note structure
+ *   type   - The type of the note
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void note_common(FAR struct tcb_s *tcb,
+                        FAR struct note_common_s *note,
+                        uint8_t length, uint8_t type)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_HIRES
+  struct timespec ts;
+
+  clock_systime_timespec(&ts);
+#else
+  clock_t systime = clock_systime_ticks();
+#endif
+
+  /* Save all of the common fields */
+
+  note->nc_length   = length;
+  note->nc_type     = type;
+  note->nc_priority = tcb->sched_priority;
+#ifdef CONFIG_SMP
+  note->nc_cpu      = tcb->cpu;
+#endif
+  sched_note_flatten(note->nc_pid, &tcb->pid, sizeof(tcb->pid));
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_HIRES
+  sched_note_flatten(note->nc_systime_nsec, &ts.tv_nsec, sizeof(ts.tv_nsec));
+  sched_note_flatten(note->nc_systime_sec, &ts.tv_sec, sizeof(ts.tv_sec));
+#else
+  /* Save the LS 32-bits of the system timer in little endian order */
+
+  sched_note_flatten(note->nc_systime, &systime, sizeof(systime));
+#endif
+}
+
+/****************************************************************************
+ * Name: note_isenabled
+ *
+ * Description:
+ *   Check whether the instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+static inline int note_isenabled(void)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!(g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_ENABLE))
+    {
+      return false;
+    }
+
+#ifdef CONFIG_SMP
+  /* Ignore notes that are not in the set of monitored CPUs */
+
+  if ((g_note_filter.mode.cpuset & (1 << this_cpu())) == 0)
+    {
+      /* Not in the set of monitored CPUs.  Do not log the note. */
+
+      return false;
+    }
+#endif
+#endif
+
+  return true;
+}
+
+/****************************************************************************
+ * Name: note_isenabled_switch
+ *
+ * Description:
+ *   Check whether the switch instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SWITCH
+static inline int note_isenabled_switch(void)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!note_isenabled())
+    {
+      return false;
+    }
+
+  /* If the switch trace is disabled, do nothing. */
+
+  if ((g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_SWITCH) == 0)
+    {
+      return false;
+    }
+#endif
+
+  return true;
+}
+#endif
+
+/****************************************************************************
+ * Name: note_isenabled_syscall
+ *
+ * Description:
+ *   Check whether the syscall instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   nr - syscall number
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_SYSCALL
+static inline int note_isenabled_syscall(int nr)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!note_isenabled())
+    {
+      return false;
+    }
+
+  /* Exclude the case of syscall called by the interrupt handler which is
+   * not traced.
+   */
+
+  if (up_interrupt_context())
+    {
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+#ifdef CONFIG_SMP
+      int cpu = this_cpu();
+#else
+      int cpu = 0;
+#endif
+
+      if (g_note_disabled_irq_nest[cpu] > 0)
+        {
+          return false;
+        }
+#else
+      return false;
+#endif
+    }
+
+  /* If the syscall trace is disabled or the syscall number is masked,
+   * do nothing.
+   */
+
+  if (!(g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_SYSCALL) ||
+      NOTE_FILTER_SYSCALLMASK_ISSET(nr - CONFIG_SYS_RESERVED,
+                                    &g_note_filter.syscall_mask))
+    {
+      return false;
+    }
+#endif
+
+  return true;
+}
+#endif
+
+/****************************************************************************
+ * Name: note_isenabled_irqhandler
+ *
+ * Description:
+ *   Check whether the interrupt handler instrumentation is enabled.
+ *
+ * Input Parameters:
+ *   irq   - IRQ number
+ *   enter - interrupt enter/leave flag
+ *
+ * Returned Value:
+ *   True is returned if the instrumentation is enabled.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_INSTRUMENTATION_IRQHANDLER
+static inline int note_isenabled_irq(int irq, bool enter)
+{
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+  if (!note_isenabled())
+    {
+      return false;
+    }
+
+  /* If the IRQ trace is disabled or the IRQ number is masked, disable
+   * subsequent syscall traces until leaving the interrupt handler
+   */
+
+  if (!(g_note_filter.mode.flag & NOTE_FILTER_MODE_FLAG_IRQ) ||
+      NOTE_FILTER_IRQMASK_ISSET(irq, &g_note_filter.irq_mask))
+    {
+#ifdef CONFIG_SMP
+      int cpu = this_cpu();
+#else
+      int cpu = 0;
+#endif

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to