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

[GitHub] [nuttx] pkarashchenko commented on a diff in pull request #9691: sched/clock/clock_getcpuclockid: add clock_getcpuclockid implementation

pkarashchenko commented on code in PR #9691:
URL: https://github.com/apache/nuttx/pull/9691#discussion_r1251686442


##########
sched/clock/clock_gettime.c:
##########
@@ -131,6 +137,55 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
         }
 #endif /* CONFIG_CLOCK_TIMEKEEPING */
     }
+#ifdef CONFIG_SCHED_CRITMONITOR
+  else if (clock_type == CLOCK_THREAD_CPUTIME_ID)
+    {
+      FAR struct tcb_s *tcb;
+
+      if (pid == 0)
+        {
+          /* Fetch the THREAD_CPUTIME for current thread */
+
+          tcb = nxsched_self();
+        }
+      else
+        {
+          tcb = nxsched_get_tcb(pid);
+        }
+
+      up_perf_convert(tcb->run_time, tp);
+    }
+  else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
+    {
+      FAR struct tcb_s *tcb;
+
+      if (pid == 0)
+        {
+          /* Fetch the PROCESS_CPUTIME for current process */
+
+          tcb = nxsched_self();
+        }
+      else
+        {
+           tcb = nxsched_get_tcb(pid);
+        }
+
+      FAR struct task_group_s *group = tcb->group;
+      unsigned long runtime = 0;
+      irqstate_t flags;
+      int i;

Review Comment:
   C89 incompatible



##########
sched/clock/clock_getcpuclockid.c:
##########
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * sched/clock/clock_getcpuclockid.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 <nuttx/config.h>
+
+#include <time.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "clock/clock.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: clock_getcpuclockid
+ *
+ * Description:
+ *   The function shall return clock id of the CPU-time clock of the process
+ *   specified by pid
+ *
+ * Input Parameters:
+ *   pid - the specified process id
+ *   clockid - the clock type that need to setup
+ *
+ * Returned Value:
+ *   Return 0 on success, return error number on error
+ *
+ ****************************************************************************/
+
+int clock_getcpuclockid(pid_t pid, FAR clockid_t *clockid)
+{
+  if (pid < 0)
+    {
+      set_errno(EINVAL);
+      return ERROR;
+    }
+
+  /* If the pid is 0, we need to use the pid of current process */
+
+  if (pid == 0)
+    {
+      pid = getpid();
+    }
+
+  /* For clock_getcpuclockid, the clock type are
+   * CLOCK_PROCESS_CPUTIME_ID
+   */
+
+  *clockid = (pid << CLOCK_SHIFT) | CLOCK_PROCESS_CPUTIME_ID;
+  return OK;

Review Comment:
   Can this be a part of libc?



-- 
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