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