log_channel.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/log_channel.h>
  6. #include <aws/common/condition_variable.h>
  7. #include <aws/common/log_writer.h>
  8. #include <aws/common/mutex.h>
  9. #include <aws/common/string.h>
  10. #include <aws/common/thread.h>
  11. #include <stdio.h>
  12. /*
  13. * Basic channel implementations - synchronized foreground, synchronized background
  14. */
  15. struct aws_log_foreground_channel {
  16. struct aws_mutex sync;
  17. };
  18. static int s_foreground_channel_send(struct aws_log_channel *channel, struct aws_string *log_line) {
  19. struct aws_log_foreground_channel *impl = (struct aws_log_foreground_channel *)channel->impl;
  20. AWS_ASSERT(channel->writer->vtable->write);
  21. aws_mutex_lock(&impl->sync);
  22. (channel->writer->vtable->write)(channel->writer, log_line);
  23. aws_mutex_unlock(&impl->sync);
  24. /*
  25. * send is considered a transfer of ownership. write is not a transfer of ownership.
  26. * So it's always the channel's responsibility to clean up all log lines that enter
  27. * it as soon as they are no longer needed.
  28. */
  29. aws_string_destroy(log_line);
  30. return AWS_OP_SUCCESS;
  31. }
  32. static void s_foreground_channel_clean_up(struct aws_log_channel *channel) {
  33. struct aws_log_foreground_channel *impl = (struct aws_log_foreground_channel *)channel->impl;
  34. aws_mutex_clean_up(&impl->sync);
  35. aws_mem_release(channel->allocator, impl);
  36. }
  37. static struct aws_log_channel_vtable s_foreground_channel_vtable = {
  38. .send = s_foreground_channel_send,
  39. .clean_up = s_foreground_channel_clean_up,
  40. };
  41. int aws_log_channel_init_foreground(
  42. struct aws_log_channel *channel,
  43. struct aws_allocator *allocator,
  44. struct aws_log_writer *writer) {
  45. struct aws_log_foreground_channel *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_log_foreground_channel));
  46. if (impl == NULL) {
  47. return AWS_OP_ERR;
  48. }
  49. if (aws_mutex_init(&impl->sync)) {
  50. aws_mem_release(allocator, impl);
  51. return AWS_OP_ERR;
  52. }
  53. channel->vtable = &s_foreground_channel_vtable;
  54. channel->allocator = allocator;
  55. channel->writer = writer;
  56. channel->impl = impl;
  57. return AWS_OP_SUCCESS;
  58. }
  59. struct aws_log_background_channel {
  60. struct aws_mutex sync;
  61. struct aws_thread background_thread;
  62. struct aws_array_list pending_log_lines;
  63. struct aws_condition_variable pending_line_signal;
  64. bool finished;
  65. };
  66. static int s_background_channel_send(struct aws_log_channel *channel, struct aws_string *log_line) {
  67. struct aws_log_background_channel *impl = (struct aws_log_background_channel *)channel->impl;
  68. aws_mutex_lock(&impl->sync);
  69. aws_array_list_push_back(&impl->pending_log_lines, &log_line);
  70. aws_condition_variable_notify_one(&impl->pending_line_signal);
  71. aws_mutex_unlock(&impl->sync);
  72. return AWS_OP_SUCCESS;
  73. }
  74. static void s_background_channel_clean_up(struct aws_log_channel *channel) {
  75. struct aws_log_background_channel *impl = (struct aws_log_background_channel *)channel->impl;
  76. aws_mutex_lock(&impl->sync);
  77. impl->finished = true;
  78. aws_condition_variable_notify_one(&impl->pending_line_signal);
  79. aws_mutex_unlock(&impl->sync);
  80. aws_thread_join(&impl->background_thread);
  81. aws_thread_clean_up(&impl->background_thread);
  82. aws_condition_variable_clean_up(&impl->pending_line_signal);
  83. aws_array_list_clean_up(&impl->pending_log_lines);
  84. aws_mutex_clean_up(&impl->sync);
  85. aws_mem_release(channel->allocator, impl);
  86. }
  87. static struct aws_log_channel_vtable s_background_channel_vtable = {
  88. .send = s_background_channel_send,
  89. .clean_up = s_background_channel_clean_up,
  90. };
  91. static bool s_background_wait(void *context) {
  92. struct aws_log_background_channel *impl = (struct aws_log_background_channel *)context;
  93. /*
  94. * Condition variable predicates are checked under mutex protection
  95. */
  96. return impl->finished || aws_array_list_length(&impl->pending_log_lines) > 0;
  97. }
  98. /**
  99. * This is where the background thread spends 99.999% of its time.
  100. * We broke this out into its own function so that the stacktrace clearly shows
  101. * what this thread is doing. We've had a lot of cases where users think this
  102. * thread is deadlocked because it's stuck here. We want it to be clear
  103. * that it's doing nothing on purpose. It's waiting for log messages...
  104. */
  105. AWS_NO_INLINE
  106. static void aws_background_logger_listen_for_messages(struct aws_log_background_channel *impl) {
  107. aws_condition_variable_wait_pred(&impl->pending_line_signal, &impl->sync, s_background_wait, impl);
  108. }
  109. static void aws_background_logger_thread(void *thread_data) {
  110. (void)thread_data;
  111. struct aws_log_channel *channel = (struct aws_log_channel *)thread_data;
  112. AWS_ASSERT(channel->writer->vtable->write);
  113. struct aws_log_background_channel *impl = (struct aws_log_background_channel *)channel->impl;
  114. struct aws_array_list log_lines;
  115. AWS_FATAL_ASSERT(aws_array_list_init_dynamic(&log_lines, channel->allocator, 10, sizeof(struct aws_string *)) == 0);
  116. while (true) {
  117. aws_mutex_lock(&impl->sync);
  118. aws_background_logger_listen_for_messages(impl);
  119. size_t line_count = aws_array_list_length(&impl->pending_log_lines);
  120. bool finished = impl->finished;
  121. if (line_count == 0) {
  122. aws_mutex_unlock(&impl->sync);
  123. if (finished) {
  124. break;
  125. }
  126. continue;
  127. }
  128. aws_array_list_swap_contents(&impl->pending_log_lines, &log_lines);
  129. aws_mutex_unlock(&impl->sync);
  130. /*
  131. * Consider copying these into a page-sized stack buffer (string) and then making the write calls
  132. * against it rather than the individual strings. Might be a savings when > 1 lines (cut down on
  133. * write calls).
  134. */
  135. for (size_t i = 0; i < line_count; ++i) {
  136. struct aws_string *log_line = NULL;
  137. AWS_FATAL_ASSERT(aws_array_list_get_at(&log_lines, &log_line, i) == AWS_OP_SUCCESS);
  138. (channel->writer->vtable->write)(channel->writer, log_line);
  139. /*
  140. * send is considered a transfer of ownership. write is not a transfer of ownership.
  141. * So it's always the channel's responsibility to clean up all log lines that enter
  142. * it as soon as they are no longer needed.
  143. */
  144. aws_string_destroy(log_line);
  145. }
  146. aws_array_list_clear(&log_lines);
  147. }
  148. aws_array_list_clean_up(&log_lines);
  149. }
  150. int aws_log_channel_init_background(
  151. struct aws_log_channel *channel,
  152. struct aws_allocator *allocator,
  153. struct aws_log_writer *writer) {
  154. struct aws_log_background_channel *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_log_background_channel));
  155. if (impl == NULL) {
  156. return AWS_OP_ERR;
  157. }
  158. impl->finished = false;
  159. if (aws_mutex_init(&impl->sync)) {
  160. goto clean_up_sync_init_fail;
  161. }
  162. if (aws_array_list_init_dynamic(&impl->pending_log_lines, allocator, 10, sizeof(struct aws_string *))) {
  163. goto clean_up_pending_log_lines_init_fail;
  164. }
  165. if (aws_condition_variable_init(&impl->pending_line_signal)) {
  166. goto clean_up_pending_line_signal_init_fail;
  167. }
  168. if (aws_thread_init(&impl->background_thread, allocator)) {
  169. goto clean_up_background_thread_init_fail;
  170. }
  171. channel->vtable = &s_background_channel_vtable;
  172. channel->allocator = allocator;
  173. channel->impl = impl;
  174. channel->writer = writer;
  175. /*
  176. * Logging thread should need very little stack, but let's defer this to later
  177. */
  178. struct aws_thread_options thread_options = *aws_default_thread_options();
  179. thread_options.name = aws_byte_cursor_from_c_str("AwsLogger"); /* 15 characters is max for Linux */
  180. if (aws_thread_launch(&impl->background_thread, aws_background_logger_thread, channel, &thread_options) ==
  181. AWS_OP_SUCCESS) {
  182. return AWS_OP_SUCCESS;
  183. }
  184. aws_thread_clean_up(&impl->background_thread);
  185. clean_up_background_thread_init_fail:
  186. aws_condition_variable_clean_up(&impl->pending_line_signal);
  187. clean_up_pending_line_signal_init_fail:
  188. aws_array_list_clean_up(&impl->pending_log_lines);
  189. clean_up_pending_log_lines_init_fail:
  190. aws_mutex_clean_up(&impl->sync);
  191. clean_up_sync_init_fail:
  192. aws_mem_release(allocator, impl);
  193. return AWS_OP_ERR;
  194. }
  195. void aws_log_channel_clean_up(struct aws_log_channel *channel) {
  196. AWS_ASSERT(channel->vtable->clean_up);
  197. (channel->vtable->clean_up)(channel);
  198. }