blob: 51408e071ed5cb2d2d2e6485ea2a2dcbe8d67844 [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>
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
Yao Chen63010542018-08-20 16:15:33 -0700122void note_log_drop() {
123 statsdLoggerWrite.noteDrop();
124}
125
Yao Chenb3be9ea2018-05-07 16:57:13 -0700126/* log_init_lock assumed */
127static int __write_to_statsd_initialize_locked() {
128 if (!statsdLoggerWrite.open || ((*statsdLoggerWrite.open)() < 0)) {
129 if (statsdLoggerWrite.close) {
130 (*statsdLoggerWrite.close)();
131 return -ENODEV;
132 }
133 }
134 return 1;
135}
136
137static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
Yao Chen4d1deed2018-05-22 15:57:08 -0700138 int save_errno;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700139 struct timespec ts;
140 size_t len, i;
141
142 for (len = i = 0; i < nr; ++i) {
143 len += vec[i].iov_len;
144 }
145 if (!len) {
146 return -EINVAL;
147 }
148
149 save_errno = errno;
150 clock_gettime(CLOCK_REALTIME, &ts);
151
Yao Chen4d1deed2018-05-22 15:57:08 -0700152 int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700153 errno = save_errno;
154 return ret;
155}
156
157static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
158 int ret, save_errno = errno;
159
160 statsd_writer_init_lock();
161
162 if (write_to_statsd == __write_to_statsd_init) {
163 ret = __write_to_statsd_initialize_locked();
164 if (ret < 0) {
165 statsd_writer_init_unlock();
166 errno = save_errno;
167 return ret;
168 }
169
170 write_to_statsd = __write_to_stats_daemon;
171 }
172
173 statsd_writer_init_unlock();
174
175 ret = write_to_statsd(vec, nr);
176 errno = save_errno;
177 return ret;
Yao Chen4d1deed2018-05-22 15:57:08 -0700178}