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 | 81b3eab | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 28 | // Hijack this header as a common include file used by most all sources |
| 29 | // to report some utilities defined here and there. |
| 30 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | // Furnished in main.cpp. Caller must own and free returned value |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 34 | char *uidToName(uid_t uid); |
| 35 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 36 | // Furnished in LogStatistics.cpp. Caller must own and free returned value |
| 37 | char *pidToName(pid_t pid); |
Mark Salyzyn | 17ed679 | 2015-04-20 13:35:15 -0700 | [diff] [blame] | 38 | char *tidToName(pid_t tid); |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 39 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 40 | // Furnished in main.cpp. Thread safe. |
| 41 | const char *tagToName(uint32_t tag); |
| 42 | |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 45 | static inline bool worstUidEnabledForLogid(log_id_t id) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 46 | return (id != LOG_ID_CRASH) && (id != LOG_ID_KERNEL) && (id != LOG_ID_EVENTS); |
Mark Salyzyn | ae76923 | 2015-03-17 17:17:25 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 49 | class LogBuffer; |
| 50 | |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame^] | 51 | #define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve |
| 52 | // non-chatty UIDs less than this age in hours |
| 53 | #define EXPIRE_THRESHOLD 4 // A smaller expire count is considered too |
| 54 | // chatty for the temporal expire messages |
| 55 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 56 | class LogBufferElement { |
| 57 | const log_id_t mLogId; |
| 58 | const uid_t mUid; |
| 59 | const pid_t mPid; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 60 | const pid_t mTid; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 61 | char *mMsg; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 62 | union { |
| 63 | const unsigned short mMsgLen; // mMSg != NULL |
| 64 | unsigned short mDropped; // mMsg == NULL |
| 65 | }; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 66 | const uint64_t mSequence; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 67 | const log_time mRealTime; |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 68 | static atomic_int_fast64_t sequence; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 69 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 70 | // assumption: mMsg == NULL |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 71 | size_t populateDroppedMessage(char *&buffer, |
| 72 | LogBuffer *parent); |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 73 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 74 | public: |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 75 | LogBufferElement(log_id_t log_id, log_time realtime, |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 76 | uid_t uid, pid_t pid, pid_t tid, |
| 77 | const char *msg, unsigned short len); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 78 | virtual ~LogBufferElement(); |
| 79 | |
| 80 | log_id_t getLogId() const { return mLogId; } |
| 81 | uid_t getUid(void) const { return mUid; } |
| 82 | pid_t getPid(void) const { return mPid; } |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 83 | pid_t getTid(void) const { return mTid; } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 84 | unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; } |
| 85 | unsigned short setDropped(unsigned short value) { |
| 86 | if (mMsg) { |
| 87 | free(mMsg); |
| 88 | mMsg = NULL; |
| 89 | } |
| 90 | return mDropped = value; |
| 91 | } |
| 92 | unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; } |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 93 | uint64_t getSequence(void) const { return mSequence; } |
| 94 | static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | log_time getRealTime(void) const { return mRealTime; } |
| 96 | |
Mark Salyzyn | 344bff4 | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 97 | uint32_t getTag(void) const; |
| 98 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 99 | static const uint64_t FLUSH_ERROR; |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 100 | uint64_t flushTo(SocketClient *writer, LogBuffer *parent); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #endif |