| 48 | } |
| 49 | |
| 50 | Status EventsWriter::InitIfNeeded() { |
| 51 | if (recordio_writer_ != nullptr) { |
| 52 | CHECK(!filename_.empty()); |
| 53 | if (!FileStillExists().ok()) { |
| 54 | // Warn user of data loss and let .reset() below do basic cleanup. |
| 55 | if (num_outstanding_events_ > 0) { |
| 56 | LOG(WARNING) << "Re-initialization, attempting to open a new file, " |
| 57 | << num_outstanding_events_ << " events will be lost."; |
| 58 | } |
| 59 | } else { |
| 60 | // No-op: File is present and writer is initialized. |
| 61 | return Status::OK(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int64 time_in_seconds = env_->NowMicros() / 1000000; |
| 66 | |
| 67 | filename_ = |
| 68 | strings::Printf("%s.out.tfevents.%010lld.%s%s", file_prefix_.c_str(), |
| 69 | static_cast<int64>(time_in_seconds), |
| 70 | port::Hostname().c_str(), file_suffix_.c_str()); |
| 71 | |
| 72 | // Reset recordio_writer (which has a reference to recordio_file_) so final |
| 73 | // Flush() and Close() call have access to recordio_file_. |
| 74 | recordio_writer_.reset(); |
| 75 | |
| 76 | TF_RETURN_WITH_CONTEXT_IF_ERROR( |
| 77 | env_->NewWritableFile(filename_, &recordio_file_), |
| 78 | "Creating writable file ", filename_); |
| 79 | recordio_writer_.reset(new io::RecordWriter(recordio_file_.get())); |
| 80 | if (recordio_writer_ == nullptr) { |
| 81 | return errors::Unknown("Could not create record writer"); |
| 82 | } |
| 83 | num_outstanding_events_ = 0; |
| 84 | VLOG(1) << "Successfully opened events file: " << filename_; |
| 85 | { |
| 86 | // Write the first event with the current version, and flush |
| 87 | // right away so the file contents will be easily determined. |
| 88 | |
| 89 | Event event; |
| 90 | event.set_wall_time(time_in_seconds); |
| 91 | event.set_file_version(strings::StrCat(kVersionPrefix, kCurrentVersion)); |
| 92 | WriteEvent(event); |
| 93 | TF_RETURN_WITH_CONTEXT_IF_ERROR(Flush(), "Flushing first event."); |
| 94 | } |
| 95 | return Status::OK(); |
| 96 | } |
| 97 | |
| 98 | string EventsWriter::FileName() { |
| 99 | if (filename_.empty()) { |