Skip to content

Commit 8ad95cb

Browse files
committed
Add "cpu-load" feature, for getting cpu-load stats every 0.5s
Signed-off-by: falkTX <[email protected]>
1 parent 8e70439 commit 8ad95cb

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/effects.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,16 @@ static int g_audio_monitor_count;
745745
static jack_port_t *g_midi_in_port;
746746
static jack_position_t g_jack_pos;
747747
static bool g_jack_rolling;
748+
static uint32_t g_jack_xruns;
748749
static volatile double g_transport_bpb;
749750
static volatile double g_transport_bpm;
750751
static volatile bool g_transport_reset;
751752
static volatile enum TransportSyncMode g_transport_sync_mode;
752753
static bool g_aggregated_midi_enabled;
753754
static bool g_processing_enabled;
754755
static bool g_verbose_debug;
756+
static bool g_cpu_load_enabled;
757+
static volatile bool g_cpu_load_trigger;
755758

756759
// Wall clock time since program startup
757760
static uint64_t g_monotonic_frame_count = 0;
@@ -844,6 +847,7 @@ static void AllocatePortBuffers(effect_t* effect, int in_size, int out_size);
844847
static int BufferSize(jack_nframes_t nframes, void* data);
845848
static void FreeWheelMode(int starting, void* data);
846849
static void PortRegistration(jack_port_id_t port_id, int reg, void* data);
850+
static int XRun(void* data);
847851
static void RunPostPonedEvents(int ignored_effect_id);
848852
static void* PostPonedEventsThread(void* arg);
849853
#ifdef __MOD_DEVICES__
@@ -1121,6 +1125,15 @@ static void PortRegistration(jack_port_id_t port_id, int reg, void* data)
11211125
UNUSED_PARAM(data);
11221126
}
11231127

1128+
static int XRun(void *data)
1129+
{
1130+
++g_jack_xruns;
1131+
1132+
return 0;
1133+
1134+
UNUSED_PARAM(data);
1135+
}
1136+
11241137
static bool ShouldIgnorePostPonedEffectEvent(int effect_id, postponed_cached_effect_events* cached_events)
11251138
{
11261139
if (effect_id == cached_events->last_effect_id)
@@ -1225,7 +1238,14 @@ static void RunPostPonedEvents(int ignored_effect_id)
12251238
list_splice_init(&g_rtsafe_list, &queue);
12261239
pthread_mutex_unlock(&g_rtsafe_mutex);
12271240

1228-
if (list_empty(&queue))
1241+
// fetch this value only once per run
1242+
const bool cpu_load_trigger = g_jack_global_client != NULL && g_cpu_load_trigger;
1243+
1244+
if (cpu_load_trigger)
1245+
{
1246+
g_cpu_load_trigger = false;
1247+
}
1248+
else if (list_empty(&queue))
12291249
{
12301250
// nothing to do
12311251
if (g_verbose_debug) {
@@ -1260,7 +1280,7 @@ static void RunPostPonedEvents(int ignored_effect_id)
12601280
INIT_LIST_HEAD(&cached_output_mon.symbols.siblings);
12611281

12621282
// if all we have are jack_midi_connect requests, do not send feedback to server
1263-
bool got_only_jack_midi_requests = true;
1283+
bool got_only_jack_midi_requests = !cpu_load_trigger;
12641284

12651285
if (g_verbose_debug) {
12661286
puts("DEBUG: RunPostPonedEvents() Before the queue iteration");
@@ -1580,6 +1600,18 @@ static void RunPostPonedEvents(int ignored_effect_id)
15801600
}
15811601
}
15821602

1603+
if (cpu_load_trigger)
1604+
{
1605+
snprintf(buf, FEEDBACK_BUF_SIZE, "cpu_load %f %f %d", jack_cpu_load(g_jack_global_client),
1606+
#ifdef HAVE_JACK2_1_9_23
1607+
jack_max_cpu_load(g_jack_global_client),
1608+
#else
1609+
0.f,
1610+
#endif
1611+
g_jack_xruns);
1612+
socket_send_feedback_debug(buf);
1613+
}
1614+
15831615
if (g_verbose_debug) {
15841616
puts("DEBUG: RunPostPonedEvents() After the queue iteration");
15851617
fflush(stdout);
@@ -2877,6 +2909,18 @@ static int ProcessGlobalClient(jack_nframes_t nframes, void *arg)
28772909
if (UpdateGlobalJackPosition(pos_flag, false))
28782910
needs_post = true;
28792911

2912+
if (g_cpu_load_enabled)
2913+
{
2914+
const uint32_t cpu_update_rate = g_sample_rate / 2;
2915+
const uint32_t frame_check = g_monotonic_frame_count % cpu_update_rate;
2916+
2917+
if (frame_check + nframes >= cpu_update_rate)
2918+
{
2919+
g_cpu_load_trigger = true;
2920+
needs_post = true;
2921+
}
2922+
}
2923+
28802924
if (needs_post)
28812925
sem_post(&g_postevents_semaphore);
28822926

@@ -4194,6 +4238,7 @@ int effects_init(void* client)
41944238
jack_set_process_callback(g_jack_global_client, ProcessGlobalClient, NULL);
41954239
jack_set_buffer_size_callback(g_jack_global_client, BufferSize, NULL);
41964240
jack_set_port_registration_callback(g_jack_global_client, PortRegistration, NULL);
4241+
jack_set_xrun_callback(g_jack_global_client, XRun, NULL);
41974242

41984243
#ifdef HAVE_HYLIA
41994244
/* Init hylia */
@@ -8368,6 +8413,16 @@ int effects_aggregated_midi_enable(int enable)
83688413
return SUCCESS;
83698414
}
83708415

8416+
int effects_cpu_load_enable(int enable)
8417+
{
8418+
if (g_jack_global_client == NULL)
8419+
return ERR_INVALID_OPERATION;
8420+
8421+
g_cpu_load_enabled = enable != 0;
8422+
effects_output_data_ready();
8423+
return SUCCESS;
8424+
}
8425+
83718426
int effects_freewheeling_enable(int enable)
83728427
{
83738428
if (g_jack_global_client == NULL)

src/effects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ int effects_state_load(const char *dir);
188188
int effects_state_save(const char *dir);
189189
int effects_state_set_tmpdir(const char *dir);
190190
int effects_aggregated_midi_enable(int enable);
191+
int effects_cpu_load_enable(int enable);
191192
int effects_freewheeling_enable(int enable);
192193
int effects_processing_enable(int enable);
193194
int effects_monitor_audio_levels(const char *source_port_name, int enable);

src/mod-host.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ static void feature_enable(proto_t *proto)
610610

611611
if (!strcmp(feature, "aggregated-midi"))
612612
resp = effects_aggregated_midi_enable(enabled);
613+
else if (!strcmp(feature, "cpu-load"))
614+
resp = effects_cpu_load_enable(enabled);
613615
else if (!strcmp(feature, "freewheeling"))
614616
resp = effects_freewheeling_enable(enabled);
615617
else if (!strcmp(feature, "processing"))

src/rtmempool/rtmempool.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* RealTime Memory Pool, heavily based on work by Nedko Arnaudov
33
* Copyright (C) 2006-2009 Nedko Arnaudov <[email protected]>
4-
* Copyright (C) 2013-2016 Filipe Coelho <[email protected]>
4+
* Copyright (C) 2013-2025 Filipe Coelho <[email protected]>
55
*
66
* This program is free software; you can redistribute it and/or
77
* modify it under the terms of the GNU General Public License as
@@ -87,7 +87,7 @@ bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr,
8787

8888
pthread_mutexattr_t atts;
8989
pthread_mutexattr_init(&atts);
90-
#ifdef __ARM_ARCH_7A__
90+
#ifdef __MOD_DEVICES__
9191
pthread_mutexattr_setprotocol(&atts, PTHREAD_PRIO_INHERIT);
9292
#endif
9393
pthread_mutex_init(&poolPtr->mutex, &atts);

0 commit comments

Comments
 (0)