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 : // This file provides the PageFaultSimulation class.
16 :
17 : #ifndef SYZYGY_SIMULATE_PAGE_FAULT_SIMULATION_H_
18 : #define SYZYGY_SIMULATE_PAGE_FAULT_SIMULATION_H_
19 :
20 : #include "syzygy/simulate/simulation_event_handler.h"
21 : #include "syzygy/trace/parse/parser.h"
22 :
23 : namespace simulate {
24 :
25 : // An implementation of SimulationEventHandler. PageFaultSimulation simply
26 : // counts the total number of page-faults that happen in the specified
27 : // functions. Sample usage:
28 : //
29 : // PageFaultSimulation simulation;
30 : //
31 : // simulation.set_page_size(0x2000);
32 : // simulation.set_pages_per_code_fault(10);
33 : // simulation.OnProcessStarted(time, 0);
34 : // simulation.OnFunctionEntry(time, 5);
35 : // simulation.OnFunctionEntry(time, 200);
36 : // simulator.SerializeToJSON(file, pretty_print);
37 : //
38 : // If the pages per code fault are not set, then the default value of
39 : // 8 is used.
40 : //
41 : // If the page size is not set, then it's deduced from the trace file data
42 : // or, if that's not possible, it's set to the default value of 0x1000 (4 KB).
43 : class PageFaultSimulation : public SimulationEventHandler {
44 : public:
45 : typedef block_graph::BlockGraph::Block Block;
46 : typedef std::set<uint32> PageSet;
47 :
48 : // The default page size, in case neither the user nor the system
49 : // provide one.
50 : static const size_t kDefaultPageSize = 0x1000;
51 :
52 : // The default number of pages loaded on each code-fault.
53 : static const size_t kDefaultPagesPerCodeFault = 8;
54 :
55 : // Constructs a new PageFaultSimulation instance.
56 : PageFaultSimulation();
57 :
58 : // @name Accessors
59 : // @{
60 E : const PageSet& pages() const { return pages_; }
61 E : size_t fault_count() const { return fault_count_; }
62 E : size_t page_size() const { return page_size_; }
63 E : size_t pages_per_code_fault() const { return pages_per_code_fault_; }
64 : // @}
65 :
66 : // @name Mutators
67 : // @{
68 E : void set_page_size(size_t page_size) {
69 E : DCHECK(page_size > 0);
70 E : page_size_ = page_size;
71 E : }
72 E : void set_pages_per_code_fault(size_t pages_per_code_fault) {
73 E : DCHECK(pages_per_code_fault > 0);
74 E : pages_per_code_fault_ = pages_per_code_fault;
75 E : }
76 : // @}
77 :
78 : // @name SimulationEventHandler implementation
79 : // @{
80 : // Sets the initial page size, if it's not set already.
81 : void OnProcessStarted(base::Time time, size_t default_page_size) override;
82 :
83 : // Registers the page faults, given a certain code block.
84 : void OnFunctionEntry(base::Time time, const Block* block) override;
85 :
86 : // The serialization consists of a single dictionary containing
87 : // the block number of each block that pagefaulted.
88 : bool SerializeToJSON(FILE* output, bool pretty_print) override;
89 : // @}
90 :
91 : protected:
92 : // A set which contains the block number of the pages that
93 : // were faulted in the trace files.
94 : PageSet pages_;
95 :
96 : // The total number of page-faults detected.
97 : size_t fault_count_;
98 :
99 : // The size of each page, in bytes. If not set, PageFaultSimulator will
100 : // try to load the system value, or uses kDefaultPageSize
101 : // if it's unavailable.
102 : size_t page_size_;
103 :
104 : // The number of pages each code-fault loads. If not set,
105 : // PageFaultSimulator uses kDefaultPagesPerFault.
106 : size_t pages_per_code_fault_;
107 : };
108 :
109 : } // namespace simulate
110 :
111 : #endif // SYZYGY_SIMULATE_PAGE_FAULT_SIMULATION_H_
|