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 #ifdef __ANDROID__
19 #include <cutils/properties.h>
20 #endif
21 #include <errno.h>
22 #include <sys/time.h>
23 #include <sys/uio.h>
24 #include "statsd_writer.h"
25
26 static const uint32_t kStatsEventTag = 1937006964;
27
28 extern struct android_log_transport_write statsdLoggerWrite;
29
30 static int __write_to_statsd_init(struct iovec* vec, size_t nr);
31 static int (*__write_to_statsd)(struct iovec* vec, size_t nr) = __write_to_statsd_init;
32
note_log_drop(int error,int atomId)33 void note_log_drop(int error, int atomId) {
34 statsdLoggerWrite.noteDrop(error, atomId);
35 }
36
stats_log_close()37 void stats_log_close() {
38 statsd_writer_init_lock();
39 __write_to_statsd = __write_to_statsd_init;
40 if (statsdLoggerWrite.close) {
41 (*statsdLoggerWrite.close)();
42 }
43 statsd_writer_init_unlock();
44 }
45
write_buffer_to_statsd(void * buffer,size_t size,uint32_t atomId)46 int write_buffer_to_statsd(void* buffer, size_t size, uint32_t atomId) {
47 int ret = 1;
48
49 #ifdef __ANDROID__
50 bool statsdEnabled = property_get_bool("ro.statsd.enable", true);
51 #else
52 bool statsdEnabled = false;
53 #endif
54
55 if (statsdEnabled) {
56 struct iovec vecs[2];
57 vecs[0].iov_base = (void*)&kStatsEventTag;
58 vecs[0].iov_len = sizeof(kStatsEventTag);
59 vecs[1].iov_base = buffer;
60 vecs[1].iov_len = size;
61
62 ret = __write_to_statsd(vecs, 2);
63
64 if (ret < 0) {
65 note_log_drop(ret, atomId);
66 }
67 }
68
69 return ret;
70 }
71
__write_to_stats_daemon(struct iovec * vec,size_t nr)72 static int __write_to_stats_daemon(struct iovec* vec, size_t nr) {
73 int save_errno;
74 struct timespec ts;
75 size_t len, i;
76
77 for (len = i = 0; i < nr; ++i) {
78 len += vec[i].iov_len;
79 }
80 if (!len) {
81 return -EINVAL;
82 }
83
84 save_errno = errno;
85 #if defined(__ANDROID__)
86 clock_gettime(CLOCK_REALTIME, &ts);
87 #else
88 struct timeval tv;
89 gettimeofday(&tv, NULL);
90 ts.tv_sec = tv.tv_sec;
91 ts.tv_nsec = tv.tv_usec * 1000;
92 #endif
93
94 int ret = (int)(*statsdLoggerWrite.write)(&ts, vec, nr);
95 errno = save_errno;
96 return ret;
97 }
98
__write_to_statsd_initialize_locked()99 static int __write_to_statsd_initialize_locked() {
100 if (!statsdLoggerWrite.open || ((*statsdLoggerWrite.open)() < 0)) {
101 if (statsdLoggerWrite.close) {
102 (*statsdLoggerWrite.close)();
103 return -ENODEV;
104 }
105 }
106 return 1;
107 }
108
__write_to_statsd_init(struct iovec * vec,size_t nr)109 static int __write_to_statsd_init(struct iovec* vec, size_t nr) {
110 int ret, save_errno = errno;
111
112 statsd_writer_init_lock();
113
114 if (__write_to_statsd == __write_to_statsd_init) {
115 ret = __write_to_statsd_initialize_locked();
116 if (ret < 0) {
117 statsd_writer_init_unlock();
118 errno = save_errno;
119 return ret;
120 }
121
122 __write_to_statsd = __write_to_stats_daemon;
123 }
124
125 statsd_writer_init_unlock();
126
127 ret = __write_to_statsd(vec, nr);
128 errno = save_errno;
129 return ret;
130 }
131