未验证 提交 81e80e19 编写于 作者: O openharmony_ci 提交者: Gitee

!1258 添加init的启动关键事件

Merge pull request !1258 from cheng_jinsong/master_cc_add_init_bootevent
...@@ -251,13 +251,22 @@ HOOK_MGR *GetBootStageHookMgr() ...@@ -251,13 +251,22 @@ HOOK_MGR *GetBootStageHookMgr()
INIT_TIMING_STAT g_bootJob = {0}; INIT_TIMING_STAT g_bootJob = {0};
static void RecordInitBootEvent(const char *initBootEvent)
{
const char *bootEventArgv[] = {"init", initBootEvent};
PluginExecCmd("bootevent", ARRAY_LENGTH(bootEventArgv), bootEventArgv);
return;
}
static void BootStateChange(int start, const char *content) static void BootStateChange(int start, const char *content)
{ {
if (start == 0) { if (start == 0) {
clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.startTime)); clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.startTime));
RecordInitBootEvent(content);
INIT_LOGI("boot job %s start.", content); INIT_LOGI("boot job %s start.", content);
} else { } else {
clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.endTime)); clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.endTime));
RecordInitBootEvent(content);
long long diff = InitDiffTime(&g_bootJob); long long diff = InitDiffTime(&g_bootJob);
INIT_LOGI("boot job %s finish diff %lld us.", content, diff); INIT_LOGI("boot job %s finish diff %lld us.", content, diff);
} }
...@@ -363,9 +372,9 @@ void SystemConfig(void) ...@@ -363,9 +372,9 @@ void SystemConfig(void)
options.flags = 0; options.flags = 0;
options.preHook = InitPreHook; options.preHook = InitPreHook;
options.postHook = InitPostHook; options.postHook = InitPostHook;
InitServiceSpace(); InitServiceSpace();
HookMgrExecute(GetBootStageHookMgr(), INIT_GLOBAL_INIT, (void *)&timingStat, (void *)&options); HookMgrExecute(GetBootStageHookMgr(), INIT_GLOBAL_INIT, (void *)&timingStat, (void *)&options);
RecordInitBootEvent("init.prepare");
HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, (void *)&timingStat, (void *)&options); HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, (void *)&timingStat, (void *)&options);
InitParamService(); InitParamService();
...@@ -376,7 +385,9 @@ void SystemConfig(void) ...@@ -376,7 +385,9 @@ void SystemConfig(void)
// load SELinux context and policy // load SELinux context and policy
// Do not move position! // Do not move position!
PluginExecCmdByName("loadSelinuxPolicy", ""); PluginExecCmdByName("loadSelinuxPolicy", "");
RecordInitBootEvent("init.prepare");
RecordInitBootEvent("init.ParseCfg");
LoadSpecialParam(); LoadSpecialParam();
// parse parameters // parse parameters
...@@ -385,6 +396,7 @@ void SystemConfig(void) ...@@ -385,6 +396,7 @@ void SystemConfig(void)
// read config // read config
HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, (void *)&timingStat, (void *)&options); HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, (void *)&timingStat, (void *)&options);
ReadConfig(); ReadConfig();
RecordInitBootEvent("init.ParseCfg");
INIT_LOGI("boot parse config file done."); INIT_LOGI("boot parse config file done.");
HookMgrExecute(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, (void *)&timingStat, (void *)&options); HookMgrExecute(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, (void *)&timingStat, (void *)&options);
......
...@@ -102,6 +102,34 @@ static int AddServiceBootEvent(const char *serviceName, const char *paramName) ...@@ -102,6 +102,34 @@ static int AddServiceBootEvent(const char *serviceName, const char *paramName)
return 0; return 0;
} }
static void AddInitBootEvent(const char *bootEventName)
{
ListNode *found = NULL;
found = OH_ListFind(&bootEventList, (void *)bootEventName, ParseBooteventCompareProc);
if (found != NULL) {
INIT_CHECK_ONLY_RETURN(clock_gettime(CLOCK_MONOTONIC,
&(((BOOT_EVENT_PARAM_ITEM *)found)->timestamp[BOOTEVENT_READY])) == 0);
return;
}
BOOT_EVENT_PARAM_ITEM *item = malloc(sizeof(BOOT_EVENT_PARAM_ITEM));
if (item == NULL) {
return;
}
OH_ListInit(&item->node);
if (clock_gettime(CLOCK_MONOTONIC, &(item->timestamp[BOOTEVENT_FORK])) != 0) {
free(item);
return;
}
item->paramName = strdup(bootEventName);
if (item->paramName == NULL) {
free(item);
return;
}
OH_ListAddTail(&bootEventList, (ListNode *)&item->node);
return;
}
#define BOOT_EVENT_BOOT_COMPLETED "bootevent.boot.completed" #define BOOT_EVENT_BOOT_COMPLETED "bootevent.boot.completed"
static void BootEventParaFireByName(const char *paramName) static void BootEventParaFireByName(const char *paramName)
...@@ -128,7 +156,7 @@ static void BootEventParaFireByName(const char *paramName) ...@@ -128,7 +156,7 @@ static void BootEventParaFireByName(const char *paramName)
return; return;
} }
// All parameters are fired, set boot completed now ... // All parameters are fired, set boot completed now ...
INIT_LOGI("All bootevents are fired, boot complete now ..."); INIT_LOGI("All boot events are fired, boot complete now ...");
SystemWriteParam(BOOT_EVENT_BOOT_COMPLETED, "true"); SystemWriteParam(BOOT_EVENT_BOOT_COMPLETED, "true");
return; return;
} }
...@@ -139,31 +167,31 @@ static void ServiceParseBootEventHook(SERVICE_PARSE_CTX *serviceParseCtx) ...@@ -139,31 +167,31 @@ static void ServiceParseBootEventHook(SERVICE_PARSE_CTX *serviceParseCtx)
int cnt; int cnt;
cJSON *bootEvents = cJSON_GetObjectItem(serviceParseCtx->serviceNode, BOOT_EVENT_FIELD_NAME); cJSON *bootEvents = cJSON_GetObjectItem(serviceParseCtx->serviceNode, BOOT_EVENT_FIELD_NAME);
// No bootevents in config file // No boot events in config file
if (bootEvents == NULL) { if (bootEvents == NULL) {
return; return;
} }
SERVICE_INFO_CTX ctx = {0}; SERVICE_INFO_CTX ctx = {0};
ctx.serviceName = serviceParseCtx->serviceName; ctx.serviceName = serviceParseCtx->serviceName;
HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL); HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL);
// Single bootevent in config file // Single boot event in config file
if (!cJSON_IsArray(bootEvents)) { if (!cJSON_IsArray(bootEvents)) {
if (AddServiceBootEvent(serviceParseCtx->serviceName, if (AddServiceBootEvent(serviceParseCtx->serviceName,
cJSON_GetStringValue(bootEvents)) != 0) { cJSON_GetStringValue(bootEvents)) != 0) {
INIT_LOGI("Add service bootevent failed %s", serviceParseCtx->serviceName); INIT_LOGI("Add service bootEvent failed %s", serviceParseCtx->serviceName);
return; return;
} }
g_bootEventNum++; g_bootEventNum++;
return; return;
} }
// Multiple bootevents in config file // Multiple boot events in config file
cnt = cJSON_GetArraySize(bootEvents); cnt = cJSON_GetArraySize(bootEvents);
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
cJSON *item = cJSON_GetArrayItem(bootEvents, i); cJSON *item = cJSON_GetArrayItem(bootEvents, i);
if (AddServiceBootEvent(serviceParseCtx->serviceName, if (AddServiceBootEvent(serviceParseCtx->serviceName,
cJSON_GetStringValue(item)) != 0) { cJSON_GetStringValue(item)) != 0) {
INIT_LOGI("Add service bootevent failed %s", serviceParseCtx->serviceName); INIT_LOGI("Add service bootEvent failed %s", serviceParseCtx->serviceName);
continue; continue;
} }
g_bootEventNum++; g_bootEventNum++;
...@@ -173,17 +201,21 @@ static void ServiceParseBootEventHook(SERVICE_PARSE_CTX *serviceParseCtx) ...@@ -173,17 +201,21 @@ static void ServiceParseBootEventHook(SERVICE_PARSE_CTX *serviceParseCtx)
static int DoBootEventCmd(int id, const char *name, int argc, const char **argv) static int DoBootEventCmd(int id, const char *name, int argc, const char **argv)
{ {
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter"); PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
// argv[0] samgr.ready.true if (strcmp(argv[0], "init") == 0) {
BootEventParaFireByName(argv[0]); AddInitBootEvent(argv[1]);
} else {
// argv[0] samgr.ready.true
BootEventParaFireByName(argv[0]);
}
return 0; return 0;
} }
static int AddItemToJson(cJSON *root, const char *name, double startime, int tid, double durTime) static int AddItemToJson(cJSON *root, const char *name, double startTime, int tid, double durTime)
{ {
cJSON *obj = cJSON_CreateObject(); // release obj at traverse done cJSON *obj = cJSON_CreateObject(); // release obj at traverse done
INIT_CHECK_RETURN_VALUE(obj != NULL, -1); INIT_CHECK_RETURN_VALUE(obj != NULL, -1);
cJSON_AddStringToObject(obj, "name", name); cJSON_AddStringToObject(obj, "name", name);
cJSON_AddNumberToObject(obj, "ts", startime); cJSON_AddNumberToObject(obj, "ts", startTime);
cJSON_AddStringToObject(obj, "ph", "X"); cJSON_AddStringToObject(obj, "ph", "X");
cJSON_AddNumberToObject(obj, "pid", 0); cJSON_AddNumberToObject(obj, "pid", 0);
cJSON_AddNumberToObject(obj, "tid", tid); cJSON_AddNumberToObject(obj, "tid", tid);
...@@ -192,9 +224,9 @@ static int AddItemToJson(cJSON *root, const char *name, double startime, int tid ...@@ -192,9 +224,9 @@ static int AddItemToJson(cJSON *root, const char *name, double startime, int tid
return 0; return 0;
} }
static int BooteventTraversal(ListNode *node, void *root) static int BootEventTraversal(ListNode *node, void *root)
{ {
static int tid = 1; // 1 bootevent start num static int tid = 1; // 1 boot event start num
BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node; BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node;
double forkTime = item->timestamp[BOOTEVENT_FORK].tv_sec * SECTOMSEC + double forkTime = item->timestamp[BOOTEVENT_FORK].tv_sec * SECTOMSEC +
(double)item->timestamp[BOOTEVENT_FORK].tv_nsec / MSECTONSEC; (double)item->timestamp[BOOTEVENT_FORK].tv_nsec / MSECTONSEC;
...@@ -217,19 +249,26 @@ static int SaveServiceBootEvent(int id, const char *name, int argc, const char * ...@@ -217,19 +249,26 @@ static int SaveServiceBootEvent(int id, const char *name, int argc, const char *
INIT_CHECK_RETURN_VALUE(nowTime > 0, -1); INIT_CHECK_RETURN_VALUE(nowTime > 0, -1);
struct tm *p = localtime(&nowTime); struct tm *p = localtime(&nowTime);
INIT_CHECK_RETURN_VALUE(p != NULL, -1); INIT_CHECK_RETURN_VALUE(p != NULL, -1);
char booteventFileName[BOOT_EVENT_FILEPATH_MAX_LEN] = ""; char bootEventFileName[BOOT_EVENT_FILEPATH_MAX_LEN] = "";
INIT_CHECK_RETURN_VALUE(snprintf(booteventFileName, BOOT_EVENT_FILEPATH_MAX_LEN, INIT_CHECK_RETURN_VALUE(snprintf(bootEventFileName, BOOT_EVENT_FILEPATH_MAX_LEN,
BOOTEVENT_OUTPUT_PATH"%d%d%d-%d%d.bootevent", BOOTEVENT_OUTPUT_PATH"%d%d%d-%d%d.bootevent",
1900 + p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min) >= 0, -1); // 1900 is start year 1900 + p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min) >= 0, -1); // 1900 is start year
CheckAndCreatFile(booteventFileName, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); CheckAndCreatFile(bootEventFileName, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
FILE *tmpFile = fopen(booteventFileName, "wr"); FILE *tmpFile = fopen(bootEventFileName, "wr");
INIT_CHECK_RETURN_VALUE(tmpFile != NULL, -1); INIT_CHECK_RETURN_VALUE(tmpFile != NULL, -1);
cJSON *root = cJSON_CreateArray(); cJSON *root = cJSON_CreateArray();
INIT_CHECK_RETURN_VALUE(root != NULL, -1); if (root == NULL) {
OH_ListTraversal(&bootEventList, (void *)root, BooteventTraversal, 0); (void)fclose(tmpFile);
return -1;
}
OH_ListTraversal(&bootEventList, (void *)root, BootEventTraversal, 0);
char *buff = cJSON_Print(root); char *buff = cJSON_Print(root);
INIT_CHECK_RETURN_VALUE(buff != NULL, -1); if (buff == NULL) {
INIT_CHECK_RETURN_VALUE(fprintf(tmpFile, "%s\n", buff) >= 0, -1); cJSON_Delete(root);
(void)fclose(tmpFile);
return -1;
}
INIT_CHECK_ONLY_ELOG(fprintf(tmpFile, "%s\n", buff) >= 0, "save boot event file failed");
free(buff); free(buff);
cJSON_Delete(root); cJSON_Delete(root);
(void)fflush(tmpFile); (void)fflush(tmpFile);
...@@ -258,16 +297,16 @@ static void DumpServiceBootEvent(SERVICE_INFO_CTX *serviceCtx) ...@@ -258,16 +297,16 @@ static void DumpServiceBootEvent(SERVICE_INFO_CTX *serviceCtx)
return; return;
} }
BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)serviceExtData->data; BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)serviceExtData->data;
char booteventForkTimeStamp[BOOT_EVENT_TIMESTAMP_MAX_LEN] = ""; char bootEventForkTimeStamp[BOOT_EVENT_TIMESTAMP_MAX_LEN] = "";
char booteventReadyTimeStamp[BOOT_EVENT_TIMESTAMP_MAX_LEN] = ""; char bootEventReadyTimeStamp[BOOT_EVENT_TIMESTAMP_MAX_LEN] = "";
INIT_CHECK_ONLY_RETURN(sprintf_s(booteventForkTimeStamp, BOOT_EVENT_TIMESTAMP_MAX_LEN, "%f", INIT_CHECK_ONLY_RETURN(sprintf_s(bootEventForkTimeStamp, BOOT_EVENT_TIMESTAMP_MAX_LEN, "%f",
item->timestamp[BOOTEVENT_FORK].tv_sec + item->timestamp[BOOTEVENT_FORK].tv_sec +
(double)item->timestamp[BOOTEVENT_FORK].tv_nsec / SECTONSEC) >= 0); (double)item->timestamp[BOOTEVENT_FORK].tv_nsec / SECTONSEC) >= 0);
INIT_CHECK_ONLY_RETURN(sprintf_s(booteventReadyTimeStamp, BOOT_EVENT_TIMESTAMP_MAX_LEN, "%f", INIT_CHECK_ONLY_RETURN(sprintf_s(bootEventReadyTimeStamp, BOOT_EVENT_TIMESTAMP_MAX_LEN, "%f",
(long)item->timestamp[BOOTEVENT_READY].tv_sec + (long)item->timestamp[BOOTEVENT_READY].tv_sec +
(double)item->timestamp[BOOTEVENT_READY].tv_nsec / SECTONSEC) >= 0); (double)item->timestamp[BOOTEVENT_READY].tv_nsec / SECTONSEC) >= 0);
printf("\t%-20.20s\t%-50s\t%-20.20s\t%-20.20s\n", serviceCtx->serviceName, item->paramName, printf("\t%-20.20s\t%-50s\t%-20.20s\t%-20.20s\n", serviceCtx->serviceName, item->paramName,
booteventForkTimeStamp, booteventReadyTimeStamp); bootEventForkTimeStamp, bootEventReadyTimeStamp);
} }
return; return;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册