1 /*
2 * Copyright (C) 2019 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_buffer_writer.h"
18
19 #include <errno.h>
20 #include <sys/time.h>
21 #include <sys/uio.h>
22
23 #include "stats_buffer_writer_impl.h"
24 #include "stats_buffer_writer_queue.h"
25 #include "statsd_writer.h"
26
27 static const uint32_t kStatsEventTag = 1937006964;
28
29 extern struct android_log_transport_write statsdLoggerWrite;
30
31 static int __write_to_statsd_init(struct iovec* vec, size_t nr);
32 static int (*__write_to_statsd)(struct iovec* vec, size_t nr) = __write_to_statsd_init;
33
34 /**
35 * @brief Logs the error code associated with atom loss
36 *
37 * @param error To distinguish source of error, the errno code values must be negative,
38 * while the libstatssocket internal error codes are positive
39 */
note_log_drop(int error,int atomId)40 void note_log_drop(int error, int atomId) {
41 statsdLoggerWrite.noteDrop(error, atomId);
42 }
43
stats_log_close()44 void stats_log_close() {
45 statsd_writer_init_lock();
46 __write_to_statsd = __write_to_statsd_init;
47 if (statsdLoggerWrite.close) {
48 (*statsdLoggerWrite.close)();
49 }
50 statsd_writer_init_unlock();
51 }
52
stats_log_is_closed()53 int stats_log_is_closed() {
54 return statsdLoggerWrite.isClosed && (*statsdLoggerWrite.isClosed)();
55 }
56
write_buffer_to_statsd(void * buffer,size_t size,uint32_t atomId)57 int write_buffer_to_statsd(void* buffer, size_t size, uint32_t atomId) {
58 const int kQueueOverflowErrorCode = 1;
59 if (should_write_via_queue(atomId)) {
60 const bool ret = write_buffer_to_statsd_queue(buffer, size, atomId);
61 if (!ret) {
62 // to account on the loss, note atom drop with predefined internal error code
63 note_log_drop(kQueueOverflowErrorCode, atomId);
64 }
65 return ret;
66 }
67 return write_buffer_to_statsd_impl(buffer, size, atomId, true);
68 }
69
write_buffer_to_statsd_impl(void * buffer,size_t size,uint32_t atomId,bool doNoteDrop)70 int write_buffer_to_statsd_impl(void* buffer, size_t size, uint32_t atomId, bool doNoteDrop) {
71 int ret = 1;
72
73 struct iovec vecs[2];
74 vecs[0].iov_base = (void*)&kStatsEventTag;
75 vecs[0].iov_len = sizeof(kStatsEventTag);
76 vecs[1].iov_base = buffer;
77 vecs[1].iov_len = size;
78
79 ret = __write_to_statsd(vecs, 2);
80
81 if (ret < 0 && doNoteDrop) {
82 note_log_drop(ret, atomId);
83 }
84
85 return ret;
86 }
87
__write_to_stats_daemon(struct iovec * vec,size_t nr)88 static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
89 int save_errno;
90 struct timespec ts;
91 size_t len, i;
92
93 for (len = i = 0; i < nr; ++i) {
94 len += vec[i].iov_len;
95 }
96 if (!len) {
97 return -EINVAL;
98 }
99
100 save_errno = errno;
101 #if defined(__ANDROID__)
102 clock_gettime(CLOCK_REALTIME, &ts);
103 #else
104 struct timeval tv;
105 gettimeofday(&tv, NULL);
106 ts.tv_sec = tv.tv_sec;
107 ts.tv_nsec = tv.tv_usec * 1000;
108 #endif
109
110 int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
111 errno = save_errno;
112 return ret;
113 }
114
__write_to_statsd_initialize_locked()115 static int __write_to_statsd_initialize_locked() {
116 if (!statsdLoggerWrite.open || ((*statsdLoggerWrite.open)() < 0)) {
117 if (statsdLoggerWrite.close) {
118 (*statsdLoggerWrite.close)();
119 return -ENODEV;
120 }
121 }
122 return 1;
123 }
124
__write_to_statsd_init(struct iovec * vec,size_t nr)125 static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
126 int ret, save_errno = errno;
127
128 statsd_writer_init_lock();
129
130 if (__write_to_statsd == __write_to_statsd_init) {
131 ret = __write_to_statsd_initialize_locked();
132 if (ret < 0) {
133 statsd_writer_init_unlock();
134 errno = save_errno;
135 return ret;
136 }
137
138 __write_to_statsd = __write_to_stats_daemon;
139 }
140
141 statsd_writer_init_unlock();
142
143 ret = __write_to_statsd(vec, nr);
144 errno = save_errno;
145 return ret;
146 }
147