]> granicus.if.org Git - handbrake/commitdiff
added thread sleep interval throttling.
authorjohnallen <johnallenemail@gmail.com>
Sun, 14 Jan 2007 17:02:40 +0000 (17:02 +0000)
committerjohnallen <johnallenemail@gmail.com>
Sun, 14 Jan 2007 17:02:40 +0000 (17:02 +0000)
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
internal.h
muxcommon.c
reader.c
work.c

diff --git a/fifo.c b/fifo.c
index 93e3e162ea4e880fd3e7947aaed4bd3e2aab8643..5bdcb773a3385454e5a5841ce2a5971ea50e9788 100644 (file)
--- 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 );
index 36ead1bf0e40a41aa6afa966478d8e94ecf08d33..d75d71e05ccea388ee416fd82d160390705f429d 100644 (file)
@@ -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 );
index 70e384e8a707e6dc5f39e82f2bf3c3581d8830b3..467e14778435c1159e1e1a8e1cd51733d77d03e3 100644 (file)
@@ -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 )
index 47ec9d5469090a3d13f7f33016315d20a8662f33..e1cb0f3d5024c087cf3edbda47811c50c6b14fda 100644 (file)
--- 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 6529e2fbb5b2fbd2df1729f0bb814e9768e3f8e2..127ff8315448641a4d43b62171a8ea86e499fc89 100644 (file)
--- 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 );
+                       }
+               }
     }
 }