Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Cherry | 64458c7 | 2019-10-15 15:10:26 -0700 | [diff] [blame] | 17 | #pragma once |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 24 | |
| 25 | #include "LogWriter.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | |
Tom Cherry | 3dd3ec3 | 2020-06-02 15:39:21 -0700 | [diff] [blame] | 27 | #include "LogStatistics.h" |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 29 | #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 Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 35 | class __attribute__((packed)) LogBufferElement { |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 36 | public: |
Tom Cherry | a3c5ff5 | 2020-05-21 13:56:33 -0700 | [diff] [blame] | 37 | 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 Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 39 | LogBufferElement(const LogBufferElement& elem); |
Tom Cherry | 9b4246d | 2020-06-17 11:40:55 -0700 | [diff] [blame] | 40 | LogBufferElement(LogBufferElement&& elem) noexcept; |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 41 | ~LogBufferElement(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 42 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 43 | uint32_t GetTag() const; |
| 44 | uint16_t SetDropped(uint16_t value); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 46 | bool FlushTo(LogWriter* writer, LogStatistics* parent, bool lastSame); |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 47 | |
Tom Cherry | 3dd3ec3 | 2020-06-02 15:39:21 -0700 | [diff] [blame] | 48 | LogStatisticsElement ToLogStatisticsElement() const; |
| 49 | |
Tom Cherry | 9787f9a | 2020-05-19 19:01:16 -0700 | [diff] [blame] | 50 | 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 Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 80 | }; |