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