blob: b263ca5375101f5ced78104e9f5a98a0d1282e9f [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2014 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
Tom Cherry64458c72019-10-15 15:10:26 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070019#include <stdint.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070020#include <stdlib.h>
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070021#include <sys/types.h>
22
Mark Salyzynaeaaf812016-09-30 13:30:33 -070023#include <log/log.h>
Tom Cherry283c9a12020-05-14 19:25:05 -070024
25#include "LogWriter.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080026
Tom Cherry3dd3ec32020-06-02 15:39:21 -070027#include "LogStatistics.h"
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070028
Mark Salyzyn501c3732017-03-10 14:31:54 -080029#define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve
30 // non-chatty UIDs less than this age in hours
31#define EXPIRE_THRESHOLD 10 // A smaller expire count is considered too
32 // chatty for the temporal expire messages
33#define EXPIRE_RATELIMIT 10 // maximum rate in seconds to report expiration
Mark Salyzyn833a9b12015-05-15 15:58:17 -070034
Christopher Ferris74e74f92017-08-02 17:54:27 -070035class __attribute__((packed)) LogBufferElement {
Tom Cherry64e90162020-05-07 14:44:43 -070036 public:
Tom Cherrya3c5ff52020-05-21 13:56:33 -070037 LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
38 uint64_t sequence, const char* msg, uint16_t len);
Mark Salyzyn501c3732017-03-10 14:31:54 -080039 LogBufferElement(const LogBufferElement& elem);
Tom Cherry9b4246d2020-06-17 11:40:55 -070040 LogBufferElement(LogBufferElement&& elem) noexcept;
Christopher Ferris74e74f92017-08-02 17:54:27 -070041 ~LogBufferElement();
Mark Salyzyn0175b072014-02-26 09:50:16 -080042
Tom Cherry9787f9a2020-05-19 19:01:16 -070043 uint32_t GetTag() const;
44 uint16_t SetDropped(uint16_t value);
Mark Salyzyn0175b072014-02-26 09:50:16 -080045
Tom Cherry283c9a12020-05-14 19:25:05 -070046 bool FlushTo(LogWriter* writer, LogStatistics* parent, bool lastSame);
Tom Cherry9787f9a2020-05-19 19:01:16 -070047
Tom Cherry3dd3ec32020-06-02 15:39:21 -070048 LogStatisticsElement ToLogStatisticsElement() const;
49
Tom Cherry9787f9a2020-05-19 19:01:16 -070050 log_id_t log_id() const { return static_cast<log_id_t>(log_id_); }
51 uid_t uid() const { return uid_; }
52 pid_t pid() const { return pid_; }
53 pid_t tid() const { return tid_; }
54 uint16_t msg_len() const { return dropped_ ? 0 : msg_len_; }
55 const char* msg() const { return dropped_ ? nullptr : msg_; }
56 uint64_t sequence() const { return sequence_; }
57 log_time realtime() const { return realtime_; }
58 uint16_t dropped_count() const { return dropped_ ? dropped_count_ : 0; }
59
60 private:
61 // assumption: mDropped == true
62 size_t PopulateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame);
63
64 // sized to match reality of incoming log packets
65 const uint32_t uid_;
66 const uint32_t pid_;
67 const uint32_t tid_;
68 uint64_t sequence_;
69 log_time realtime_;
70 union {
71 char* msg_; // mDropped == false
72 int32_t tag_; // mDropped == true
73 };
74 union {
75 const uint16_t msg_len_; // mDropped == false
76 uint16_t dropped_count_; // mDropped == true
77 };
78 const uint8_t log_id_;
79 bool dropped_;
Mark Salyzyn0175b072014-02-26 09:50:16 -080080};