1 /************************************************************************
2 * file name         : tree_widget_loader.h
3 * ----------------- :
4 * creation time     : 2016/08/18
5 * author            : Victor Zarubkin
6 * email             : v.s.zarubkin@gmail.com
7 * ----------------- :
8 * description       : The file contains declaration of EasyTreeWidgetLoader which aim is
9 *                   : to load EasyProfiler blocks hierarchy in separate thread.
10 * ----------------- :
11 * change log        : * 2016/08/18 Victor Zarubkin: moved sources from blocks_tree_widget.h/.cpp
12 *                   :       and renamed Prof* to Easy*.
13 *                   :
14 *                   : *
15 * ----------------- :
16 * license           : Lightweight profiler library for c++
17 *                   : Copyright(C) 2016-2017  Sergey Yagovtsev, Victor Zarubkin
18 *                   :
19 *                   : Licensed under either of
20 *                   :     * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
21 *                   :     * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
22 *                   : at your option.
23 *                   :
24 *                   : The MIT License
25 *                   :
26 *                   : Permission is hereby granted, free of charge, to any person obtaining a copy
27 *                   : of this software and associated documentation files (the "Software"), to deal
28 *                   : in the Software without restriction, including without limitation the rights
29 *                   : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
30 *                   : of the Software, and to permit persons to whom the Software is furnished
31 *                   : to do so, subject to the following conditions:
32 *                   :
33 *                   : The above copyright notice and this permission notice shall be included in all
34 *                   : copies or substantial portions of the Software.
35 *                   :
36 *                   : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
37 *                   : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 *                   : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
39 *                   : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
40 *                   : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
41 *                   : USE OR OTHER DEALINGS IN THE SOFTWARE.
42 *                   :
43 *                   : The Apache License, Version 2.0 (the "License")
44 *                   :
45 *                   : You may not use this file except in compliance with the License.
46 *                   : You may obtain a copy of the License at
47 *                   :
48 *                   : http://www.apache.org/licenses/LICENSE-2.0
49 *                   :
50 *                   : Unless required by applicable law or agreed to in writing, software
51 *                   : distributed under the License is distributed on an "AS IS" BASIS,
52 *                   : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53 *                   : See the License for the specific language governing permissions and
54 *                   : limitations under the License.
55 ************************************************************************/
56 
57 #ifndef EASY_TREE_WIDGET_LOADER_H
58 #define EASY_TREE_WIDGET_LOADER_H
59 
60 #include <stdlib.h>
61 #include <vector>
62 #include <thread>
63 #include <atomic>
64 #include <easy/reader.h>
65 #include "common_types.h"
66 
67 //////////////////////////////////////////////////////////////////////////
68 
69 class EasyTreeWidgetItem;
70 
71 #ifndef EASY_TREE_WIDGET__USE_VECTOR
72 using Items = ::std::unordered_map<::profiler::block_index_t, EasyTreeWidgetItem*, ::estd::hash<::profiler::block_index_t> >;
73 #else
74 using Items = ::std::vector<EasyTreeWidgetItem*>;
75 #endif
76 
77 using ThreadedItems = ::std::vector<::std::pair<::profiler::thread_id_t, EasyTreeWidgetItem*> >;
78 using RootsMap = ::std::unordered_map<::profiler::thread_id_t, EasyTreeWidgetItem*, ::estd::hash<::profiler::thread_id_t> >;
79 using IdItems = ::std::unordered_map<::profiler::block_id_t, EasyTreeWidgetItem*, ::estd::hash<::profiler::block_index_t> >;
80 
81 //////////////////////////////////////////////////////////////////////////
82 
83 enum EasyTreeMode : uint8_t
84 {
85     EasyTreeMode_Full,
86     EasyTreeMode_Plain
87 };
88 
89 //////////////////////////////////////////////////////////////////////////
90 
91 class EasyTreeWidgetLoader Q_DECL_FINAL
92 {
93     ThreadedItems   m_topLevelItems; ///<
94     Items                   m_items; ///<
95     IdItems               m_iditems; ///<
96     ::std::thread          m_thread; ///<
97     ::std::atomic_bool      m_bDone; ///<
98     ::std::atomic_bool m_bInterrupt; ///<
99     ::std::atomic<int>   m_progress; ///<
100     EasyTreeMode             m_mode; ///<
101 
102 public:
103 
104     EasyTreeWidgetLoader();
105     ~EasyTreeWidgetLoader();
106 
107     int progress() const;
108     bool done() const;
109 
110     void takeTopLevelItems(ThreadedItems& _output);
111     void takeItems(Items& _output);
112 
113     void interrupt(bool _wait = false);
114     void fillTree(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, EasyTreeMode _mode);
115     void fillTreeBlocks(const::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _beginTime, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, EasyTreeMode _mode);
116 
117 private:
118 
119     bool interrupted() const;
120     void setDone();
121     void setProgress(int _progress);
122 
123     void setTreeInternal1(::profiler::timestamp_t& _beginTime, const unsigned int _blocksNumber, const ::profiler::thread_blocks_tree_t& _blocksTree, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units);
124     void setTreeInternal2(const ::profiler::timestamp_t& _beginTime, const ::profiler_gui::TreeBlocks& _blocks, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, bool _addZeroBlocks, bool _decoratedThreadNames, bool _hexThreadId, ::profiler_gui::TimeUnits _units);
125     size_t setTreeInternal(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem* _parent, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units);
126     size_t setTreeInternalPlain(const ::profiler::BlocksTreeRoot& _threadRoot, ::profiler::block_index_t _firstCswitch, const ::profiler::timestamp_t& _beginTime, const ::profiler::BlocksTree::children_t& _children, EasyTreeWidgetItem* _parent, EasyTreeWidgetItem* _frame, ::profiler::timestamp_t _left, ::profiler::timestamp_t _right, bool _strict, ::profiler::timestamp_t& _duration, bool _addZeroBlocks, ::profiler_gui::TimeUnits _units);
127 
128     ::profiler::timestamp_t calculateChildrenDurationRecursive(const ::profiler::BlocksTree::children_t& _children, ::profiler::block_id_t _id);
129 
130 }; // END of class EasyTreeWidgetLoader.
131 
132 //////////////////////////////////////////////////////////////////////////
133 
134 #endif // EASY_TREE_WIDGET_LOADER_H
135