[FFmpeg-devel] [PATCH 1/3] libavutil: prefix log messages with thread name
Michael Niedermayer
michaelni at gmx.at
Wed Jul 25 00:05:10 CEST 2012
On Wed, Jul 18, 2012 at 11:22:19AM -0400, Martin Carroll wrote:
> To make it easier to see which thread is doing what, the logger can
> now be configured to prefix all log messages with the name of the
> calling thread. The name is printed if the thread was previously
> registered with av_log_set_threadname(). To enable this feature,
> the variable print_threadname in libavutil/log.c must be set to 1.
>
> Signed-off-by: Martin Carroll <martin.carroll at alcatel-lucent.com>
> ---
> libavutil/log.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++------
> libavutil/log.h | 11 ++++++
> 2 files changed, 104 insertions(+), 11 deletions(-)
>
> diff --git a/libavutil/log.c b/libavutil/log.c
> index 53c756b..bee612f 100644
> --- a/libavutil/log.c
> +++ b/libavutil/log.c
> @@ -30,11 +30,34 @@
> #include <unistd.h>
> #endif
> #include <stdlib.h>
> +#include <pthread.h>
> #include "avutil.h"
> #include "log.h"
>
> #define LINE_SZ 1024
>
> +#define MAX_THREAD_INFOS 10
a single normal transcoding on a modern desktop might use
30+ threads through a single multithreaded encoder & decoder
considering the possibility of multiple encoders and decoders
a few hundread can surely end up existing at the same time ...
> +#define MAX_THREAD_NAMELEN 128
> +
> +/*
> + * we do *not* use thread-specific data to implement this
non doxygen compatible comment
> + * data structure, because we want the creat*ing* thread to
> + * be able to set the creat*ed* thread's name, and implementing
maybe iam missing something but why do we want the creating thread
to set it ?
> + * that functionality is easier with a globally accessed (and
> + * properly locked) table.
> + */
> +typedef struct {
> + pthread_t pid;
> + char name[MAX_THREAD_NAMELEN];
> + void *fcn;
> + int instance_num_of_fcn;
> + int sole_instance_of_fcn;
> +} Thread_info;
> +
> +static pthread_rwlock_t thread_info_rwlock = PTHREAD_RWLOCK_INITIALIZER;
> +static Thread_info thread_info[MAX_THREAD_INFOS];
> +static int num_thread_infos = 0;
> +static int print_threadname = 0; /* set to 1 to prefix all log messages with name of thread */
> static int av_log_level = AV_LOG_INFO;
> static int flags;
>
> @@ -163,11 +186,53 @@ static int get_category(void *ptr){
> return avc->category + 16;
> }
>
> +void av_log_set_threadname(pthread_t pid, const char *name, void *fcn)
> +{
> + pthread_rwlock_wrlock(&thread_info_rwlock);
tabs are forbidden in ffmpeg git
> + if (num_thread_infos < MAX_THREAD_INFOS) {
> + Thread_info *info;
> + int instance_num_of_fcn = 0;
> + int sole_instance_of_fcn = 1;
> + int i;
> +
> + for (i = 0; i < num_thread_infos; ++i) {
> + info = &thread_info[i];
> + if (info->fcn == fcn) {
> + info->sole_instance_of_fcn = 0;
> + sole_instance_of_fcn = 0;
> + ++instance_num_of_fcn;
> + }
> + }
> + info = &thread_info[num_thread_infos];
> + info->pid = pid;
> + info->fcn = fcn;
> + info->instance_num_of_fcn = instance_num_of_fcn;
> + info->sole_instance_of_fcn = sole_instance_of_fcn;
> + snprintf(info->name, MAX_THREAD_NAMELEN, "%s", name);
> + info->name[MAX_THREAD_NAMELEN-1] = 0;
> + ++num_thread_infos;
> + }
> + pthread_rwlock_unlock(&thread_info_rwlock);
> +}
Thats just a function to add a thread, threads can be removed as
well, in a a multi thrededed converter that uses libavcodec there
could be millions of threads created and destroyed over its lifetime
just by converting a large directory of mp3 files ...
> +
> +static Thread_info* lookup_thread_info(void)
> +{
> + int i;
> +
> + pthread_t pid = pthread_self();
> + for (i = 0; i < num_thread_infos; ++i) {
> + Thread_info* info = &thread_info[i];
> + if (pthread_equal(info->pid, pid))
> + return info;
> + }
this is O(n) where TLS could be O(1), or in other words this doesnt
scale well if the number of threads would be large
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Good people do not need laws to tell them to act responsibly, while bad
people will find a way around the laws. -- Plato
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120725/a2c21830/attachment.asc>
More information about the ffmpeg-devel
mailing list