dlog_logger: refactor QoS distributions
[platform/core/system/dlog.git] / src / logger / logger_internal.h
1 #pragma once
2
3 #include "log_storage.h"
4
5 #include <syslog.h>
6
7 #include <ctype.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14 #include <linux/limits.h>
15 #include <sys/epoll.h>
16 #include <sys/socket.h>
17 #include <sys/wait.h>
18 #include <sys/resource.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <sys/un.h>
22 #include <logpipe.h>
23 #include <log_file.h>
24 #include <logconfig.h>
25 #include <ptrs_list.h>
26 #include <sd-daemon.h>
27 #include <backend_androidlogger.h>
28 #include <queued_entry_timestamp.h>
29 #include <dlogutil-internal.h>
30 #include "qos.h"
31 #include "fd_entity.h"
32 #include "reader_common.h"
33 #include "reader_logger.h"
34 #include "reader_memory.h"
35 #include "reader_pipe.h"
36 #include "socket.h"
37 #include "dlogutil_line.h"
38 #include "log_buffer.h"
39 #include "writer.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /* supported main() return codes */
46 enum {
47         DLOG_EXIT_SUCCESS = 0,
48         DLOG_EXIT_ERR_CONFIG,
49         DLOG_EXIT_ERR_RUNTIME
50 };
51
52 /* Size in bytes of the pipe given to libdlog clients.
53  * The default size (1 page, usually 4 kiB) is fairly small
54  * which leads programs that log a lot to easily clog the
55  * pipe. On the other hand, the amount of memory allocated
56  * to pipes specifically can be limited and on most relevant
57  * targets seems to be set to 16ki pages (64 MiB raw memory)
58  * so pipes should not be too large so as not to waste it. */
59 #define PIPE_REQUESTED_SIZE (64 * 1024) // 16 pages at 4 kiB per page
60
61 /* Dumping readers should ideally get a larger pipe than clients
62  * so that it doesn't get easily clogged by the odd spammer.
63  * We can afford more generosity here because these are invoked
64  * quite rarely yet require handling priority, either because
65  * they are called by a developer manually or because a crashing
66  * program caused a system state dump. */
67 #define DUMPING_READER_PIPE_SIZE (4 * PIPE_REQUESTED_SIZE)
68
69 #define FILE_PATH_SIZE (256)
70 #define MAX_CONNECTION_Q 100
71 #define DELIMITER " "
72 #define CONF_PREFIX "dlog_logger_conf_"
73
74 enum {
75         BUF_PARAM_TIME_MIN = 0,
76         BUF_PARAM_TIME_MAX = 3600,
77         BUF_PARAM_TIME_DEFAULT = 10,
78 };
79
80 enum {
81         BUF_PARAM_BYTES_MIN = 0,
82         BUF_PARAM_BYTES_MAX = 1 << 24,
83         BUF_PARAM_BYTES_DEFAULT = 1 << 20
84 };
85
86 enum {
87         LOGGER_DEVICE_THROTTLING_DEFAULT = 100,
88 };
89
90 extern struct backend_data {
91         bool use_logger_by_default;
92         char logger_devices[LOG_ID_MAX][MAX_CONF_VAL_LEN];
93         int logger_device_throttling[LOG_ID_MAX];
94         struct reader_logger *logger_readers[LOG_ID_MAX];
95
96         /* Lazy polling is Android Logger specific, since in that case
97          * the immediate storage is handled by the kernel and the logs
98          * aren't going anywhere, so the daemon can avoid doing anything
99          * at boot time (which would just waste system resources).
100          *
101          * For Pipe, the daemon needs to be actively gathering logs for
102          * reliability (not missing anything in case of, say, crash dump)
103          * and to unclog pipes. */
104         int lazy_polling_total;
105         int lazy_polling_sleep;
106 } g_backend;
107
108
109 /* Writers and readers can be a bit confusing in what they refer to.
110  * Something that does write() calls is usually actually a reader,
111  * and something that read()s is typically a writer. This is because
112  * the terminology is from the point of view of the primary log storage
113  * and not the daemon itself. Additionally, under the Android Logger
114  * the situation is even more complex because there's a hybrid structure
115  * that sits at both edges of the daemon at the same time and read()s,
116  * then immediately write()s data. Nevertheless it is classified as a
117  * reader because of its relationship with the primary log storage.
118  *
119  * Here's a picture for Pipe backend:
120  *
121  *             ╔══════════════════════ daemon ════════════════════╗
122  * ┌─────────┐ ║  ┌────────┐    ┌─────────────────┐    ┌────────┐ ║  ┌─────────────────┐
123  * │ libdlog ├─╫─>│ struct ├───>│ primary storage ├───>│ struct ├─╫─>│ /var/log/dlog/x │
124  * │  client │ ║  │ writer │    │ char buffer[N]; │    │ reader │ ║  │   or  dlogutil  │
125  * └─────────┘ ║  └────────┘    └─────────────────┘    └────────┘ ║  └─────────────────┘
126  *             ╚══════════════════════════════════════════════════╝
127  *
128  * And here's one for the Android Logger and Zero Copy backends:
129  *
130  *                                                  ╔═══ daemon ══╗
131  * ┌─────────┐                  ┌─────────────────┐ ║  ┌────────┐ ║  ┌─────────────────┐
132  * │ libdlog ├─────────────────>│ primary storage ├─╫─>│ struct ├─╫─>│ /var/log/dlog/x │
133  * │  client │                  │   /dev/log_x    │ ║  │ reader │ ║  │                 │
134  * └─────────┘                  └─────────────────┘ ║  └────────┘ ║  └─────────────────┘
135  *                                                  ╚═════════════╝
136  *
137  * Of course, the null backend has no relevant data structures. */
138
139
140 struct logger {
141         struct epoll_metadata epoll_common;
142         struct epoll_metadata epoll_socket;
143         int                   epolltime;
144         list_head             writers;
145         list_head             readers_logger;
146         struct log_buffer*    buffers[LOG_ID_MAX];
147         struct buf_params     buf_params;
148         int                   exiting;
149         struct qos_module     qos;
150         list_head             compressed_memories;
151 };
152
153 struct logger_config_data {
154         struct buf_params buf_params;
155         list_head logfile_configs;
156         int epoll_time;
157         int is_buffer_enabled[LOG_ID_MAX];
158         struct buffer_config_data buffers[LOG_ID_MAX];
159         char *dynamic_config_dir;
160         char *qos_file_path;
161         int qos_max_throughput;
162         int qos_limit_duration;
163         int qos_threshold;
164         int qos_threshold_reapply;
165         qos_distribution_func distribution_func;
166         char *first_time_file_path;
167         log_print_format default_format;
168 };
169
170 void remove_reader_fd_entities(struct logger *server, struct reader_common *reader);
171 void check_if_fd_limit_reached(struct logger *server, int err);
172 int service_writer_kmsg(struct logger *server, struct writer *wr, struct epoll_event *event);
173 int service_writer_socket(struct logger *server, struct writer *wr, struct epoll_event *event);
174 int service_writer_syslog(struct logger *server, struct writer *wr, struct epoll_event *event);
175 void logger_add_writer(struct logger *l, struct writer *wr);
176 int create_fifo_fds(struct logger *server, int pipe_fd[2], int flags, bool dump);
177 int add_reader_pipe(struct logger *server, struct reader_pipe *reader);
178 int add_reader_memory(struct logger *server, struct reader_memory *reader);
179 void flush_logfile_timely(struct log_file *file, struct timespec ts, int flush_time);
180 int get_now(struct now_t *now);
181
182 #ifdef __cplusplus
183 }
184 #endif