xiaoxiang781216 commented on code in PR #4042:
URL: https://github.com/apache/nuttx-apps/pull/4042#discussion_r4573582080


##########
testing/sched/exitmem/exitmem_main.c:
##########
@@ -0,0 +1,248 @@
+/****************************************************************************
+ * apps/testing/sched/exitmem/exitmem_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define EXITMEM_ROUNDS   8    /* Default number of children */
+#define EXITMEM_PERCENT  40   /* Default part of the free memory */
+#define EXITMEM_WAIT     30   /* Free memory checks, 100 ms apart */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: free_kib
+ *
+ * Description:
+ *   Return the free memory in KiB, or -1.
+ *
+ ****************************************************************************/
+
+static long free_kib(void)
+{
+  long npages = sysconf(_SC_AVPHYS_PAGES);
+  long pgsize = sysconf(_SC_PAGESIZE);
+
+  if (npages < 0 || pgsize < 1024)
+    {
+      return -1;
+    }
+
+  return npages * (pgsize / 1024);
+}
+
+/****************************************************************************
+ * Name: pin
+ *
+ * Description:
+ *   Run the calling thread on one CPU only.
+ *
+ ****************************************************************************/
+
+static void pin(int cpu)
+{
+#ifdef CONFIG_SMP
+  cpu_set_t cpuset;
+
+  CPU_ZERO(&cpuset);
+  CPU_SET(cpu, &cpuset);
+  if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
+    {
+      printf("exitmem: sched_setaffinity(%d) failed: %d\n", cpu, errno);
+    }
+
+  sched_yield();
+#endif
+}
+
+/****************************************************************************
+ * Name: side_thread
+ *
+ * Description:
+ *   Run on another CPU and end: that CPU then idles after the child.
+ *
+ ****************************************************************************/
+
+static FAR void *side_thread(FAR void *arg)
+{
+  pin((int)(intptr_t)arg);
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: child
+ *
+ * Description:
+ *   Run on cpu (and a thread on third), take size KiB, write every page
+ *   and end.
+ *
+ ****************************************************************************/
+
+static void child(int cpu, int third, long size)
+{
+  long pgsize = sysconf(_SC_PAGESIZE);
+  pthread_t thread;
+  FAR char *buf;
+  long off;
+
+  pin(cpu);
+  if (pthread_create(&thread, NULL, side_thread,
+                     (FAR void *)(intptr_t)third) == 0)
+    {
+      pthread_join(thread, NULL);
+    }
+
+  /* A heap of fixed size can be smaller than the free memory */
+
+  while ((buf = malloc((size_t)size * 1024)) == NULL && size > 64)

Review Comment:
   ```suggestion
     while ((buf = malloc(size * 1024)) == NULL && size > 64)
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to