• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "perf_protobuf_io.h"
6 
7 #include <vector>
8 
9 #include "base/logging.h"
10 
11 #include "file_utils.h"
12 
13 namespace quipper {
14 
SerializeFromFile(const string & filename,PerfDataProto * perf_data_proto)15 bool SerializeFromFile(const string& filename, PerfDataProto* perf_data_proto) {
16   return SerializeFromFileWithOptions(filename, PerfParserOptions(),
17                                       perf_data_proto);
18 }
19 
SerializeFromFileWithOptions(const string & filename,const PerfParserOptions & options,PerfDataProto * perf_data_proto)20 bool SerializeFromFileWithOptions(const string& filename,
21                                   const PerfParserOptions& options,
22                                   PerfDataProto* perf_data_proto) {
23   PerfReader reader;
24   if (!reader.ReadFile(filename)) return false;
25 
26   PerfParser parser(&reader, options);
27   if (!parser.ParseRawEvents()) return false;
28 
29   if (!reader.Serialize(perf_data_proto)) return false;
30 
31   // Append parser stats to protobuf.
32   PerfSerializer::SerializeParserStats(parser.stats(), perf_data_proto);
33   return true;
34 }
35 
DeserializeToFile(const PerfDataProto & perf_data_proto,const string & filename)36 bool DeserializeToFile(const PerfDataProto& perf_data_proto,
37                        const string& filename) {
38   PerfReader reader;
39   return reader.Deserialize(perf_data_proto) && reader.WriteFile(filename);
40 }
41 
WriteProtobufToFile(const PerfDataProto & perf_data_proto,const string & filename)42 bool WriteProtobufToFile(const PerfDataProto& perf_data_proto,
43                          const string& filename) {
44   string output;
45   perf_data_proto.SerializeToString(&output);
46 
47   return BufferToFile(filename, output);
48 }
49 
ReadProtobufFromFile(PerfDataProto * perf_data_proto,const string & filename)50 bool ReadProtobufFromFile(PerfDataProto* perf_data_proto,
51                           const string& filename) {
52   std::vector<char> buffer;
53   if (!FileToBuffer(filename, &buffer)) return false;
54 
55   bool ret = perf_data_proto->ParseFromArray(buffer.data(), buffer.size());
56 
57   LOG(INFO) << "#events" << perf_data_proto->events_size();
58 
59   return ret;
60 }
61 
62 }  // namespace quipper
63