serialization_utils.h 1.8 KB

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