shaojinchun 3 лет назад
Родитель
Сommit
e3d972d76f
3 измененных файлов с 79 добавлено и 1 удалено
  1. 6 0
      components/lwp/lwp.h
  2. 61 0
      components/lwp/lwp_pid.c
  3. 12 1
      components/lwp/lwp_syscall.c

+ 6 - 0
components/lwp/lwp.h

@@ -68,6 +68,11 @@ struct rt_lwp
     struct rt_mpu_info mpu_info;
 #endif /* ARCH_MM_MPU */
 #endif
+
+#ifdef RT_USING_SMP
+    int bind_cpu;
+#endif
+
     uint8_t lwp_type;
     uint8_t reserv[3];
 
@@ -151,6 +156,7 @@ void lwp_mmu_switch(struct rt_thread *thread);
 #endif
 void lwp_user_setting_save(rt_thread_t thread);
 void lwp_user_setting_restore(rt_thread_t thread);
+int lwp_setaffinity(pid_t pid, int cpu);
 
 #ifdef RT_USING_USERSPACE
 struct __pthread {

+ 61 - 0
components/lwp/lwp_pid.c

@@ -1028,3 +1028,64 @@ void lwp_wait_subthread_exit(void)
         rt_thread_mdelay(10);
     }
 }
+
+static int _lwp_setaffinity(pid_t pid, int cpu)
+{
+    struct rt_lwp *lwp;
+    int ret = -1;
+
+    lwp = lwp_from_pid(pid);
+    if (lwp)
+    {
+#ifdef RT_USING_SMP
+        rt_list_t *list;
+
+        lwp->bind_cpu = cpu;
+        for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
+        {
+            rt_thread_t thread;
+
+            thread = rt_list_entry(list, struct rt_thread, sibling);
+            rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_size_t)cpu);
+        }
+#endif
+        ret = 0;
+    }
+    return ret;
+}
+
+int lwp_setaffinity(pid_t pid, int cpu)
+{
+    rt_base_t level;
+    int ret;
+
+#ifdef RT_USING_SMP
+    if (cpu < 0 || cpu > RT_CPUS_NR)
+    {
+        cpu = RT_CPUS_NR;
+    }
+#endif
+    level = rt_hw_interrupt_disable();
+    ret = _lwp_setaffinity(pid, cpu);
+    rt_hw_interrupt_enable(level);
+    return ret;
+}
+
+#ifdef RT_USING_SMP
+static void cmd_cpu_bind(int argc, char** argv)
+{
+    int pid;
+    int cpu;
+
+    if (argc < 3)
+    {
+        rt_kprintf("Useage: cpu_bind pid cpu\n");
+        return;
+    }
+
+    pid = atoi(argv[1]);
+    cpu = atoi(argv[2]);
+    lwp_setaffinity((pid_t)pid, cpu);
+}
+MSH_CMD_EXPORT_ALIAS(cmd_cpu_bind, cpu_bind, set a process bind to a cpu);
+#endif

+ 12 - 1
components/lwp/lwp_syscall.c

@@ -1325,6 +1325,9 @@ rt_thread_t sys_thread_create(void *arg[])
         goto fail;
     }
 
+#ifdef RT_USING_SMP
+    thread->bind_cpu = lwp->bind_cpu;
+#endif
     thread->cleanup = lwp_cleanup;
     thread->user_entry = (void (*)(void *))arg[1];
     thread->user_stack = (void *)user_stack;
@@ -1494,6 +1497,9 @@ long _sys_clone(void *arg[])
         goto fail;
     }
 
+#ifdef RT_USING_SMP
+    thread->bind_cpu = lwp->bind_cpu;
+#endif
     thread->cleanup = lwp_cleanup;
     thread->user_entry = RT_NULL;
     thread->user_stack = RT_NULL;
@@ -4015,6 +4021,11 @@ int sys_madvise(void *addr, size_t len, int behav)
 }
 #endif
 
+int sys_setaffinity(pid_t pid, int cpu)
+{
+    return lwp_setaffinity(pid, cpu);
+}
+
 const static void* func_table[] =
 {
     (void *)sys_exit,            /* 01 */
@@ -4152,7 +4163,7 @@ const static void* func_table[] =
     (void *)sys_thread_sigprocmask,
 #ifdef ARCH_MM_MMU
     (void *)sys_cacheflush,
-    (void *)sys_notimpl,
+    (void *)sys_setaffinity,
     (void *)sys_notimpl,
 #else
     (void *)sys_notimpl,