| 140 | }; |
| 141 | |
| 142 | void DumpToFileInDirImpl(string_view filename, string_view contents, |
| 143 | const CanonicalDebugOptions& opts) { |
| 144 | if (opts.dumping_to_stdout()) { |
| 145 | LOG(ERROR) << "Refusing to write " << filename |
| 146 | << " to stdout. Pass --xla_dump_to=<path> to write to a file."; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | if (opts.dump_to.empty()) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | const string& dir = opts.dump_to; |
| 155 | VLOG(1) << "Dumping " << filename << " to " << dir; |
| 156 | |
| 157 | tensorflow::Env* env = tensorflow::Env::Default(); |
| 158 | // Two threads can race to observe the absence of the dump directory and |
| 159 | // simultaneously try to create it, causing the "losing" thread to get a |
| 160 | // "directory already exists" error. We can work around this by checking |
| 161 | // again whether the dir exists. |
| 162 | if (!env->IsDirectory(dir).ok()) { |
| 163 | auto status = env->RecursivelyCreateDir(dir); |
| 164 | if (!status.ok() && !env->IsDirectory(dir).ok()) { |
| 165 | LOG(ERROR) << "Could not create directory " << dir |
| 166 | << " for dumping XLA debug data: " << status; |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Make sure we are not going to dump more modules than the user has asked. |
| 172 | if (opts.dump_max_hlo_modules > 0) { |
| 173 | std::vector<string> matches; |
| 174 | auto pattern = tensorflow::io::JoinPath(dir, "*module_*.0000.*"); |
| 175 | auto status = env->GetMatchingPaths(pattern, &matches); |
| 176 | if (!status.ok()) { |
| 177 | LOG(ERROR) << "Could not get matching paths for pattern " << pattern |
| 178 | << ": " << status; |
| 179 | } |
| 180 | if (matches.size() > opts.dump_max_hlo_modules) { |
| 181 | LOG(ERROR) << "Have already dumped " << matches.size() |
| 182 | << " modules, more than the limit of " |
| 183 | << opts.dump_max_hlo_modules; |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | string file_path = |
| 189 | tensorflow::io::JoinPath(dir, SanitizeFileName(string(filename))); |
| 190 | auto status = tensorflow::WriteStringToFile(env, file_path, contents); |
| 191 | if (!status.ok()) { |
| 192 | LOG(ERROR) << "Could not write XLA debug data to " << file_path << ": " |
| 193 | << status; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void DumpToFileInDirOrStdoutImpl(string_view filename, string_view contents, |
| 198 | const CanonicalDebugOptions& opts) { |
no test coverage detected