1 /**
2 * Copyright 2022 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MINDSPORE_CCSRC_DISTRIBUTED_CLUSTER_TOPOLOGY_UTILS_H_
18 #define MINDSPORE_CCSRC_DISTRIBUTED_CLUSTER_TOPOLOGY_UTILS_H_
19
20 #include <climits>
21 #include <string>
22 #include <memory>
23 #include <chrono>
24 #include "utils/log_adapter.h"
25 #include "utils/ms_utils.h"
26 #include "actor/msg.h"
27 #include "include/backend/distributed/cluster/topology/common.h"
28
29 namespace mindspore {
30 namespace distributed {
31 namespace cluster {
32 namespace topology {
FillMetaServerAddress(struct MetaServerAddress * address)33 __attribute__((unused)) static bool FillMetaServerAddress(struct MetaServerAddress *address) {
34 MS_EXCEPTION_IF_NULL(address);
35
36 // Get the address of meta server from the environment.
37 auto ip = common::GetEnv(kEnvMetaServerHost);
38 auto ms_port = common::GetEnv(kEnvMetaServerPort);
39 if (ip.empty()) {
40 MS_LOG(ERROR) << "Failed to get ip of meta server from environment variables.";
41 return false;
42 }
43 if (ms_port.empty()) {
44 MS_LOG(ERROR) << "Failed to get port of meta server from environment variables.";
45 return false;
46 }
47 auto port = std::stoi(ms_port.c_str(), nullptr, kDecimal);
48 // Valid port number range.
49 static const int min_port = 1;
50 static const int max_port = 65535;
51 if (port < min_port || port > max_port) {
52 MS_LOG(ERROR) << "The port number of meta server node: " << port << " is invalid (1~65535).";
53 return false;
54 }
55
56 // Fill the meta server address.
57 address->ip = ip;
58 address->port = port;
59 return true;
60 }
61
CreateMessage(const std::string & dest_url,const std::string & name,const std::string & content)62 __attribute__((unused)) static std::unique_ptr<MessageBase> CreateMessage(const std::string &dest_url,
63 const std::string &name,
64 const std::string &content) {
65 std::unique_ptr<MessageBase> message = std::make_unique<MessageBase>();
66 message->name = name;
67 message->from = AID("", "");
68 message->to = AID("", dest_url);
69 message->body = content;
70 return message;
71 }
72
CreateMessage(const std::string & dest_url,const MessageName & name,const std::string & content)73 __attribute__((unused)) static std::unique_ptr<MessageBase> CreateMessage(const std::string &dest_url,
74 const MessageName &name,
75 const std::string &content) {
76 return CreateMessage(dest_url, std::to_string(static_cast<int>(name)), content);
77 }
78
Now()79 __attribute__((unused)) static std::chrono::high_resolution_clock::time_point Now() {
80 return std::chrono::high_resolution_clock::now();
81 }
82
ElapsedTime(const std::chrono::high_resolution_clock::time_point & start_time)83 __attribute__((unused)) static std::chrono::milliseconds ElapsedTime(
84 const std::chrono::high_resolution_clock::time_point &start_time) {
85 return std::chrono::duration_cast<std::chrono::milliseconds>(Now() - start_time);
86 }
87
CheckFilePath(const std::string & path)88 __attribute__((unused)) static bool CheckFilePath(const std::string &path) {
89 char real_path[PATH_MAX] = {0};
90 if (realpath(path.data(), real_path) == nullptr) {
91 return false;
92 }
93 return true;
94 }
95 } // namespace topology
96 } // namespace cluster
97 } // namespace distributed
98 } // namespace mindspore
99 #endif // MINDSPORE_CCSRC_DISTRIBUTED_CLUSTER_TOPOLOGY_UTILS_H_
100