提交 f3010462 编写于 作者: C cheng_jinsong

init: code optimizing

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 3e106f56
......@@ -42,6 +42,7 @@ void PluginExecCmdByName(const char *name, const char *cmdContent);
void PluginExecCmdByCmdIndex(int index, const char *cmdContent);
int PluginExecCmd(const char *name, int argc, const char **argv);
const char *PluginGetCmdIndex(const char *cmdStr, int *index);
const char *GetPluginCmdNameByIndex(int index);
int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd);
......
......@@ -719,7 +719,7 @@ void DoCmdByName(const char *name, const char *cmdContent)
}
(void)clock_gettime(CLOCK_MONOTONIC, &cmdTimer.endTime);
long long diff = InitDiffTime(&cmdTimer);
INIT_LOGV("Command %s execute time %lld", name, diff);
INIT_LOGV("Execute command \"%s %s\" took %lld ms", name, cmdContent, diff / 1000); // 1000 is convert us to ms
}
void DoCmdByIndex(int index, const char *cmdContent)
......@@ -731,20 +731,25 @@ void DoCmdByIndex(int index, const char *cmdContent)
INIT_TIMING_STAT cmdTimer;
(void)clock_gettime(CLOCK_MONOTONIC, &cmdTimer.startTime);
const struct CmdTable *commCmds = GetCommCmdTable(&cmdCnt);
const char *cmdName = NULL;
if (index < cmdCnt) {
cmdName = commCmds[index].name;
ExecCmd(&commCmds[index], cmdContent);
INIT_LOGV("Command: %s content: %s", commCmds[index].name, cmdContent);
} else {
int number = 0;
const struct CmdTable *cmds = GetCmdTable(&number);
if (index < (cmdCnt + number)) {
cmdName = cmds[index - cmdCnt].name;
ExecCmd(&cmds[index - cmdCnt], cmdContent);
INIT_LOGV("Command: %s content: %s", cmds[index - cmdCnt].name, cmdContent);
} else {
PluginExecCmdByCmdIndex(index, cmdContent);
cmdName = GetPluginCmdNameByIndex(index);
if (cmdName == NULL) {
cmdName = "Unknown";
}
}
}
(void)clock_gettime(CLOCK_MONOTONIC, &cmdTimer.endTime);
long long diff = InitDiffTime(&cmdTimer);
INIT_LOGV("Command execute time %lld", diff);
INIT_LOGV("Execute command \"%s %s\" took %lld ms", cmdName, cmdContent, diff / 1000); // 1000 is convert us to ms
}
......@@ -122,14 +122,22 @@ const struct CmdTable *GetCmdTable(int *number)
void PluginExecCmdByName(const char *name, const char *cmdContent)
{
}
void PluginExecCmdByCmdIndex(int index, const char *cmdContent)
{
}
const char *PluginGetCmdIndex(const char *cmdStr, int *index)
{
return NULL;
}
const char *GetPluginCmdNameByIndex(int index)
{
return NULL;
}
int SetFileCryptPolicy(const char *dir)
{
return 0;
}
\ No newline at end of file
}
......@@ -144,20 +144,38 @@ static int CompareCmdId(const HashNode *node, const void *key)
return cmd->cmdId - *(int *)key;
}
void PluginExecCmdByCmdIndex(int index, const char *cmdContent)
static PluginCmd *GetPluginCmdByIndex(int index)
{
int hashCode = ((index >> 16) & 0x0000ffff) - 1; // 16 left shift
int cmdId = (index & 0x0000ffff);
HashNode *node = OH_HashMapFind(GetGroupHashMap(NODE_TYPE_CMDS),
hashCode, (const void *)&cmdId, CompareCmdId);
if (node == NULL) {
return;
return NULL;
}
InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
if (groupNode == NULL || groupNode->data.cmd == NULL) {
return NULL;
}
return groupNode->data.cmd;
}
const char *GetPluginCmdNameByIndex(int index)
{
PluginCmd *cmd = GetPluginCmdByIndex(index);
if (cmd == NULL) {
return NULL;
}
return cmd->name;
}
void PluginExecCmdByCmdIndex(int index, const char *cmdContent)
{
PluginCmd *cmd = GetPluginCmdByIndex(index);
if (cmd == NULL) {
INIT_LOGW("Cannot find plugin command with index %d", index);
return;
}
PluginCmd *cmd = groupNode->data.cmd;
INIT_LOGV("Command: %s cmdContent: %s", cmd->name, cmdContent);
PluginExecCmd_(cmd, cmdContent);
}
......
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed 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
......@@ -40,28 +40,34 @@
#define PRETIMEOUT_GAP 5
#define PRETIMEOUT_DIV 2
static void WaitAtStartup(const char *source)
#define WATCHDOG_DEV "/dev/watchdog"
static int WaitForWatchDogDevice(void)
{
unsigned int count = 0;
struct stat sourceInfo;
const unsigned int waitTime = 500000;
do {
while ((stat(WATCHDOG_DEV, &sourceInfo) < 0) && (errno == ENOENT) && (count < WAIT_MAX_COUNT)) {
usleep(waitTime);
count++;
} while ((stat(source, &sourceInfo) < 0) && (errno == ENOENT) && (count < WAIT_MAX_COUNT));
}
if (count == WAIT_MAX_COUNT) {
INIT_LOGE("wait for file:%s failed after %u seconds.", source, (WAIT_MAX_COUNT * waitTime) / CONVERSION_BASE);
INIT_LOGE("Wait for watchdog device failed after %u seconds", (WAIT_MAX_COUNT * waitTime) / CONVERSION_BASE);
return 0;
}
return;
return 1;
}
int main(int argc, const char *argv[])
{
WaitAtStartup("/dev/watchdog");
int fd = open("/dev/watchdog", O_RDWR);
if (fd == -1) {
INIT_LOGE("Can't open /dev/watchdog.");
return 1;
if (WaitForWatchDogDevice() == 0) {
return -1;
}
int fd = open(WATCHDOG_DEV, O_RDWR | O_CLOEXEC);
if (fd < 0) {
INIT_LOGE("Open watchdog device failed, err = %d", errno);
return -1;
}
int interval = 0;
......@@ -76,11 +82,10 @@ int main(int argc, const char *argv[])
}
gap = (gap > 0) ? gap : DEFAULT_GAP;
INIT_LOGI("Watchdog started (interval %d, margin %d), fd = %d\n", interval, gap, fd);
#ifdef OHOS_LITE_WATCHDOG
#ifndef LINUX_WATCHDOG
if (setpriority(PRIO_PROCESS, 0, 14) != 0) { // 14 is process priority
INIT_LOGE("setpriority failed err=%d\n", errno);
INIT_LOGE("setpriority failed, err=%d", errno);
}
#endif
#endif
......@@ -93,12 +98,16 @@ int main(int argc, const char *argv[])
#endif
int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeoutSet);
if (ret) {
INIT_LOGE("Failed to set timeout to %d\n", timeoutSet);
if (ret < 0) {
INIT_LOGE("ioctl failed with command WDIOC_SETTIMEOUT, err = %d", errno);
close(fd);
return -1;
}
ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeoutGet);
if (ret) {
INIT_LOGE("Failed to get timeout\n");
if (ret < 0) {
INIT_LOGE("ioctl failed with command WDIOC_GETTIMEOUT, err = %d", errno);
close(fd);
return -1;
}
if (timeoutGet > 0) {
......@@ -106,15 +115,19 @@ int main(int argc, const char *argv[])
}
#ifdef WDIOC_SETPRETIMEOUT
preTimeout = timeoutGet - PRETIMEOUT_GAP; // ensure pretimeout smaller then timeout
preTimeout = timeoutGet - PRETIMEOUT_GAP; // ensure pre timeout smaller then timeout
if (preTimeout > 0) {
ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &preTimeout);
if (ret) {
INIT_LOGE("Failed to set pretimeout to %d\n", preTimeout);
if (ret < 0) {
INIT_LOGE("ioctl failed with command WDIOC_SETPRETIMEOUT, err = %d", errno);
close(fd);
return -1;
}
ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &preTimeoutGet);
if (ret) {
INIT_LOGE("Failed to get pretimeout\n");
if (ret < 0) {
INIT_LOGE("ioctl failed with command WDIOC_GETPRETIMEOUT, err = %d", errno);
close(fd);
return -1;
}
}
......@@ -123,8 +136,14 @@ int main(int argc, const char *argv[])
}
#endif
INIT_LOGI("watchdog started (interval %d, margin %d)", interval, gap);
while (1) {
ioctl(fd, WDIOC_KEEPALIVE);
ret = ioctl(fd, WDIOC_KEEPALIVE);
if (ret < 0) {
// Fed watchdog failed, we don't need to quit the process.
// Wait for kernel to trigger panic.
INIT_LOGE("ioctl failed with command WDIOC_KEEPALIVE, err = %d", errno);
}
sleep(interval);
}
close(fd);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册