1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <iostream>
16 #include <vector>
17 
18 #include "tensorflow/core/summary/schema.h"
19 #include "tensorflow/core/summary/summary_db_writer.h"
20 #include "tensorflow/core/lib/db/sqlite.h"
21 #include "tensorflow/core/lib/io/record_reader.h"
22 #include "tensorflow/core/platform/init_main.h"
23 #include "tensorflow/core/util/command_line_flags.h"
24 #include "tensorflow/core/util/event.pb.h"
25 
26 namespace tensorflow {
27 namespace {
28 
29 template <typename T>
AddCommas(T n)30 string AddCommas(T n) {
31   static_assert(std::is_integral<T>::value, "is_integral");
32   string s = strings::StrCat(n);
33   if (s.size() > 3) {
34     int extra = s.size() / 3 - (s.size() % 3 == 0 ? 1 : 0);
35     s.append(extra, 'X');
36     int c = 0;
37     for (int i = s.size() - 1; i > 0; --i) {
38       s[i] = s[i - extra];
39       if (++c % 3 == 0) {
40         s[--i] = ',';
41         --extra;
42       }
43     }
44   }
45   return s;
46 }
47 
main(int argc,char * argv[])48 int main(int argc, char* argv[]) {
49   string path;
50   string events;
51   string experiment_name;
52   string run_name;
53   string user_name;
54   std::vector<Flag> flag_list = {
55       Flag("db", &path, "Path of SQLite DB file"),
56       Flag("events", &events, "TensorFlow record proto event log file"),
57       Flag("experiment_name", &experiment_name, "The DB experiment_name value"),
58       Flag("run_name", &run_name, "The DB run_name value"),
59       Flag("user_name", &user_name, "The DB user_name value"),
60   };
61   string usage = Flags::Usage(argv[0], flag_list);
62   bool parse_result = Flags::Parse(&argc, argv, flag_list);
63   if (!parse_result || path.empty()) {
64     std::cerr << "The loader tool imports tf.Event record files, created by\n"
65               << "SummaryFileWriter, into the sorts of SQLite database files\n"
66               << "created by SummaryDbWriter.\n\n"
67               << "In addition to the flags below, the environment variables\n"
68               << "defined by core/lib/db/sqlite.cc can also be set.\n\n"
69               << usage;
70     return -1;
71   }
72   port::InitMain(argv[0], &argc, &argv);
73   Env* env = Env::Default();
74 
75   LOG(INFO) << "Opening SQLite file: " << path;
76   Sqlite* db;
77   TF_CHECK_OK(Sqlite::Open(
78       path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX,
79       &db));
80   core::ScopedUnref unref_db(db);
81 
82   LOG(INFO) << "Initializing TensorBoard schema";
83   TF_CHECK_OK(SetupTensorboardSqliteDb(db));
84 
85   LOG(INFO) << "Creating SummaryDbWriter";
86   SummaryWriterInterface* db_writer;
87   TF_CHECK_OK(CreateSummaryDbWriter(db, experiment_name, run_name, user_name,
88                                     env, &db_writer));
89   core::ScopedUnref unref(db_writer);
90 
91   LOG(INFO) << "Loading TF event log: " << events;
92   std::unique_ptr<RandomAccessFile> file;
93   TF_CHECK_OK(env->NewRandomAccessFile(events, &file));
94   io::RecordReader reader(file.get());
95 
96   uint64 start = env->NowMicros();
97   uint64 records = 0;
98   uint64 offset = 0;
99   tstring record;
100   while (true) {
101     std::unique_ptr<Event> event = std::unique_ptr<Event>(new Event);
102     Status s = reader.ReadRecord(&offset, &record);
103     if (s.code() == error::OUT_OF_RANGE) break;
104     TF_CHECK_OK(s);
105     if (!ParseProtoUnlimited(event.get(), record)) {
106       LOG(FATAL) << "Corrupt tf.Event record"
107                  << " offset=" << (offset - record.size())
108                  << " size=" << static_cast<int>(record.size());
109     }
110     TF_CHECK_OK(db_writer->WriteEvent(std::move(event)));
111     ++records;
112   }
113   uint64 elapsed = env->NowMicros() - start;
114   uint64 bps = (elapsed == 0 ? offset : static_cast<uint64>(
115                                             offset / (elapsed / 1000000.0)));
116   LOG(INFO) << "Loaded " << AddCommas(offset) << " bytes with "
117             << AddCommas(records) << " records at " << AddCommas(bps) << " bps";
118   return 0;
119 }
120 
121 }  // namespace
122 }  // namespace tensorflow
123 
main(int argc,char * argv[])124 int main(int argc, char* argv[]) { return tensorflow::main(argc, argv); }
125