mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 17 | #include <string.h> |
| 18 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 19 | #include <cinttypes> |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <tuple> |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
| 24 | #include "android-base/logging.h" |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 26 | |
| 27 | #include "wifilogd/byte_buffer.h" |
| 28 | #include "wifilogd/command_processor.h" |
| 29 | #include "wifilogd/local_utils.h" |
| 30 | #include "wifilogd/protocol.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace wifilogd { |
| 34 | |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 35 | using ::android::base::unique_fd; |
| 36 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 37 | using local_utils::CopyFromBufferOrDie; |
| 38 | using local_utils::GetMaxVal; |
| 39 | |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | uint32_t NsecToUsec(uint32_t nsec) { return nsec / 1000; } |
| 42 | } // namespace |
| 43 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 44 | CommandProcessor::CommandProcessor(size_t buffer_size_bytes) |
| 45 | : current_log_buffer_(buffer_size_bytes), os_(new Os()) {} |
| 46 | |
| 47 | CommandProcessor::CommandProcessor(size_t buffer_size_bytes, |
| 48 | std::unique_ptr<Os> os) |
| 49 | : current_log_buffer_(buffer_size_bytes), os_(std::move(os)) {} |
| 50 | |
mukesh agrawal | cc8458a | 2016-10-06 15:45:35 -0700 | [diff] [blame] | 51 | bool CommandProcessor::ProcessCommand(const void* input_buffer, |
| 52 | size_t n_bytes_read, int fd) { |
| 53 | unique_fd wrapped_fd(fd); |
| 54 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 55 | if (n_bytes_read < sizeof(protocol::Command)) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | const auto& command_header = |
| 60 | CopyFromBufferOrDie<protocol::Command>(input_buffer, n_bytes_read); |
| 61 | switch (command_header.opcode) { |
| 62 | using protocol::Opcode; |
| 63 | case Opcode::kWriteAsciiMessage: |
| 64 | // Copy the entire command to the log. This defers the cost of |
| 65 | // validating the rest of the CommandHeader until we dump the |
| 66 | // message. |
| 67 | // |
| 68 | // Note that most messages will be written but never read. So, in |
| 69 | // the common case, the validation cost is actually eliminated, |
| 70 | // rather than just deferred. |
| 71 | return CopyCommandToLog(input_buffer, n_bytes_read); |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 72 | case Opcode::kDumpBuffers: |
| 73 | return Dump(std::move(wrapped_fd)); |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | // Private methods below. |
| 78 | |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 79 | std::string CommandProcessor::TimestampHeader::ToString() const { |
| 80 | const auto& awake_time = since_boot_awake_only; |
| 81 | const auto& up_time = since_boot_with_sleep; |
| 82 | const auto& wall_time = since_epoch; |
| 83 | return base::StringPrintf("%" PRIu32 ".%06" PRIu32 |
| 84 | " " |
| 85 | "%" PRIu32 ".%06" PRIu32 |
| 86 | " " |
| 87 | "%" PRIu32 ".%06" PRIu32, |
| 88 | awake_time.secs, NsecToUsec(awake_time.nsecs), |
| 89 | up_time.secs, NsecToUsec(up_time.nsecs), |
| 90 | wall_time.secs, NsecToUsec(wall_time.nsecs)); |
| 91 | } |
| 92 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 93 | bool CommandProcessor::CopyCommandToLog(const void* command_buffer, |
| 94 | size_t command_len_in) { |
| 95 | const uint16_t command_len = |
| 96 | SAFELY_CLAMP(command_len_in, uint16_t, 0, protocol::kMaxMessageSize); |
| 97 | |
| 98 | uint16_t total_size; |
| 99 | static_assert(GetMaxVal(total_size) > sizeof(TimestampHeader) && |
| 100 | GetMaxVal(total_size) - sizeof(TimestampHeader) >= |
| 101 | protocol::kMaxMessageSize, |
| 102 | "total_size cannot represent some input messages"); |
| 103 | total_size = sizeof(TimestampHeader) + command_len; |
| 104 | CHECK(current_log_buffer_.CanFitEver(total_size)); |
| 105 | |
| 106 | if (!current_log_buffer_.CanFitNow(total_size)) { |
| 107 | // TODO(b/31867689): |
| 108 | // 1. compress current buffer |
| 109 | // 2. move old buffer to linked list |
| 110 | // 3. prune old buffers, if needed |
| 111 | current_log_buffer_.Clear(); |
| 112 | } |
| 113 | CHECK(current_log_buffer_.CanFitNow(total_size)); |
| 114 | |
| 115 | TimestampHeader tstamp_header; |
| 116 | tstamp_header.since_boot_awake_only = os_->GetTimestamp(CLOCK_MONOTONIC); |
| 117 | tstamp_header.since_boot_with_sleep = os_->GetTimestamp(CLOCK_BOOTTIME); |
| 118 | tstamp_header.since_epoch = os_->GetTimestamp(CLOCK_REALTIME); |
| 119 | |
| 120 | ByteBuffer<sizeof(TimestampHeader) + protocol::kMaxMessageSize> message_buf; |
| 121 | message_buf.AppendOrDie(&tstamp_header, sizeof(tstamp_header)); |
| 122 | message_buf.AppendOrDie(command_buffer, command_len); |
| 123 | |
| 124 | bool did_write = current_log_buffer_.Append( |
| 125 | message_buf.data(), |
| 126 | SAFELY_CLAMP(message_buf.size(), uint16_t, 0, GetMaxVal<uint16_t>())); |
| 127 | if (!did_write) { |
| 128 | // Given that we checked for enough free space, Append() should |
| 129 | // have succeeded. Hence, a failure here indicates a logic error, |
| 130 | // rather than a runtime error. |
| 131 | LOG(FATAL) << "Unexpected failure to Append()"; |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
mukesh agrawal | fd08064 | 2016-10-07 15:45:37 -0700 | [diff] [blame] | 137 | bool CommandProcessor::Dump(unique_fd dump_fd) { |
| 138 | const uint8_t* buf = nullptr; |
| 139 | size_t buflen = 0; |
| 140 | static_assert( |
| 141 | GetMaxVal(buflen) - sizeof(TimestampHeader) - sizeof(protocol::Command) >= |
| 142 | GetMaxVal<decltype(protocol::Command::payload_len)>(), |
| 143 | "buflen cannot accommodate some messages"); |
| 144 | |
| 145 | MessageBuffer::ScopedRewinder rewinder(¤t_log_buffer_); |
| 146 | std::tie(buf, buflen) = current_log_buffer_.ConsumeNextMessage(); |
| 147 | while (buf) { |
| 148 | const auto& tstamp_header = |
| 149 | CopyFromBufferOrDie<TimestampHeader>(buf, buflen); |
| 150 | buf += sizeof(tstamp_header); |
| 151 | buflen -= sizeof(tstamp_header); |
| 152 | |
| 153 | const std::string timestamp_string(tstamp_header.ToString() + '\n'); |
| 154 | size_t n_written; |
| 155 | Os::Errno err; |
| 156 | std::tie(n_written, err) = |
| 157 | os_->Write(dump_fd, timestamp_string.data(), timestamp_string.size()); |
| 158 | if (err == EINTR) { |
| 159 | // The next write will probably succeed. We dont't retry the current |
| 160 | // message, however, because we want to guarantee forward progress. |
| 161 | // |
| 162 | // TODO(b/32098735): Increment a counter, and attempt to output that |
| 163 | // counter after we've dumped all the log messages. |
| 164 | } else if (err) { |
| 165 | // Any error other than EINTR is considered unrecoverable. |
| 166 | LOG(ERROR) << "Terminating log dump, due to " << strerror(err); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | std::tie(buf, buflen) = current_log_buffer_.ConsumeNextMessage(); |
| 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
mukesh agrawal | 6306897 | 2016-09-29 17:42:46 -0700 | [diff] [blame] | 176 | } // namespace wifilogd |
| 177 | } // namespace android |