blob: b778f92b8b580bcb5926b62e3e3bd404b766dfaf [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
39/* branchless on many architectures. */
40#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
41
Howard Rod1730092018-10-22 17:37:42 -070042#ifndef htole32
43#if __BYTE_ORDER == __LITTLE_ENDIAN
44#define htole32(x) (x)
45#else
46#define htole32(x) __bswap_32(x)
47#endif
48#endif
49
Yao Chen14c6db62019-03-26 13:51:53 -070050#ifndef htole64
51#if __BYTE_ORDER == __LITTLE_ENDIAN
52#define htole64(x) (x)
53#else
54#define htole64(x) __bswap_64(x)
55#endif
56#endif
57
Yao Chenb3be9ea2018-05-07 16:57:13 -070058static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Yao Chen63010542018-08-20 16:15:33 -070059static atomic_int dropped = 0;
Yao Chen1d357612018-11-08 15:43:08 -080060static atomic_int log_error = 0;
Yao Chen14c6db62019-03-26 13:51:53 -070061static atomic_int atom_tag = 0;
Yao Chenb3be9ea2018-05-07 16:57:13 -070062
63void statsd_writer_init_lock() {
64 /*
65 * If we trigger a signal handler in the middle of locked activity and the
66 * signal handler logs a message, we could get into a deadlock state.
67 */
68 pthread_mutex_lock(&log_init_lock);
69}
70
71int statd_writer_trylock() {
72 return pthread_mutex_trylock(&log_init_lock);
73}
74
75void statsd_writer_init_unlock() {
76 pthread_mutex_unlock(&log_init_lock);
77}
78
79static int statsdAvailable();
80static int statsdOpen();
81static void statsdClose();
82static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
Yao Chen63010542018-08-20 16:15:33 -070083static void statsdNoteDrop();
Yao Chenb3be9ea2018-05-07 16:57:13 -070084
85struct android_log_transport_write statsdLoggerWrite = {
Yao Chen63010542018-08-20 16:15:33 -070086 .name = "statsd",
87 .sock = -EBADF,
88 .available = statsdAvailable,
89 .open = statsdOpen,
90 .close = statsdClose,
91 .write = statsdWrite,
92 .noteDrop = statsdNoteDrop,
Yao Chenb3be9ea2018-05-07 16:57:13 -070093};
94
95/* log_init_lock assumed */
96static int statsdOpen() {
97 int i, ret = 0;
98
99 i = atomic_load(&statsdLoggerWrite.sock);
100 if (i < 0) {
Howard Rod1730092018-10-22 17:37:42 -0700101 int flags = SOCK_DGRAM;
102#ifdef SOCK_CLOEXEC
103 flags |= SOCK_CLOEXEC;
104#endif
105#ifdef SOCK_NONBLOCK
106 flags |= SOCK_NONBLOCK;
107#endif
108 int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, flags, 0));
Yao Chenb3be9ea2018-05-07 16:57:13 -0700109 if (sock < 0) {
110 ret = -errno;
111 } else {
112 struct sockaddr_un un;
113 memset(&un, 0, sizeof(struct sockaddr_un));
114 un.sun_family = AF_UNIX;
115 strcpy(un.sun_path, "/dev/socket/statsdw");
116
117 if (TEMP_FAILURE_RETRY(
118 connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) {
119 ret = -errno;
120 switch (ret) {
121 case -ENOTCONN:
122 case -ECONNREFUSED:
123 case -ENOENT:
124 i = atomic_exchange(&statsdLoggerWrite.sock, ret);
125 /* FALLTHRU */
126 default:
127 break;
128 }
129 close(sock);
130 } else {
131 ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
132 if ((ret >= 0) && (ret != sock)) {
133 close(ret);
134 }
135 ret = 0;
136 }
137 }
138 }
139
140 return ret;
141}
142
143static void __statsdClose(int negative_errno) {
144 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
145 if (sock >= 0) {
146 close(sock);
147 }
148}
149
150static void statsdClose() {
151 __statsdClose(-EBADF);
152}
153
154static int statsdAvailable() {
155 if (atomic_load(&statsdLoggerWrite.sock) < 0) {
156 if (access("/dev/socket/statsdw", W_OK) == 0) {
157 return 0;
158 }
159 return -EBADF;
160 }
161 return 1;
162}
163
Yao Chen14c6db62019-03-26 13:51:53 -0700164static void statsdNoteDrop(int error, int tag) {
Yao Chen63010542018-08-20 16:15:33 -0700165 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Yao Chen1d357612018-11-08 15:43:08 -0800166 atomic_exchange_explicit(&log_error, error, memory_order_relaxed);
Yao Chen14c6db62019-03-26 13:51:53 -0700167 atomic_exchange_explicit(&atom_tag, tag, memory_order_relaxed);
Yao Chen63010542018-08-20 16:15:33 -0700168}
169
Yao Chenb3be9ea2018-05-07 16:57:13 -0700170static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
171 ssize_t ret;
172 int sock;
173 static const unsigned headerLength = 1;
174 struct iovec newVec[nr + headerLength];
175 android_log_header_t header;
176 size_t i, payloadSize;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700177
178 sock = atomic_load(&statsdLoggerWrite.sock);
179 if (sock < 0) switch (sock) {
180 case -ENOTCONN:
181 case -ECONNREFUSED:
182 case -ENOENT:
183 break;
184 default:
185 return -EBADF;
186 }
187 /*
188 * struct {
189 * // what we provide to socket
190 * android_log_header_t header;
191 * // caller provides
192 * union {
193 * struct {
194 * char prio;
195 * char payload[];
196 * } string;
197 * struct {
198 * uint32_t tag
199 * char payload[];
200 * } binary;
201 * };
202 * };
203 */
204
205 header.tid = gettid();
206 header.realtime.tv_sec = ts->tv_sec;
207 header.realtime.tv_nsec = ts->tv_nsec;
208
209 newVec[0].iov_base = (unsigned char*)&header;
210 newVec[0].iov_len = sizeof(header);
211
212 // If we dropped events before, try to tell statsd.
213 if (sock >= 0) {
214 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
215 if (snapshot) {
Yao Chen14c6db62019-03-26 13:51:53 -0700216 android_log_event_long_t buffer;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700217 header.id = LOG_ID_STATS;
Yao Chen1d357612018-11-08 15:43:08 -0800218 // store the last log error in the tag field. This tag field is not used by statsd.
219 buffer.header.tag = htole32(atomic_load(&log_error));
Yao Chen14c6db62019-03-26 13:51:53 -0700220 buffer.payload.type = EVENT_TYPE_LONG;
221 // format:
222 // |atom_tag|dropped_count|
223 int64_t composed_long = atomic_load(&atom_tag);
224 // Send 2 int32's via an int64.
225 composed_long = ((composed_long << 32) | ((int64_t)snapshot));
226 buffer.payload.data = htole64(composed_long);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700227
228 newVec[headerLength].iov_base = &buffer;
229 newVec[headerLength].iov_len = sizeof(buffer);
230
231 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
232 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
233 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
234 }
235 }
236 }
237
238 header.id = LOG_ID_STATS;
239
240 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
241 newVec[i].iov_base = vec[i - headerLength].iov_base;
242 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
243
244 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
245 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
246 if (newVec[i].iov_len) {
247 ++i;
248 }
249 break;
250 }
251 }
252
253 /*
254 * The write below could be lost, but will never block.
255 *
256 * ENOTCONN occurs if statsd has died.
257 * ENOENT occurs if statsd is not running and socket is missing.
258 * ECONNREFUSED occurs if we can not reconnect to statsd.
259 * EAGAIN occurs if statsd is overloaded.
260 */
261 if (sock < 0) {
262 ret = sock;
263 } else {
264 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
265 if (ret < 0) {
266 ret = -errno;
267 }
268 }
269 switch (ret) {
270 case -ENOTCONN:
271 case -ECONNREFUSED:
272 case -ENOENT:
273 if (statd_writer_trylock()) {
274 return ret; /* in a signal handler? try again when less stressed
275 */
276 }
277 __statsdClose(ret);
278 ret = statsdOpen();
279 statsd_writer_init_unlock();
280
281 if (ret < 0) {
282 return ret;
283 }
284
285 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
286 if (ret < 0) {
287 ret = -errno;
288 }
289 /* FALLTHRU */
290 default:
291 break;
292 }
293
294 if (ret > (ssize_t)sizeof(header)) {
295 ret -= sizeof(header);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700296 }
297
298 return ret;
299}