You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "xiaoxiang781216 (via GitHub)" <gi...@apache.org> on 2023/09/29 05:13:22 UTC

[GitHub] [nuttx] xiaoxiang781216 opened a new pull request, #10834: timer: handle perf count overflow

xiaoxiang781216 opened a new pull request, #10834:
URL: https://github.com/apache/nuttx/pull/10834

   ## Summary
   
   perf_getime always returns an incremented cycle value If the hardware does not support perf event, it will use arch_alarm's up_perf_gettime.
   
   ## Impact
   
   new perf_ api
   
   ## Testing
   
   ci


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] raiden00pl commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341076216


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   or better - add an interface for setting CPU freq required by perf. This can be useful when measuring performance when CPU frequency scaling is used. I don't know if it's possible now.
   Then arch specific logic only needs to set the CPU freq.



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341032292


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   The argument is hard to decide, or let's change the argument of up_perf_init to void?



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] raiden00pl commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341403772


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   OK, we can deal with this later



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] raiden00pl commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341076216


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   or better - add an interface for setting CPU freq required by perf. This can be useful when measuring performance when CPU frequency scaling is used. I don't know if it's possible now.



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341279216


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   All are possible, since you are refining arch perf specific code, it's better to leave this issue until you come up a good solution.



-- 
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-unsubscribe@nuttx.apache.org

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


Re: [PR] timer: handle perf count overflow [nuttx]

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl merged PR #10834:
URL: https://github.com/apache/nuttx/pull/10834


-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] raiden00pl commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341021186


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   shouldn't we call `up_perf_init()` here ? then arch specific logic doesn't have to worry about it anymore



-- 
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-unsubscribe@nuttx.apache.org

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


[GitHub] [nuttx] raiden00pl commented on a diff in pull request #10834: timer: handle perf count overflow

Posted by "raiden00pl (via GitHub)" <gi...@apache.org>.
raiden00pl commented on code in PR #10834:
URL: https://github.com/apache/nuttx/pull/10834#discussion_r1341056484


##########
sched/clock/clock_perf.c:
##########
@@ -0,0 +1,176 @@
+/****************************************************************************
+ * sched/clock/clock_perf.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#include <nuttx/clock.h>
+#include <nuttx/arch.h>
+#include <nuttx/wdog.h>
+
+#if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct perf_s
+{
+  struct wdog_s wdog;
+  unsigned long last;
+  unsigned long overflow;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct perf_s g_perf;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_update
+ ****************************************************************************/
+
+static void perf_update(wdparm_t arg)
+{
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf_gettime();
+  wd_start((FAR struct wdog_s *)arg, tick, perf_update, arg);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  clock_t tick = (clock_t)LONG_MAX * TICK_PER_SEC / up_perf_getfreq();
+
+  perf->last = up_perf_gettime();
+
+  /* Periodic check for overflow */
+
+  wd_start(&perf->wdog, tick, perf_update, (wdparm_t)perf);
+}
+
+/****************************************************************************
+ * perf_gettime
+ ****************************************************************************/
+
+clock_t perf_gettime(void)
+{
+  FAR struct perf_s *perf = &g_perf;
+  unsigned long now = up_perf_gettime();
+
+  /* Check if overflow */
+
+  if (now < perf->last)
+    {
+      perf->overflow++;
+    }
+
+  perf->last = now;
+  return (clock_t)now | (clock_t)perf->overflow << 32;
+}
+
+/****************************************************************************
+ * perf_convert
+ ****************************************************************************/
+
+void perf_convert(clock_t elapsed, FAR struct timespec *ts)
+{
+  unsigned long freq = up_perf_getfreq();
+
+  ts->tv_sec  = elapsed / freq;
+  elapsed -= ts->tv_sec * freq;
+  ts->tv_nsec = NSEC_PER_SEC * elapsed / freq;
+}
+
+#elif defined(CONFIG_ALARM_ARCH) || defined (CONFIG_TIMER_ARCH) || \
+      defined(CONFIG_ARCH_PERF_EVENTS)
+
+/****************************************************************************
+ * perf_init
+ ****************************************************************************/
+
+void perf_init(void)
+{
+}

Review Comment:
   We still need to provide information about CPU freq to up_perf_init. How we do that ? 
   Can we use the common CPU frequency definition for boards ? something like `BOARD_SYSTICK_CLOCK` or `BOARD_SYSCLK_FREQUENCY`. 



-- 
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-unsubscribe@nuttx.apache.org

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