Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <unistd.h>
12 : #include <signal.h>
13 : #include <stdlib.h>
14 : #include <sys/resource.h>
15 : #include <errno.h>
16 : #include "tsd/tsd.h"
17 : #ifndef CONFIG_HCCP_LLT
18 : #include "dlog_pub.h"
19 : #endif
20 : #include "user_log.h"
21 : #include "param.h"
22 : #include "ra_adp.h"
23 : #include "securec.h"
24 : #include "dl_hal_function.h"
25 :
26 : typedef void (*SighandlerT)(int);
27 :
28 0 : STATIC int HccpExecCmd(const char *cmd)
29 : {
30 0 : SighandlerT oldHandler = signal(SIGCHLD, SIG_DFL);
31 : int ret;
32 :
33 0 : ret = system(cmd);
34 0 : (void)signal(SIGCHLD, oldHandler);
35 0 : CHK_PRT_RETURN(ret == -1 || ret == HCCP_KEY_EXPIRED, hccp_err("exec failed, ret[%d], errno[%d]", ret, errno), -1);
36 0 : return 0;
37 : }
38 :
39 0 : STATIC int HccpAddToCgroup(int logicId)
40 : {
41 0 : const char *addMemoryFile = "/var/add_to_cgroup_usermemory.sh";
42 0 : const char *addCtrlCpuFile = "/var/add_to_cgroup_ctrlcpu.sh";
43 0 : char cmd[HCCP_CMD_MAX_LEN] = {0};
44 : enum ProductType productType;
45 : int ret;
46 :
47 : // Cache result after first query, skip exception checking for subsequent queries
48 0 : productType = RsGetProductType(logicId);
49 0 : CHK_PRT_RETURN(productType == PRODUCT_TYPE_INVALID, hccp_err("RsGetProductType failed, logicId:%d", logicId),
50 : -EINVAL);
51 :
52 : // add user memory cgroup
53 0 : if ((productType == PRODUCT_TYPE_910 || productType == PRODUCT_TYPE_310p) && access(addMemoryFile, F_OK) == 0) {
54 0 : ret = snprintf_s(cmd, HCCP_CMD_MAX_LEN, HCCP_CMD_MAX_LEN - 1, "sudo %s hccp_service.bin", addMemoryFile);
55 0 : CHK_PRT_RETURN(ret <= 0, hccp_err("snprintf_s for cmd failed, ret:%d", ret), -EINVAL);
56 0 : ret = HccpExecCmd(cmd);
57 0 : CHK_PRT_RETURN(ret != 0, hccp_err("HccpExecCmd add usermemory cgroup failed, ret:%d", ret), ret);
58 : }
59 :
60 : // add ctrlcpu cgroup explicitly in VF scenario: check file due to compatibility issue
61 0 : if (productType == PRODUCT_TYPE_910_93 && access(addCtrlCpuFile, F_OK) == 0) {
62 0 : ret = snprintf_s(cmd, HCCP_CMD_MAX_LEN, HCCP_CMD_MAX_LEN - 1, "sudo %s %d", addCtrlCpuFile, getpid());
63 0 : CHK_PRT_RETURN(ret <= 0, hccp_err("snprintf_s for cmd failed, ret:%d", ret), -EINVAL);
64 0 : ret = HccpExecCmd(cmd);
65 : // ignore return value if add ctrlcpu cgroup unsuccessful, will result in HccpSetAffinity
66 0 : CHK_PRT_RETURN(ret != 0, hccp_run_warn("HccpExecCmd add ctrlcpu cgroup unsuccessful, ret:%d", ret), 0);
67 : }
68 :
69 0 : return 0;
70 : }
71 :
72 0 : STATIC int HccpChangeNumOfFile(void)
73 : {
74 0 : struct rlimit limit = {0, 0};
75 : int ret;
76 :
77 0 : ret = getrlimit(RLIMIT_NOFILE, &limit);
78 0 : CHK_PRT_RETURN(ret, hccp_err("getrlimit failed, ret = %d, errno = %d\n", ret, errno), ret);
79 :
80 0 : limit.rlim_cur = limit.rlim_max;
81 0 : ret = setrlimit(RLIMIT_NOFILE, &limit);
82 0 : CHK_PRT_RETURN(ret, hccp_err("setrlimit failed, ret = %d, errno = %d\n", ret, errno), ret);
83 :
84 0 : return 0;
85 : }
86 :
87 0 : STATIC int HccpSetLogInfo(struct HccpInitParam *param)
88 : {
89 : #define HUNDREDS_DIGIT 100
90 : int ret;
91 0 : int enableEvent = param->logLevel / HUNDREDS_DIGIT;
92 0 : int level = param->logLevel % HUNDREDS_DIGIT;
93 : #ifndef CONFIG_HCCP_LLT
94 : LogAttr logattr = {0};
95 :
96 : logattr.type = APPLICATION;
97 : logattr.pid = param->pid;
98 : logattr.deviceId = param->backupFlag ? param->backupChipId : param->chipId;
99 : ret = dlog_setlevel(-1, level, enableEvent);
100 : CHK_PRT_RETURN(ret,
101 : hccp_err("hccp set log level failed, ret:%d, log level:%d, enableEvent:%d", ret, level, enableEvent), ret);
102 :
103 : ret = DlogSetAttr(logattr);
104 : CHK_PRT_RETURN(ret,
105 : hccp_err("hccp set attr chip_id:%u, backupFlag:%d, backupChipId:%u failed, ret:%d", param->chipId,
106 : param->backupFlag, param->backupChipId, ret),
107 : ret);
108 : #endif
109 0 : return 0;
110 : }
111 :
112 : #ifndef CONFIG_HCCP_LLT
113 : int main(int argc, char *argv[])
114 : #else
115 0 : int llt_main(int argc, char *argv[])
116 : #endif
117 : {
118 0 : struct HccpInitParam param = {0};
119 : struct timeval start, end;
120 0 : float timeCost = 0.0;
121 : int ret;
122 :
123 0 : hccp_run_info("hccp init start!");
124 0 : ret = HccpChangeNumOfFile();
125 0 : CHK_PRT_RETURN(ret, hccp_err("hccp change limit of nofile failed, ret = %d", ret), ret);
126 :
127 : // use default logicId:0 to get product type
128 0 : ret = HccpAddToCgroup(0);
129 0 : CHK_PRT_RETURN(ret != 0, hccp_err("HccpAddToCgroup error[%d] logicId[%d]", ret, 0), ret);
130 :
131 0 : ret = DlHalInit();
132 0 : CHK_PRT_RETURN(ret, hccp_err("dl_hal_init error[%d]", ret), ret);
133 :
134 0 : ret = HccpParamParse(argc, argv, ¶m);
135 0 : if (ret != 0) {
136 0 : hccp_err("hccp_param_parse error[%d]", ret);
137 0 : goto out;
138 : }
139 :
140 0 : ret = HccpSetLogInfo(¶m);
141 0 : if (ret != 0) {
142 0 : hccp_err("hccp_set_log_info error[%d]", ret);
143 0 : goto out;
144 : }
145 :
146 0 : RsGetCurTime(&start);
147 :
148 0 : ret = HccpInit(param.chipId, param.pid, param.hdcType, param.whiteListStatus);
149 0 : if (ret) {
150 0 : hccp_err("hccp init error[%d]", ret);
151 0 : goto hccp_init_fail;
152 : }
153 :
154 0 : RsGetCurTime(&end);
155 0 : HccpTimeInterval(&end, &start, &timeCost);
156 0 : hccp_run_info("hccp init ok cost [%f] ms logic_id[%d], tgid[%d]", timeCost, param.logicId, param.pid);
157 :
158 0 : ret = SendStartUpFinishMsg((uint32_t)param.logicId, 0, (uint32_t)param.pid, 0);
159 0 : if (ret) {
160 0 : hccp_err("SendStartUpFinishMsg error[%d]", ret);
161 : }
162 :
163 0 : ret = HccpDeinit(param.chipId);
164 0 : if (ret) {
165 0 : hccp_err("hccp deinit error[%d]", ret);
166 0 : goto hccp_init_fail;
167 : }
168 :
169 0 : hccp_run_info("hccp deinit ok! logic_id=%d", param.logicId);
170 :
171 0 : hccp_init_fail:
172 0 : out:
173 0 : DlHalDeinit();
174 0 : return ret;
175 : }
|