8a6aa5f5bf
purpose: 1 sched_lock is very time-consuming, and reducing its invocations can improve performance. 2 sched_lock is prone to misuse, and narrowing its scope of use is to prevent people from referencing incorrect code and using it test: We can use qemu for testing. compiling make distclean -j20; ./tools/configure.sh -l qemu-armv8a:nsh_smp ;make -j20 running qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic -machine virt,virtualization=on,gic-version=3 -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -kernel ./nuttx We have also tested this patch on other ARM hardware platforms. Signed-off-by: hujun5 <hujun5@xiaomi.com>
142 lines
4.5 KiB
C
142 lines
4.5 KiB
C
/****************************************************************************
|
|
* sched/sched/sched_getaffinity.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 <sys/types.h>
|
|
#include <sched.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
#include "sched/sched.h"
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: nxsched_get_affinity
|
|
*
|
|
* Description:
|
|
* nxsched_get_affinity() writes the affinity mask of the thread whose ID
|
|
* is pid into the cpu_set_t pointed to by mask. The cpusetsize
|
|
* argument specifies the size (in bytes) of mask. If pid is zero, then
|
|
* the mask of the calling thread is returned.
|
|
*
|
|
* nxsched_get_affinity() is identical to the function sched_getaffinity(),
|
|
* differing only in its return value: This function does not modify the
|
|
* errno variable.
|
|
*
|
|
* This is a non-standard, internal OS function and is not intended for
|
|
* use by application logic. Applications should use the standard
|
|
* sched_getparam().
|
|
*
|
|
* Input Parameters:
|
|
* pid - The ID of thread whose affinity set will be retrieved.
|
|
* cpusetsize - Size of mask. MUST be sizeofcpu_set_t().
|
|
* mask - The location to return the thread's new affinity set.
|
|
*
|
|
* Returned Value:
|
|
* Zero (OK) if successful. Otherwise, a negated errno value is returned:
|
|
*
|
|
* ESRCH The task whose ID is pid could not be found.
|
|
*
|
|
****************************************************************************/
|
|
|
|
int nxsched_get_affinity(pid_t pid, size_t cpusetsize, FAR cpu_set_t *mask)
|
|
{
|
|
FAR struct tcb_s *tcb;
|
|
irqstate_t flags;
|
|
int ret;
|
|
|
|
DEBUGASSERT(cpusetsize == sizeof(cpu_set_t) && mask != NULL);
|
|
|
|
/* Verify that the PID corresponds to a real task */
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (pid == 0)
|
|
{
|
|
tcb = this_task();
|
|
}
|
|
else
|
|
{
|
|
tcb = nxsched_get_tcb(pid);
|
|
}
|
|
|
|
if (tcb == NULL)
|
|
{
|
|
ret = -ESRCH;
|
|
}
|
|
else
|
|
{
|
|
/* Return the affinity mask from the TCB. */
|
|
|
|
*mask = tcb->affinity;
|
|
ret = OK;
|
|
}
|
|
|
|
leave_critical_section(flags);
|
|
return ret;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: sched_getaffinity
|
|
*
|
|
* Description:
|
|
* sched_getaffinity() writes the affinity mask of the thread whose ID
|
|
* is pid into the cpu_set_t pointed to by mask. The cpusetsize
|
|
* argument specifies the size (in bytes) of mask. If pid is zero, then
|
|
* the mask of the calling thread is returned.
|
|
*
|
|
* This function is a simply wrapper around nxsched_get_affinity() that
|
|
* sets the errno value in the event of an error.
|
|
*
|
|
* Input Parameters:
|
|
* pid - The ID of thread whose affinity set will be retrieved.
|
|
* cpusetsize - Size of mask. MUST be sizeofcpu_set_t().
|
|
* mask - The location to return the thread's new affinity set.
|
|
*
|
|
* Returned Value:
|
|
* 0 if successful. Otherwise, ERROR (-1) is returned, and errno is
|
|
* set appropriately:
|
|
*
|
|
* ESRCH The task whose ID is pid could not be found.
|
|
*
|
|
****************************************************************************/
|
|
|
|
int sched_getaffinity(pid_t pid, size_t cpusetsize, FAR cpu_set_t *mask)
|
|
{
|
|
int ret = nxsched_get_affinity(pid, cpusetsize, mask);
|
|
if (ret < 0)
|
|
{
|
|
set_errno(-ret);
|
|
ret = ERROR;
|
|
}
|
|
|
|
return ret;
|
|
}
|