提交 a732ac5f 编写于 作者: X xlei1030

修复cpu核绑实现

Signed-off-by: Nxlei1030 <xionglei6@huawei.com>
上级 93bec381
......@@ -19,6 +19,6 @@ extern "C" {
#endif
void RegisterFdHoldWatcher(int sock);
#ifdef __cplusplus
extern "C" {
}
#endif
#endif // BASE_STARTUP_INITLITE_FD_HOLDER_SERVICE_H
......@@ -17,6 +17,8 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <sched.h>
#include <stdio.h>
#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);
......
......@@ -18,8 +18,6 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <stdio.h>
#ifdef __MUSL__
#include <stropts.h>
#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;
}
......
......@@ -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);
......
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册