17 #include <mach/clock.h> 18 #include <mach/mach.h> 28 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
29 clock_get_time(cclock, &mts);
30 mach_port_deallocate(mach_task_self(), cclock);
31 ts->tv_sec = mts.tv_sec;
32 ts->tv_nsec = mts.tv_nsec;
34 clock_gettime(CLOCK_MONOTONIC, ts);
48 inline static unsigned long current_time_millis() {
52 return t.tv_sec * 1000L + t.tv_nsec / 1000000L;
56 PhaseData* m_data =
nullptr;
58 bool m_track_memory =
false;
59 bool m_disabled =
false;
61 inline void append_child(PhaseData* data) {
62 if(m_data->first_child) {
63 PhaseData* last = m_data->first_child;
64 while(last->next_sibling) {
65 last = last->next_sibling;
67 last->next_sibling = data;
69 m_data->first_child = data;
73 inline void track_alloc_internal(
size_t bytes) {
75 m_data->mem_current += bytes;
76 m_data->mem_peak = std::max(m_data->mem_peak, m_data->mem_current);
77 if(m_parent) m_parent->track_alloc_internal(bytes);
81 inline void track_free_internal(
size_t bytes) {
83 m_data->mem_current -= bytes;
84 if(m_parent) m_parent->track_free_internal(bytes);
89 m_track_memory =
false;
92 inline void resume() {
93 m_track_memory =
true;
96 inline void init(
const char* title) {
99 if(m_parent) m_parent->pause();
100 m_data =
new PhaseData();
101 if(m_parent) m_parent->resume();
102 m_data->title(title);
104 m_data->mem_off = m_parent ? m_parent->m_data->mem_current : 0;
105 m_data->mem_current = 0;
106 m_data->mem_peak = 0;
108 m_data->time_end = 0;
109 m_data->time_start = current_time_millis();
114 inline void finish() {
115 m_data->time_end = current_time_millis();
119 m_parent->append_child(m_data);
127 s_current = m_parent;
143 inline static auto wrap(
const char* title, F func) ->
144 typename std::result_of<F(StatPhase&)>::type {
162 inline static auto wrap(
const char* title, F func) ->
163 typename std::result_of<F()>::type {
179 if(s_current) s_current->track_alloc_internal(bytes);
191 if(s_current) s_current->track_free_internal(bytes);
199 if(s_current) s_current->pause();
207 if(s_current) s_current->resume();
218 inline static void log(
const char* key,
const T& value) {
219 if(s_current) s_current->
log_stat(key, value);
264 inline void split(
const char* new_title) {
269 PhaseData* old_data = m_data;
273 m_data->mem_off = old_data->mem_off + old_data->mem_current;
286 inline void split(
const std::string& new_title) {
287 split(new_title.c_str());
298 inline void log_stat(
const char* key,
const T& value) {
301 m_data->log_stat(key, value);
313 m_data->time_end = current_time_millis();
Contains the text compression and encoding framework.
void split(const std::string &new_title)
Starts a new phase as a sibling, reusing the same object.
static void track_free(size_t bytes)
Tracks a memory deallocation of the given size for the current phase.
StatPhase(const std::string &str)
Creates a new statistics phase.
static void pause_tracking()
Pauses the tracking of memory allocations in the current phase.
StatPhase()
Creates a inert statistics phase without any effect.
Provides access to runtime and memory measurement in statistics phases.
StatPhase(const char *title)
Creates a new statistics phase.
void split(const char *new_title)
Starts a new phase as a sibling, reusing the same object.
json::Object to_json()
Constructs the JSON representation of the measured data.
~StatPhase()
Destroys and ends the phase.
Represents a JSON object that behaves like a dictionary.
void log_stat(const char *key, const T &value)
Logs a user statistic for this phase.
static void log(const char *key, const T &value)
Logs a user statistic for the current phase.
void get_monotonic_time(struct timespec *ts)
static auto wrap(const char *title, F func) -> typename std::result_of< F(StatPhase &)>::type
Executes a lambda as a single statistics phase.
static void resume_tracking()
Resumes the tracking of memory allocations in the current phase.
static void track_alloc(size_t bytes)
Tracks a memory allocation of the given size for the current phase.
static auto wrap(const char *title, F func) -> typename std::result_of< F()>::type
Executes a lambda as a single statistics phase.