diff --git a/services/init/include/fd_holder_service.h b/services/init/include/fd_holder_service.h index 7275f153c4e9768acc58f8075a720d336b7ef532..5b23bfdc72f67bf031d9246e860ade2a6ee9cfee 100644 --- a/services/init/include/fd_holder_service.h +++ b/services/init/include/fd_holder_service.h @@ -19,6 +19,6 @@ extern "C" { #endif void RegisterFdHoldWatcher(int sock); #ifdef __cplusplus -extern "C" { +} #endif #endif // BASE_STARTUP_INITLITE_FD_HOLDER_SERVICE_H diff --git a/services/init/include/init_service.h b/services/init/include/init_service.h index f55f724627b1cba94a4be922b71c4e1ec1381946..450f0a57af21366656cf2f331eb7e1a8f10e449b 100755 --- a/services/init/include/init_service.h +++ b/services/init/include/init_service.h @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "cJSON.h" #include "init_cmds.h" @@ -89,11 +91,6 @@ typedef enum { END_RECV_READY, } ServiceEndMode; -typedef struct { - int *cpus; - int cpuNum; -} CpuArgs; - typedef struct { uid_t uID; gid_t *gIDArray; @@ -148,7 +145,7 @@ typedef struct Service_ { size_t fdCount; TimerHandle timer; ServiceJobs serviceJobs; - CpuArgs cpuInfo; + cpu_set_t cpuSet; } Service; int ServiceStart(Service *service); diff --git a/services/init/init_common_service.c b/services/init/init_common_service.c index 766d70708c36dcd9870c6e4685c963fb64e5497d..ce4226406f1348a7298009580bea30561c673504 100755 --- a/services/init/init_common_service.c +++ b/services/init/init_common_service.c @@ -18,8 +18,6 @@ #include #include #include -#include -#include #ifdef __MUSL__ #include #endif @@ -238,32 +236,17 @@ static void PublishHoldFds(Service *service) static int BindCpuCore(Service *service) { - if (service == NULL || service->cpuInfo.cpuNum <= 0) { + if (service == NULL) { return SERVICE_SUCCESS; } - long cpuNum = sysconf(_SC_NPROCESSORS_CONF); - INIT_ERROR_CHECK(service->cpuInfo.cpuNum <= cpuNum, return SERVICE_FAILURE, - "%s cpus cores exceeds total number of device cores", service->name); - int index = 0; - cpu_set_t setMask; - CPU_ZERO(&setMask); - int pid = getpid(); - for (int i = 0; i < service->cpuInfo.cpuNum; i++) { - index = (int)service->cpuInfo.cpus[i]; - if ((int)cpuNum <= index) { - INIT_LOGW("%s core number %d of CPU cores does not exist", service->name, index); - continue; - } - if (CPU_ISSET(index, &setMask)) { - continue; - } - CPU_SET(index, &setMask); - } - if (sched_setaffinity(pid, sizeof(setMask), &setMask) != 0) { - INIT_LOGI("%s set affinity between process(pid=%d) with CPU's core failed", service->name, pid); - } else { - INIT_LOGE("%s set affinity between process(pid=%d) with CPU's core successfully", service->name, pid); +#ifndef __LITEOS__ + int pid = getpid(); + if (sched_setaffinity(pid, sizeof(service->cpuSet), &service->cpuSet) != 0) { + INIT_LOGE("%s set affinity between process(pid=%d) with CPU's core failed", service->name, pid); + return SERVICE_FAILURE; } + INIT_LOGI("%s set affinity between process(pid=%d) with CPU's core successfully", service->name, pid); +#endif return SERVICE_SUCCESS; } diff --git a/services/init/init_service_manager.c b/services/init/init_service_manager.c index 7314c1f9ed119fd650488517cc2a61d0341a39a8..6ae9af337208cf8bcfc9b15558bc2278ba45d936 100755 --- a/services/init/init_service_manager.c +++ b/services/init/init_service_manager.c @@ -175,6 +175,7 @@ Service *AddService(const char *name) node->data.service = service; service->name = node->name; service->status = SERVICE_IDLE; + CPU_ZERO(&service->cpuSet); g_serviceSpace.serviceCount++; INIT_LOGV("AddService %s", node->name); return service; @@ -203,11 +204,6 @@ void ReleaseService(Service *service) FreeServiceArg(&service->writePidArgs); FreeServiceArg(&service->capsArgs); - if (service->cpuInfo.cpus != NULL) { - free(service->cpuInfo.cpus); - service->cpuInfo.cpus = NULL; - } - service->cpuInfo.cpuNum = 0; if (service->servPerm.caps != NULL) { free(service->servPerm.caps); service->servPerm.caps = NULL; @@ -767,12 +763,7 @@ int GetCritical(const cJSON *curArrItem, Service *curServ, const char *attrName return SERVICE_SUCCESS; } -static int Comparefunc(const void *before, const void *after) -{ - return (*(int*)before - *(int*)after); -} - -static int GetCpuArgs(const cJSON *argJson, const char *name, CpuArgs *args) +static int GetCpuArgs(const cJSON *argJson, const char *name, Service *service) { INIT_ERROR_CHECK(argJson != NULL, return SERVICE_FAILURE, "Invalid argJson"); cJSON *obj = cJSON_GetObjectItem(argJson, name); @@ -781,31 +772,20 @@ static int GetCpuArgs(const cJSON *argJson, const char *name, CpuArgs *args) int ret = cJSON_IsArray(obj); INIT_ERROR_CHECK(ret, return SERVICE_FAILURE, "Invalid type"); int count = cJSON_GetArraySize(obj); - int tmpArray[count]; + int cpus = -1; + int cpuNumMax = sysconf(_SC_NPROCESSORS_CONF); for (int i = 0; i < count; ++i) { cJSON *item = cJSON_GetArrayItem(obj, i); INIT_ERROR_CHECK(item != NULL, return SERVICE_FAILURE, "prase invalid"); - tmpArray[i] = (int)cJSON_GetNumberValue(item); - } - qsort(tmpArray, count, sizeof(int), Comparefunc); - int cpuCount = 0; - for (int j = 0; j < count; j++) { - if (j == 0 && tmpArray[0] == 0) { - tmpArray[cpuCount++] = 0; + cpus = (int)cJSON_GetNumberValue(item); + if (cpuNumMax <= cpus) { + INIT_LOGW("%s core number %d of CPU cores does not exist", service->name, cpus); continue; } - if (tmpArray[j] != tmpArray[j-1]) { - tmpArray[cpuCount++] = tmpArray[j]; + if (CPU_ISSET(cpus, &service->cpuSet)) { + continue; } - } - args->cpus=(int*)malloc(cpuCount * sizeof(int)); - INIT_ERROR_CHECK(args->cpus != NULL, return SERVICE_FAILURE, "Failed to malloc for argv"); - for (int i = 0; i < cpuCount; ++i) { - args->cpus[i] = -1; - } - args->cpuNum = cpuCount; - for (int i = 0; i < count; ++i) { - args->cpus[i] = tmpArray[i]; + CPU_SET(cpus, &service->cpuSet); } return SERVICE_SUCCESS; } @@ -843,7 +823,7 @@ int ParseOneService(const cJSON *curItem, Service *service) (void)GetServiceArgs(curItem, "writepid", MAX_WRITEPID_FILES, &service->writePidArgs); (void)GetServiceArgs(curItem, D_CAPS_STR_IN_CFG, MAX_WRITEPID_FILES, &service->capsArgs); (void)GetStringItem(curItem, APL_STR_IN_CFG, service->apl, MAX_APL_NAME); - (void)GetCpuArgs(curItem, CPU_CORE_STR_IN_CFG, &service->cpuInfo); + (void)GetCpuArgs(curItem, CPU_CORE_STR_IN_CFG, service); ret = GetServiceCaps(curItem, service); INIT_ERROR_CHECK(ret == 0, return SERVICE_FAILURE, "Failed to get caps for service %s", service->name); ret = GetDynamicService(curItem, service); diff --git a/services/init/main.c b/services/init/main.c index 5a8216bb0759f514398fa90031725e1c78017353..620a7ac9e225c0e5417c489f9d5bef53280e6803 100755 --- a/services/init/main.c +++ b/services/init/main.c @@ -17,11 +17,6 @@ static const pid_t INIT_PROCESS_PID = 1; -int __attribute__((weak)) AtlibInit(void) -{ - return 0; -} - int main(int argc, char * const argv[]) { int isSecondStage = 0; @@ -40,7 +35,6 @@ int main(int argc, char * const argv[]) LogInit(); } LogInit(); - (void)AtlibInit(); SystemInit(); SystemExecuteRcs(); SystemConfig();