1 : // Copyright 2013 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 IndexedFrequencyDataSerializer class.
16 :
17 : #ifndef SYZYGY_GRINDER_INDEXED_FREQUENCY_DATA_SERIALIZER_H_
18 : #define SYZYGY_GRINDER_INDEXED_FREQUENCY_DATA_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
30 : // basic_block_util::IndexedFrequencyMap, containing frequency information for
31 : // one or more modules, to/from a JSON file.
32 : //
33 : // The JSON file has the following structure.
34 : //
35 : // [
36 : // // Basic-block frequencies 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 : // "informations": {
59 : // "num_entries": 100,
60 : // "num_columns": 3,
61 : // "data_type": 1,
62 : // "frequency_size": 4
63 : // },
64 : // // Basic-block frequencies list, encoded as sequence of frequencies
65 : // // [offset, frequency1, frequency2, ...], where offset is the RVA to
66 : // // the first instruction byte of the basic block in the original
67 : // // image.
68 : // "frequencies": [
69 : // [100, 10000, 12, 1],
70 : // [200, 123456, 124, 12]
71 : // ]
72 : // },
73 : // // Basic-block frequencies list for module 2.
74 : // ...
75 : // ]
76 : class IndexedFrequencyDataSerializer {
77 : public:
78 : typedef basic_block_util::ModuleIndexedFrequencyMap ModuleIndexedFrequencyMap;
79 :
80 : IndexedFrequencyDataSerializer();
81 :
82 : // Sets the pretty-printing status.
83 E : void set_pretty_print(bool value) { pretty_print_ = value; }
84 :
85 : // Saves the given frequency map to a file at @p file_path.
86 : bool SaveAsJson(const ModuleIndexedFrequencyMap& frequency_map,
87 : const base::FilePath& file_path);
88 :
89 : // Saves the given frequency map to a file previously opened for writing.
90 : bool SaveAsJson(const ModuleIndexedFrequencyMap& frequency_map,
91 : FILE* file);
92 :
93 : // Populates a frequency map from a JSON file, given by @p file_path.
94 : bool LoadFromJson(const base::FilePath& file_path,
95 : ModuleIndexedFrequencyMap* frequency_map);
96 :
97 : protected:
98 : // Populates a frequency map from JSON data. Exposed for unit-testing
99 : // purposes.
100 : bool PopulateFromJsonValue(const base::Value* json_value,
101 : ModuleIndexedFrequencyMap* frequency_map);
102 :
103 : // If true, the JSON output will be pretty printed for easier human
104 : // consumption.
105 : bool pretty_print_;
106 :
107 : private:
108 : DISALLOW_COPY_AND_ASSIGN(IndexedFrequencyDataSerializer);
109 : };
110 :
111 : } // namespace grinder
112 :
113 : #endif // SYZYGY_GRINDER_INDEXED_FREQUENCY_DATA_SERIALIZER_H_
|