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 | |
| 17 | #ifndef _LOGD_LOG_BUFFER_ELEMENT_H__ |
| 18 | #define _LOGD_LOG_BUFFER_ELEMENT_H__ |
| 19 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 20 | #include <stdatomic.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 24 | #include <sysutils/SocketClient.h> |
| 25 | #include <log/log.h> |
| 26 | #include <log/log_read.h> |
| 27 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 28 | class LogBuffer; |
| 29 | |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 30 | #define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve |
| 31 | // non-chatty UIDs less than this age in hours |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 32 | #define EXPIRE_THRESHOLD 10 // A smaller expire count is considered too |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 33 | // chatty for the temporal expire messages |
Mark Salyzyn | 047cc07 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 34 | #define EXPIRE_RATELIMIT 10 // maximum rate in seconds to report expiration |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 35 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | class LogBufferElement { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 37 | |
| 38 | friend LogBuffer; |
| 39 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 40 | const log_id_t mLogId; |
| 41 | const uid_t mUid; |
| 42 | const pid_t mPid; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 43 | const pid_t mTid; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 44 | char *mMsg; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 45 | union { |
| 46 | const unsigned short mMsgLen; // mMSg != NULL |
| 47 | unsigned short mDropped; // mMsg == NULL |
| 48 | }; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 49 | const uint64_t mSequence; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 50 | log_time mRealTime; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 51 | static atomic_int_fast64_t sequence; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 53 | // assumption: mMsg == NULL |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 54 | size_t populateDroppedMessage(char *&buffer, |
| 55 | LogBuffer *parent); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 56 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 57 | public: |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 58 | LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 59 | uid_t uid, pid_t pid, pid_t tid, |
| 60 | const char *msg, unsigned short len); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 61 | virtual ~LogBufferElement(); |
| 62 | |
| 63 | log_id_t getLogId() const { return mLogId; } |
| 64 | uid_t getUid(void) const { return mUid; } |
| 65 | pid_t getPid(void) const { return mPid; } |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 66 | pid_t getTid(void) const { return mTid; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 67 | unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; } |
| 68 | unsigned short setDropped(unsigned short value) { |
| 69 | if (mMsg) { |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 70 | delete [] mMsg; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 71 | mMsg = NULL; |
| 72 | } |
| 73 | return mDropped = value; |
| 74 | } |
| 75 | unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; } |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 76 | uint64_t getSequence(void) const { return mSequence; } |
| 77 | static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 78 | log_time getRealTime(void) const { return mRealTime; } |
| 79 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 80 | uint32_t getTag(void) const; |
| 81 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 82 | static const uint64_t FLUSH_ERROR; |
Mark Salyzyn | 7b87365 | 2015-12-03 15:38:35 -0800 | [diff] [blame] | 83 | uint64_t flushTo(SocketClient *writer, LogBuffer *parent, bool privileged); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | #endif |