Skip to content

Commit

Permalink
add lot of printfs
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK committed Mar 10, 2025
1 parent b0a12fc commit cecce7e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/aws/common/private/thread_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions source/log_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion source/posix/cross_process_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions source/posix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
65 changes: 65 additions & 0 deletions source/thread_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#include <aws/common/condition_variable.h>
#include <aws/common/linked_list.h>
#include <aws/common/mutex.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>



/*
* lock guarding the unjoined thread count and pending join list
Expand Down Expand Up @@ -174,10 +182,67 @@ 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);
aws_linked_list_swap_contents(&empty, &s_pending_join_managed_threads);
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);
}

0 comments on commit cecce7e

Please sign in to comment.