1 /**
2 Lightweight profiler library for c++
3 Copyright(C) 2016-2017  Sergey Yagovtsev, Victor Zarubkin
4 
5 Licensed under either of
6     * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
7     * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
8 at your option.
9 
10 The MIT License
11     Permission is hereby granted, free of charge, to any person obtaining a copy
12     of this software and associated documentation files (the "Software"), to deal
13     in the Software without restriction, including without limitation the rights
14     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
15     of the Software, and to permit persons to whom the Software is furnished
16     to do so, subject to the following conditions:
17 
18     The above copyright notice and this permission notice shall be included in all
19     copies or substantial portions of the Software.
20 
21     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
22     INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23     PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24     LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26     USE OR OTHER DEALINGS IN THE SOFTWARE.
27 
28 
29 The Apache License, Version 2.0 (the "License");
30     You may not use this file except in compliance with the License.
31     You may obtain a copy of the License at
32 
33     http://www.apache.org/licenses/LICENSE-2.0
34 
35     Unless required by applicable law or agreed to in writing, software
36     distributed under the License is distributed on an "AS IS" BASIS,
37     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38     See the License for the specific language governing permissions and
39     limitations under the License.
40 
41 **/
42 
43 #include "thread_storage.h"
44 #include "current_thread.h"
45 #include "current_time.h"
46 
ThreadStorage()47 ThreadStorage::ThreadStorage()
48     : nonscopedBlocks(16)
49     , frameStartTime(0)
50     , id(getCurrentThreadId())
51     , stackSize(0)
52     , allowChildren(true)
53     , named(false)
54     , guarded(false)
55     , frameOpened(false)
56     , halt(false)
57 {
58     expired = ATOMIC_VAR_INIT(0);
59     profiledFrameOpened = ATOMIC_VAR_INIT(false);
60 }
61 
storeValue(profiler::timestamp_t _timestamp,profiler::block_id_t _id,profiler::DataType _type,const void * _data,size_t _size,bool _isArray,profiler::ValueId _vin)62 void ThreadStorage::storeValue(profiler::timestamp_t _timestamp, profiler::block_id_t _id, profiler::DataType _type, const void* _data, size_t _size, bool _isArray, profiler::ValueId _vin)
63 {
64     const uint16_t serializedDataSize = static_cast<uint16_t>(sizeof(profiler::ArbitraryValue) + _size);
65     void* data = blocks.closedList.allocate(serializedDataSize);
66 
67     ::new (data) profiler::ArbitraryValue(_timestamp, _vin.m_id, _id, static_cast<uint16_t>(_size), _type, _isArray);
68 
69     char* cdata = reinterpret_cast<char*>(data);
70     memcpy(cdata + sizeof(profiler::ArbitraryValue), _data, _size);
71 
72     blocks.usedMemorySize += serializedDataSize;
73 }
74 
storeBlock(const profiler::Block & block)75 void ThreadStorage::storeBlock(const profiler::Block& block)
76 {
77 #if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0
78     EASY_LOCAL_STATIC_PTR(const BaseBlockDescriptor*, desc, \
79                           MANAGER.addBlockDescriptor(EASY_OPTION_STORAGE_EXPAND_BLOCKS_ON ? profiler::ON : profiler::OFF, EASY_UNIQUE_LINE_ID, "EasyProfiler.ExpandStorage", \
80                                                      __FILE__, __LINE__, profiler::BlockType::Block, EASY_COLOR_INTERNAL_EVENT));
81 
82     EASY_THREAD_LOCAL static profiler::timestamp_t beginTime = 0ULL;
83     EASY_THREAD_LOCAL static profiler::timestamp_t endTime = 0ULL;
84 #endif
85 
86     uint16_t nameLength = static_cast<uint16_t>(strlen(block.name()));
87     uint16_t serializedDataSize = static_cast<uint16_t>(sizeof(profiler::BaseBlockData) + nameLength + 1);
88 
89 #if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0
90     const bool expanded = (desc->m_status & profiler::ON) && blocks.closedList.need_expand(serializedDataSize);
91     if (expanded) beginTime = getCurrentTime();
92 #endif
93 
94     void* data = blocks.closedList.allocate(serializedDataSize);
95 
96 #if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0
97     if (expanded) endTime = getCurrentTime();
98 #endif
99 
100     ::new (data) profiler::SerializedBlock(block, nameLength);
101     blocks.usedMemorySize += serializedDataSize;
102 
103 #if EASY_OPTION_MEASURE_STORAGE_EXPAND != 0
104     if (expanded)
105     {
106         profiler::Block b(beginTime, desc->id(), "");
107         b.finish(endTime);
108 
109         serializedDataSize = static_cast<uint16_t>(sizeof(profiler::BaseBlockData) + 1);
110         data = blocks.closedList.allocate(serializedDataSize);
111         ::new (data) profiler::SerializedBlock(b, 0);
112         blocks.usedMemorySize += serializedDataSize;
113     }
114 #endif
115 }
116 
storeCSwitch(const CSwitchBlock & block)117 void ThreadStorage::storeCSwitch(const CSwitchBlock& block)
118 {
119     uint16_t nameLength = static_cast<uint16_t>(strlen(block.name()));
120     uint16_t serializedDataSize = static_cast<uint16_t>(sizeof(profiler::CSwitchEvent) + nameLength + 1);
121     void* data = sync.closedList.allocate(serializedDataSize);
122     ::new (data) profiler::SerializedCSwitch(block, nameLength);
123     sync.usedMemorySize += serializedDataSize;
124 }
125 
clearClosed()126 void ThreadStorage::clearClosed()
127 {
128     blocks.clearClosed();
129     sync.clearClosed();
130 }
131 
popSilent()132 void ThreadStorage::popSilent()
133 {
134     if (!blocks.openedList.empty())
135     {
136         profiler::Block& top = blocks.openedList.back();
137         top.m_end = top.m_begin;
138         if (!top.m_isScoped)
139             nonscopedBlocks.pop();
140         blocks.openedList.pop_back();
141     }
142 }
143 
beginFrame()144 void ThreadStorage::beginFrame()
145 {
146     if (!frameOpened)
147     {
148         frameStartTime = getCurrentTime();
149         frameOpened = true;
150     }
151 }
152 
endFrame()153 profiler::timestamp_t ThreadStorage::endFrame()
154 {
155     frameOpened = false;
156     return getCurrentTime() - frameStartTime;
157 }
158