blob: 72770d48a8581709c92f260b46da6a0e861af81b [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
17#include "include/stats_event_list.h"
18
19#include <string.h>
Howard Rod1730092018-10-22 17:37:42 -070020#include <sys/time.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070021#include "statsd_writer.h"
22
23#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
24
25typedef struct {
26 uint32_t tag;
27 unsigned pos; /* Read/write position into buffer */
28 unsigned count[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* Number of elements */
29 unsigned list[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* pos for list counter */
30 unsigned list_nest_depth;
31 unsigned len; /* Length or raw buffer. */
32 bool overflow;
33 bool list_stop; /* next call decrement list_nest_depth and issue a stop */
34 enum {
35 kAndroidLoggerRead = 1,
36 kAndroidLoggerWrite = 2,
37 } read_write_flag;
38 uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
39} android_log_context_internal;
40
41extern struct android_log_transport_write statsdLoggerWrite;
42
43static int __write_to_statsd_init(struct iovec* vec, size_t nr);
44static int (*write_to_statsd)(struct iovec* vec, size_t nr) = __write_to_statsd_init;
45
46// Similar to create_android_logger(), but instead of allocation a new buffer,
47// this function resets the buffer for resuse.
48void reset_log_context(android_log_context ctx) {
49 if (!ctx) {
50 return;
51 }
52 android_log_context_internal* context = (android_log_context_internal*)(ctx);
53 uint32_t tag = context->tag;
54 memset(context, 0, sizeof(android_log_context_internal));
55
56 context->tag = tag;
57 context->read_write_flag = kAndroidLoggerWrite;
58 size_t needed = sizeof(uint8_t) + sizeof(uint8_t);
59 if ((context->pos + needed) > MAX_EVENT_PAYLOAD) {
60 context->overflow = true;
61 }
62 /* Everything is a list */
63 context->storage[context->pos + 0] = EVENT_TYPE_LIST;
64 context->list[0] = context->pos + 1;
65 context->pos += needed;
66}
67
68int stats_write_list(android_log_context ctx) {
69 android_log_context_internal* context;
70 const char* msg;
71 ssize_t len;
72
73 context = (android_log_context_internal*)(ctx);
74 if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
75 return -EBADF;
76 }
77
78 if (context->list_nest_depth) {
79 return -EIO;
80 }
81
82 /* NB: if there was overflow, then log is truncated. Nothing reported */
83 context->storage[1] = context->count[0];
84 len = context->len = context->pos;
85 msg = (const char*)context->storage;
86 /* it's not a list */
87 if (context->count[0] <= 1) {
88 len -= sizeof(uint8_t) + sizeof(uint8_t);
89 if (len < 0) {
90 len = 0;
91 }
92 msg += sizeof(uint8_t) + sizeof(uint8_t);
93 }
94
95 struct iovec vec[2];
96 vec[0].iov_base = &context->tag;
97 vec[0].iov_len = sizeof(context->tag);
98 vec[1].iov_base = (void*)msg;
99 vec[1].iov_len = len;
100 return write_to_statsd(vec, 2);
101}
102
103int write_to_logger(android_log_context ctx, log_id_t id) {
104 int retValue = 0;
105
106 if (WRITE_TO_LOGD) {
107 retValue = android_log_write_list(ctx, id);
108 }
109
110 if (WRITE_TO_STATSD) {
111 // log_event_list's cast operator is overloaded.
112 int ret = stats_write_list(ctx);
113 // In debugging phase, we may write to both logd and statsd. Prefer to
114 // return statsd socket write error code here.
115 if (ret < 0) {
116 retValue = ret;
117 }
118 }
119
120 return retValue;
121}
122
Yao Chen63010542018-08-20 16:15:33 -0700123void note_log_drop() {
124 statsdLoggerWrite.noteDrop();
125}
126
Yao Chen602d2922018-10-02 10:51:49 -0700127void stats_log_close() {
128 statsd_writer_init_lock();
Yao Chen763e3ae2018-10-15 16:12:48 -0700129 write_to_statsd = __write_to_statsd_init;
Yao Chen602d2922018-10-02 10:51:49 -0700130 if (statsdLoggerWrite.close) {
131 (*statsdLoggerWrite.close)();
132 }
133 statsd_writer_init_unlock();
134}
135
Yao Chenb3be9ea2018-05-07 16:57:13 -0700136/* log_init_lock assumed */
137static int __write_to_statsd_initialize_locked() {
138 if (!statsdLoggerWrite.open || ((*statsdLoggerWrite.open)() < 0)) {
139 if (statsdLoggerWrite.close) {
140 (*statsdLoggerWrite.close)();
141 return -ENODEV;
142 }
143 }
144 return 1;
145}
146
147static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
Yao Chen4d1deed2018-05-22 15:57:08 -0700148 int save_errno;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700149 struct timespec ts;
150 size_t len, i;
151
152 for (len = i = 0; i < nr; ++i) {
153 len += vec[i].iov_len;
154 }
155 if (!len) {
156 return -EINVAL;
157 }
158
159 save_errno = errno;
Howard Rod1730092018-10-22 17:37:42 -0700160#if defined(__ANDROID__)
Yao Chenb3be9ea2018-05-07 16:57:13 -0700161 clock_gettime(CLOCK_REALTIME, &ts);
Howard Rod1730092018-10-22 17:37:42 -0700162#else
163 struct timeval tv;
164 gettimeofday(&tv, NULL);
165 ts.tv_sec = tv.tv_sec;
166 ts.tv_nsec = tv.tv_usec * 1000;
167#endif
Yao Chenb3be9ea2018-05-07 16:57:13 -0700168
Yao Chen4d1deed2018-05-22 15:57:08 -0700169 int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700170 errno = save_errno;
171 return ret;
172}
173
174static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
175 int ret, save_errno = errno;
176
177 statsd_writer_init_lock();
178
179 if (write_to_statsd == __write_to_statsd_init) {
180 ret = __write_to_statsd_initialize_locked();
181 if (ret < 0) {
182 statsd_writer_init_unlock();
183 errno = save_errno;
184 return ret;
185 }
186
187 write_to_statsd = __write_to_stats_daemon;
188 }
189
190 statsd_writer_init_unlock();
191
192 ret = write_to_statsd(vec, nr);
193 errno = save_errno;
194 return ret;
Yao Chen4d1deed2018-05-22 15:57:08 -0700195}