Commit b787a2f2 authored by Arkadiusz Hiler's avatar Arkadiusz Hiler

lib/core: Print thread:tid with igt_log for non-main threads

So that we know what's the source of messages.

igt_thread.c is created to facilitate more threading-related
functionality that will come in the following patch.
Signed-off-by: default avatarArkadiusz Hiler <arkadiusz.hiler@intel.com>
Reviewed-by: Petri Latvala's avatarPetri Latvala <petri.latvala@intel.com>
parent f0ade50c
......@@ -64,6 +64,8 @@ lib_source_list = \
igt_sysfs.h \
igt_sysrq.c \
igt_sysrq.h \
igt_thread.c \
igt_thread.h \
igt_x86.h \
igt_x86.c \
igt_vec.c \
......
......@@ -72,6 +72,7 @@
#include "igt_rc.h"
#include "igt_list.h"
#include "igt_device_scan.h"
#include "igt_thread.h"
#define UNW_LOCAL_ONLY
#include <libunwind.h>
......@@ -2687,6 +2688,12 @@ void igt_log(const char *domain, enum igt_log_level level, const char *format, .
va_end(args);
}
static pthread_key_t __vlog_line_continuation;
igt_constructor {
pthread_key_create(&__vlog_line_continuation, NULL);
}
/**
* igt_vlog:
* @domain: the log domain, or NULL for no domain
......@@ -2705,6 +2712,7 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
{
FILE *file;
char *line, *formatted_line;
char *thread_id;
const char *program_name;
const char *igt_log_level_str[] = {
"DEBUG",
......@@ -2713,7 +2721,8 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
"CRITICAL",
"NONE"
};
static bool line_continuation = false;
static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
assert(format);
......@@ -2723,23 +2732,37 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
program_name = command_str;
#endif
if (igt_thread_is_main()) {
thread_id = strdup("");
} else {
if (asprintf(&thread_id, "[thread:%d] ", gettid()) == -1)
thread_id = NULL;
}
if (!thread_id)
goto out;
if (list_subtests && level <= IGT_LOG_WARN)
return;
if (vasprintf(&line, format, args) == -1)
return;
if (line_continuation) {
if (pthread_getspecific(__vlog_line_continuation)) {
formatted_line = strdup(line);
if (!formatted_line)
goto out;
} else if (asprintf(&formatted_line, "(%s:%d) %s%s%s: %s", program_name,
getpid(), (domain) ? domain : "", (domain) ? "-" : "",
} else if (asprintf(&formatted_line, "(%s:%d) %s%s%s%s: %s", program_name,
getpid(), thread_id, (domain) ? domain : "", (domain) ? "-" : "",
igt_log_level_str[level], line) == -1) {
goto out;
}
line_continuation = line[strlen(line) - 1] != '\n';
if (line[strlen(line) - 1] == '\n')
pthread_setspecific(__vlog_line_continuation, (void*) false);
else
pthread_setspecific(__vlog_line_continuation, (void*) true);
/* append log buffer */
_igt_log_buffer_append(formatted_line);
......@@ -2758,6 +2781,8 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
goto out;
}
pthread_mutex_lock(&print_mutex);
/* use stderr for warning messages and above */
if (level >= IGT_LOG_WARN) {
file = stderr;
......@@ -2768,13 +2793,18 @@ void igt_vlog(const char *domain, enum igt_log_level level, const char *format,
/* prepend all except information messages with process, domain and log
* level information */
if (level != IGT_LOG_INFO)
if (level != IGT_LOG_INFO) {
fwrite(formatted_line, sizeof(char), strlen(formatted_line),
file);
else
} else {
fwrite(thread_id, sizeof(char), strlen(thread_id), file);
fwrite(line, sizeof(char), strlen(line), file);
}
pthread_mutex_unlock(&print_mutex);
out:
free(thread_id);
free(line);
}
......
/*
* Copyright © 2020 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <pthread.h>
#include "igt_core.h"
#include "igt_thread.h"
static pthread_key_t __igt_is_main_thread;
bool igt_thread_is_main(void)
{
return pthread_getspecific(__igt_is_main_thread) != NULL;
}
igt_constructor {
pthread_key_create(&__igt_is_main_thread, NULL);
pthread_setspecific(__igt_is_main_thread, (void*) 0x1);
}
/*
* Copyright © 2020 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
bool igt_thread_is_main(void);
......@@ -26,6 +26,7 @@ lib_sources = [
'igt_syncobj.c',
'igt_sysfs.c',
'igt_sysrq.c',
'igt_thread.c',
'igt_vec.c',
'igt_vgem.c',
'igt_x86.c',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment