2016-02-16 23:21:12 +03:00
|
|
|
/**
|
|
|
|
Lightweight profiler library for c++
|
2018-01-29 23:29:43 +03:00
|
|
|
Copyright(C) 2016-2018 Sergey Yagovtsev, Victor Zarubkin
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2017-03-30 06:14:23 +03:00
|
|
|
Licensed under either of
|
2017-09-28 21:04:28 +03:00
|
|
|
* MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
|
2017-03-30 06:14:23 +03:00
|
|
|
* Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
at your option.
|
2016-11-13 16:39:59 +03:00
|
|
|
|
2017-03-30 06:14:23 +03:00
|
|
|
The MIT License
|
2017-09-28 21:04:28 +03:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is furnished
|
|
|
|
to do so, subject to the following conditions:
|
2016-11-13 16:39:59 +03:00
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
2016-11-13 16:39:59 +03:00
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2016-11-13 16:39:59 +03:00
|
|
|
|
|
|
|
|
2017-03-30 06:14:23 +03:00
|
|
|
The Apache License, Version 2.0 (the "License");
|
2017-09-28 21:04:28 +03:00
|
|
|
You may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-30 06:14:23 +03:00
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2016-02-17 23:43:37 +03:00
|
|
|
**/
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2017-03-05 22:59:03 +03:00
|
|
|
#ifndef EASY_PROFILER_MANAGER_H
|
|
|
|
#define EASY_PROFILER_MANAGER_H
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2017-11-09 23:34:17 +03:00
|
|
|
#include <easy/details/profiler_public_types.h>
|
2017-03-05 23:50:38 +03:00
|
|
|
|
2017-11-09 23:56:46 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Do not move this include to other place!
|
|
|
|
// It should be included before Windows.h which is included in spin_lock.h
|
|
|
|
# include <easy/easy_socket.h>
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2016-02-18 19:27:17 +03:00
|
|
|
#include "spin_lock.h"
|
2016-09-13 23:03:01 +03:00
|
|
|
#include "hashed_cstr.h"
|
2017-09-30 19:51:33 +03:00
|
|
|
#include "thread_storage.h"
|
2017-03-05 23:50:38 +03:00
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
#include <atomic>
|
2016-02-18 19:27:17 +03:00
|
|
|
#include <map>
|
2018-05-07 21:42:17 +03:00
|
|
|
#include <ostream>
|
2016-09-13 23:03:01 +03:00
|
|
|
#include <unordered_map>
|
2016-09-09 06:14:34 +03:00
|
|
|
#include <thread>
|
2017-08-19 22:10:51 -05:00
|
|
|
#include <type_traits>
|
2018-05-07 21:42:17 +03:00
|
|
|
#include <vector>
|
2016-08-28 02:41:02 +03:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-04-22 03:34:26 +03:00
|
|
|
using processid_t = uint64_t;
|
2016-12-12 22:26:32 +03:00
|
|
|
|
2016-09-22 23:06:43 +03:00
|
|
|
class BlockDescriptor;
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
namespace profiler {
|
|
|
|
class ValueId;
|
|
|
|
}
|
2017-11-09 23:34:17 +03:00
|
|
|
|
2016-09-22 23:06:43 +03:00
|
|
|
class ProfileManager
|
2016-08-28 02:41:02 +03:00
|
|
|
{
|
2017-11-09 23:12:54 +03:00
|
|
|
#ifndef EASY_MAGIC_STATIC_AVAILABLE
|
2016-09-22 23:06:43 +03:00
|
|
|
friend class ProfileManagerInstance;
|
|
|
|
#endif
|
2016-09-11 16:57:35 +03:00
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
ProfileManager();
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
using atomic_timestamp_t = std::atomic<profiler::timestamp_t>;
|
|
|
|
using guard_lock_t = profiler::guard_lock<profiler::spin_lock>;
|
2018-04-22 03:34:26 +03:00
|
|
|
using map_of_threads_stacks = std::map<profiler::thread_id_t, ThreadStorage>;
|
2018-05-07 21:42:17 +03:00
|
|
|
using block_descriptors_t = std::vector<BlockDescriptor*>;
|
|
|
|
using descriptors_map_t = std::unordered_map<profiler::string_with_hash, profiler::block_id_t>;
|
2016-09-13 23:03:01 +03:00
|
|
|
|
2017-04-09 10:23:59 +03:00
|
|
|
const processid_t m_processId;
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
#if defined(EASY_CHRONO_CLOCK) || defined(_WIN32)
|
|
|
|
const int64_t m_cpuFrequency;
|
|
|
|
#endif
|
|
|
|
|
2017-04-09 10:23:59 +03:00
|
|
|
map_of_threads_stacks m_threads;
|
|
|
|
block_descriptors_t m_descriptors;
|
|
|
|
descriptors_map_t m_descriptorsMap;
|
2018-04-02 01:53:29 +03:00
|
|
|
uint64_t m_descriptorsMemorySize;
|
2018-05-07 21:42:17 +03:00
|
|
|
|
|
|
|
#if !defined(EASY_CHRONO_CLOCK) && !defined(_WIN32)
|
|
|
|
std::atomic<int64_t> m_cpuFrequency;
|
|
|
|
#endif
|
|
|
|
|
2017-04-09 10:23:59 +03:00
|
|
|
profiler::timestamp_t m_beginTime;
|
|
|
|
profiler::timestamp_t m_endTime;
|
2018-04-22 03:34:26 +03:00
|
|
|
atomic_timestamp_t m_frameMax;
|
|
|
|
atomic_timestamp_t m_frameAvg;
|
|
|
|
atomic_timestamp_t m_frameCur;
|
2017-04-09 10:23:59 +03:00
|
|
|
profiler::spin_lock m_spin;
|
|
|
|
profiler::spin_lock m_storedSpin;
|
|
|
|
profiler::spin_lock m_dumpSpin;
|
|
|
|
std::atomic<profiler::thread_id_t> m_mainThreadId;
|
2017-12-28 22:21:54 +03:00
|
|
|
std::atomic_bool m_profilerStatus;
|
2017-04-09 10:23:59 +03:00
|
|
|
std::atomic_bool m_isEventTracingEnabled;
|
|
|
|
std::atomic_bool m_isAlreadyListening;
|
|
|
|
std::atomic_bool m_frameMaxReset;
|
|
|
|
std::atomic_bool m_frameAvgReset;
|
2017-11-02 22:43:37 +03:00
|
|
|
std::atomic_bool m_stopDumping;
|
2016-09-13 23:03:01 +03:00
|
|
|
|
2016-09-05 22:11:03 +03:00
|
|
|
std::string m_csInfoFilename = "/tmp/cs_profiling_info.log";
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
std::thread m_listenThread;
|
|
|
|
std::atomic_bool m_stopListen;
|
2016-09-28 00:37:20 +03:00
|
|
|
|
2016-02-16 23:21:12 +03:00
|
|
|
public:
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2017-11-09 23:12:54 +03:00
|
|
|
ProfileManager(const ProfileManager&) = delete;
|
|
|
|
ProfileManager(ProfileManager&&) = delete;
|
|
|
|
ProfileManager& operator = (const ProfileManager&) = delete;
|
|
|
|
ProfileManager& operator = (ProfileManager&&) = delete;
|
|
|
|
|
2016-02-17 18:18:06 +03:00
|
|
|
static ProfileManager& instance();
|
2016-09-07 21:32:14 +03:00
|
|
|
~ProfileManager();
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-09-22 23:06:43 +03:00
|
|
|
const profiler::BaseBlockDescriptor* addBlockDescriptor(profiler::EasyBlockStatus _defaultStatus,
|
|
|
|
const char* _autogenUniqueId,
|
|
|
|
const char* _name,
|
|
|
|
const char* _filename,
|
|
|
|
int _line,
|
|
|
|
profiler::block_type_t _block_type,
|
2016-12-21 21:59:40 +03:00
|
|
|
profiler::color_t _color,
|
|
|
|
bool _copyName = false);
|
2016-09-06 23:03:05 +03:00
|
|
|
|
2018-03-07 20:52:39 +03:00
|
|
|
void storeValue(const profiler::BaseBlockDescriptor* _desc, profiler::DataType _type, const void* _data, uint16_t _size, bool _isArray, profiler::ValueId _vin);
|
2016-12-12 22:26:32 +03:00
|
|
|
bool storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName);
|
2017-04-17 22:27:10 +03:00
|
|
|
bool storeBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, profiler::timestamp_t _beginTime, profiler::timestamp_t _endTime);
|
2016-08-21 14:46:16 +03:00
|
|
|
void beginBlock(profiler::Block& _block);
|
2017-04-17 22:27:10 +03:00
|
|
|
void beginNonScopedBlock(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName);
|
2016-09-07 21:32:14 +03:00
|
|
|
void endBlock();
|
2018-05-07 21:42:17 +03:00
|
|
|
|
2017-03-31 21:17:08 +03:00
|
|
|
profiler::timestamp_t maxFrameDuration();
|
2017-04-03 23:08:52 +03:00
|
|
|
profiler::timestamp_t avgFrameDuration();
|
|
|
|
profiler::timestamp_t curFrameDuration() const;
|
2018-01-08 02:56:51 +03:00
|
|
|
|
2016-12-21 21:59:40 +03:00
|
|
|
void setEnabled(bool isEnable);
|
2018-01-08 02:56:51 +03:00
|
|
|
EASY_FORCE_INLINE bool isEnabled() const {
|
|
|
|
return m_profilerStatus.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:15:07 +03:00
|
|
|
void setEventTracingEnabled(bool _isEnable);
|
2017-03-01 11:14:02 +02:00
|
|
|
bool isEventTracingEnabled() const;
|
2016-08-28 02:41:02 +03:00
|
|
|
uint32_t dumpBlocksToFile(const char* filename);
|
2016-09-20 22:57:34 +03:00
|
|
|
const char* registerThread(const char* name, profiler::ThreadGuard& threadGuard);
|
2016-11-20 17:09:50 +03:00
|
|
|
const char* registerThread(const char* name);
|
2016-08-28 02:41:02 +03:00
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
void setContextSwitchLogFilename(const char* name);
|
|
|
|
const char* getContextSwitchLogFilename() const;
|
2016-09-05 22:11:03 +03:00
|
|
|
|
2016-09-13 23:03:01 +03:00
|
|
|
void beginContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::thread_id_t _target_thread_id, const char* _target_process, bool _lockSpin = true);
|
2016-12-12 22:26:32 +03:00
|
|
|
void endContextSwitch(profiler::thread_id_t _thread_id, processid_t _process_id, profiler::timestamp_t _endtime, bool _lockSpin = true);
|
2016-12-01 23:30:43 +03:00
|
|
|
void startListen(uint16_t _port);
|
|
|
|
void stopListen();
|
2017-03-01 11:14:02 +02:00
|
|
|
bool isListening() const;
|
2016-09-28 00:37:20 +03:00
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
profiler::timestamp_t ticks2ns(profiler::timestamp_t ticks) const;
|
|
|
|
profiler::timestamp_t ticks2us(profiler::timestamp_t ticks) const;
|
|
|
|
|
|
|
|
static bool isMainThread();
|
|
|
|
static profiler::timestamp_t this_thread_frameTime(profiler::Duration _durationCast);
|
|
|
|
static profiler::timestamp_t this_thread_frameTimeLocalMax(profiler::Duration _durationCast);
|
|
|
|
static profiler::timestamp_t this_thread_frameTimeLocalAvg(profiler::Duration _durationCast);
|
|
|
|
static profiler::timestamp_t main_thread_frameTime(profiler::Duration _durationCast);
|
|
|
|
static profiler::timestamp_t main_thread_frameTimeLocalMax(profiler::Duration _durationCast);
|
|
|
|
static profiler::timestamp_t main_thread_frameTimeLocalAvg(profiler::Duration _durationCast);
|
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
private:
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
void listen(uint16_t _port);
|
|
|
|
|
|
|
|
uint32_t dumpBlocksToStream(std::ostream& _outputStream, bool _lockSpin, bool _async);
|
|
|
|
void setBlockStatus(profiler::block_id_t _id, profiler::EasyBlockStatus _status);
|
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
void registerThread();
|
|
|
|
|
2017-04-02 14:23:11 +03:00
|
|
|
void beginFrame();
|
|
|
|
void endFrame();
|
|
|
|
|
2016-12-21 21:59:40 +03:00
|
|
|
void enableEventTracer();
|
|
|
|
void disableEventTracer();
|
|
|
|
|
2017-09-28 21:04:28 +03:00
|
|
|
static char checkThreadExpired(ThreadStorage& _registeredThread);
|
2016-11-20 17:09:50 +03:00
|
|
|
|
2016-09-28 00:37:20 +03:00
|
|
|
void storeBlockForce(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t& _timestamp);
|
|
|
|
void storeBlockForce2(const profiler::BaseBlockDescriptor* _desc, const char* _runtimeName, ::profiler::timestamp_t _timestamp);
|
|
|
|
|
2016-12-12 22:26:32 +03:00
|
|
|
ThreadStorage& _threadStorage(profiler::thread_id_t _thread_id);
|
2016-09-13 23:03:01 +03:00
|
|
|
ThreadStorage* _findThreadStorage(profiler::thread_id_t _thread_id);
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
ThreadStorage& threadStorage(profiler::thread_id_t _thread_id)
|
2016-12-12 22:26:32 +03:00
|
|
|
{
|
|
|
|
guard_lock_t lock(m_spin);
|
|
|
|
return _threadStorage(_thread_id);
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:42:17 +03:00
|
|
|
ThreadStorage* findThreadStorage(profiler::thread_id_t _thread_id)
|
2016-09-13 23:03:01 +03:00
|
|
|
{
|
|
|
|
guard_lock_t lock(m_spin);
|
|
|
|
return _findThreadStorage(_thread_id);
|
|
|
|
}
|
2017-04-17 22:27:10 +03:00
|
|
|
|
|
|
|
}; // END of class ProfileManager.
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2017-03-05 22:59:03 +03:00
|
|
|
#endif // EASY_PROFILER_MANAGER_H
|