blob: 30d3357e6cace89cacb81ba419013c9dcc8ceb01 [file] [log] [blame]
Raul Silvera0c3c35e2016-09-21 13:14:55 -07001/*
2 * Copyright (c) 2016, Google Inc.
3 * All rights reserved.
lannadorai2b536c92017-07-11 18:10:46 -07004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
Raul Silvera0c3c35e2016-09-21 13:14:55 -07006 */
7
lannadorai2b536c92017-07-11 18:10:46 -07008#ifndef PERFTOOLS_PROFILES_PROTO_BUILDER_H_
9#define PERFTOOLS_PROFILES_PROTO_BUILDER_H_
Raul Silvera0c3c35e2016-09-21 13:14:55 -070010
11#include <stddef.h>
12#include <algorithm>
13#include <memory>
14#include <string>
15#include <tuple>
16#include <unordered_map>
lannadorai2b536c92017-07-11 18:10:46 -070017namespace perftools {
18namespace profiles {
Raul Silvera0c3c35e2016-09-21 13:14:55 -070019
lannadorai2b536c92017-07-11 18:10:46 -070020typedef int64_t int64;
21typedef uint64_t uint64;
22typedef std::string string;
23
24}
25}
Raul Silvera0c3c35e2016-09-21 13:14:55 -070026#include "profile.pb.h"
Raul Silvera0c3c35e2016-09-21 13:14:55 -070027
28namespace perftools {
29namespace profiles {
lannadorai2b536c92017-07-11 18:10:46 -070030
Raul Silvera0c3c35e2016-09-21 13:14:55 -070031// Provides mechanisms to facilitate the generation of profiles
32// on a compressed protobuf:
33// - Manages the creation of the string table.
34// - Manages the creation of Functions for symbolized profiles.
35// - Creates the association between locations and mappings.
36// The caller should populate the profile with samples and their
37// corresponding sample types, and any other optional fields.
38class Builder {
39 public:
40 Builder();
41
42 // Adds a string to the profile string table if not already present.
43 // Returns a unique integer id for this string.
44 int64 StringId(const char *str);
45
46 // Adds a function with these attributes to the profile function
47 // table, if not already present. Returns a unique integer id for
48 // this function.
lannadorai2b536c92017-07-11 18:10:46 -070049 uint64 FunctionId(const char *name, const char *system_name,
50 const char *file, int64 start_line);
Raul Silvera0c3c35e2016-09-21 13:14:55 -070051
52 // Adds mappings for the currently running binary to the profile.
53 void AddCurrentMappings();
54
55 // Prepares the profile for encoding. Returns true on success.
56 // If the profile has no locations, inserts location using the
57 // location_ids from the samples as addresses.
58 // Associates the locations to mappings by comparing the location
59 // address into the mapping address range.
60 bool Finalize();
61
62 // Serializes and compresses the profile into a string, replacing
63 // its contents. It calls Finalize() and returns whether the
64 // encoding was successful.
65 bool Emit(string *output);
66
67 // Serializes and compresses a profile into a string, replacing its
68 // contents. Returns false if there were errors on the serialization
69 // or compression, and the output string will not contain valid data.
70 static bool Marshal(const Profile &profile, string *output);
71
72 // Serializes and compresses a profile into a file represented by a
73 // file descriptor. Returns false if there were errors on the
74 // serialization or compression.
75 static bool MarshalToFile(const Profile &profile, int fd);
76
77 // Serializes and compresses a profile into a file, creating a new
78 // file or replacing its contents if it already exists.
79 static bool MarshalToFile(const Profile &profile, const char *filename);
80
81 // Determines if the profile is internally consistent (suitable for
82 // serialization). Returns true if no errors were encountered.
83 static bool CheckValid(const Profile &profile);
84
85 // Extract the profile from the builder object. No further calls
86 // should be made to the builder after this.
87 std::unique_ptr<Profile> Consume() { return std::move(profile_); }
88
89 // Returns the underlying profile, to populate any fields not
90 // managed by the builder. The fields function and string_table
91 // should be populated through Builder::StringId and
92 // Builder::FunctionId.
93 Profile *mutable_profile() { return profile_.get(); }
94
95 private:
96 // Holds the information about a function to facilitate deduplication.
97 typedef std::tuple<int64, int64, int64, int64> Function;
98 class FunctionHasher {
99 public:
100 size_t operator()(const Function &f) const {
101 int64 hash = std::get<0>(f);
102 hash = hash + ((hash << 8) ^ std::get<1>(f));
103 hash = hash + ((hash << 8) ^ std::get<2>(f));
104 hash = hash + ((hash << 8) ^ std::get<3>(f));
105 return static_cast<size_t>(hash);
106 }
107 };
108
109 // Hashes to deduplicate strings and functions.
110 std::unordered_map<string, int64> strings_;
111 std::unordered_map<Function, int64, FunctionHasher> functions_;
112
113 // Actual profile being updated.
114 std::unique_ptr<Profile> profile_;
115};
116
117} // namespace profiles
118} // namespace perftools
119
lannadorai2b536c92017-07-11 18:10:46 -0700120#endif // PERFTOOLS_PROFILES_PROTO_BUILDER_H_