Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 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 | */ |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 16 | #include "repository.hpp" |
| 17 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 18 | #include <sys/stat.h> |
| 19 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <phosphor-logging/log.hpp> |
| 22 | #include <xyz/openbmc_project/Common/File/error.hpp> |
| 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace pels |
| 27 | { |
| 28 | |
| 29 | namespace fs = std::filesystem; |
| 30 | using namespace phosphor::logging; |
| 31 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 32 | |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 33 | constexpr size_t warningPercentage = 95; |
| 34 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 35 | /** |
| 36 | * @brief Returns the amount of space the file uses on disk. |
| 37 | * |
| 38 | * This is different than just the regular size of the file. |
| 39 | * |
| 40 | * @param[in] file - The file to get the size of |
| 41 | * |
| 42 | * @return size_t The disk space the file uses |
| 43 | */ |
| 44 | size_t getFileDiskSize(const std::filesystem::path& file) |
| 45 | { |
| 46 | constexpr size_t statBlockSize = 512; |
| 47 | struct stat statData; |
| 48 | auto rc = stat(file.c_str(), &statData); |
| 49 | if (rc != 0) |
| 50 | { |
| 51 | auto e = errno; |
| 52 | std::string msg = "call to stat() failed on " + file.native() + |
| 53 | " with errno " + std::to_string(e); |
| 54 | log<level::ERR>(msg.c_str()); |
| 55 | abort(); |
| 56 | } |
| 57 | |
| 58 | return statData.st_blocks * statBlockSize; |
| 59 | } |
| 60 | |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 61 | Repository::Repository(const std::filesystem::path& basePath, size_t repoSize, |
| 62 | size_t maxNumPELs) : |
| 63 | _logPath(basePath / "logs"), |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 64 | _maxRepoSize(repoSize), _maxNumPELs(maxNumPELs), |
| 65 | _archivePath(basePath / "logs" / "archive") |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 66 | { |
| 67 | if (!fs::exists(_logPath)) |
| 68 | { |
| 69 | fs::create_directories(_logPath); |
| 70 | } |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 71 | |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 72 | if (!fs::exists(_archivePath)) |
| 73 | { |
| 74 | fs::create_directories(_archivePath); |
| 75 | } |
| 76 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 77 | restore(); |
| 78 | } |
| 79 | |
| 80 | void Repository::restore() |
| 81 | { |
| 82 | for (auto& dirEntry : fs::directory_iterator(_logPath)) |
| 83 | { |
| 84 | try |
| 85 | { |
| 86 | if (!fs::is_regular_file(dirEntry.path())) |
| 87 | { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | std::ifstream file{dirEntry.path()}; |
| 92 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 93 | std::istreambuf_iterator<char>()}; |
| 94 | file.close(); |
| 95 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 96 | PEL pel{data}; |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 97 | if (pel.valid()) |
| 98 | { |
Matt Spinler | a3c12a4 | 2019-11-21 13:25:32 -0600 | [diff] [blame] | 99 | // If the host hasn't acked it, reset the host state so |
| 100 | // it will get sent up again. |
| 101 | if (pel.hostTransmissionState() == TransmissionState::sent) |
| 102 | { |
| 103 | pel.setHostTransmissionState(TransmissionState::newPEL); |
| 104 | try |
| 105 | { |
| 106 | write(pel, dirEntry.path()); |
| 107 | } |
| 108 | catch (std::exception& e) |
| 109 | { |
| 110 | log<level::ERR>( |
| 111 | "Failed to save PEL after updating host state", |
| 112 | entry("PELID=0x%X", pel.id())); |
| 113 | } |
| 114 | } |
| 115 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 116 | PELAttributes attributes{dirEntry.path(), |
| 117 | getFileDiskSize(dirEntry.path()), |
| 118 | pel.privateHeader().creatorID(), |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 119 | pel.userHeader().subsystem(), |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 120 | pel.userHeader().severity(), |
| 121 | pel.userHeader().actionFlags(), |
| 122 | pel.hostTransmissionState(), |
| 123 | pel.hmcTransmissionState()}; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 124 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 125 | using pelID = LogID::Pel; |
| 126 | using obmcID = LogID::Obmc; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 127 | _pelAttributes.emplace( |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 128 | LogID(pelID(pel.id()), obmcID(pel.obmcLogID())), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 129 | attributes); |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 130 | |
| 131 | updateRepoStats(attributes, true); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
| 135 | log<level::ERR>( |
| 136 | "Found invalid PEL file while restoring. Removing.", |
| 137 | entry("FILENAME=%s", dirEntry.path().c_str())); |
| 138 | fs::remove(dirEntry.path()); |
| 139 | } |
| 140 | } |
| 141 | catch (std::exception& e) |
| 142 | { |
| 143 | log<level::ERR>("Hit exception while restoring PEL File", |
| 144 | entry("FILENAME=%s", dirEntry.path().c_str()), |
| 145 | entry("ERROR=%s", e.what())); |
| 146 | } |
| 147 | } |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 148 | |
| 149 | // Get size of archive folder |
| 150 | for (auto& dirEntry : fs::directory_iterator(_archivePath)) |
| 151 | { |
| 152 | _archiveSize += getFileDiskSize(dirEntry); |
| 153 | } |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time) |
| 157 | { |
| 158 | char name[50]; |
| 159 | sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB, |
| 160 | time.yearLSB, time.month, time.day, time.hour, time.minutes, |
| 161 | time.seconds, time.hundredths, pelID); |
| 162 | return std::string{name}; |
| 163 | } |
| 164 | |
| 165 | void Repository::add(std::unique_ptr<PEL>& pel) |
| 166 | { |
Matt Spinler | df43a30 | 2019-11-21 13:16:56 -0600 | [diff] [blame] | 167 | pel->setHostTransmissionState(TransmissionState::newPEL); |
| 168 | pel->setHMCTransmissionState(TransmissionState::newPEL); |
| 169 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 170 | auto path = _logPath / getPELFilename(pel->id(), pel->commitTime()); |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 171 | |
| 172 | write(*(pel.get()), path); |
| 173 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 174 | PELAttributes attributes{path, |
| 175 | getFileDiskSize(path), |
| 176 | pel->privateHeader().creatorID(), |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 177 | pel->userHeader().subsystem(), |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 178 | pel->userHeader().severity(), |
| 179 | pel->userHeader().actionFlags(), |
Matt Spinler | 346f99a | 2019-11-21 13:06:35 -0600 | [diff] [blame] | 180 | pel->hostTransmissionState(), |
| 181 | pel->hmcTransmissionState()}; |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 182 | |
| 183 | using pelID = LogID::Pel; |
| 184 | using obmcID = LogID::Obmc; |
| 185 | _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())), |
| 186 | attributes); |
| 187 | |
Matt Spinler | 44893cc | 2020-08-26 11:34:17 -0500 | [diff] [blame] | 188 | _lastPelID = pel->id(); |
| 189 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 190 | updateRepoStats(attributes, true); |
| 191 | |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 192 | processAddCallbacks(*pel); |
| 193 | } |
| 194 | |
| 195 | void Repository::write(const PEL& pel, const fs::path& path) |
| 196 | { |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 197 | std::ofstream file{path, std::ios::binary}; |
| 198 | |
| 199 | if (!file.good()) |
| 200 | { |
| 201 | // If this fails, the filesystem is probably full so it isn't like |
| 202 | // we could successfully create yet another error log here. |
| 203 | auto e = errno; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 204 | fs::remove(path); |
| 205 | log<level::ERR>("Unable to open PEL file for writing", |
| 206 | entry("ERRNO=%d", e), entry("PATH=%s", path.c_str())); |
| 207 | throw file_error::Open(); |
| 208 | } |
| 209 | |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 210 | auto data = pel.data(); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 211 | file.write(reinterpret_cast<const char*>(data.data()), data.size()); |
| 212 | |
| 213 | if (file.fail()) |
| 214 | { |
| 215 | // Same note as above about not being able to create an error log |
| 216 | // for this case even if we wanted. |
| 217 | auto e = errno; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 218 | file.close(); |
| 219 | fs::remove(path); |
| 220 | log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e), |
| 221 | entry("PATH=%s", path.c_str())); |
| 222 | throw file_error::Write(); |
| 223 | } |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 226 | std::optional<Repository::LogID> Repository::remove(const LogID& id) |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 227 | { |
| 228 | auto pel = findPEL(id); |
Patrick Williams | ff6b598 | 2021-04-22 09:04:17 -0500 | [diff] [blame] | 229 | if (pel == _pelAttributes.end()) |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 230 | { |
Patrick Williams | ff6b598 | 2021-04-22 09:04:17 -0500 | [diff] [blame] | 231 | return std::nullopt; |
Matt Spinler | 5f5352e | 2020-03-05 16:23:27 -0600 | [diff] [blame] | 232 | } |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 233 | |
Patrick Williams | ff6b598 | 2021-04-22 09:04:17 -0500 | [diff] [blame] | 234 | LogID actualID = pel->first; |
| 235 | updateRepoStats(pel->second, false); |
| 236 | |
| 237 | log<level::DEBUG>("Removing PEL from repository", |
| 238 | entry("PEL_ID=0x%X", actualID.pelID.id), |
| 239 | entry("OBMC_LOG_ID=%d", actualID.obmcID.id)); |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 240 | |
| 241 | if (fs::exists(pel->second.path)) |
| 242 | { |
| 243 | // Check for existense of new archive folder |
| 244 | if (!fs::exists(_archivePath)) |
| 245 | { |
| 246 | fs::create_directories(_archivePath); |
| 247 | } |
| 248 | |
| 249 | // Move log file to archive folder |
| 250 | auto fileName = _archivePath / pel->second.path.filename(); |
| 251 | fs::rename(pel->second.path, fileName); |
| 252 | |
| 253 | // Update size of file |
| 254 | _archiveSize += getFileDiskSize(fileName); |
| 255 | } |
| 256 | |
Patrick Williams | ff6b598 | 2021-04-22 09:04:17 -0500 | [diff] [blame] | 257 | _pelAttributes.erase(pel); |
| 258 | |
| 259 | processDeleteCallbacks(actualID.pelID.id); |
| 260 | |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 261 | return actualID; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 264 | std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) |
| 265 | { |
| 266 | auto pel = findPEL(id); |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 267 | if (pel != _pelAttributes.end()) |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 268 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 269 | std::ifstream file{pel->second.path.c_str()}; |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 270 | if (!file.good()) |
| 271 | { |
| 272 | auto e = errno; |
| 273 | log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 274 | entry("PATH=%s", pel->second.path.c_str())); |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 275 | throw file_error::Open(); |
| 276 | } |
| 277 | |
| 278 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 279 | std::istreambuf_iterator<char>()}; |
| 280 | return data; |
| 281 | } |
| 282 | |
| 283 | return std::nullopt; |
| 284 | } |
| 285 | |
Matt Spinler | 6d51224 | 2019-12-09 13:44:17 -0600 | [diff] [blame] | 286 | std::optional<sdbusplus::message::unix_fd> Repository::getPELFD(const LogID& id) |
| 287 | { |
| 288 | auto pel = findPEL(id); |
| 289 | if (pel != _pelAttributes.end()) |
| 290 | { |
| 291 | FILE* fp = fopen(pel->second.path.c_str(), "rb"); |
| 292 | |
| 293 | if (fp == nullptr) |
| 294 | { |
| 295 | auto e = errno; |
| 296 | log<level::ERR>("Unable to open PEL File", entry("ERRNO=%d", e), |
| 297 | entry("PATH=%s", pel->second.path.c_str())); |
| 298 | throw file_error::Open(); |
| 299 | } |
| 300 | |
| 301 | // Must leave the file open here. It will be closed by sdbusplus |
| 302 | // when it sends it back over D-Bus. |
| 303 | |
| 304 | return fileno(fp); |
| 305 | } |
| 306 | return std::nullopt; |
| 307 | } |
| 308 | |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 309 | void Repository::for_each(ForEachFunc func) const |
| 310 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 311 | for (const auto& [id, attributes] : _pelAttributes) |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 312 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 313 | std::ifstream file{attributes.path}; |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 314 | |
| 315 | if (!file.good()) |
| 316 | { |
| 317 | auto e = errno; |
| 318 | log<level::ERR>("Repository::for_each: Unable to open PEL file", |
| 319 | entry("ERRNO=%d", e), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 320 | entry("PATH=%s", attributes.path.c_str())); |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 321 | continue; |
| 322 | } |
| 323 | |
| 324 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 325 | std::istreambuf_iterator<char>()}; |
| 326 | file.close(); |
| 327 | |
| 328 | PEL pel{data}; |
| 329 | |
| 330 | try |
| 331 | { |
| 332 | if (func(pel)) |
| 333 | { |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | catch (std::exception& e) |
| 338 | { |
| 339 | log<level::ERR>("Repository::for_each function exception", |
| 340 | entry("ERROR=%s", e.what())); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 345 | void Repository::processAddCallbacks(const PEL& pel) const |
| 346 | { |
| 347 | for (auto& [name, func] : _addSubscriptions) |
| 348 | { |
| 349 | try |
| 350 | { |
| 351 | func(pel); |
| 352 | } |
| 353 | catch (std::exception& e) |
| 354 | { |
| 355 | log<level::ERR>("PEL Repository add callback exception", |
| 356 | entry("NAME=%s", name.c_str()), |
| 357 | entry("ERROR=%s", e.what())); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void Repository::processDeleteCallbacks(uint32_t id) const |
| 363 | { |
| 364 | for (auto& [name, func] : _deleteSubscriptions) |
| 365 | { |
| 366 | try |
| 367 | { |
| 368 | func(id); |
| 369 | } |
| 370 | catch (std::exception& e) |
| 371 | { |
| 372 | log<level::ERR>("PEL Repository delete callback exception", |
| 373 | entry("NAME=%s", name.c_str()), |
| 374 | entry("ERROR=%s", e.what())); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 379 | std::optional<std::reference_wrapper<const Repository::PELAttributes>> |
| 380 | Repository::getPELAttributes(const LogID& id) const |
| 381 | { |
| 382 | auto pel = findPEL(id); |
| 383 | if (pel != _pelAttributes.end()) |
| 384 | { |
| 385 | return pel->second; |
| 386 | } |
| 387 | |
| 388 | return std::nullopt; |
| 389 | } |
| 390 | |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 391 | void Repository::setPELHostTransState(uint32_t pelID, TransmissionState state) |
| 392 | { |
| 393 | LogID id{LogID::Pel{pelID}}; |
| 394 | auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(), |
| 395 | [&id](const auto& a) { return a.first == id; }); |
| 396 | |
| 397 | if ((attr != _pelAttributes.end()) && (attr->second.hostState != state)) |
| 398 | { |
| 399 | PELUpdateFunc func = [state](PEL& pel) { |
| 400 | pel.setHostTransmissionState(state); |
| 401 | }; |
| 402 | |
| 403 | try |
| 404 | { |
| 405 | updatePEL(attr->second.path, func); |
| 406 | |
| 407 | attr->second.hostState = state; |
| 408 | } |
| 409 | catch (std::exception& e) |
| 410 | { |
| 411 | log<level::ERR>("Unable to update PEL host transmission state", |
| 412 | entry("PATH=%s", attr->second.path.c_str()), |
| 413 | entry("ERROR=%s", e.what())); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void Repository::setPELHMCTransState(uint32_t pelID, TransmissionState state) |
| 419 | { |
| 420 | LogID id{LogID::Pel{pelID}}; |
| 421 | auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(), |
| 422 | [&id](const auto& a) { return a.first == id; }); |
| 423 | |
| 424 | if ((attr != _pelAttributes.end()) && (attr->second.hmcState != state)) |
| 425 | { |
| 426 | PELUpdateFunc func = [state](PEL& pel) { |
| 427 | pel.setHMCTransmissionState(state); |
| 428 | }; |
| 429 | |
| 430 | try |
| 431 | { |
| 432 | updatePEL(attr->second.path, func); |
| 433 | |
| 434 | attr->second.hmcState = state; |
| 435 | } |
| 436 | catch (std::exception& e) |
| 437 | { |
| 438 | log<level::ERR>("Unable to update PEL HMC transmission state", |
| 439 | entry("PATH=%s", attr->second.path.c_str()), |
| 440 | entry("ERROR=%s", e.what())); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void Repository::updatePEL(const fs::path& path, PELUpdateFunc updateFunc) |
| 446 | { |
| 447 | std::ifstream file{path}; |
| 448 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 449 | std::istreambuf_iterator<char>()}; |
| 450 | file.close(); |
| 451 | |
| 452 | PEL pel{data}; |
| 453 | |
| 454 | if (pel.valid()) |
| 455 | { |
| 456 | updateFunc(pel); |
| 457 | |
| 458 | write(pel, path); |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | throw std::runtime_error( |
| 463 | "Unable to read a valid PEL when trying to update it"); |
| 464 | } |
| 465 | } |
| 466 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 467 | bool Repository::isServiceableSev(const PELAttributes& pel) |
| 468 | { |
| 469 | auto sevType = static_cast<SeverityType>(pel.severity & 0xF0); |
| 470 | auto sevPVEntry = |
| 471 | pel_values::findByValue(pel.severity, pel_values::severityValues); |
| 472 | std::string sevName = std::get<pel_values::registryNamePos>(*sevPVEntry); |
| 473 | |
| 474 | bool check1 = (sevType == SeverityType::predictive) || |
| 475 | (sevType == SeverityType::unrecoverable) || |
| 476 | (sevType == SeverityType::critical); |
| 477 | |
| 478 | bool check2 = ((sevType == SeverityType::recovered) || |
| 479 | (sevName == "symptom_recovered")) && |
| 480 | !pel.actionFlags.test(hiddenFlagBit); |
| 481 | |
| 482 | bool check3 = (sevName == "symptom_predictive") || |
| 483 | (sevName == "symptom_unrecoverable") || |
| 484 | (sevName == "symptom_critical"); |
| 485 | |
| 486 | return check1 || check2 || check3; |
| 487 | } |
| 488 | |
| 489 | void Repository::updateRepoStats(const PELAttributes& pel, bool pelAdded) |
| 490 | { |
| 491 | auto isServiceable = Repository::isServiceableSev(pel); |
| 492 | auto bmcPEL = CreatorID::openBMC == static_cast<CreatorID>(pel.creator); |
| 493 | |
| 494 | auto adjustSize = [pelAdded, &pel](auto& runningSize) { |
| 495 | if (pelAdded) |
| 496 | { |
| 497 | runningSize += pel.sizeOnDisk; |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | runningSize = std::max(static_cast<int64_t>(runningSize) - |
| 502 | static_cast<int64_t>(pel.sizeOnDisk), |
| 503 | static_cast<int64_t>(0)); |
| 504 | } |
| 505 | }; |
| 506 | |
| 507 | adjustSize(_sizes.total); |
| 508 | |
| 509 | if (bmcPEL) |
| 510 | { |
| 511 | adjustSize(_sizes.bmc); |
| 512 | if (isServiceable) |
| 513 | { |
| 514 | adjustSize(_sizes.bmcServiceable); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | adjustSize(_sizes.bmcInfo); |
| 519 | } |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | adjustSize(_sizes.nonBMC); |
| 524 | if (isServiceable) |
| 525 | { |
| 526 | adjustSize(_sizes.nonBMCServiceable); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | adjustSize(_sizes.nonBMCInfo); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 535 | bool Repository::sizeWarning() |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 536 | { |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 537 | std::error_code ec; |
| 538 | |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 539 | if ((_archiveSize > 0) && ((_sizes.total + _archiveSize) > |
| 540 | ((_maxRepoSize * warningPercentage) / 100))) |
| 541 | { |
| 542 | log<level::INFO>( |
| 543 | "Repository::sizeWarning function:Deleting the files in archive"); |
| 544 | |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 545 | for (const auto& dirEntry : fs::directory_iterator(_archivePath)) |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 546 | { |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 547 | fs::remove(dirEntry.path(), ec); |
| 548 | if (ec) |
| 549 | { |
| 550 | log<level::INFO>( |
| 551 | "Repository::sizeWarning function:Could not delete " |
| 552 | "a file in PEL archive", |
| 553 | entry("FILENAME=%s", dirEntry.path().c_str())); |
| 554 | } |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 555 | } |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 556 | |
| 557 | _archiveSize = 0; |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 558 | } |
| 559 | |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 560 | return (_sizes.total > (_maxRepoSize * warningPercentage / 100)) || |
| 561 | (_pelAttributes.size() > _maxNumPELs); |
| 562 | } |
| 563 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 564 | std::vector<Repository::AttributesReference> |
| 565 | Repository::getAllPELAttributes(SortOrder order) const |
| 566 | { |
| 567 | std::vector<Repository::AttributesReference> attributes; |
| 568 | |
| 569 | std::for_each( |
| 570 | _pelAttributes.begin(), _pelAttributes.end(), |
| 571 | [&attributes](auto& pelEntry) { attributes.push_back(pelEntry); }); |
| 572 | |
| 573 | std::sort(attributes.begin(), attributes.end(), |
| 574 | [order](const auto& left, const auto& right) { |
| 575 | if (order == SortOrder::ascending) |
| 576 | { |
| 577 | return left.get().second.path < right.get().second.path; |
| 578 | } |
| 579 | return left.get().second.path > right.get().second.path; |
| 580 | }); |
| 581 | |
| 582 | return attributes; |
| 583 | } |
| 584 | |
| 585 | std::vector<uint32_t> Repository::prune() |
| 586 | { |
| 587 | std::vector<uint32_t> obmcLogIDs; |
| 588 | std::string msg = "Pruning PEL repository that takes up " + |
| 589 | std::to_string(_sizes.total) + " bytes and has " + |
| 590 | std::to_string(_pelAttributes.size()) + " PELs"; |
| 591 | log<level::INFO>(msg.c_str()); |
| 592 | |
| 593 | // Set up the 5 functions to check if the PEL category |
| 594 | // is still over its limits. |
| 595 | |
| 596 | // BMC informational PELs should only take up 15% |
| 597 | IsOverLimitFunc overBMCInfoLimit = [this]() { |
| 598 | return _sizes.bmcInfo > _maxRepoSize * 15 / 100; |
| 599 | }; |
| 600 | |
| 601 | // BMC non informational PELs should only take up 30% |
| 602 | IsOverLimitFunc overBMCNonInfoLimit = [this]() { |
| 603 | return _sizes.bmcServiceable > _maxRepoSize * 30 / 100; |
| 604 | }; |
| 605 | |
| 606 | // Non BMC informational PELs should only take up 15% |
| 607 | IsOverLimitFunc overNonBMCInfoLimit = [this]() { |
| 608 | return _sizes.nonBMCInfo > _maxRepoSize * 15 / 100; |
| 609 | }; |
| 610 | |
| 611 | // Non BMC non informational PELs should only take up 15% |
| 612 | IsOverLimitFunc overNonBMCNonInfoLimit = [this]() { |
| 613 | return _sizes.nonBMCServiceable > _maxRepoSize * 30 / 100; |
| 614 | }; |
| 615 | |
| 616 | // Bring the total number of PELs down to 80% of the max |
| 617 | IsOverLimitFunc tooManyPELsLimit = [this]() { |
| 618 | return _pelAttributes.size() > _maxNumPELs * 80 / 100; |
| 619 | }; |
| 620 | |
| 621 | // Set up the functions to determine which category a PEL is in. |
| 622 | // TODO: Return false in these functions if a PEL caused a guard record. |
| 623 | |
| 624 | // A BMC informational PEL |
| 625 | IsPELTypeFunc isBMCInfo = [](const PELAttributes& pel) { |
| 626 | return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) && |
| 627 | !Repository::isServiceableSev(pel); |
| 628 | }; |
| 629 | |
| 630 | // A BMC non informational PEL |
| 631 | IsPELTypeFunc isBMCNonInfo = [](const PELAttributes& pel) { |
| 632 | return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) && |
| 633 | Repository::isServiceableSev(pel); |
| 634 | }; |
| 635 | |
| 636 | // A non BMC informational PEL |
| 637 | IsPELTypeFunc isNonBMCInfo = [](const PELAttributes& pel) { |
| 638 | return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) && |
| 639 | !Repository::isServiceableSev(pel); |
| 640 | }; |
| 641 | |
| 642 | // A non BMC non informational PEL |
| 643 | IsPELTypeFunc isNonBMCNonInfo = [](const PELAttributes& pel) { |
| 644 | return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) && |
| 645 | Repository::isServiceableSev(pel); |
| 646 | }; |
| 647 | |
| 648 | // When counting PELs, count every PEL |
Patrick Williams | d26fa3e | 2021-04-21 15:22:23 -0500 | [diff] [blame] | 649 | IsPELTypeFunc isAnyPEL = [](const PELAttributes& /*pel*/) { return true; }; |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 650 | |
| 651 | // Check all 4 categories, which will result in at most 90% |
| 652 | // usage (15 + 30 + 15 + 30). |
| 653 | removePELs(overBMCInfoLimit, isBMCInfo, obmcLogIDs); |
| 654 | removePELs(overBMCNonInfoLimit, isBMCNonInfo, obmcLogIDs); |
| 655 | removePELs(overNonBMCInfoLimit, isNonBMCInfo, obmcLogIDs); |
| 656 | removePELs(overNonBMCNonInfoLimit, isNonBMCNonInfo, obmcLogIDs); |
| 657 | |
| 658 | // After the above pruning check if there are still too many PELs, |
| 659 | // which can happen depending on PEL sizes. |
| 660 | if (_pelAttributes.size() > _maxNumPELs) |
| 661 | { |
| 662 | removePELs(tooManyPELsLimit, isAnyPEL, obmcLogIDs); |
| 663 | } |
| 664 | |
| 665 | if (!obmcLogIDs.empty()) |
| 666 | { |
| 667 | std::string msg = "Number of PELs removed to save space: " + |
| 668 | std::to_string(obmcLogIDs.size()); |
| 669 | log<level::INFO>(msg.c_str()); |
| 670 | } |
| 671 | |
| 672 | return obmcLogIDs; |
| 673 | } |
| 674 | |
| 675 | void Repository::removePELs(IsOverLimitFunc& isOverLimit, |
| 676 | IsPELTypeFunc& isPELType, |
| 677 | std::vector<uint32_t>& removedBMCLogIDs) |
| 678 | { |
| 679 | if (!isOverLimit()) |
| 680 | { |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | auto attributes = getAllPELAttributes(SortOrder::ascending); |
| 685 | |
| 686 | // Make 4 passes on the PELs, stopping as soon as isOverLimit |
| 687 | // returns false. |
| 688 | // Pass 1: only delete HMC acked PELs |
| 689 | // Pass 2: only delete OS acked PELs |
| 690 | // Pass 3: only delete PHYP sent PELs |
| 691 | // Pass 4: delete all PELs |
| 692 | static const std::vector<std::function<bool(const PELAttributes& pel)>> |
| 693 | stateChecks{[](const auto& pel) { |
| 694 | return pel.hmcState == TransmissionState::acked; |
| 695 | }, |
| 696 | |
| 697 | [](const auto& pel) { |
| 698 | return pel.hostState == TransmissionState::acked; |
| 699 | }, |
| 700 | |
| 701 | [](const auto& pel) { |
| 702 | return pel.hostState == TransmissionState::sent; |
| 703 | }, |
| 704 | |
Patrick Williams | d26fa3e | 2021-04-21 15:22:23 -0500 | [diff] [blame] | 705 | [](const auto& /*pel*/) { return true; }}; |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 706 | |
| 707 | for (const auto& stateCheck : stateChecks) |
| 708 | { |
| 709 | for (auto it = attributes.begin(); it != attributes.end();) |
| 710 | { |
| 711 | const auto& pel = it->get(); |
| 712 | if (isPELType(pel.second) && stateCheck(pel.second)) |
| 713 | { |
| 714 | auto removedID = pel.first.obmcID.id; |
| 715 | remove(pel.first); |
| 716 | |
| 717 | removedBMCLogIDs.push_back(removedID); |
| 718 | |
| 719 | attributes.erase(it); |
| 720 | |
| 721 | if (!isOverLimit()) |
| 722 | { |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | ++it; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | if (!isOverLimit()) |
| 733 | { |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
Sumit Kumar | 2ccdcef | 2021-07-31 10:04:58 -0500 | [diff] [blame] | 739 | void Repository::archivePEL(const PEL& pel) |
| 740 | { |
| 741 | if (pel.valid()) |
| 742 | { |
| 743 | auto path = _archivePath / getPELFilename(pel.id(), pel.commitTime()); |
| 744 | |
| 745 | write(pel, path); |
| 746 | |
| 747 | _archiveSize += getFileDiskSize(path); |
| 748 | } |
| 749 | } |
| 750 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 751 | } // namespace pels |
| 752 | } // namespace openpower |