Skip to content

Commit 0987a53

Browse files
committed
Add extra logging to debug the event loop
This should make tracking down the root cause of the event loop pauses on X11 easier. And the infrastructure should come in handy in the future as well.
1 parent 099510f commit 0987a53

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ clean:
1818
debug:
1919
python3 setup.py build $(VVAL) --debug
2020

21+
debug-event-loop:
22+
python3 setup.py build $(VVAL) --debug --extra-logging=event-loop
23+
2124
# Build with the ASAN and UBSAN sanitizers
2225
asan:
2326
python3 setup.py build $(VVAL) --debug --sanitize

glfw/init.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ void _glfwInputError(int code, const char* format, ...)
193193
_glfwErrorCallback(code, description);
194194
}
195195

196+
void
197+
_glfwDebug(const char *format, ...) {
198+
if (format)
199+
{
200+
va_list vl;
201+
202+
fprintf(stderr, "[%.4f] ", glfwGetTime());
203+
va_start(vl, format);
204+
vfprintf(stderr, format, vl);
205+
va_end(vl);
206+
fprintf(stderr, "\n");
207+
}
208+
209+
}
196210

197211
//////////////////////////////////////////////////////////////////////////
198212
////// GLFW public API //////

glfw/internal.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,21 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value);
743743
void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement);
744744
void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window);
745745

746-
#if defined(__GNUC__)
746+
#if defined(__GNUC__) || defined(__clang__)
747747
void _glfwInputError(int code, const char* format, ...)
748748
__attribute__((format(printf, 2, 3)));
749+
void _glfwDebug(const char* format, ...)
750+
__attribute__((format(printf, 1, 2)));
749751
#else
750752
void _glfwInputError(int code, const char* format, ...);
753+
void _glfwDebug(const char* format, ...);
751754
#endif
752755

756+
#ifdef DEBUG_EVENT_LOOP
757+
#define EVDBG(...) _glfwDebug(__VA_ARGS__)
758+
#else
759+
#define EVDBG(...)
760+
#endif
753761

754762
//////////////////////////////////////////////////////////////////////////
755763
////// GLFW internal API //////

glfw/main_loop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void _glfwPlatformRunMainLoop(GLFWtickcallback callback, void* data) {
2929
keep_going = GLFW_TRUE;
3030
tick_callback_requested = GLFW_FALSE;
3131
while(keep_going) {
32+
EVDBG("tick_callback_requested: %d", tick_callback_requested);
3233
while (tick_callback_requested) {
3334
tick_callback_requested = GLFW_FALSE;
3435
callback(data);

glfw/x11_window.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ GLFWbool _glfwDispatchX11Events(void);
5959

6060
static void
6161
handleEvents(double timeout) {
62+
EVDBG("starting handleEvents(%.2f)", timeout);
6263
int display_read_ok = pollForEvents(&_glfw.x11.eventLoopData, timeout);
63-
if (display_read_ok) _glfwDispatchX11Events();
64+
EVDBG("display_read_ok: %d", display_read_ok);
65+
if (display_read_ok) {
66+
_glfwDispatchX11Events();
67+
EVDBG("_glfwDispatchX11Events() done");
68+
}
6469
glfw_ibus_dispatch(&_glfw.x11.xkb.ibus);
6570
glfw_dbus_session_bus_dispatch();
71+
EVDBG("other dispatch done");
6672
}
6773

6874
static GLFWbool

setup.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ def copy(self):
178178

179179

180180
def init_env(
181-
debug=False, sanitize=False, native_optimizations=True, profile=False
181+
debug=False, sanitize=False, native_optimizations=True, profile=False,
182+
extra_logging=()
182183
):
183184
native_optimizations = native_optimizations and not sanitize and not debug
184185
cc, ccver = cc_version()
@@ -200,6 +201,8 @@ def init_env(
200201
)
201202
)
202203
cppflags = shlex.split(cppflags)
204+
for el in extra_logging:
205+
cppflags.append('-DDEBUG_{}'.format(el.upper().replace('-', '_')))
203206
cflags = os.environ.get(
204207
'OVERRIDE_CFLAGS', (
205208
'-Wextra -Wno-missing-field-initializers -Wall -std=c11'
@@ -508,7 +511,7 @@ def build(args, native_optimizations=True):
508511
compilation_database = {
509512
(k['file'], k.get('output')): k['arguments'] for k in compilation_database
510513
}
511-
env = init_env(args.debug, args.sanitize, native_optimizations, args.profile)
514+
env = init_env(args.debug, args.sanitize, native_optimizations, args.profile, args.extra_logging)
512515
try:
513516
compile_c_extension(
514517
kitty_env(), 'kitty/fast_data_types', args.incremental, compilation_database, all_keys, *find_c_files()
@@ -842,6 +845,13 @@ def option_parser(): # {{{
842845
default='lib',
843846
help='The name of the directory inside --prefix in which to store compiled files. Defaults to "lib"'
844847
)
848+
p.add_argument(
849+
'--extra-logging',
850+
action='append',
851+
choices=('event-loop',),
852+
help='Turn on extra logging for debugging in this build. Can be specified multiple times, to turn'
853+
'on different types of logging.'
854+
)
845855
return p
846856
# }}}
847857

0 commit comments

Comments
 (0)