From cecce7e9e423b77b2a4c32e94e35ffccf85b4216 Mon Sep 17 00:00:00 2001 From: Dengke Tang Date: Mon, 10 Mar 2025 17:49:49 +0000 Subject: [PATCH] add lot of printfs --- include/aws/common/private/thread_shared.h | 1 + source/common.c | 2 +- source/log_writer.c | 2 + source/posix/cross_process_lock.c | 1 - source/posix/thread.c | 2 + source/thread_shared.c | 65 ++++++++++++++++++++++ 6 files changed, 71 insertions(+), 2 deletions(-) diff --git a/include/aws/common/private/thread_shared.h b/include/aws/common/private/thread_shared.h index 19e648319..e91205f12 100644 --- a/include/aws/common/private/thread_shared.h +++ b/include/aws/common/private/thread_shared.h @@ -40,5 +40,6 @@ AWS_COMMON_API size_t aws_thread_get_managed_thread_count(void); * The handler before fork in the parent process. */ void aws_pthread_atfork_on_fork_prepare(void); +void aws_pthread_atfork_after_fork_child(void); #endif /* AWS_COMMON_PRIVATE_THREAD_SHARED_H */ diff --git a/source/common.c b/source/common.c index eed29174b..f292cecc3 100644 --- a/source/common.c +++ b/source/common.c @@ -392,7 +392,7 @@ void aws_common_library_init(struct aws_allocator *allocator) { #endif #ifndef AWS_OS_WINDOWS - if (pthread_atfork(aws_pthread_atfork_on_fork_prepare, NULL, NULL) != 0) { + if (pthread_atfork(aws_pthread_atfork_on_fork_prepare, NULL, aws_pthread_atfork_after_fork_child) != 0) { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: failed to register pthread at fork"); } #endif diff --git a/source/log_writer.c b/source/log_writer.c index 9f5090627..da752509b 100644 --- a/source/log_writer.c +++ b/source/log_writer.c @@ -24,12 +24,14 @@ struct aws_file_writer { static int s_aws_file_writer_write(struct aws_log_writer *writer, const struct aws_string *output) { struct aws_file_writer *impl = (struct aws_file_writer *)writer->impl; + // printf("log discriptor is %d \n", fileno(impl->log_file)); size_t length = output->len; if (fwrite(output->bytes, 1, length, impl->log_file) < length) { int errno_value = ferror(impl->log_file) ? errno : 0; /* Always cache errno before potential side-effect */ return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_WRITE_FAILURE); } + fflush(impl->log_file); return AWS_OP_SUCCESS; } diff --git a/source/posix/cross_process_lock.c b/source/posix/cross_process_lock.c index 1ef5d2b5f..f94834184 100644 --- a/source/posix/cross_process_lock.c +++ b/source/posix/cross_process_lock.c @@ -133,7 +133,6 @@ struct aws_cross_process_lock *aws_cross_process_lock_try_acquire( void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) { if (instance_lock) { - flock(instance_lock->locked_fd, LOCK_UN); close(instance_lock->locked_fd); AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock file released for fd %d", instance_lock->locked_fd); aws_mem_release(instance_lock->allocator, instance_lock); diff --git a/source/posix/thread.c b/source/posix/thread.c index 34b5dbe94..2a54a152c 100644 --- a/source/posix/thread.c +++ b/source/posix/thread.c @@ -350,6 +350,7 @@ int aws_thread_launch( } attr_return = pthread_create(&thread->thread_id, attributes_ptr, thread_fn, (void *)wrapper); + printf("Created Thread ID %d:\n", (int)thread->thread_id); if (attr_return) { AWS_LOGF_ERROR(AWS_LS_COMMON_THREAD, "id=%p: pthread_create() failed with %d", (void *)thread, attr_return); @@ -440,6 +441,7 @@ enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thre } int aws_thread_join(struct aws_thread *thread) { + printf("Joining Thread ID %d:\n", (int)thread->thread_id); if (thread->detach_state == AWS_THREAD_JOINABLE) { int err_no = pthread_join(thread->thread_id, 0); diff --git a/source/thread_shared.c b/source/thread_shared.c index e3c414aaf..e5a3fa791 100644 --- a/source/thread_shared.c +++ b/source/thread_shared.c @@ -9,6 +9,14 @@ #include #include #include +#include +#include +#include +#include +#include +#include + + /* * lock guarding the unjoined thread count and pending join list @@ -174,6 +182,34 @@ void aws_pthread_atfork_on_fork_prepare(void) { * in child process. * Fork will copy the list, but not the real threads. */ + + pid_t pid = getpid(); + char path[128]; + DIR *dir; + struct dirent *entry; + + // Format the path to the task directory for this process + snprintf(path, sizeof(path), "/proc/%d/task", pid); + + // Open the directory + if ((dir = opendir(path)) == NULL) { + perror("Failed to open task directory"); + return; + } + pid_t tid = syscall(SYS_gettid); + + printf("Current Thread ID is %d:\n", tid); + printf("Threads for process %d:\n", pid); + + // Read each entry in the directory + while ((entry = readdir(dir)) != NULL) { + // Skip "." and ".." entries + if (entry->d_name[0] != '.') { + printf("- Thread ID: %s\n", entry->d_name); + } + } + + closedir(dir); struct aws_linked_list empty; aws_linked_list_init(&empty); aws_mutex_lock(&s_managed_thread_lock); @@ -181,3 +217,32 @@ void aws_pthread_atfork_on_fork_prepare(void) { aws_mutex_unlock(&s_managed_thread_lock); aws_thread_join_and_free_wrapper_list(&empty); } +void aws_pthread_atfork_after_fork_child(void) { + pid_t pid = getpid(); + char path[128]; + DIR *dir; + struct dirent *entry; + + // Format the path to the task directory for this process + snprintf(path, sizeof(path), "/proc/%d/task", pid); + + // Open the directory + if ((dir = opendir(path)) == NULL) { + perror("Failed to open task directory"); + return; + } + pid_t tid = syscall(SYS_gettid); + + printf("Current Thread ID CHILD is %d:\n", tid); + printf("Threads for process CHILD %d:\n", pid); + + // Read each entry in the directory + while ((entry = readdir(dir)) != NULL) { + // Skip "." and ".." entries + if (entry->d_name[0] != '.') { + printf("- Thread ID CHILD: %s\n", entry->d_name); + } + } + + closedir(dir); +} \ No newline at end of file