File: ActiveFunctions.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (241 lines) | stat: -rw-r--r-- 6,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "stdafx.h"
#include "ActiveFunctions.h"
#include "Engine.h"
#include "Utils/StackTrace.h"
#include "Utils/Cache.h"

namespace storm {

	/**
	 * Collect stack traces for all UThreads on a single OS thread.
	 */
	class TraceCollector {
	public:
		// Resulting stack traces:
		vector<::StackTrace> traces;

		// Collect stack traces. If 'thisThread' is true, then we also capture this UThread.
		void capture(Bool includeThisThread) {
			vector<os::UThread> stacks = os::Thread::current().idleUThreads();

			if (includeThisThread) {
				captureCurrent();
			}

			for (size_t i = 0; i < stacks.size(); i++) {
				if (!stacks[i].detour(util::memberVoidFn(this, &TraceCollector::captureCurrent))) {
					WARNING(L"Failed to execute detour!");
				}
			}
		}

	private:
		// Capture the current UThread.
		void captureCurrent() {
			traces.push_back(stackTrace());
		}
	};


	PauseThreads::PauseThreads(Engine &engine) : signal(0), wait(0) {
		data = new ActiveFunctions();

		vector<os::Thread> threads = engine.allThreads();
		threadCount = threads.size();

		os::Thread current = os::Thread::current();
		for (size_t i = 0; i < threads.size(); i++) {
			if (threads[i] == current) {
				threadCount--;
			} else {
				os::UThread::spawn(util::memberVoidFn(this, &PauseThreads::threadMain), &threads[i]);
			}
		}

		// Capture the call stacks of us while we are waiting.
		captureStacks(true);

		// Wait for all of them to have entered the thread main function, and have collected stack traces!
		// Note: This halts the user-level scheduler for this thread. Otherwise, the stacks we captured above
		// might no longer be alove when we have finished waiting. We *could* capture our stacks after the
		// other threads are done, but that makes it much harder to write robust reload tests (and can easily
		// be confusing when using the PauseThreads class, as one would not really expect it to allow
		// other UThreads to run when it pauses threads).
		for (size_t i = 0; i < threadCount; i++)
			signal.down();

		// Prepare the data for consumption!
		data->done();
	}

	PauseThreads::~PauseThreads() {
		// Clear data to avoid stale references.
		data->clear();
		data->release();

		// Make sure all operations from this thread are visible to others.
		dataBarrier();

		// Signal all threads to wake up.
		for (size_t i = 0; i < threadCount; i++)
			wait.up();

		// ...and wait for them to wake up. Otherwise, we might destroy 'wait' too early.
		for (size_t i = 0; i < threadCount; i++)
			signal.down();

		// Clear our local ICache as well.
		clearLocalICache();
	}

	void PauseThreads::threadMain() {
		// Capture threads, don't include this thread.
		captureStacks(false);

		// Tell the main thread that we are done.
		signal.up();

		// Start waiting.
		wait.down();

		// Tell the main thread that we are done.
		signal.up();

		// Make any updates visible to this core.
		clearLocalICache();
	}

	void PauseThreads::captureStacks(Bool includeCurrent) {
		TraceCollector collector;
		collector.capture(includeCurrent);

		util::Lock::L z(dataLock);
		data->addThread(collector.traces);
	}


	/**
	 * PauseThreadsData.
	 */

	wostream &operator <<(wostream &to, const ActiveOffset &offset) {
		return to << offset.offset << L" x" << offset.count;
	}


	class FrameCompare {
	public:
		const vector<StackFrame> &frames;

		FrameCompare(const vector<StackFrame> &frames) : frames(frames) {}

		bool operator() (size_t a, size_t b) const {
			return size_t(frames[a].fn()) < size_t(frames[b].fn());
		}
		bool operator() (size_t a, const void *b) const {
			return size_t(frames[a].fn()) < size_t(b);
		}
		bool operator() (const void *a, size_t b) const {
			return size_t(a) < size_t(frames[b].fn());
		}
	};

	ActiveFunctions::ActiveFunctions() : refs(1) {}

	void ActiveFunctions::addRef() {
		atomicIncrement(refs);
	}

	void ActiveFunctions::release() {
		if (atomicDecrement(refs) == 0)
			delete this;
	}

	vector<ActiveOffset> ActiveFunctions::find(const void *function) const {
		size_t fnSize = runtime::codeSize(function);
		const byte *fnStart = (const byte *)function;
		const void *fnEnd = fnStart + fnSize;

		FrameCompare compare(frames);
		vector<size_t>::const_iterator first =
			std::lower_bound(sortedFrames.begin(), sortedFrames.end(), fnStart, compare);
		vector<size_t>::const_iterator last =
			std::upper_bound(sortedFrames.begin(), sortedFrames.end(), fnEnd, compare);

		vector<ActiveOffset> result;
		for (vector<size_t>::const_iterator i = first; i != last; ++i) {
			const StackFrame &frame = frames[*i];
			const byte *stackPos = (const byte *)frame.fn();
			size_t offset = stackPos - fnStart;

			if (result.empty()) {
				result.push_back(ActiveOffset(offset, 1));
			} else if (result.back().offset == offset) {
				result.back().count++;
			} else {
				result.push_back(ActiveOffset(offset, 1));
			}
		}

		return result;
	}

	size_t ActiveFunctions::replace(const void *function, size_t offset, const void *replace, size_t rOffset) const {
		const void *exactPtr = (const byte *)function + offset;
		const void *replacePtr = (const byte *)replace + rOffset;

		FrameCompare compare(frames);
		vector<size_t>::const_iterator found =
			std::lower_bound(sortedFrames.begin(), sortedFrames.end(), exactPtr, compare);

		code::Binary *replaceBinary = code::codeBinary(replace);
		code::Arena *arena = replaceBinary->engine().arena();

		size_t replaced = 0;
		while (!compare(exactPtr, *found)) {
			const StackFrame &frame = frames[*found];
			if (frame.returnLocation) {
				frame.updateReturnLocation(replacePtr);
				// *(const void **)frame.returnLocation = replacePtr;
				replaced++;

				// Update any EH information on the stack:
				if (*found + 1 < frames.size())
					arena->updateEhInfo(replace, rOffset, frames[*found + 1].returnLocation);
			}
			++found;
		}

		return replaced;
	}

	void ActiveFunctions::addThread(const vector<::StackTrace> &src) {
		threadStart.push_back(uthreadStart.size());

		for (size_t i = 0; i < src.size(); i++) {
			uthreadStart.push_back(frames.size());

			const ::StackTrace &st = src[i];
			for (nat j = 0; j < st.count(); j++)
				frames.push_back(st[j]);
		}
	}

	void ActiveFunctions::done() {
		// Note: We know that the function allocations in the GC are pinned at this point, so it
		// makes sense to do this here, even without location dependency objects, etc.

		sortedFrames.reserve(frames.size());
		for (size_t i = 0; i < frames.size(); i++)
			sortedFrames.push_back(i);

		std::sort(sortedFrames.begin(), sortedFrames.end(), FrameCompare(frames));
	}

	void ActiveFunctions::clear() {
		frames.clear();
		uthreadStart.clear();
		threadStart.clear();
	}

}