blob: 073b67fa0e77a87adafea182dd30f84eb464e3d3 [file] [log] [blame]
Yao Chenb3be9ea2018-05-07 16:57:13 -07001/*
2 * Copyright (C) 2018, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "statsd_writer.h"
17
Howard Rod1730092018-10-22 17:37:42 -070018#include <cutils/fs.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070019#include <cutils/sockets.h>
Howard Rod1730092018-10-22 17:37:42 -070020#include <cutils/threads.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070021#include <errno.h>
22#include <fcntl.h>
23#include <inttypes.h>
24#include <poll.h>
25#include <private/android_filesystem_config.h>
26#include <private/android_logger.h>
27#include <stdarg.h>
28#include <stdatomic.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/stat.h>
33#include <sys/types.h>
Tom Cherry6f6ef392019-01-16 14:17:08 -080034#include <sys/uio.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070035#include <sys/un.h>
36#include <time.h>
37#include <unistd.h>
38
Yao Chenb3be9ea2018-05-07 16:57:13 -070039static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Yao Chen63010542018-08-20 16:15:33 -070040static atomic_int dropped = 0;
Yao Chen1d357612018-11-08 15:43:08 -080041static atomic_int log_error = 0;
Yao Chencf776d92019-03-26 13:51:53 -070042static atomic_int atom_tag = 0;
Yao Chenb3be9ea2018-05-07 16:57:13 -070043
44void statsd_writer_init_lock() {
45 /*
46 * If we trigger a signal handler in the middle of locked activity and the
47 * signal handler logs a message, we could get into a deadlock state.
48 */
49 pthread_mutex_lock(&log_init_lock);
50}
51
52int statd_writer_trylock() {
53 return pthread_mutex_trylock(&log_init_lock);
54}
55
56void statsd_writer_init_unlock() {
57 pthread_mutex_unlock(&log_init_lock);
58}
59
60static int statsdAvailable();
61static int statsdOpen();
62static void statsdClose();
63static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
Yao Chen63010542018-08-20 16:15:33 -070064static void statsdNoteDrop();
Yao Chenb3be9ea2018-05-07 16:57:13 -070065
66struct android_log_transport_write statsdLoggerWrite = {
Yao Chen63010542018-08-20 16:15:33 -070067 .name = "statsd",
68 .sock = -EBADF,
69 .available = statsdAvailable,
70 .open = statsdOpen,
71 .close = statsdClose,
72 .write = statsdWrite,
73 .noteDrop = statsdNoteDrop,
Yao Chenb3be9ea2018-05-07 16:57:13 -070074};
75
76/* log_init_lock assumed */
77static int statsdOpen() {
78 int i, ret = 0;
79
80 i = atomic_load(&statsdLoggerWrite.sock);
81 if (i < 0) {
Howard Rod1730092018-10-22 17:37:42 -070082 int flags = SOCK_DGRAM;
83#ifdef SOCK_CLOEXEC
84 flags |= SOCK_CLOEXEC;
85#endif
86#ifdef SOCK_NONBLOCK
87 flags |= SOCK_NONBLOCK;
88#endif
89 int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, flags, 0));
Yao Chenb3be9ea2018-05-07 16:57:13 -070090 if (sock < 0) {
91 ret = -errno;
92 } else {
Yao Chen0a75f922019-04-02 13:51:46 -070093 int sndbuf = 1 * 1024 * 1024; // set max send buffer size 1MB
94 socklen_t bufLen = sizeof(sndbuf);
95 // SO_RCVBUF does not have an effect on unix domain socket, but SO_SNDBUF does.
96 // Proceed to connect even setsockopt fails.
97 setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, bufLen);
Yao Chenb3be9ea2018-05-07 16:57:13 -070098 struct sockaddr_un un;
99 memset(&un, 0, sizeof(struct sockaddr_un));
100 un.sun_family = AF_UNIX;
101 strcpy(un.sun_path, "/dev/socket/statsdw");
102
103 if (TEMP_FAILURE_RETRY(
104 connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) {
105 ret = -errno;
106 switch (ret) {
107 case -ENOTCONN:
108 case -ECONNREFUSED:
109 case -ENOENT:
110 i = atomic_exchange(&statsdLoggerWrite.sock, ret);
111 /* FALLTHRU */
112 default:
113 break;
114 }
115 close(sock);
116 } else {
117 ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
118 if ((ret >= 0) && (ret != sock)) {
119 close(ret);
120 }
121 ret = 0;
122 }
123 }
124 }
125
126 return ret;
127}
128
129static void __statsdClose(int negative_errno) {
130 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
131 if (sock >= 0) {
132 close(sock);
133 }
134}
135
136static void statsdClose() {
137 __statsdClose(-EBADF);
138}
139
140static int statsdAvailable() {
141 if (atomic_load(&statsdLoggerWrite.sock) < 0) {
142 if (access("/dev/socket/statsdw", W_OK) == 0) {
143 return 0;
144 }
145 return -EBADF;
146 }
147 return 1;
148}
149
Yao Chencf776d92019-03-26 13:51:53 -0700150static void statsdNoteDrop(int error, int tag) {
Yao Chen63010542018-08-20 16:15:33 -0700151 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Yao Chen1d357612018-11-08 15:43:08 -0800152 atomic_exchange_explicit(&log_error, error, memory_order_relaxed);
Yao Chencf776d92019-03-26 13:51:53 -0700153 atomic_exchange_explicit(&atom_tag, tag, memory_order_relaxed);
Yao Chen63010542018-08-20 16:15:33 -0700154}
155
Yao Chenb3be9ea2018-05-07 16:57:13 -0700156static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
157 ssize_t ret;
158 int sock;
159 static const unsigned headerLength = 1;
160 struct iovec newVec[nr + headerLength];
161 android_log_header_t header;
162 size_t i, payloadSize;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700163
164 sock = atomic_load(&statsdLoggerWrite.sock);
165 if (sock < 0) switch (sock) {
166 case -ENOTCONN:
167 case -ECONNREFUSED:
168 case -ENOENT:
169 break;
170 default:
171 return -EBADF;
172 }
173 /*
174 * struct {
175 * // what we provide to socket
176 * android_log_header_t header;
177 * // caller provides
178 * union {
179 * struct {
180 * char prio;
181 * char payload[];
182 * } string;
183 * struct {
184 * uint32_t tag
185 * char payload[];
186 * } binary;
187 * };
188 * };
189 */
190
191 header.tid = gettid();
192 header.realtime.tv_sec = ts->tv_sec;
193 header.realtime.tv_nsec = ts->tv_nsec;
194
195 newVec[0].iov_base = (unsigned char*)&header;
196 newVec[0].iov_len = sizeof(header);
197
198 // If we dropped events before, try to tell statsd.
199 if (sock >= 0) {
200 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
201 if (snapshot) {
Yao Chencf776d92019-03-26 13:51:53 -0700202 android_log_event_long_t buffer;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700203 header.id = LOG_ID_STATS;
Yao Chen1d357612018-11-08 15:43:08 -0800204 // store the last log error in the tag field. This tag field is not used by statsd.
Elliott Hughesb55731d2019-10-02 12:03:23 -0700205 buffer.header.tag = atomic_load(&log_error);
Yao Chencf776d92019-03-26 13:51:53 -0700206 buffer.payload.type = EVENT_TYPE_LONG;
207 // format:
208 // |atom_tag|dropped_count|
209 int64_t composed_long = atomic_load(&atom_tag);
210 // Send 2 int32's via an int64.
211 composed_long = ((composed_long << 32) | ((int64_t)snapshot));
Elliott Hughesb55731d2019-10-02 12:03:23 -0700212 buffer.payload.data = composed_long;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700213
214 newVec[headerLength].iov_base = &buffer;
215 newVec[headerLength].iov_len = sizeof(buffer);
216
217 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
218 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
219 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
220 }
221 }
222 }
223
224 header.id = LOG_ID_STATS;
225
226 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
227 newVec[i].iov_base = vec[i - headerLength].iov_base;
228 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
229
230 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
231 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
232 if (newVec[i].iov_len) {
233 ++i;
234 }
235 break;
236 }
237 }
238
239 /*
240 * The write below could be lost, but will never block.
241 *
242 * ENOTCONN occurs if statsd has died.
243 * ENOENT occurs if statsd is not running and socket is missing.
244 * ECONNREFUSED occurs if we can not reconnect to statsd.
245 * EAGAIN occurs if statsd is overloaded.
246 */
247 if (sock < 0) {
248 ret = sock;
249 } else {
250 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
251 if (ret < 0) {
252 ret = -errno;
253 }
254 }
255 switch (ret) {
256 case -ENOTCONN:
257 case -ECONNREFUSED:
258 case -ENOENT:
259 if (statd_writer_trylock()) {
260 return ret; /* in a signal handler? try again when less stressed
261 */
262 }
263 __statsdClose(ret);
264 ret = statsdOpen();
265 statsd_writer_init_unlock();
266
267 if (ret < 0) {
268 return ret;
269 }
270
271 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
272 if (ret < 0) {
273 ret = -errno;
274 }
275 /* FALLTHRU */
276 default:
277 break;
278 }
279
280 if (ret > (ssize_t)sizeof(header)) {
281 ret -= sizeof(header);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700282 }
283
284 return ret;
285}