|
| 1 | +/************************************************************************ |
| 2 | + * NASA Docket No. GSC-19,200-1, and identified as "cFS Draco" |
| 3 | + * |
| 4 | + * Copyright (c) 2023 United States Government as represented by the |
| 5 | + * Administrator of the National Aeronautics and Space Administration. |
| 6 | + * All Rights Reserved. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | + * not use this file except in compliance with the License. You may obtain |
| 10 | + * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + ************************************************************************/ |
| 18 | + |
| 19 | +/** |
| 20 | + * \file |
| 21 | + * |
| 22 | + * This file Contains all of the api calls for manipulating files |
| 23 | + * in a file system / C library that implements the POSIX-style file API |
| 24 | + */ |
| 25 | + |
| 26 | +/**************************************************************************************** |
| 27 | + INCLUDE FILES |
| 28 | + ***************************************************************************************/ |
| 29 | + |
| 30 | +/* |
| 31 | + * Inclusions Defined by OSAL layer. |
| 32 | + * |
| 33 | + */ |
| 34 | + |
| 35 | +#include "os-impl-taskaffinity.h" |
| 36 | +#include "os-impl-tasks.h" |
| 37 | +#include "os-shared-idmap.h" |
| 38 | +#include "osapi-task-affinity.h" |
| 39 | + |
| 40 | +/* |
| 41 | +** System Include Files |
| 42 | +*/ |
| 43 | +#include <stdio.h> |
| 44 | +#include <string.h> |
| 45 | +#include <errno.h> |
| 46 | +#include <time.h> |
| 47 | +#include <stdlib.h> |
| 48 | + |
| 49 | +/**************************************************************************************** |
| 50 | + DEFINES |
| 51 | + ***************************************************************************************/ |
| 52 | + |
| 53 | +/**************************************************************************************** |
| 54 | + Named File API |
| 55 | + ***************************************************************************************/ |
| 56 | +/* |
| 57 | + * ---------------------------------------------------------------------- |
| 58 | + * The OS_TaskAffinityGetCoresConfigured() is an api call to obtain information from OS |
| 59 | + * for the number of configured cores |
| 60 | + * |
| 61 | + * Returns the number of configured cores |
| 62 | + * ---------------------------------------------------------------------- |
| 63 | + */ |
| 64 | +uint32 OS_TaskAffinityGetCoresConfigured_Impl(void) |
| 65 | +{ |
| 66 | + return OS_TaskAffinity_Proc_Conf(); |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | + * ---------------------------------------------------------------------- |
| 71 | + * The OS_TaskAffinitySetAffinity() is an api call to set affinity to a task |
| 72 | + * |
| 73 | + * Sets an affinity from cpuset to task with provided task_id |
| 74 | + * ---------------------------------------------------------------------- |
| 75 | + */ |
| 76 | +int32 OS_TaskAffinitySetAffinity_Impl(const OS_object_token_t *token, const OS_cpuset_t cpuset) |
| 77 | +{ |
| 78 | + cpu_set_t lcl_cpuset; |
| 79 | + OS_impl_task_internal_record_t *impl; |
| 80 | + int32 return_value; |
| 81 | + uint32 i; |
| 82 | + |
| 83 | + impl = OS_OBJECT_TABLE_GET(OS_impl_task_table, *token); |
| 84 | + |
| 85 | + CPU_ZERO(&lcl_cpuset); |
| 86 | + |
| 87 | + /* Populate POSIX cpuset from OSAL CPU Set using the macro */ |
| 88 | + for (i = 0; (i < OS_MAX_CPUS) && (i < OS_TaskAffinityGetCoresConfigured()); i++) |
| 89 | + { |
| 90 | + /* Check if the bit is set in the OSAL structure */ |
| 91 | + if (OS_CPUSET_ISSET(i, &cpuset)) |
| 92 | + { |
| 93 | + /* If it is, set the corresponding bit in the POSIX structure */ |
| 94 | + CPU_SET(i, &lcl_cpuset); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return_value = pthread_setaffinity_np(impl->id, sizeof(lcl_cpuset), &lcl_cpuset); |
| 99 | + |
| 100 | + if (return_value == 0) |
| 101 | + { |
| 102 | + return OS_SUCCESS; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + return OS_ERROR; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * ---------------------------------------------------------------------- |
| 112 | + * The OS_TaskAffinityGetAffinity() is an api call to get affinity to a task |
| 113 | + * |
| 114 | + * Writes affinity to cpuset from task with provided task_id |
| 115 | + * ---------------------------------------------------------------------- |
| 116 | + */ |
| 117 | +int32 OS_TaskAffinityGetAffinity_Impl(const OS_object_token_t *token, OS_cpuset_t *cpuset) |
| 118 | +{ |
| 119 | + cpu_set_t local_cpuset; |
| 120 | + OS_impl_task_internal_record_t *impl; |
| 121 | + int32 return_value; |
| 122 | + uint32 i; |
| 123 | + |
| 124 | + impl = OS_OBJECT_TABLE_GET(OS_impl_task_table, *token); |
| 125 | + |
| 126 | + CPU_ZERO(&local_cpuset); |
| 127 | + return_value = pthread_getaffinity_np(impl->id, sizeof(local_cpuset), &local_cpuset); |
| 128 | + |
| 129 | + if (return_value == 0) |
| 130 | + { |
| 131 | + /* Clear the OSAL affinity mask using the macro */ |
| 132 | + OS_CPUSET_ZERO(cpuset); |
| 133 | + |
| 134 | + /* Populate the OSAL affinity mask from the POSIX mask */ |
| 135 | + for (i = 0; (i < OS_MAX_CPUS) && (i < OS_TaskAffinityGetCoresConfigured()); i++) |
| 136 | + { |
| 137 | + /* If the bit is set in the POSIX structure */ |
| 138 | + if (CPU_ISSET(i, &local_cpuset)) |
| 139 | + { |
| 140 | + /* set the corresponding bit in the OSAL structure */ |
| 141 | + OS_CPUSET_SET(i, cpuset); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return OS_SUCCESS; |
| 146 | + } |
| 147 | + |
| 148 | + return OS_ERROR; |
| 149 | +} |
0 commit comments