1 | /******************************************************************************* |
2 | Copyright (c) The Taichi Authors (2016- ). All Rights Reserved. |
3 | The use of this software is governed by the LICENSE file. |
4 | *******************************************************************************/ |
5 | |
6 | #pragma once |
7 | |
8 | #include <vector> |
9 | #include <map> |
10 | #include <memory> |
11 | #include <mutex> |
12 | #include <thread> |
13 | |
14 | #include "taichi/common/core.h" |
15 | #include "taichi/system/timer.h" |
16 | |
17 | namespace taichi { |
18 | |
19 | class ProfilerRecords; |
20 | |
21 | // Captures running time between the construction and destruction of the |
22 | // profiler instance |
23 | class ScopedProfiler { |
24 | public: |
25 | explicit ScopedProfiler(std::string name, uint64 elements = -1); |
26 | |
27 | void stop(); |
28 | |
29 | static void enable(); |
30 | |
31 | static void disable(); |
32 | |
33 | ~ScopedProfiler(); |
34 | |
35 | private: |
36 | std::string name_; |
37 | float64 start_time_; |
38 | uint64 elements_; |
39 | bool stopped_; |
40 | }; |
41 | |
42 | // A profiling system for multithreaded applications |
43 | class Profiling { |
44 | public: |
45 | void print_profile_info(); |
46 | void clear_profile_info(); |
47 | ProfilerRecords *get_this_thread_profiler(); |
48 | static Profiling &get_instance(); |
49 | |
50 | private: |
51 | std::mutex mut_; |
52 | std::unordered_map<std::thread::id, ProfilerRecords *> profilers_; |
53 | }; |
54 | |
55 | #define TI_PROFILER(name) taichi::ScopedProfiler _profiler_##__LINE__(name); |
56 | |
57 | #define TI_AUTO_PROF TI_PROFILER(__FUNCTION__) |
58 | |
59 | } // namespace taichi |
60 | |