1 : // Copyright 2012 Google Inc. 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 : // Declares the BasicBlockEntryCountSerializer class.
16 :
17 : #ifndef SYZYGY_GRINDER_BASIC_BLOCK_ENTRY_COUNT_SERIALIZER_H_
18 : #define SYZYGY_GRINDER_BASIC_BLOCK_ENTRY_COUNT_SERIALIZER_H_
19 :
20 : #include <map>
21 : #include <vector>
22 :
23 : #include "base/values.h"
24 : #include "syzygy/grinder/basic_block_util.h"
25 : #include "syzygy/grinder/grinder.h"
26 :
27 : namespace grinder {
28 :
29 : // This class serializes and deserializes a basic_block_util::EntryCountMap,
30 : // containing entry count information for one or more modules, to/from a JSON
31 : // file.
32 : //
33 : // The JSON file has the following structure.
34 : //
35 : // [
36 : // // Basic-block entry counts for module 1. Note that the module
37 : // // information refers to the original module, as opposed to the
38 : // // instrumented copy.
39 : // {
40 : // "metadata": {
41 : // "command_line": "\"foo.exe\"",
42 : // "creation_time": "Wed, 19 Sep 2012 17:33:52 GMT",
43 : // "toolchain_version": {
44 : // "major": 0,
45 : // "minor": 2,
46 : // "build": 7,
47 : // "patch": 0,
48 : // "last_change": "0"
49 : // },
50 : // "module_signature": {
51 : // "path": "C:\\foo\\bar.dll",
52 : // "base_address": 1904279552,
53 : // "module_size": 180224,
54 : // "module_time_date_stamp": "0x46F7885059FE32",
55 : // "module_checksum": "0x257AF"
56 : // }
57 : // },
58 : // // Basic-block entry count pairs, encoded as pairs of
59 : // // [offset, count], where offset is the RVA to the first instruction
60 : // // byte of the basic block in the original image.
61 : // "entry_counts": [
62 : // [100, 10000],
63 : // [200, 123456]
64 : // ]
65 : // },
66 : // // Basic-block entry counts for module 2.
67 : // ...
68 : // ]
69 : class BasicBlockEntryCountSerializer {
70 : public:
71 : typedef basic_block_util::ModuleEntryCountMap ModuleEntryCountMap;
72 :
73 : BasicBlockEntryCountSerializer();
74 :
75 : // Sets the pretty-printing status.
76 E : void set_pretty_print(bool value) { pretty_print_ = value; }
77 :
78 : // Saves the given entry count map to a file at @p file_path.
79 : bool SaveAsJson(const ModuleEntryCountMap& entry_counts,
80 : const base::FilePath& file_path);
81 :
82 : // Saves the given entry count map to a file previously opened for writing.
83 : bool SaveAsJson(const ModuleEntryCountMap& entry_counts,
84 : FILE* file);
85 :
86 : // Populates an entry count map from a JSON file, given by @p file_path.
87 : bool LoadFromJson(const base::FilePath& file_path,
88 : ModuleEntryCountMap* entry_counts);
89 :
90 : protected:
91 : // Populates an entry count map from JSON data. Exposed for unit-testing
92 : // purposes.
93 : bool PopulateFromJsonValue(const base::Value* json_value,
94 : ModuleEntryCountMap* entry_counts);
95 :
96 : // If true, the JSON output will be pretty printed for easier human
97 : // consumption.
98 : bool pretty_print_;
99 :
100 : private:
101 : DISALLOW_COPY_AND_ASSIGN(BasicBlockEntryCountSerializer);
102 : };
103 :
104 : } // namespace grinder
105 :
106 : #endif // SYZYGY_GRINDER_BASIC_BLOCK_ENTRY_COUNT_SERIALIZER_H_
|