• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #include "include/common/debug/rdr/base_recorder.h"
17 #include "include/common/debug/common.h"
18 #include "include/common/utils/comm_manager.h"
19 #include "include/common/debug/env_config_parser.h"
20 
21 namespace mindspore {
22 namespace {
23 constexpr int kMaxNameLength = 64;
24 }  // namespace
BaseRecorder()25 BaseRecorder::BaseRecorder() : module_(""), name_(""), directory_(""), filename_(""), timestamp_("") {}
BaseRecorder(const std::string & module,const std::string & name)26 BaseRecorder::BaseRecorder(const std::string &module, const std::string &name)
27     : module_(module), name_(name), filename_("") {
28   directory_ = mindspore::EnvConfigParser::GetInstance().RdrPath();
29 
30   if (name.length() > kMaxNameLength) {
31     name_ = name.substr(0, kMaxNameLength);
32     MS_LOG(WARNING) << "The name length is " << name.length() << ", exceeding the limit " << kMaxNameLength
33                     << ". It will be intercepted as '" << name_ << "'.";
34   }
35 
36   std::string err_msg = module_ + ":" + name_ + " set filename failed.";
37   if (!filename_.empty() && !Common::IsFilenameValid(filename_, MAX_FILENAME_LENGTH, err_msg)) {
38     filename_ = "";
39   }
40   auto sys_time = GetTimeString();
41   for (auto ch : sys_time) {
42     if (ch == '.') {
43       break;
44     }
45     if (ch != '-' && ch != ':') {
46       timestamp_.push_back(ch);
47     }
48   }
49 }
50 
GetFileRealPath(const std::string & suffix) const51 std::optional<std::string> BaseRecorder::GetFileRealPath(const std::string &suffix) const {
52   std::string filename;
53   if (filename_.empty()) {
54     filename = module_ + delimiter_ + name_;
55     if (!suffix.empty()) {
56       filename += delimiter_ + suffix;
57     }
58     filename += delimiter_ + timestamp_;
59   } else {
60     filename = filename_;
61     if (!suffix.empty()) {
62       filename = filename_ + delimiter_ + suffix;
63     }
64   }
65   std::string file_path = directory_ + "rank_" + std::to_string(GetRank()) + "/rdr/" + filename;
66   auto realpath = Common::CreatePrefixPath(file_path);
67   if (!realpath.has_value()) {
68     MS_LOG(ERROR) << "Get real path failed. "
69                   << "Info: module=" << module_ << ", name=" << name_ << ", "
70                   << "path=" << file_path << ".";
71   }
72 
73   return realpath;
74 }
75 }  // namespace mindspore
76