Debian Codegraph @

crt/aws-c-common/source/log_channel.c

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