1 /*
2 
3 Copyright (c) 2014-2018, Arvid Norberg, Steven Siloti
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #include "libtorrent/aux_/session_call.hpp"
34 
35 namespace libtorrent { namespace aux {
36 
37 #ifdef TORRENT_PROFILE_CALLS
38 static std::mutex g_calls_mutex;
39 static std::unordered_map<std::string, int> g_blocking_calls;
40 #endif
41 
blocking_call()42 void blocking_call()
43 {
44 #ifdef TORRENT_PROFILE_CALLS
45 	char stack[2048];
46 	print_backtrace(stack, sizeof(stack), 20);
47 	std::unique_lock<std::mutex> l(g_calls_mutex);
48 	g_blocking_calls[stack] += 1;
49 #endif
50 }
51 
dump_call_profile()52 void dump_call_profile()
53 {
54 #ifdef TORRENT_PROFILE_CALLS
55 	FILE* out = fopen("blocking_calls.txt", "w+");
56 
57 	std::map<int, std::string> profile;
58 
59 	std::unique_lock<std::mutex> l(g_calls_mutex);
60 	for (auto const& c : g_blocking_calls)
61 	{
62 		profile[c.second] = c.first;
63 	}
64 	for (std::map<int, std::string>::const_reverse_iterator i = profile.rbegin()
65 		, end(profile.rend()); i != end; ++i)
66 	{
67 		std::fprintf(out, "\n\n%d\n%s\n", i->first, i->second.c_str());
68 	}
69 	fclose(out);
70 #endif
71 }
72 
torrent_wait(bool & done,aux::session_impl & ses)73 void torrent_wait(bool& done, aux::session_impl& ses)
74 {
75 	blocking_call();
76 	std::unique_lock<std::mutex> l(ses.mut);
77 	while (!done) { ses.cond.wait(l); }
78 }
79 
80 } } // namespace aux namespace libtorrent
81