• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_
6 #define COMPONENTS_METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 namespace metrics {
13 
14 class MetricSample;
15 
16 // Metrics helpers to serialize and deserialize metrics collected by
17 // ChromeOS.
18 namespace SerializationUtils {
19 
20 // If there are more than 100,000 messages in the file, discard the remaining
21 // messages to avoid running out of memory.
22 extern const int kMaxMessagesPerRead;
23 
24 // Deserializes a sample passed as a string and return a sample.
25 // The return value will either be a scoped_ptr to a Metric sample (if the
26 // deserialization was successful) or a nullptr scoped_ptr.
27 std::unique_ptr<MetricSample> ParseSample(const std::string& sample);
28 
29 // Reads all samples from a file and truncate the file when done.
30 void ReadAndTruncateMetricsFromFile(
31     const std::string& filename,
32     std::vector<std::unique_ptr<MetricSample>>* metrics);
33 
34 // Serializes a sample and write it to filename.
35 // The format for the message is:
36 //  message_size, serialized_message
37 // where
38 //  * message_size is the total length of the message (message_size +
39 //    serialized_message) on 4 bytes
40 //  * serialized_message is the serialized version of sample (using ToString)
41 //
42 //  NB: the file will never leave the device so message_size will be written
43 //  with the architecture's endianness.
44 bool WriteMetricToFile(const MetricSample& sample, const std::string& filename);
45 
46 // Maximum length of a serialized message
47 static const size_t kMessageMaxLength = 1024;
48 
49 }  // namespace SerializationUtils
50 }  // namespace metrics
51 
52 #endif  // COMPONENTS_METRICS_SERIALIZATION_SERIALIZATION_UTILS_H_
53