summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/profiler.h
blob: 8ea65ac7f0722d75d9fa0379a8074f0f5d439115 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    profiler.h

    Functions to manage profiling of MAME execution.

****************************************************************************

    Profiling is scope-based. To start profiling, put a profiler scope
    object on the stack. To end profiling, just end the scope:

    {
        auto scope = g_profiler.start(PROFILER_VIDEO);

        your_work_here();
    }

    the profiler handles a FILO list so calls may be nested.

***************************************************************************/

#ifndef MAME_EMU_PROFILER_H
#define MAME_EMU_PROFILER_H

#pragma once

#include "emufwd.h"

#include "attotime.h"

#include "eminline.h" // for get_profile_ticks()
#include "osdcore.h"

#include <cstdio>
#include <cstdlib>
#include <string>


//**************************************************************************
//  CONSTANTS
//**************************************************************************

enum profile_type
{
	PROFILER_DEVICE_FIRST = 0,
	PROFILER_DEVICE_MAX = PROFILER_DEVICE_FIRST + 256,
	PROFILER_DRC_COMPILE,
	PROFILER_MEM_REMAP,
	PROFILER_MEMREAD,
	PROFILER_MEMWRITE,
	PROFILER_VIDEO,
	PROFILER_DRAWGFX,
	PROFILER_COPYBITMAP,
	PROFILER_TILEMAP_DRAW,
	PROFILER_TILEMAP_DRAW_ROZ,
	PROFILER_TILEMAP_UPDATE,
	PROFILER_BLIT,
	PROFILER_SOUND,
	PROFILER_TIMER_CALLBACK,
	PROFILER_INPUT,             // input.cpp and inptport.cpp
	PROFILER_MOVIE_REC,         // movie recording
	PROFILER_LOGERROR,          // logerror
	PROFILER_LUA,               // LUA
	PROFILER_EXTRA,             // everything else

	// the USER types are available to driver writers to profile
	// custom sections of the code
	PROFILER_USER1,
	PROFILER_USER2,
	PROFILER_USER3,
	PROFILER_USER4,
	PROFILER_USER5,
	PROFILER_USER6,
	PROFILER_USER7,
	PROFILER_USER8,

	PROFILER_PROFILER,
	PROFILER_IDLE,
	PROFILER_TOTAL
};
DECLARE_ENUM_INCDEC_OPERATORS(profile_type)



//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************


// ======================> real_profiler_state

class real_profiler_state
{
public:
	class scope
	{
	private:
		real_profiler_state &m_host;
		bool m_active;

	public:
		scope(scope const &) = delete;
		scope &operator=(scope const &) = delete;

		scope(scope &&that) noexcept : m_host(that.m_host), m_active(that.m_active)
		{
			that.m_active = false;
		}

		scope(real_profiler_state &host, profile_type type) noexcept : m_host(host), m_active(false)
		{
			if (m_host.enabled())
			{
				m_host.real_start(type);
				m_active = true;
			}
		}

		~scope()
		{
			stop();
		}

		void stop() noexcept
		{
			if (m_active)
			{
				m_host.real_stop();
				m_active = false;
			}
		}
	};

	// construction/destruction
	real_profiler_state();

	// getters
	bool enabled() const noexcept
	{
		return m_filoptr != nullptr;
	}
	const char *text(running_machine &machine);

	// enable/disable
	void enable(bool state = true) noexcept
	{
		if (state != enabled())
		{
			reset(state);
		}
	}

	// start/stop
	[[nodiscard]] auto start(profile_type type) noexcept { return scope(*this, type); }

private:
	// an entry in the FILO
	struct filo_entry
	{
		int             type;                       // type of entry
		osd_ticks_t     start;                      // start time
	};

	void reset(bool enabled) noexcept;
	void update_text(running_machine &machine);

	//-------------------------------------------------
	//  real_start - mark the beginning of a
	//  profiler entry
	//-------------------------------------------------
	ATTR_FORCE_INLINE void real_start(profile_type type) noexcept
	{
		// fail if we overflow
		if (UNEXPECTED(m_filoptr >= &m_filo[std::size(m_filo) - 1]))
		{
			std::fprintf(stderr, "Profiler FILO overflow (type = %d)\n", int(type));
			std::abort();
		}

		// get current tick count
		osd_ticks_t curticks = get_profile_ticks();

		// update previous entry
		m_data[m_filoptr->type] += curticks - m_filoptr->start;

		// move to next entry
		m_filoptr++;

		// fill in this entry
		m_filoptr->type = type;
		m_filoptr->start = curticks;
	}

	//-------------------------------------------------
	//  real_stop - mark the end of a profiler entry
	//-------------------------------------------------
	ATTR_FORCE_INLINE void real_stop() noexcept
	{
		// degenerate scenario
		if (UNEXPECTED(m_filoptr <= m_filo))
			return;

		// get current tick count
		osd_ticks_t curticks = get_profile_ticks();

		// account for the time taken
		m_data[m_filoptr->type] += curticks - m_filoptr->start;

		// move back an entry
		m_filoptr--;

		// reset previous entry start time
		m_filoptr->start = curticks;
	}

	// internal state
	filo_entry *        m_filoptr;                  // current FILO index
	std::string         m_text;                     // profiler text
	attotime            m_text_time;                // profiler text last update
	filo_entry          m_filo[32];                 // array of FILO entries
	osd_ticks_t         m_data[PROFILER_TOTAL + 1]; // array of data
};


// ======================> dummy_profiler_state

class dummy_profiler_state
{
public:
	class scope
	{
	private:
		dummy_profiler_state &m_host;

	public:
		scope(scope const &) = delete;
		scope &operator=(scope const &) = delete;
		scope(scope &&that) noexcept = default;
		scope(dummy_profiler_state &host, profile_type type) noexcept : m_host(host) { }
		~scope() { m_host.real_stop(); }
		void stop() noexcept { }
	};

	// construction/destruction
	dummy_profiler_state();

	// getters
	bool enabled() const noexcept { return false; }
	const char *text(running_machine &machine) { return ""; }

	// enable/disable
	void enable(bool state = true) noexcept { }

	// start/stop
	[[nodiscard]] auto start(profile_type type) noexcept { return scope(*this, type); }

private:
	void real_stop() noexcept { }
};


// ======================> profiler_state

#ifdef MAME_PROFILER
typedef real_profiler_state profiler_state;
#else
typedef dummy_profiler_state profiler_state;
#endif



//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

extern profiler_state g_profiler;


#endif // MAME_EMU_PROFILER_H