summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjohnallen <[email protected]>2007-01-08 03:18:40 +0000
committerjohnallen <[email protected]>2007-01-08 03:18:40 +0000
commit18189c197f5d5e04e8e8cd37de0caedb1a469a38 (patch)
treeb0fbfe562fd5d8c371033d316fe455eb89c3eb45
parentdc22ce2c46ee62a46963d92ccb4a7fc26bd21d26 (diff)
added worker thread sleep throttling.
each of the work object threads now self adjust their sleep interval based on the "fullness" of their fifo. 80% is the choose threshold. Work objects with a fifo fullness of greater than 80% increase their sleep interval. This allows other work object with less than 80% fullness more CPU usage. Also adjusted thread_func, reader, and muxer sleep intervals to more reasonable values. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@98 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--libhb/common.h1
-rw-r--r--libhb/fifo.c11
-rw-r--r--libhb/hb.c4
-rw-r--r--libhb/internal.h1
-rw-r--r--libhb/muxcommon.c14
-rw-r--r--libhb/reader.c3
-rw-r--r--libhb/work.c19
-rw-r--r--macosx/HandBrake.xcodeproj/project.pbxproj2
-rw-r--r--test/test.c2
9 files changed, 43 insertions, 14 deletions
diff --git a/libhb/common.h b/libhb/common.h
index 0e53dde24..859711f81 100644
--- a/libhb/common.h
+++ b/libhb/common.h
@@ -352,6 +352,7 @@ struct hb_work_object_s
volatile int * done;
hb_work_object_t * next;
+ int thread_sleep_interval;
#endif
};
diff --git a/libhb/fifo.c b/libhb/fifo.c
index acdb21188..93e3e162e 100644
--- a/libhb/fifo.c
+++ b/libhb/fifo.c
@@ -101,6 +101,17 @@ int hb_fifo_is_full( hb_fifo_t * f )
return ret;
}
+float hb_fifo_percent_full( hb_fifo_t * f )
+{
+ float ret;
+
+ hb_lock( f->lock );
+ ret = f->size / f->capacity;
+ hb_unlock( f->lock );
+
+ return ret;
+}
+
hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
{
hb_buffer_t * b;
diff --git a/libhb/hb.c b/libhb/hb.c
index 28f3245cb..977e3dc71 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -98,7 +98,7 @@ hb_handle_t * hb_init_real( int verbose, int update_check )
"update_thread" );
break;
}
- hb_snooze( 50 );
+ hb_snooze( 500 );
}
}
@@ -664,7 +664,7 @@ static void thread_func( void * _h )
hb_unlock( h->state_lock );
}
- hb_snooze( 50 );
+ hb_snooze( 1000 );
}
if( h->work_thread )
diff --git a/libhb/internal.h b/libhb/internal.h
index e13008f9b..36ead1bf0 100644
--- a/libhb/internal.h
+++ b/libhb/internal.h
@@ -57,6 +57,7 @@ void hb_buffer_close( hb_buffer_t ** );
hb_fifo_t * hb_fifo_init();
int hb_fifo_size( hb_fifo_t * );
int hb_fifo_is_full( hb_fifo_t * );
+float hb_fifo_percent_full( hb_fifo_t * f );
hb_buffer_t * hb_fifo_get( hb_fifo_t * );
hb_buffer_t * hb_fifo_see( hb_fifo_t * );
hb_buffer_t * hb_fifo_see2( hb_fifo_t * );
diff --git a/libhb/muxcommon.c b/libhb/muxcommon.c
index f1723ccf8..99fe6fc91 100644
--- a/libhb/muxcommon.c
+++ b/libhb/muxcommon.c
@@ -108,7 +108,7 @@ static void MuxerFunc( void * _mux )
break;
}
- hb_snooze( 50 );
+ hb_snooze( 200 );
}
/* Create file, write headers */
@@ -134,13 +134,21 @@ static void MuxerFunc( void * _mux )
hb_list_add( list, track );
}
- while( !*job->die && !job->done )
+ int thread_sleep_interval = 50;
+ while( !*job->die && !job->done )
{
if( !( track = GetTrack( list ) ) )
{
- hb_snooze( 50 );
+ hb_snooze( thread_sleep_interval );
+ thread_sleep_interval += 1;
continue;
}
+ thread_sleep_interval = MAX(1, (thread_sleep_interval - 1));
+#if 0
+ if ((thread_sleep_interval <= 1) || (thread_sleep_interval > 100)) {
+ hb_log("%s: %d", "Muxer", thread_sleep_interval);
+ }
+#endif
buf = hb_fifo_get( track->fifo );
if( job->pass != 1 )
diff --git a/libhb/reader.c b/libhb/reader.c
index ed4a675ff..47ec9d546 100644
--- a/libhb/reader.c
+++ b/libhb/reader.c
@@ -100,8 +100,7 @@ static void ReaderFunc( void * _r )
while( !*r->die && !r->job->done &&
hb_fifo_is_full( fifo ) )
{
- hb_snooze( 1 );
- //hb_log("sleep: ReaderFunc");
+ hb_snooze( 50 );
}
hb_fifo_push( fifo, buf );
}
diff --git a/libhb/work.c b/libhb/work.c
index 38346f80a..d1f2aaf5c 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -40,7 +40,7 @@ hb_thread_t * hb_work_init( hb_list_t * jobs, int cpu_count,
}
/**
- * Interates through job list and calls do_job for each job.
+ * Iterates through job list and calls do_job for each job.
* @param _work Handle work object.
*/
static void work_func( void * _work )
@@ -247,6 +247,7 @@ 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->init( w, job );
w->thread = hb_thread_init( w->name, work_loop, w,
HB_LOW_PRIORITY );
@@ -254,6 +255,7 @@ static void do_job( hb_job_t * job, int cpu_count )
done = 0;
w = hb_list_item( job->list_work, 0 );
+ w->thread_sleep_interval = 50;
w->init( w, job );
while( !*job->die )
{
@@ -268,7 +270,7 @@ static void do_job( hb_job_t * job, int cpu_count )
{
break;
}
- hb_snooze( 50 );
+ hb_snooze( w->thread_sleep_interval );
}
hb_list_rem( job->list_work, w );
w->close( w );
@@ -325,13 +327,20 @@ 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_is_full( w->fifo_out ) ||
+ if( (hb_fifo_percent_full( w->fifo_out ) > 0.8) ||
!( buf_in = hb_fifo_get( w->fifo_in ) ) )
{
- hb_snooze( 50 );
+ hb_snooze( w->thread_sleep_interval );
+ w->thread_sleep_interval += 1;
continue;
}
-
+ w->thread_sleep_interval = MAX(1, (w->thread_sleep_interval - 1));
+#if 0
+ if ((w->thread_sleep_interval <= 1) || (w->thread_sleep_interval > 100)) {
+ hb_log("%s: %d", w->name, w->thread_sleep_interval);
+ }
+#endif
w->work( w, &buf_in, &buf_out );
if( buf_in )
{
diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj
index b77f580b7..2ba0a1ba3 100644
--- a/macosx/HandBrake.xcodeproj/project.pbxproj
+++ b/macosx/HandBrake.xcodeproj/project.pbxproj
@@ -49,7 +49,7 @@
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
4D1125D709D72FD200E0657B /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = /usr/lib/libz.dylib; sourceTree = "<absolute>"; };
4D118405053054CD00C39CA9 /* HandBrake.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = HandBrake.icns; sourceTree = "<group>"; };
- 4D1EA2DA0993B01000FDC1A2 /* Instant HandBrake.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Instant HandBrake.app"; sourceTree = BUILT_PRODUCTS_DIR; };
+ 4D1EA2DA0993B01000FDC1A2 /* Instant HandBrake.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = "Instant HandBrake.app"; sourceTree = BUILT_PRODUCTS_DIR; };
4D1EA2DC0993B01000FDC1A2 /* Express.plist */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = Express.plist; sourceTree = "<group>"; };
4D1EA3000993B13700FDC1A2 /* Express.nib */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = Express.nib; path = English.lproj/Express.nib; sourceTree = "<group>"; };
4D1EA31A0993B24700FDC1A2 /* ExpressController.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = ExpressController.h; sourceTree = "<group>"; };
diff --git a/test/test.c b/test/test.c
index faf994c97..dc66aa6f4 100644
--- a/test/test.c
+++ b/test/test.c
@@ -158,9 +158,9 @@ int main( int argc, char ** argv )
}
}
}
+ hb_snooze( 200 );
#else
hb_snooze( 200 );
- //hb_log("sleep: HBTest");
#endif
HandleEvents( h );