blob: afe401f5c5391d91b3754993b1725b37a7b34032 [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
18#include <cutils/sockets.h>
19#include <endian.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <inttypes.h>
23#include <poll.h>
24#include <private/android_filesystem_config.h>
25#include <private/android_logger.h>
26#include <stdarg.h>
27#include <stdatomic.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/un.h>
34#include <time.h>
35#include <unistd.h>
36
37/* branchless on many architectures. */
38#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
39
40static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Yao Chen63010542018-08-20 16:15:33 -070041static atomic_int dropped = 0;
Yao Chenb3be9ea2018-05-07 16:57:13 -070042
43void statsd_writer_init_lock() {
44 /*
45 * If we trigger a signal handler in the middle of locked activity and the
46 * signal handler logs a message, we could get into a deadlock state.
47 */
48 pthread_mutex_lock(&log_init_lock);
49}
50
51int statd_writer_trylock() {
52 return pthread_mutex_trylock(&log_init_lock);
53}
54
55void statsd_writer_init_unlock() {
56 pthread_mutex_unlock(&log_init_lock);
57}
58
59static int statsdAvailable();
60static int statsdOpen();
61static void statsdClose();
62static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
Yao Chen63010542018-08-20 16:15:33 -070063static void statsdNoteDrop();
Yao Chenb3be9ea2018-05-07 16:57:13 -070064
65struct android_log_transport_write statsdLoggerWrite = {
Yao Chen63010542018-08-20 16:15:33 -070066 .name = "statsd",
67 .sock = -EBADF,
68 .available = statsdAvailable,
69 .open = statsdOpen,
70 .close = statsdClose,
71 .write = statsdWrite,
72 .noteDrop = statsdNoteDrop,
Yao Chenb3be9ea2018-05-07 16:57:13 -070073};
74
75/* log_init_lock assumed */
76static int statsdOpen() {
77 int i, ret = 0;
78
79 i = atomic_load(&statsdLoggerWrite.sock);
80 if (i < 0) {
81 int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
82 if (sock < 0) {
83 ret = -errno;
84 } else {
85 struct sockaddr_un un;
86 memset(&un, 0, sizeof(struct sockaddr_un));
87 un.sun_family = AF_UNIX;
88 strcpy(un.sun_path, "/dev/socket/statsdw");
89
90 if (TEMP_FAILURE_RETRY(
91 connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) {
92 ret = -errno;
93 switch (ret) {
94 case -ENOTCONN:
95 case -ECONNREFUSED:
96 case -ENOENT:
97 i = atomic_exchange(&statsdLoggerWrite.sock, ret);
98 /* FALLTHRU */
99 default:
100 break;
101 }
102 close(sock);
103 } else {
104 ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
105 if ((ret >= 0) && (ret != sock)) {
106 close(ret);
107 }
108 ret = 0;
109 }
110 }
111 }
112
113 return ret;
114}
115
116static void __statsdClose(int negative_errno) {
117 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
118 if (sock >= 0) {
119 close(sock);
120 }
121}
122
123static void statsdClose() {
124 __statsdClose(-EBADF);
125}
126
127static int statsdAvailable() {
128 if (atomic_load(&statsdLoggerWrite.sock) < 0) {
129 if (access("/dev/socket/statsdw", W_OK) == 0) {
130 return 0;
131 }
132 return -EBADF;
133 }
134 return 1;
135}
136
Yao Chen63010542018-08-20 16:15:33 -0700137static void statsdNoteDrop() {
138 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
139}
140
Yao Chenb3be9ea2018-05-07 16:57:13 -0700141static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
142 ssize_t ret;
143 int sock;
144 static const unsigned headerLength = 1;
145 struct iovec newVec[nr + headerLength];
146 android_log_header_t header;
147 size_t i, payloadSize;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700148
149 sock = atomic_load(&statsdLoggerWrite.sock);
150 if (sock < 0) switch (sock) {
151 case -ENOTCONN:
152 case -ECONNREFUSED:
153 case -ENOENT:
154 break;
155 default:
156 return -EBADF;
157 }
158 /*
159 * struct {
160 * // what we provide to socket
161 * android_log_header_t header;
162 * // caller provides
163 * union {
164 * struct {
165 * char prio;
166 * char payload[];
167 * } string;
168 * struct {
169 * uint32_t tag
170 * char payload[];
171 * } binary;
172 * };
173 * };
174 */
175
176 header.tid = gettid();
177 header.realtime.tv_sec = ts->tv_sec;
178 header.realtime.tv_nsec = ts->tv_nsec;
179
180 newVec[0].iov_base = (unsigned char*)&header;
181 newVec[0].iov_len = sizeof(header);
182
183 // If we dropped events before, try to tell statsd.
184 if (sock >= 0) {
185 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
186 if (snapshot) {
187 android_log_event_int_t buffer;
188 header.id = LOG_ID_STATS;
189 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
190 buffer.payload.type = EVENT_TYPE_INT;
191 buffer.payload.data = htole32(snapshot);
192
193 newVec[headerLength].iov_base = &buffer;
194 newVec[headerLength].iov_len = sizeof(buffer);
195
196 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
197 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
198 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
199 }
200 }
201 }
202
203 header.id = LOG_ID_STATS;
204
205 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
206 newVec[i].iov_base = vec[i - headerLength].iov_base;
207 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
208
209 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
210 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
211 if (newVec[i].iov_len) {
212 ++i;
213 }
214 break;
215 }
216 }
217
218 /*
219 * The write below could be lost, but will never block.
220 *
221 * ENOTCONN occurs if statsd has died.
222 * ENOENT occurs if statsd is not running and socket is missing.
223 * ECONNREFUSED occurs if we can not reconnect to statsd.
224 * EAGAIN occurs if statsd is overloaded.
225 */
226 if (sock < 0) {
227 ret = sock;
228 } else {
229 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
230 if (ret < 0) {
231 ret = -errno;
232 }
233 }
234 switch (ret) {
235 case -ENOTCONN:
236 case -ECONNREFUSED:
237 case -ENOENT:
238 if (statd_writer_trylock()) {
239 return ret; /* in a signal handler? try again when less stressed
240 */
241 }
242 __statsdClose(ret);
243 ret = statsdOpen();
244 statsd_writer_init_unlock();
245
246 if (ret < 0) {
247 return ret;
248 }
249
250 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
251 if (ret < 0) {
252 ret = -errno;
253 }
254 /* FALLTHRU */
255 default:
256 break;
257 }
258
259 if (ret > (ssize_t)sizeof(header)) {
260 ret -= sizeof(header);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700261 }
262
263 return ret;
264}