blob: d736f7e217a9a91e56360f3963f54589abfa4264 [file] [log] [blame]
Yao Chenf7bc6ab2018-04-18 13:45:48 -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
40namespace android {
41namespace util {
42
43static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
44
45void statsd_writer_init_lock() {
46 /*
47 * If we trigger a signal handler in the middle of locked activity and the
48 * signal handler logs a message, we could get into a deadlock state.
49 */
50 pthread_mutex_lock(&log_init_lock);
51}
52
53int statd_writer_trylock() {
54 return pthread_mutex_trylock(&log_init_lock);
55}
56
57void statsd_writer_init_unlock() {
58 pthread_mutex_unlock(&log_init_lock);
59}
60
61static int statsdAvailable();
62static int statsdOpen();
63static void statsdClose();
64static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
65
66struct android_log_transport_write statsdLoggerWrite = {
67 .name = "statsd",
68 .available = statsdAvailable,
69 .open = statsdOpen,
70 .close = statsdClose,
71 .write = statsdWrite,
72};
73
74std::atomic_int android_log_transport_write::sock(-EBADF);
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) {
82 int sock = TEMP_FAILURE_RETRY(
83 socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
84 if (sock < 0) {
85 ret = -errno;
86 } else {
87 struct sockaddr_un un;
88 memset(&un, 0, sizeof(struct sockaddr_un));
89 un.sun_family = AF_UNIX;
90 strcpy(un.sun_path, "/dev/socket/statsdw");
91
92 if (TEMP_FAILURE_RETRY(connect(sock, (struct sockaddr*)&un,
93 sizeof(struct sockaddr_un))) < 0) {
94 ret = -errno;
95 switch (ret) {
96 case -ENOTCONN:
97 case -ECONNREFUSED:
98 case -ENOENT:
99 i = atomic_exchange(&statsdLoggerWrite.sock, ret);
100 /* FALLTHRU */
101 default:
102 break;
103 }
104 close(sock);
105 } else {
106 ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
107 if ((ret >= 0) && (ret != sock)) {
108 close(ret);
109 }
110 ret = 0;
111 }
112 }
113 }
114
115 return ret;
116}
117
118static void __statsdClose(int negative_errno) {
119 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
120 if (sock >= 0) {
121 close(sock);
122 }
123}
124
125static void statsdClose() {
126 __statsdClose(-EBADF);
127}
128
129static int statsdAvailable() {
130 if (atomic_load(&statsdLoggerWrite.sock) < 0) {
131 if (access("/dev/socket/statsdw", W_OK) == 0) {
132 return 0;
133 }
134 return -EBADF;
135 }
136 return 1;
137}
138
139static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
140 ssize_t ret;
141 int sock;
142 static const unsigned headerLength = 1;
143 struct iovec newVec[nr + headerLength];
144 android_log_header_t header;
145 size_t i, payloadSize;
146 static atomic_int dropped;
147
148 sock = atomic_load(&statsdLoggerWrite.sock);
149 if (sock < 0)
150 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 =
186 atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
187 if (snapshot) {
188 android_log_event_int_t buffer;
189 header.id = LOG_ID_STATS;
190 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
191 buffer.payload.type = EVENT_TYPE_INT;
192 buffer.payload.data = htole32(snapshot);
193
194 newVec[headerLength].iov_base = &buffer;
195 newVec[headerLength].iov_len = sizeof(buffer);
196
197 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
198 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
199 atomic_fetch_add_explicit(&dropped, snapshot,
200 memory_order_relaxed);
201 }
202 }
203 }
204
205 header.id = LOG_ID_STATS;
206
207 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
208 newVec[i].iov_base = vec[i - headerLength].iov_base;
209 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
210
211 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
212 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
213 if (newVec[i].iov_len) {
214 ++i;
215 }
216 break;
217 }
218 }
219
220 /*
221 * The write below could be lost, but will never block.
222 *
223 * ENOTCONN occurs if statsd has died.
224 * ENOENT occurs if statsd is not running and socket is missing.
225 * ECONNREFUSED occurs if we can not reconnect to statsd.
226 * EAGAIN occurs if statsd is overloaded.
227 */
228 if (sock < 0) {
229 ret = sock;
230 } else {
231 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
232 if (ret < 0) {
233 ret = -errno;
234 }
235 }
236 switch (ret) {
237 case -ENOTCONN:
238 case -ECONNREFUSED:
239 case -ENOENT:
240 if (statd_writer_trylock()) {
241 return ret; /* in a signal handler? try again when less stressed
242 */
243 }
244 __statsdClose(ret);
245 ret = statsdOpen();
246 statsd_writer_init_unlock();
247
248 if (ret < 0) {
249 return ret;
250 }
251
252 ret = TEMP_FAILURE_RETRY(
253 writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
254 if (ret < 0) {
255 ret = -errno;
256 }
257 /* FALLTHRU */
258 default:
259 break;
260 }
261
262 if (ret > (ssize_t)sizeof(header)) {
263 ret -= sizeof(header);
264 } else if (ret == -EAGAIN) {
265 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
266 }
267
268 return ret;
269}
270
271} // namespace util
272} // namespace android