From f0ca9c4cba8fa357582e063ab23f6bc4b27443ff Mon Sep 17 00:00:00 2001 From: johnallen Date: Sun, 14 Jan 2007 17:02:40 +0000 Subject: [PATCH] added thread sleep interval throttling. added hb_log messages to output sleep intervals every 5 seconds. added name char array to fifo, and hb_log print fifo name when fifo_close is called. git-svn-id: svn://svn.handbrake.fr/HandBrake/branches/0.7.3/libhb@111 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- fifo.c | 12 ++++++-- internal.h | 2 +- muxcommon.c | 13 +++++++-- reader.c | 16 +++++++++-- work.c | 81 ++++++++++++++++++++++++++++++++--------------------- 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/fifo.c b/fifo.c index 93e3e162e..5bdcb773a 100644 --- a/fifo.c +++ b/fifo.c @@ -68,14 +68,16 @@ struct hb_fifo_s int size; hb_buffer_t * first; hb_buffer_t * last; + char name[80]; }; -hb_fifo_t * hb_fifo_init( int capacity ) +hb_fifo_t * hb_fifo_init( int capacity, char *name ) { hb_fifo_t * f; f = calloc( sizeof( hb_fifo_t ), 1 ); f->lock = hb_lock_init(); f->capacity = capacity; + strcat(f->name, name); return f; } @@ -127,7 +129,11 @@ hb_buffer_t * hb_fifo_get( hb_fifo_t * f ) b->next = NULL; f->size -= 1; hb_unlock( f->lock ); - + if(f->size > (f->capacity + 5)) + { + hb_log( "hb_fifo_get: %s, capacity %d, size %d", f->name, f->capacity, f->size ); + } + return b; } @@ -194,7 +200,7 @@ void hb_fifo_close( hb_fifo_t ** _f ) hb_fifo_t * f = *_f; hb_buffer_t * b; - hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) ); + hb_log( "fifo_close: %s, trashing %d buffer(s)", f->name, hb_fifo_size( f ) ); while( ( b = hb_fifo_get( f ) ) ) { hb_buffer_close( &b ); diff --git a/internal.h b/internal.h index 36ead1bf0..d75d71e05 100644 --- a/internal.h +++ b/internal.h @@ -54,7 +54,7 @@ void hb_buffer_realloc( hb_buffer_t *, int size ); void hb_buffer_close( hb_buffer_t ** ); -hb_fifo_t * hb_fifo_init(); +hb_fifo_t * hb_fifo_init( int, char * ); int hb_fifo_size( hb_fifo_t * ); int hb_fifo_is_full( hb_fifo_t * ); float hb_fifo_percent_full( hb_fifo_t * f ); diff --git a/muxcommon.c b/muxcommon.c index 70e384e8a..467e14778 100644 --- a/muxcommon.c +++ b/muxcommon.c @@ -135,15 +135,24 @@ static void MuxerFunc( void * _mux ) } int thread_sleep_interval = 50; + time_t last_debug_print = time( NULL ); while( !*job->die && !job->done ) { if( !( track = GetTrack( list ) ) ) { hb_snooze( thread_sleep_interval ); -// thread_sleep_interval += 1; + thread_sleep_interval += 1; + if(getenv( "HB_DEBUG" )) + { + if(time(NULL) >= (last_debug_print + 5)) + { + last_debug_print = time(NULL); + hb_log("%s, thread sleep interval: %d", "muxer", thread_sleep_interval); + } + } continue; } -// thread_sleep_interval = MAX(1, (thread_sleep_interval - 1)); + thread_sleep_interval = MAX(0, (thread_sleep_interval - 1)); buf = hb_fifo_get( track->fifo ); if( job->pass != 1 ) diff --git a/reader.c b/reader.c index 47ec9d546..e1cb0f3d5 100644 --- a/reader.c +++ b/reader.c @@ -54,6 +54,7 @@ static void ReaderFunc( void * _r ) hb_buffer_t * buf; hb_list_t * list; int chapter; + time_t last_debug_print = time( NULL ); if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) ) { @@ -91,7 +92,8 @@ static void ReaderFunc( void * _r ) hb_demux_ps( r->ps, list ); - while( ( buf = hb_list_item( list, 0 ) ) ) + int thread_sleep_interval = 50; + while( ( buf = hb_list_item( list, 0 ) ) ) { hb_list_rem( list, buf ); fifo = GetFifoForId( r->job, buf->id ); @@ -100,8 +102,18 @@ static void ReaderFunc( void * _r ) while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) ) { - hb_snooze( 50 ); + hb_snooze( thread_sleep_interval ); + thread_sleep_interval += 1; + if(getenv( "HB_DEBUG" )) + { + if(time(NULL) >= (last_debug_print +5)) + { + last_debug_print = time(NULL); + hb_log("%s, thread sleep interval: %d", "reader", thread_sleep_interval); + } + } } + thread_sleep_interval = MAX(0, (thread_sleep_interval - 1)); hb_fifo_push( fifo, buf ); } else diff --git a/work.c b/work.c index 6529e2fbb..127ff8315 100644 --- a/work.c +++ b/work.c @@ -120,11 +120,11 @@ static void do_job( hb_job_t * job, int cpu_count ) job->vbitrate, job->pass ); } - job->fifo_mpeg2 = hb_fifo_init( 2048 ); - job->fifo_raw = hb_fifo_init( 8 ); - job->fifo_sync = hb_fifo_init( 8 ); - job->fifo_render = hb_fifo_init( 8 ); - job->fifo_mpeg4 = hb_fifo_init( 8 ); + job->fifo_mpeg2 = hb_fifo_init( 2048, "mpeg2" ); + job->fifo_raw = hb_fifo_init( 8, "raw" ); + job->fifo_sync = hb_fifo_init( 8, "sync" ); + job->fifo_render = hb_fifo_init( 8, "render" ); + job->fifo_mpeg4 = hb_fifo_init( 8, "mpeg4" ); /* Synchronization */ hb_list_add( job->list_work, ( w = getWork( WORK_SYNC ) ) ); @@ -167,8 +167,8 @@ static void do_job( hb_job_t * job, int cpu_count ) { hb_log( " + subtitle %x, %s", subtitle->id, subtitle->lang ); - subtitle->fifo_in = hb_fifo_init( 8 ); - subtitle->fifo_raw = hb_fifo_init( 8 ); + subtitle->fifo_in = hb_fifo_init( 8, "subtitle in" ); + subtitle->fifo_raw = hb_fifo_init( 8, "subtitle raw" ); hb_list_add( job->list_work, ( w = getWork( WORK_DECSUB ) ) ); w->fifo_in = subtitle->fifo_in; @@ -192,10 +192,10 @@ static void do_job( hb_job_t * job, int cpu_count ) audio = hb_list_item( title->list_audio, i ); hb_log( " + %x, %s", audio->id, audio->lang ); - audio->fifo_in = hb_fifo_init( 2048 ); - audio->fifo_raw = hb_fifo_init( 8 ); - audio->fifo_sync = hb_fifo_init( 8 ); - audio->fifo_out = hb_fifo_init( 8 ); + audio->fifo_in = hb_fifo_init( 2048, "audio in" ); + audio->fifo_raw = hb_fifo_init( 8, "audio raw" ); + audio->fifo_sync = hb_fifo_init( 8 , "audio sync" ); + audio->fifo_out = hb_fifo_init( 8, "audio out" ); switch( audio->codec ) { @@ -247,15 +247,15 @@ static void do_job( hb_job_t * job, int cpu_count ) { w = hb_list_item( job->list_work, i ); w->done = &job->done; - w->thread_sleep_interval = 10; + w->thread_sleep_interval = 1000; w->init( w, job ); - w->thread = hb_thread_init( w->name, work_loop, w, - HB_LOW_PRIORITY ); + w->thread = hb_thread_init( w->name, work_loop, w, HB_LOW_PRIORITY ); } done = 0; w = hb_list_item( job->list_work, 0 ); - w->thread_sleep_interval = 50; + w->thread_sleep_interval = 1; + time_t last_debug_print = time( NULL ); w->init( w, job ); while( !*job->die ) { @@ -271,6 +271,21 @@ static void do_job( hb_job_t * job, int cpu_count ) break; } hb_snooze( w->thread_sleep_interval ); + + if(getenv( "HB_DEBUG" )) + { + if(time(NULL) >= (last_debug_print + 5)) + { + int i; + hb_work_object_t * w; + last_debug_print = time(NULL); + for( i = 0; i < hb_list_count( job->list_work ); i++ ) + { + w = hb_list_item( job->list_work, i ); + hb_log("%s, thread sleep interval: %d", w->name, w->thread_sleep_interval); + } + } + } } hb_list_rem( job->list_work, w ); w->close( w ); @@ -327,24 +342,26 @@ static void work_loop( void * _w ) hb_lock( job->pause ); hb_unlock( job->pause ); #endif - if( hb_fifo_is_full( w->fifo_out ) || -// if( (hb_fifo_percent_full( w->fifo_out ) > 0.8) || - !( buf_in = hb_fifo_get( w->fifo_in ) ) ) - { + if( hb_fifo_is_full( w->fifo_out )) + { + w->thread_sleep_interval += 1; hb_snooze( w->thread_sleep_interval ); -// w->thread_sleep_interval += 1; continue; - } -// w->thread_sleep_interval = MAX(1, (w->thread_sleep_interval - 1)); - - w->work( w, &buf_in, &buf_out ); - if( buf_in ) - { - hb_buffer_close( &buf_in ); - } - if( buf_out ) - { - hb_fifo_push( w->fifo_out, buf_out ); - } + } + w->thread_sleep_interval = MAX(0, (w->thread_sleep_interval - 1)); + + buf_in = hb_fifo_get( w->fifo_in ); + if(buf_in) + { + w->work( w, &buf_in, &buf_out ); + if( buf_in ) + { + hb_buffer_close( &buf_in ); + } + if( buf_out ) + { + hb_fifo_push( w->fifo_out, buf_out ); + } + } } } -- 2.40.0