Преглед изворни кода

libobs: Add profiler calls

Palana пре 10 година
родитељ
комит
cf6b75e067
6 измењених фајлова са 125 додато и 6 уклоњено
  1. 33 3
      libobs/obs-encoder.c
  2. 11 0
      libobs/obs-hotkey.c
  3. 3 0
      libobs/obs-internal.h
  4. 9 0
      libobs/obs-module.c
  5. 65 3
      libobs/obs-video.c
  6. 4 0
      libobs/obs.c

+ 33 - 3
libobs/obs-encoder.c

@@ -617,9 +617,16 @@ static void full_stop(struct obs_encoder *encoder)
 	}
 }
 
+static const char *do_encode_name = "do_encode";
 static inline void do_encode(struct obs_encoder *encoder,
 		struct encoder_frame *frame)
 {
+	profile_start(do_encode_name);
+	if (!encoder->profile_encoder_encode_name)
+		encoder->profile_encoder_encode_name =
+			profile_store_name(obs_get_profiler_name_store(),
+					"encode(%s)", encoder->context.name);
+
 	struct encoder_packet pkt = {0};
 	bool received = false;
 	bool success;
@@ -628,8 +635,10 @@ static inline void do_encode(struct obs_encoder *encoder,
 	pkt.timebase_den = encoder->timebase_den;
 	pkt.encoder = encoder;
 
+	profile_start(encoder->profile_encoder_encode_name);
 	success = encoder->info.encode(encoder->context.data, frame, &pkt,
 			&received);
+	profile_end(encoder->profile_encoder_encode_name);
 	if (!success) {
 		full_stop(encoder);
 		blog(LOG_ERROR, "Error encoding with encoder '%s'",
@@ -652,10 +661,15 @@ static inline void do_encode(struct obs_encoder *encoder,
 
 		pthread_mutex_unlock(&encoder->callbacks_mutex);
 	}
+
+	profile_end(do_encode_name);
 }
 
+static const char *receive_video_name = "receive_video";
 static void receive_video(void *param, struct video_data *frame)
 {
+	profile_start(receive_video_name);
+
 	struct obs_encoder    *encoder  = param;
 	struct encoder_frame  enc_frame;
 
@@ -675,10 +689,15 @@ static void receive_video(void *param, struct video_data *frame)
 	do_encode(encoder, &enc_frame);
 
 	encoder->cur_pts += encoder->timebase_num;
+
+	profile_end(receive_video_name);
 }
 
+static const char *buffer_audio_name = "buffer_audio";
 static bool buffer_audio(struct obs_encoder *encoder, struct audio_data *data)
 {
+	profile_start(buffer_audio_name);
+
 	size_t samplerate = encoder->samplerate;
 	size_t size = data->frames * encoder->blocksize;
 	size_t offset_size = 0;
@@ -689,13 +708,13 @@ static bool buffer_audio(struct obs_encoder *encoder, struct audio_data *data)
 
 		/* no video yet, so don't start audio */
 		if (!v_start_ts)
-			return false;
+			goto fail;
 
 		/* audio starting point still not synced with video starting
 		 * point, so don't start audio */
 		end_ts += (uint64_t)data->frames * 1000000000ULL / samplerate;
 		if (end_ts <= v_start_ts)
-			return false;
+			goto fail;
 
 		/* ready to start audio, truncate if necessary */
 		if (data->timestamp < v_start_ts) {
@@ -718,7 +737,12 @@ static bool buffer_audio(struct obs_encoder *encoder, struct audio_data *data)
 			circlebuf_push_back(&encoder->audio_input_buffer[i],
 					data->data[i] + offset_size, size);
 
+	profile_end(buffer_audio_name);
 	return true;
+
+fail:
+	profile_end(buffer_audio_name);
+	return false;
 }
 
 static void send_audio_data(struct obs_encoder *encoder)
@@ -744,17 +768,23 @@ static void send_audio_data(struct obs_encoder *encoder)
 	encoder->cur_pts += encoder->framesize;
 }
 
+static const char *receive_audio_name = "receive_audio";
 static void receive_audio(void *param, size_t mix_idx, struct audio_data *data)
 {
+	profile_start(receive_audio_name);
+
 	struct obs_encoder *encoder = param;
 
 	if (!buffer_audio(encoder, data))
-		return;
+		goto end;
 
 	while (encoder->audio_input_buffer[0].size >= encoder->framesize_bytes)
 		send_audio_data(encoder);
 
 	UNUSED_PARAMETER(mix_idx);
+
+end:
+	profile_end(receive_audio_name);
 }
 
 void obs_encoder_add_output(struct obs_encoder *encoder,

+ 11 - 0
libobs/obs-hotkey.c

@@ -1291,12 +1291,23 @@ static inline void query_hotkeys()
 void *obs_hotkey_thread(void *arg)
 {
 	UNUSED_PARAMETER(arg);
+
+	const char *hotkey_thread_name =
+		profile_store_name(obs_get_profiler_name_store(),
+				"obs_hotkey_thread(%g ms)", 25.);
+	profile_register_root(hotkey_thread_name, (uint64_t)25000000);
+
 	while (os_event_timedwait(obs->hotkeys.stop_event, 25) == ETIMEDOUT) {
 		if (!lock())
 			continue;
 
+		profile_start(hotkey_thread_name);
 		query_hotkeys();
+		profile_end(hotkey_thread_name);
+
 		unlock();
+
+		profile_reenable_thread();
 	}
 	return NULL;
 }

+ 3 - 0
libobs/obs-internal.h

@@ -23,6 +23,7 @@
 #include "util/dstr.h"
 #include "util/threading.h"
 #include "util/platform.h"
+#include "util/profiler.h"
 #include "callback/signal.h"
 #include "callback/proc.h"
 
@@ -679,6 +680,8 @@ struct obs_encoder {
 
 	pthread_mutex_t                 callbacks_mutex;
 	DARRAY(struct encoder_callback) callbacks;
+
+	const char                      *profile_encoder_encode_name;
 };
 
 extern struct obs_encoder_info *find_encoder(const char *id);

+ 9 - 0
libobs/obs-module.c

@@ -103,11 +103,17 @@ bool obs_init_module(obs_module_t *module)
 	if (module->loaded)
 		return true;
 
+	const char *profile_name =
+		profile_store_name(obs_get_profiler_name_store(),
+				"obs_init_module(%s)", module->file);
+	profile_start(profile_name);
+
 	module->loaded = module->load();
 	if (!module->loaded)
 		blog(LOG_WARNING, "Failed to initialize module '%s'",
 				module->file);
 
+	profile_end(profile_name);
 	return module->loaded;
 }
 
@@ -185,9 +191,12 @@ static void load_all_callback(void *param, const struct obs_module_info *info)
 	UNUSED_PARAMETER(param);
 }
 
+static const char *obs_load_all_modules_name = "obs_load_all_modules";
 void obs_load_all_modules(void)
 {
+	profile_start(obs_load_all_modules_name);
 	obs_find_modules(load_all_callback, NULL);
+	profile_end(obs_load_all_modules_name);
 }
 
 static inline void make_data_dir(struct dstr *parsed_data_dir,

+ 65 - 3
libobs/obs-video.c

@@ -133,9 +133,12 @@ static inline void unmap_last_surface(struct obs_core_video *video)
 	}
 }
 
+static const char *render_main_texture_name = "render_main_texture";
 static inline void render_main_texture(struct obs_core_video *video,
 		int cur_texture)
 {
+	profile_start(render_main_texture_name);
+
 	struct vec4 clear_color;
 	vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 1.0f);
 
@@ -146,6 +149,8 @@ static inline void render_main_texture(struct obs_core_video *video,
 	obs_view_render(&obs->data.main_view);
 
 	video->textures_rendered[cur_texture] = true;
+
+	profile_end(render_main_texture_name);
 }
 
 static inline gs_effect_t *get_scale_effect_internal(
@@ -194,9 +199,12 @@ static inline gs_effect_t *get_scale_effect(struct obs_core_video *video,
 	}
 }
 
+static const char *render_output_texture_name = "render_output_texture";
 static inline void render_output_texture(struct obs_core_video *video,
 		int cur_texture, int prev_texture)
 {
+	profile_start(render_output_texture_name);
+
 	gs_texture_t *texture = video->render_textures[prev_texture];
 	gs_texture_t *target  = video->output_textures[cur_texture];
 	uint32_t     width   = gs_texture_get_width(target);
@@ -217,7 +225,7 @@ static inline void render_output_texture(struct obs_core_video *video,
 	size_t      passes, i;
 
 	if (!video->textures_rendered[prev_texture])
-		return;
+		goto end;
 
 	gs_set_render_target(target, NULL);
 	set_render_size(width, height);
@@ -239,6 +247,9 @@ static inline void render_output_texture(struct obs_core_video *video,
 	gs_enable_blending(true);
 
 	video->textures_output[cur_texture] = true;
+
+end:
+	profile_end(render_output_texture_name);
 }
 
 static inline void set_eparam(gs_effect_t *effect, const char *name, float val)
@@ -247,9 +258,12 @@ static inline void set_eparam(gs_effect_t *effect, const char *name, float val)
 	gs_effect_set_float(param, val);
 }
 
+static const char *render_convert_texture_name = "render_convert_texture";
 static void render_convert_texture(struct obs_core_video *video,
 		int cur_texture, int prev_texture)
 {
+	profile_start(render_convert_texture_name);
+
 	gs_texture_t *texture = video->output_textures[prev_texture];
 	gs_texture_t *target  = video->convert_textures[cur_texture];
 	float        fwidth  = (float)video->output_width;
@@ -262,7 +276,7 @@ static void render_convert_texture(struct obs_core_video *video,
 			video->conversion_tech);
 
 	if (!video->textures_output[prev_texture])
-		return;
+		goto end;
 
 	set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
 	set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
@@ -293,11 +307,17 @@ static void render_convert_texture(struct obs_core_video *video,
 	gs_enable_blending(true);
 
 	video->textures_converted[cur_texture] = true;
+
+end:
+	profile_end(render_convert_texture_name);
 }
 
+static const char *stage_output_texture_name = "stage_output_texture";
 static inline void stage_output_texture(struct obs_core_video *video,
 		int cur_texture, int prev_texture)
 {
+	profile_start(stage_output_texture_name);
+
 	gs_texture_t   *texture;
 	bool        texture_ready;
 	gs_stagesurf_t *copy = video->copy_surfaces[cur_texture];
@@ -313,11 +333,14 @@ static inline void stage_output_texture(struct obs_core_video *video,
 	unmap_last_surface(video);
 
 	if (!texture_ready)
-		return;
+		goto end;
 
 	gs_stage_texture(copy, texture);
 
 	video->textures_copied[cur_texture] = true;
+
+end:
+	profile_end(stage_output_texture_name);
 }
 
 static inline void render_video(struct obs_core_video *video, int cur_texture,
@@ -530,6 +553,11 @@ static inline void video_sleep(struct obs_core_video *video,
 			sizeof(vframe_info));
 }
 
+static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
+static const char *output_frame_render_video_name = "render_video";
+static const char *output_frame_download_frame_name = "download_frame";
+static const char *output_frame_gs_flush_name = "gs_flush";
+static const char *output_frame_output_video_data_name = "output_video_data";
 static inline void output_frame(void)
 {
 	struct obs_core_video *video = &obs->video;
@@ -540,11 +568,23 @@ static inline void output_frame(void)
 
 	memset(&frame, 0, sizeof(struct video_data));
 
+	profile_start(output_frame_gs_context_name);
 	gs_enter_context(video->graphics);
+
+	profile_start(output_frame_render_video_name);
 	render_video(video, cur_texture, prev_texture);
+	profile_end(output_frame_render_video_name);
+
+	profile_start(output_frame_download_frame_name);
 	frame_ready = download_frame(video, prev_texture, &frame);
+	profile_end(output_frame_download_frame_name);
+
+	profile_start(output_frame_gs_flush_name);
 	gs_flush();
+	profile_end(output_frame_gs_flush_name);
+
 	gs_leave_context();
+	profile_end(output_frame_gs_context_name);
 
 	if (frame_ready) {
 		struct obs_vframe_info vframe_info;
@@ -552,13 +592,18 @@ static inline void output_frame(void)
 				sizeof(vframe_info));
 
 		frame.timestamp = vframe_info.timestamp;
+		profile_start(output_frame_output_video_data_name);
 		output_video_data(video, &frame, vframe_info.count);
+		profile_end(output_frame_output_video_data_name);
 	}
 
 	if (++video->cur_texture == NUM_TEXTURES)
 		video->cur_texture = 0;
 }
 
+static const char *tick_sources_name = "tick_sources";
+static const char *render_displays_name = "render_displays";
+static const char *output_frame_name = "output_frame";
 void *obs_video_thread(void *param)
 {
 	uint64_t last_time = 0;
@@ -568,12 +613,29 @@ void *obs_video_thread(void *param)
 
 	os_set_thread_name("libobs: graphics thread");
 
+	const char *video_thread_name =
+		profile_store_name(obs_get_profiler_name_store(),
+			"obs_video_thread(%g ms)", interval / 1000000.);
+	profile_register_root(video_thread_name, interval);
+
 	while (!video_output_stopped(obs->video.video)) {
+		profile_start(video_thread_name);
+
+		profile_start(tick_sources_name);
 		last_time = tick_sources(obs->video.video_time, last_time);
+		profile_end(tick_sources_name);
 
+		profile_start(render_displays_name);
 		render_displays();
+		profile_end(render_displays_name);
 
+		profile_start(output_frame_name);
 		output_frame();
+		profile_end(output_frame_name);
+
+		profile_end(video_thread_name);
+
+		profile_reenable_thread();
 
 		video_sleep(&obs->video, &obs->video.video_time, interval);
 	}

+ 4 - 0
libobs/obs.c

@@ -717,10 +717,13 @@ static bool obs_init(const char *locale, profiler_name_store_t *store)
 extern void initialize_crash_handler(void);
 #endif
 
+static const char *obs_startup_name = "obs_startup";
 bool obs_startup(const char *locale, profiler_name_store_t *store)
 {
 	bool success;
 
+	profile_start(obs_startup_name);
+
 	if (obs) {
 		blog(LOG_WARNING, "Tried to call obs_startup more than once");
 		return false;
@@ -731,6 +734,7 @@ bool obs_startup(const char *locale, profiler_name_store_t *store)
 #endif
 
 	success = obs_init(locale, store);
+	profile_end(obs_startup_name);
 	if (!success)
 		obs_shutdown();