LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/resource_manager/socket - socket_config.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 81 81
Test Date: 2026-08-04 10:52:23 Functions: 94.4 % 18 17

            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              : #ifndef HCCL_SOCKET_CONFIG_H
      12              : #define HCCL_SOCKET_CONFIG_H
      13              : 
      14              : #include "types.h"
      15              : #include "virtual_topo.h"
      16              : #include "hash_utils.h"
      17              : #include "log.h"
      18              : 
      19              : namespace Hccl {
      20         1128 : MAKE_ENUM(SocketRole, SERVER, CLIENT)
      21              : class SocketConfig {
      22              : public:
      23              :     RankId            remoteRank;
      24              :     LinkData          link;
      25              :     uint32_t          listeningPort{DEFAULT_LISTENING_PORT};
      26              :     const std::string tag;
      27              :     uint32_t          hostNic2DeviceNicMode_{0}; // 0 normal, 1: host(host cpu roce channel) - device(transport ibv)
      28              : 
      29           79 :     SocketConfig(RankId remoteRank, const LinkData &link, const std::string &tag)
      30           79 :         : remoteRank(remoteRank), link(link), tag(tag),
      31           79 :           role(link.GetLocalRankId() < link.GetRemoteRankId() ? SocketRole::SERVER : SocketRole::CLIENT),
      32          158 :           hccpTag(role == SocketRole::SERVER
      33          235 :                       ? tag + "_" + to_string(link.GetLocalRankId()) + "_" + to_string(link.GetRemoteRankId()) + "_" +
      34          312 :                             link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr()
      35           81 :                       : tag + "_" + to_string(link.GetRemoteRankId()) + "_" + to_string(link.GetLocalRankId()) + "_" +
      36          241 :                             link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      37           79 :     {}
      38              : 
      39              :     SocketConfig(const LinkData &link, const std::string &tag)
      40              :         : remoteRank(link.GetRemoteRankId()), link(link), tag(tag),
      41              :           role(link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT),
      42              :           hccpTag(role == SocketRole::SERVER
      43              :                       ? tag + "_" + to_string(link.GetLocalRankId()) + "_" + to_string(link.GetRemoteRankId()) + "_" +
      44              :                             link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr()
      45              :                       : tag + "_" + to_string(link.GetRemoteRankId()) + "_" + to_string(link.GetLocalRankId()) + "_" +
      46              :                             link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      47              :     {}
      48              : 
      49            8 :     SocketConfig(const LinkData &link, const std::string &tag, SocketRole role, const uint32_t listenPort)
      50            8 :         : remoteRank(link.GetRemoteRankId()), link(link), listeningPort(listenPort), tag(tag), role(role),
      51            8 :         hccpTag(role == SocketRole::SERVER
      52           16 :                     ? tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr()
      53           24 :                     : tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      54            8 :     {}
      55              : 
      56            3 :     SocketConfig(const LinkData &link, const std::string &tag, bool noRankId)
      57            3 :         : remoteRank(link.GetRemoteRankId()), link(link), tag(tag),
      58            3 :         role(link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT),
      59            6 :         hccpTag(role == SocketRole::SERVER
      60            6 :                     ? tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr()
      61            6 :                     : tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr()),
      62            3 :         noRankId(noRankId)
      63            3 :     {}
      64              : 
      65           13 :     SocketConfig(const LinkData &link, const uint32_t listenPort, const std::string &tag,
      66           13 :         uint32_t hostNic2DeviceNicMode, const uint32_t myRank, const uint32_t rmtRank):
      67           13 :         SocketConfig(link, listenPort, tag)
      68              :     {
      69           13 :         if (hostNic2DeviceNicMode == 0) {
      70            4 :             return;
      71              :         }
      72              :         // Parse commTag from tag prefix: tag format is "commTag_engine_X" or "commTag_engine_X_protocol_Y"
      73            9 :         std::string commTag = tag;
      74            9 :         size_t enginePos = commTag.find("_engine_");
      75            9 :         if (enginePos != std::string::npos) {
      76            6 :             commTag = commTag.substr(0, enginePos);
      77              :         } else {
      78            3 :             HCCL_WARNING("[SocketConfig] socketTag[%s] format error, using original tag as commTag", tag.c_str());
      79              :         }
      80            9 :         remoteRank = rmtRank;
      81            9 :         role = myRank < rmtRank ? SocketRole::SERVER : SocketRole::CLIENT;
      82            9 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
      83           14 :             hccpTag = commTag + "_" + to_string(myRank) + "_" + to_string(rmtRank) + "_" +
      84           21 :                 link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr();
      85              :         } else { // client: tag_remote_local
      86            4 :             hccpTag = commTag + "_" + to_string(rmtRank) + "_" + to_string(myRank) + "_" +
      87            6 :                 link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr();
      88              :         }
      89            9 :         hostNic2DeviceNicMode_ = hostNic2DeviceNicMode;
      90            9 :     }
      91              : 
      92           14 :     SocketConfig(const LinkData &link, const uint32_t listenPort, const std::string &tag)
      93           14 :         : remoteRank(link.GetRemoteRankId()), link(link), listeningPort(listenPort), tag(tag)
      94              :     {
      95           14 :         role = link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT;
      96              :  
      97           14 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
      98           26 :             hccpTag = tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() + 
      99           39 :                       "_" + to_string(listenPort);
     100              :         } else { // client: tag_remote_local
     101            2 :             hccpTag = tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr() + 
     102            3 :                       "_" + to_string(listenPort);
     103              :         }
     104           14 :     }
     105              : 
     106            4 :     SocketConfig(const LinkData &link, const uint32_t listenPort, const std::string &tag, const bool isServer)
     107            4 :         : remoteRank(link.GetRemoteRankId()), link(link), listeningPort(listenPort), tag(tag)
     108              :     {
     109            4 :         role = isServer ? SocketRole::SERVER : SocketRole::CLIENT;
     110              : 
     111            4 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
     112            2 :             hccpTag = tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() +
     113            3 :                       "_" + to_string(listenPort);
     114              :         } else { // client: tag_remote_local
     115            6 :             hccpTag = tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr() +
     116            9 :                       "_" + to_string(listenPort);
     117              :         }
     118            4 :     }
     119              : 
     120           33 :     SocketRole GetRole() const
     121              :     {
     122           33 :         return role;
     123              :     }
     124              : 
     125          164 :     const string &GetHccpTag() const
     126              :     {
     127          164 :         return hccpTag;
     128              :     }
     129              : 
     130              : private:
     131              :     SocketRole role{};
     132              :     string     hccpTag;
     133              : 
     134              : public:
     135              :     bool       noRankId{false};
     136              : };
     137              : } // namespace Hccl
     138              : 
     139              : namespace std {
     140              : // 特化SocketConfig的hash和equal模板,使其可用做map的key
     141              : template <> class hash<Hccl::SocketConfig> {
     142              : public:
     143          118 :     size_t operator()(const Hccl::SocketConfig &socketConfig) const
     144              :     {
     145          118 :         auto remoteRankHash = hash<Hccl::RankId>{}(socketConfig.remoteRank);
     146          118 :         auto localPortHash  = hash<Hccl::PortData>{}(socketConfig.link.GetLocalPort());
     147          118 :         auto remotePortHash = hash<Hccl::PortData>{}(socketConfig.link.GetRemotePort());
     148          118 :         auto tagHash        = hash<string>{}(socketConfig.tag);
     149          118 :         auto portHash       = hash<uint32_t>{}(socketConfig.listeningPort);
     150              : 
     151          118 :         return Hccl::HashCombine({remoteRankHash, localPortHash, remotePortHash, tagHash, portHash});
     152              :     }
     153              : };
     154              : 
     155              : template <> class equal_to<Hccl::SocketConfig> {
     156              : public:
     157           57 :     bool operator()(const Hccl::SocketConfig &config, const Hccl::SocketConfig &otherConfig) const
     158              :     {
     159              :         bool IsOthersSame =
     160          114 :             config.link.GetLocalPort().GetAddr() == otherConfig.link.GetLocalPort().GetAddr() &&
     161          111 :             config.link.GetRemotePort().GetAddr() == otherConfig.link.GetRemotePort().GetAddr() &&
     162          108 :             config.tag == otherConfig.tag &&
     163          168 :             config.GetHccpTag() == otherConfig.GetHccpTag() &&
     164           54 :             config.listeningPort == otherConfig.listeningPort;
     165              : 
     166           57 :         if (config.noRankId && otherConfig.noRankId) {
     167            3 :             return IsOthersSame;
     168              :         }
     169              : 
     170           54 :         return IsOthersSame && config.remoteRank == otherConfig.remoteRank;
     171              :     }
     172              : };
     173              : } // namespace std
     174              : 
     175              : #endif // HCCL_SOCKET_CONFIG_H
        

Generated by: LCOV version 2.0-1