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 : #include "syzygy/agent/profiler/symbol_map.h"
16 :
17 : namespace agent {
18 : namespace profiler {
19 :
20 : base::subtle::Atomic32 SymbolMap::Symbol::next_symbol_id_ = 0;
21 :
22 E : SymbolMap::SymbolMap() {
23 E : }
24 :
25 E : SymbolMap::~SymbolMap() {
26 E : }
27 :
28 : void SymbolMap::AddSymbol(const void* start_addr,
29 : size_t length,
30 E : const base::StringPiece& name) {
31 E : base::AutoLock hold(lock_);
32 :
33 E : scoped_refptr<Symbol> symbol = new Symbol(name, start_addr);
34 : // TODO(siggi): Perhaps this should be an error?
35 E : if (!symbol)
36 i : return;
37 :
38 E : Range range(reinterpret_cast<const uint8*>(start_addr), length);
39 E : RetireRangeUnlocked(range);
40 :
41 : bool inserted = addr_space_.Insert(
42 E : Range(reinterpret_cast<const uint8*>(start_addr), length), symbol);
43 E : DCHECK(inserted);
44 E : }
45 :
46 E : void SymbolMap::MoveSymbol(const void* old_addr, const void* new_addr) {
47 E : base::AutoLock hold(lock_);
48 :
49 : SymbolAddressSpace::RangeMapIter found =
50 : addr_space_.FindFirstIntersection(
51 E : Range(reinterpret_cast<const uint8*>(old_addr), 1));
52 :
53 : // If we don't have a record of the original symbol, then we can't move it.
54 : // This may occur if a symbol provider starts pushing events only after its
55 : // address space has been stocked.
56 E : if (found == addr_space_.end() || found->first.start() != old_addr)
57 i : return;
58 :
59 E : scoped_refptr<Symbol> symbol = found->second;
60 :
61 : // Note the fact that it's been moved.
62 E : symbol->Move(new_addr);
63 :
64 E : size_t length = found->first.size();
65 E : addr_space_.Remove(found);
66 :
67 : RetireRangeUnlocked(
68 E : Range(reinterpret_cast<const uint8*>(new_addr), length));
69 :
70 : bool inserted = addr_space_.Insert(
71 E : Range(reinterpret_cast<const uint8*>(new_addr), length), symbol);
72 E : DCHECK(inserted);
73 E : }
74 :
75 E : scoped_refptr<SymbolMap::Symbol> SymbolMap::FindSymbol(const void* addr) {
76 E : base::AutoLock hold(lock_);
77 :
78 : SymbolAddressSpace::RangeMapIter found =
79 : addr_space_.FindFirstIntersection(
80 E : Range(reinterpret_cast<const uint8*>(addr), 1));
81 :
82 E : if (found == addr_space_.end())
83 E : return NULL;
84 :
85 E : return found->second;
86 E : }
87 :
88 E : void SymbolMap::RetireRangeUnlocked(const Range& range) {
89 E : lock_.AssertAcquired();
90 :
91 : SymbolAddressSpace::RangeMapIterPair found =
92 E : addr_space_.FindIntersecting(range);
93 E : SymbolAddressSpace::iterator it = found.first;
94 E : for (; it != found.second; ++it)
95 E : found.first->second->Invalidate();
96 :
97 E : addr_space_.Remove(found);
98 E : }
99 :
100 : SymbolMap::Symbol::Symbol(const base::StringPiece& name, const void* address)
101 : : name_(name.begin(), name.end()),
102 : move_count_(0),
103 : id_(0),
104 E : address_(address) {
105 E : }
106 :
107 E : bool SymbolMap::Symbol::EnsureHasId() {
108 E : DCHECK(!invalid());
109 E : if (base::subtle::Acquire_Load(&id_) != 0)
110 E : return false;
111 :
112 : // Allocate a new symbol ID. Note that we may be racing against other
113 : // threads to assign this ID to the symbol, hence the compare-and-swap
114 : // below. In the case of a race, this ID may not get allocated to any
115 : // symbol.
116 E : base::subtle::Atomic32 next_id = 0;
117 : do {
118 E : next_id = base::subtle::NoBarrier_AtomicIncrement(&next_symbol_id_, 1);
119 E : } while (next_id == 0);
120 :
121 E : return base::subtle::NoBarrier_CompareAndSwap(&id_, 0, next_id) == 0;
122 E : }
123 :
124 E : void SymbolMap::Symbol::Invalidate() {
125 E : DCHECK(!invalid());
126 E : Move(NULL);
127 E : }
128 :
129 E : void SymbolMap::Symbol::Move(const void* new_address) {
130 E : DCHECK(!invalid());
131 : // TODO(siggi): The intent here is to make sure other cores see the new
132 : // value without delay. The barrier may not be what's needed to do that?
133 E : address_ = new_address;
134 E : base::subtle::Barrier_AtomicIncrement(&move_count_, 1);
135 E : }
136 :
137 : } // namespace profiler
138 : } // namespace agent
|