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