LCOV - code coverage report
Current view: top level - aicpu_cust_schedule/core - aicpusd_resource_limit.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.6 % 29 28
Test Date: 2026-07-28 10:54:05 Functions: 100.0 % 1 1

            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 "aicpusd_resource_limit.h"
      12              : #include <signal.h>
      13              : #include <string>
      14              : #include "aicpusd_util.h"
      15              : #include "aicpusd_status.h"
      16              : #include "ascend_hal.h"
      17              : 
      18              : namespace aicpu {
      19              : static constexpr const uint32_t SLEEP_100_MSECS = 100000U;
      20              : static constexpr const uint32_t MAX_EXECUTE_TRY_TIMES = 3U;
      21           15 : bool AddToCgroup(const uint32_t deviceId, const uint32_t vfId)
      22              : {
      23           15 :     uint32_t pid = drvDeviceGetBareTgid();
      24           30 :     std::string command = "cd /var/ && sudo ./add_aicpu_cust_sd_to_cgroup.sh";
      25           15 :     std::string pathStr = "/var/add_aicpu_cust_sd_to_cgroup.sh";
      26           15 :     if (access(pathStr.c_str(), F_OK) != 0) {
      27           13 :         pathStr = "/usr/local/Ascend/driver/tools/add_aicpu_cust_sd_to_cgroup.sh";
      28           13 :         if (access(pathStr.c_str(), F_OK) != 0) {
      29           13 :             aicpusd_info("Not find add_aicpu_cust_sd_to_cgroup.sh.");
      30           13 :             return true;
      31              :         }
      32            0 :         command = "cd /usr/local/Ascend/driver/tools/ && sudo ./add_aicpu_cust_sd_to_cgroup.sh";
      33              :     }
      34            2 :     (void)command.append(" ")
      35            2 :         .append(std::to_string(pid))
      36            2 :         .append(" ")
      37            4 :         .append(std::to_string(deviceId))
      38            2 :         .append(" ")
      39            2 :         .append(std::to_string(vfId));
      40              : 
      41              :     // system() may fail due to  "No child processes".
      42              :     // if SIGCHLD is set to SIG_IGN, waitpid() may report ECHILD error because it cannot find the child process.
      43              :     // The reason is that the system() relies on a feature of the system, that is,
      44              :     // when the kernel initializes the process, the processing mode of SIGCHLD signal is SIG_IGN.
      45            2 :     int32_t status = -1;
      46            2 :     uint32_t tryTimes = 0;
      47            6 :     while (tryTimes <= MAX_EXECUTE_TRY_TIMES) {
      48            5 :         status = AicpuSchedule::AicpuUtil::ExecuteCmd(command);
      49            5 :         if (status != 0) {
      50            4 :             aicpusd_run_warn("Adding aicpu custom process to cgroup was not successful, ret:[%d], cmd:[%s].", status, command.c_str());
      51            4 :             (void)usleep(SLEEP_100_MSECS);
      52              :         } else {
      53            1 :             aicpusd_info("Add aicpu custom process to cgroup successfully, cmd:[%s]", command.c_str());
      54            1 :             break;
      55              :         }
      56            4 :         tryTimes++;
      57              :     }
      58            2 :     aicpusd_run_info("Add aicpu custom process to cgroup finished, cmd:[%s]", command.c_str());
      59            2 :     return true;
      60           15 : }
      61              : }
        

Generated by: LCOV version 2.0-1