blob: fa2020c7c0e3bfa73d3ce97ac6f952adfcd3a3bc [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Spinler89fa0822019-07-17 13:54:30 -050016#include "repository.hpp"
17
Matt Spinler32a6df62023-01-12 16:30:40 -060018#include <fcntl.h>
Matt Spinlerdd325c32020-07-07 11:01:54 -050019#include <sys/stat.h>
20
Matt Spinler89fa0822019-07-17 13:54:30 -050021#include <phosphor-logging/log.hpp>
22#include <xyz/openbmc_project/Common/File/error.hpp>
23
Patrick Williams2544b412022-10-04 08:41:06 -050024#include <fstream>
25
Matt Spinler89fa0822019-07-17 13:54:30 -050026namespace openpower
27{
28namespace pels
29{
30
31namespace fs = std::filesystem;
32using namespace phosphor::logging;
33namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
34
Matt Spinler7e727a32020-07-07 15:00:17 -050035constexpr size_t warningPercentage = 95;
36
Matt Spinlerdd325c32020-07-07 11:01:54 -050037/**
38 * @brief Returns the amount of space the file uses on disk.
39 *
40 * This is different than just the regular size of the file.
41 *
42 * @param[in] file - The file to get the size of
43 *
44 * @return size_t The disk space the file uses
45 */
46size_t getFileDiskSize(const std::filesystem::path& file)
47{
48 constexpr size_t statBlockSize = 512;
49 struct stat statData;
50 auto rc = stat(file.c_str(), &statData);
51 if (rc != 0)
52 {
53 auto e = errno;
54 std::string msg = "call to stat() failed on " + file.native() +
55 " with errno " + std::to_string(e);
56 log<level::ERR>(msg.c_str());
57 abort();
58 }
59
60 return statData.st_blocks * statBlockSize;
61}
62
Matt Spinler8d5f3a22020-07-07 10:30:33 -050063Repository::Repository(const std::filesystem::path& basePath, size_t repoSize,
64 size_t maxNumPELs) :
65 _logPath(basePath / "logs"),
Sumit Kumar1d8835b2021-06-07 09:35:30 -050066 _maxRepoSize(repoSize), _maxNumPELs(maxNumPELs),
67 _archivePath(basePath / "logs" / "archive")
Matt Spinler89fa0822019-07-17 13:54:30 -050068{
69 if (!fs::exists(_logPath))
70 {
71 fs::create_directories(_logPath);
72 }
Matt Spinler475e5742019-07-18 16:09:49 -050073
Sumit Kumar1d8835b2021-06-07 09:35:30 -050074 if (!fs::exists(_archivePath))
75 {
76 fs::create_directories(_archivePath);
77 }
78
Matt Spinler475e5742019-07-18 16:09:49 -050079 restore();
80}
81
82void Repository::restore()
83{
84 for (auto& dirEntry : fs::directory_iterator(_logPath))
85 {
86 try
87 {
88 if (!fs::is_regular_file(dirEntry.path()))
89 {
90 continue;
91 }
92
93 std::ifstream file{dirEntry.path()};
94 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
95 std::istreambuf_iterator<char>()};
96 file.close();
97
Matt Spinler07eefc52019-09-26 11:18:26 -050098 PEL pel{data};
Matt Spinler475e5742019-07-18 16:09:49 -050099 if (pel.valid())
100 {
Matt Spinlera3c12a42019-11-21 13:25:32 -0600101 // If the host hasn't acked it, reset the host state so
102 // it will get sent up again.
103 if (pel.hostTransmissionState() == TransmissionState::sent)
104 {
105 pel.setHostTransmissionState(TransmissionState::newPEL);
106 try
107 {
108 write(pel, dirEntry.path());
109 }
Patrick Williams66491c62021-10-06 12:23:37 -0500110 catch (const std::exception& e)
Matt Spinlera3c12a42019-11-21 13:25:32 -0600111 {
112 log<level::ERR>(
113 "Failed to save PEL after updating host state",
114 entry("PELID=0x%X", pel.id()));
115 }
116 }
117
Matt Spinler8e65f4e2023-05-02 13:40:08 -0500118 PELAttributes attributes{
119 dirEntry.path(),
120 getFileDiskSize(dirEntry.path()),
121 pel.privateHeader().creatorID(),
122 pel.userHeader().subsystem(),
123 pel.userHeader().severity(),
124 pel.userHeader().actionFlags(),
125 pel.hostTransmissionState(),
126 pel.hmcTransmissionState(),
127 pel.plid(),
128 pel.getDeconfigFlag(),
129 pel.getGuardFlag(),
130 getMillisecondsSinceEpoch(
131 pel.privateHeader().createTimestamp())};
Matt Spinler0ff00482019-11-06 16:19:46 -0600132
Matt Spinler475e5742019-07-18 16:09:49 -0500133 using pelID = LogID::Pel;
134 using obmcID = LogID::Obmc;
Matt Spinler0ff00482019-11-06 16:19:46 -0600135 _pelAttributes.emplace(
Matt Spinler475e5742019-07-18 16:09:49 -0500136 LogID(pelID(pel.id()), obmcID(pel.obmcLogID())),
Matt Spinler0ff00482019-11-06 16:19:46 -0600137 attributes);
Matt Spinlerb188f782020-07-07 11:18:12 -0500138
139 updateRepoStats(attributes, true);
Matt Spinler475e5742019-07-18 16:09:49 -0500140 }
141 else
142 {
143 log<level::ERR>(
144 "Found invalid PEL file while restoring. Removing.",
145 entry("FILENAME=%s", dirEntry.path().c_str()));
146 fs::remove(dirEntry.path());
147 }
148 }
Patrick Williams66491c62021-10-06 12:23:37 -0500149 catch (const std::exception& e)
Matt Spinler475e5742019-07-18 16:09:49 -0500150 {
151 log<level::ERR>("Hit exception while restoring PEL File",
152 entry("FILENAME=%s", dirEntry.path().c_str()),
153 entry("ERROR=%s", e.what()));
154 }
155 }
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500156
157 // Get size of archive folder
158 for (auto& dirEntry : fs::directory_iterator(_archivePath))
159 {
160 _archiveSize += getFileDiskSize(dirEntry);
161 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500162}
163
164std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time)
165{
166 char name[50];
167 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB,
168 time.yearLSB, time.month, time.day, time.hour, time.minutes,
169 time.seconds, time.hundredths, pelID);
170 return std::string{name};
171}
172
173void Repository::add(std::unique_ptr<PEL>& pel)
174{
Matt Spinlerdf43a302019-11-21 13:16:56 -0600175 pel->setHostTransmissionState(TransmissionState::newPEL);
176 pel->setHMCTransmissionState(TransmissionState::newPEL);
177
Matt Spinler89fa0822019-07-17 13:54:30 -0500178 auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600179
180 write(*(pel.get()), path);
181
Matt Spinler8e65f4e2023-05-02 13:40:08 -0500182 PELAttributes attributes{
183 path,
184 getFileDiskSize(path),
185 pel->privateHeader().creatorID(),
186 pel->userHeader().subsystem(),
187 pel->userHeader().severity(),
188 pel->userHeader().actionFlags(),
189 pel->hostTransmissionState(),
190 pel->hmcTransmissionState(),
191 pel->plid(),
192 pel->getDeconfigFlag(),
193 pel->getGuardFlag(),
194 getMillisecondsSinceEpoch(pel->privateHeader().createTimestamp())};
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600195
196 using pelID = LogID::Pel;
197 using obmcID = LogID::Obmc;
198 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
199 attributes);
200
Matt Spinler44893cc2020-08-26 11:34:17 -0500201 _lastPelID = pel->id();
202
Matt Spinlerb188f782020-07-07 11:18:12 -0500203 updateRepoStats(attributes, true);
204
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600205 processAddCallbacks(*pel);
206}
207
208void Repository::write(const PEL& pel, const fs::path& path)
209{
Matt Spinler89fa0822019-07-17 13:54:30 -0500210 std::ofstream file{path, std::ios::binary};
211
212 if (!file.good())
213 {
214 // If this fails, the filesystem is probably full so it isn't like
215 // we could successfully create yet another error log here.
216 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500217 fs::remove(path);
218 log<level::ERR>("Unable to open PEL file for writing",
219 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
220 throw file_error::Open();
221 }
222
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600223 auto data = pel.data();
Matt Spinler89fa0822019-07-17 13:54:30 -0500224 file.write(reinterpret_cast<const char*>(data.data()), data.size());
225
226 if (file.fail())
227 {
228 // Same note as above about not being able to create an error log
229 // for this case even if we wanted.
230 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500231 file.close();
232 fs::remove(path);
233 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
234 entry("PATH=%s", path.c_str()));
235 throw file_error::Write();
236 }
Matt Spinler475e5742019-07-18 16:09:49 -0500237}
238
Matt Spinler52602e32020-07-15 12:37:28 -0500239std::optional<Repository::LogID> Repository::remove(const LogID& id)
Matt Spinler475e5742019-07-18 16:09:49 -0500240{
241 auto pel = findPEL(id);
Patrick Williamsff6b5982021-04-22 09:04:17 -0500242 if (pel == _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500243 {
Patrick Williamsff6b5982021-04-22 09:04:17 -0500244 return std::nullopt;
Matt Spinler5f5352e2020-03-05 16:23:27 -0600245 }
Matt Spinler52602e32020-07-15 12:37:28 -0500246
Patrick Williamsff6b5982021-04-22 09:04:17 -0500247 LogID actualID = pel->first;
248 updateRepoStats(pel->second, false);
249
250 log<level::DEBUG>("Removing PEL from repository",
251 entry("PEL_ID=0x%X", actualID.pelID.id),
252 entry("OBMC_LOG_ID=%d", actualID.obmcID.id));
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500253
254 if (fs::exists(pel->second.path))
255 {
256 // Check for existense of new archive folder
257 if (!fs::exists(_archivePath))
258 {
259 fs::create_directories(_archivePath);
260 }
261
262 // Move log file to archive folder
263 auto fileName = _archivePath / pel->second.path.filename();
264 fs::rename(pel->second.path, fileName);
265
266 // Update size of file
267 _archiveSize += getFileDiskSize(fileName);
268 }
269
Patrick Williamsff6b5982021-04-22 09:04:17 -0500270 _pelAttributes.erase(pel);
271
272 processDeleteCallbacks(actualID.pelID.id);
273
Matt Spinler52602e32020-07-15 12:37:28 -0500274 return actualID;
Matt Spinler89fa0822019-07-17 13:54:30 -0500275}
276
Matt Spinler2813f362019-07-19 12:45:28 -0500277std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
278{
279 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600280 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500281 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600282 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500283 if (!file.good())
284 {
285 auto e = errno;
286 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600287 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500288 throw file_error::Open();
289 }
290
291 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
292 std::istreambuf_iterator<char>()};
293 return data;
294 }
295
296 return std::nullopt;
297}
298
Matt Spinler6d512242019-12-09 13:44:17 -0600299std::optional<sdbusplus::message::unix_fd> Repository::getPELFD(const LogID& id)
300{
301 auto pel = findPEL(id);
302 if (pel != _pelAttributes.end())
303 {
Matt Spinler32a6df62023-01-12 16:30:40 -0600304 int fd = open(pel->second.path.c_str(), O_RDONLY | O_NONBLOCK);
305 if (fd == -1)
Matt Spinler6d512242019-12-09 13:44:17 -0600306 {
307 auto e = errno;
308 log<level::ERR>("Unable to open PEL File", entry("ERRNO=%d", e),
309 entry("PATH=%s", pel->second.path.c_str()));
310 throw file_error::Open();
311 }
312
313 // Must leave the file open here. It will be closed by sdbusplus
314 // when it sends it back over D-Bus.
Matt Spinler32a6df62023-01-12 16:30:40 -0600315 return fd;
Matt Spinler6d512242019-12-09 13:44:17 -0600316 }
317 return std::nullopt;
318}
319
Matt Spinler1ea78802019-11-01 13:04:59 -0500320void Repository::for_each(ForEachFunc func) const
321{
Matt Spinler0ff00482019-11-06 16:19:46 -0600322 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500323 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600324 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500325
326 if (!file.good())
327 {
328 auto e = errno;
329 log<level::ERR>("Repository::for_each: Unable to open PEL file",
330 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600331 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500332 continue;
333 }
334
335 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
336 std::istreambuf_iterator<char>()};
337 file.close();
338
339 PEL pel{data};
340
341 try
342 {
343 if (func(pel))
344 {
345 break;
346 }
347 }
Patrick Williams66491c62021-10-06 12:23:37 -0500348 catch (const std::exception& e)
Matt Spinler1ea78802019-11-01 13:04:59 -0500349 {
350 log<level::ERR>("Repository::for_each function exception",
351 entry("ERROR=%s", e.what()));
352 }
353 }
354}
355
Matt Spinler421f6532019-11-06 15:40:45 -0600356void Repository::processAddCallbacks(const PEL& pel) const
357{
358 for (auto& [name, func] : _addSubscriptions)
359 {
360 try
361 {
362 func(pel);
363 }
Patrick Williams66491c62021-10-06 12:23:37 -0500364 catch (const std::exception& e)
Matt Spinler421f6532019-11-06 15:40:45 -0600365 {
366 log<level::ERR>("PEL Repository add callback exception",
367 entry("NAME=%s", name.c_str()),
368 entry("ERROR=%s", e.what()));
369 }
370 }
371}
372
373void Repository::processDeleteCallbacks(uint32_t id) const
374{
375 for (auto& [name, func] : _deleteSubscriptions)
376 {
377 try
378 {
379 func(id);
380 }
Patrick Williams66491c62021-10-06 12:23:37 -0500381 catch (const std::exception& e)
Matt Spinler421f6532019-11-06 15:40:45 -0600382 {
383 log<level::ERR>("PEL Repository delete callback exception",
384 entry("NAME=%s", name.c_str()),
385 entry("ERROR=%s", e.what()));
386 }
387 }
388}
389
Matt Spinler0ff00482019-11-06 16:19:46 -0600390std::optional<std::reference_wrapper<const Repository::PELAttributes>>
391 Repository::getPELAttributes(const LogID& id) const
392{
393 auto pel = findPEL(id);
394 if (pel != _pelAttributes.end())
395 {
396 return pel->second;
397 }
398
399 return std::nullopt;
400}
401
Matt Spinler29d18c12019-11-21 13:31:27 -0600402void Repository::setPELHostTransState(uint32_t pelID, TransmissionState state)
403{
404 LogID id{LogID::Pel{pelID}};
405 auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
406 [&id](const auto& a) { return a.first == id; });
407
408 if ((attr != _pelAttributes.end()) && (attr->second.hostState != state))
409 {
410 PELUpdateFunc func = [state](PEL& pel) {
411 pel.setHostTransmissionState(state);
412 };
413
414 try
415 {
416 updatePEL(attr->second.path, func);
417
418 attr->second.hostState = state;
419 }
Patrick Williams66491c62021-10-06 12:23:37 -0500420 catch (const std::exception& e)
Matt Spinler29d18c12019-11-21 13:31:27 -0600421 {
422 log<level::ERR>("Unable to update PEL host transmission state",
423 entry("PATH=%s", attr->second.path.c_str()),
424 entry("ERROR=%s", e.what()));
425 }
426 }
427}
428
429void Repository::setPELHMCTransState(uint32_t pelID, TransmissionState state)
430{
431 LogID id{LogID::Pel{pelID}};
432 auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
433 [&id](const auto& a) { return a.first == id; });
434
435 if ((attr != _pelAttributes.end()) && (attr->second.hmcState != state))
436 {
437 PELUpdateFunc func = [state](PEL& pel) {
438 pel.setHMCTransmissionState(state);
439 };
440
441 try
442 {
443 updatePEL(attr->second.path, func);
444
445 attr->second.hmcState = state;
446 }
Patrick Williams66491c62021-10-06 12:23:37 -0500447 catch (const std::exception& e)
Matt Spinler29d18c12019-11-21 13:31:27 -0600448 {
449 log<level::ERR>("Unable to update PEL HMC transmission state",
450 entry("PATH=%s", attr->second.path.c_str()),
451 entry("ERROR=%s", e.what()));
452 }
453 }
454}
455
456void Repository::updatePEL(const fs::path& path, PELUpdateFunc updateFunc)
457{
458 std::ifstream file{path};
459 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
460 std::istreambuf_iterator<char>()};
461 file.close();
462
463 PEL pel{data};
464
465 if (pel.valid())
466 {
467 updateFunc(pel);
468
469 write(pel, path);
470 }
471 else
472 {
473 throw std::runtime_error(
474 "Unable to read a valid PEL when trying to update it");
475 }
476}
477
Matt Spinlerb188f782020-07-07 11:18:12 -0500478bool Repository::isServiceableSev(const PELAttributes& pel)
479{
480 auto sevType = static_cast<SeverityType>(pel.severity & 0xF0);
Patrick Williams2544b412022-10-04 08:41:06 -0500481 auto sevPVEntry = pel_values::findByValue(pel.severity,
482 pel_values::severityValues);
Matt Spinlerb188f782020-07-07 11:18:12 -0500483 std::string sevName = std::get<pel_values::registryNamePos>(*sevPVEntry);
484
485 bool check1 = (sevType == SeverityType::predictive) ||
486 (sevType == SeverityType::unrecoverable) ||
487 (sevType == SeverityType::critical);
488
489 bool check2 = ((sevType == SeverityType::recovered) ||
490 (sevName == "symptom_recovered")) &&
491 !pel.actionFlags.test(hiddenFlagBit);
492
493 bool check3 = (sevName == "symptom_predictive") ||
494 (sevName == "symptom_unrecoverable") ||
495 (sevName == "symptom_critical");
496
497 return check1 || check2 || check3;
498}
499
500void Repository::updateRepoStats(const PELAttributes& pel, bool pelAdded)
501{
502 auto isServiceable = Repository::isServiceableSev(pel);
503 auto bmcPEL = CreatorID::openBMC == static_cast<CreatorID>(pel.creator);
504
505 auto adjustSize = [pelAdded, &pel](auto& runningSize) {
506 if (pelAdded)
507 {
508 runningSize += pel.sizeOnDisk;
509 }
510 else
511 {
512 runningSize = std::max(static_cast<int64_t>(runningSize) -
513 static_cast<int64_t>(pel.sizeOnDisk),
514 static_cast<int64_t>(0));
515 }
516 };
517
518 adjustSize(_sizes.total);
519
520 if (bmcPEL)
521 {
522 adjustSize(_sizes.bmc);
523 if (isServiceable)
524 {
525 adjustSize(_sizes.bmcServiceable);
526 }
527 else
528 {
529 adjustSize(_sizes.bmcInfo);
530 }
531 }
532 else
533 {
534 adjustSize(_sizes.nonBMC);
535 if (isServiceable)
536 {
537 adjustSize(_sizes.nonBMCServiceable);
538 }
539 else
540 {
541 adjustSize(_sizes.nonBMCInfo);
542 }
543 }
544}
545
Sumit Kumarc2966922021-07-21 10:14:03 -0500546bool Repository::sizeWarning()
Matt Spinler7e727a32020-07-07 15:00:17 -0500547{
Sumit Kumarc2966922021-07-21 10:14:03 -0500548 std::error_code ec;
549
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500550 if ((_archiveSize > 0) && ((_sizes.total + _archiveSize) >
551 ((_maxRepoSize * warningPercentage) / 100)))
552 {
553 log<level::INFO>(
554 "Repository::sizeWarning function:Deleting the files in archive");
555
Sumit Kumarc2966922021-07-21 10:14:03 -0500556 for (const auto& dirEntry : fs::directory_iterator(_archivePath))
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500557 {
Sumit Kumarc2966922021-07-21 10:14:03 -0500558 fs::remove(dirEntry.path(), ec);
559 if (ec)
560 {
561 log<level::INFO>(
562 "Repository::sizeWarning function:Could not delete "
563 "a file in PEL archive",
564 entry("FILENAME=%s", dirEntry.path().c_str()));
565 }
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500566 }
Sumit Kumarc2966922021-07-21 10:14:03 -0500567
568 _archiveSize = 0;
Sumit Kumar1d8835b2021-06-07 09:35:30 -0500569 }
570
Matt Spinler7e727a32020-07-07 15:00:17 -0500571 return (_sizes.total > (_maxRepoSize * warningPercentage / 100)) ||
572 (_pelAttributes.size() > _maxNumPELs);
573}
574
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500575std::vector<Repository::AttributesReference>
576 Repository::getAllPELAttributes(SortOrder order) const
577{
578 std::vector<Repository::AttributesReference> attributes;
579
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500580 std::for_each(_pelAttributes.begin(), _pelAttributes.end(),
581 [&attributes](auto& pelEntry) {
582 attributes.push_back(pelEntry);
583 });
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500584
585 std::sort(attributes.begin(), attributes.end(),
586 [order](const auto& left, const auto& right) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500587 if (order == SortOrder::ascending)
588 {
589 return left.get().second.path < right.get().second.path;
590 }
591 return left.get().second.path > right.get().second.path;
592 });
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500593
594 return attributes;
595}
596
Sumit Kumar027bf282022-01-24 11:25:19 -0600597std::vector<uint32_t>
598 Repository::prune(const std::vector<uint32_t>& idsWithHwIsoEntry)
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500599{
600 std::vector<uint32_t> obmcLogIDs;
601 std::string msg = "Pruning PEL repository that takes up " +
602 std::to_string(_sizes.total) + " bytes and has " +
603 std::to_string(_pelAttributes.size()) + " PELs";
604 log<level::INFO>(msg.c_str());
605
606 // Set up the 5 functions to check if the PEL category
607 // is still over its limits.
608
609 // BMC informational PELs should only take up 15%
610 IsOverLimitFunc overBMCInfoLimit = [this]() {
611 return _sizes.bmcInfo > _maxRepoSize * 15 / 100;
612 };
613
614 // BMC non informational PELs should only take up 30%
615 IsOverLimitFunc overBMCNonInfoLimit = [this]() {
616 return _sizes.bmcServiceable > _maxRepoSize * 30 / 100;
617 };
618
619 // Non BMC informational PELs should only take up 15%
620 IsOverLimitFunc overNonBMCInfoLimit = [this]() {
621 return _sizes.nonBMCInfo > _maxRepoSize * 15 / 100;
622 };
623
624 // Non BMC non informational PELs should only take up 15%
625 IsOverLimitFunc overNonBMCNonInfoLimit = [this]() {
626 return _sizes.nonBMCServiceable > _maxRepoSize * 30 / 100;
627 };
628
629 // Bring the total number of PELs down to 80% of the max
630 IsOverLimitFunc tooManyPELsLimit = [this]() {
631 return _pelAttributes.size() > _maxNumPELs * 80 / 100;
632 };
633
634 // Set up the functions to determine which category a PEL is in.
635 // TODO: Return false in these functions if a PEL caused a guard record.
636
637 // A BMC informational PEL
638 IsPELTypeFunc isBMCInfo = [](const PELAttributes& pel) {
639 return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) &&
640 !Repository::isServiceableSev(pel);
641 };
642
643 // A BMC non informational PEL
644 IsPELTypeFunc isBMCNonInfo = [](const PELAttributes& pel) {
645 return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) &&
646 Repository::isServiceableSev(pel);
647 };
648
649 // A non BMC informational PEL
650 IsPELTypeFunc isNonBMCInfo = [](const PELAttributes& pel) {
651 return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) &&
652 !Repository::isServiceableSev(pel);
653 };
654
655 // A non BMC non informational PEL
656 IsPELTypeFunc isNonBMCNonInfo = [](const PELAttributes& pel) {
657 return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) &&
658 Repository::isServiceableSev(pel);
659 };
660
661 // When counting PELs, count every PEL
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500662 IsPELTypeFunc isAnyPEL = [](const PELAttributes& /*pel*/) { return true; };
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500663
664 // Check all 4 categories, which will result in at most 90%
665 // usage (15 + 30 + 15 + 30).
Sumit Kumar027bf282022-01-24 11:25:19 -0600666 removePELs(overBMCInfoLimit, isBMCInfo, idsWithHwIsoEntry, obmcLogIDs);
667 removePELs(overBMCNonInfoLimit, isBMCNonInfo, idsWithHwIsoEntry,
668 obmcLogIDs);
669 removePELs(overNonBMCInfoLimit, isNonBMCInfo, idsWithHwIsoEntry,
670 obmcLogIDs);
671 removePELs(overNonBMCNonInfoLimit, isNonBMCNonInfo, idsWithHwIsoEntry,
672 obmcLogIDs);
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500673
674 // After the above pruning check if there are still too many PELs,
675 // which can happen depending on PEL sizes.
676 if (_pelAttributes.size() > _maxNumPELs)
677 {
Sumit Kumar027bf282022-01-24 11:25:19 -0600678 removePELs(tooManyPELsLimit, isAnyPEL, idsWithHwIsoEntry, obmcLogIDs);
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500679 }
680
681 if (!obmcLogIDs.empty())
682 {
Matt Spinler45796e82022-07-01 11:25:27 -0500683 std::string m = "Number of PELs removed to save space: " +
684 std::to_string(obmcLogIDs.size());
685 log<level::INFO>(m.c_str());
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500686 }
687
688 return obmcLogIDs;
689}
690
Matt Spinler45796e82022-07-01 11:25:27 -0500691void Repository::removePELs(const IsOverLimitFunc& isOverLimit,
692 const IsPELTypeFunc& isPELType,
Sumit Kumar027bf282022-01-24 11:25:19 -0600693 const std::vector<uint32_t>& idsWithHwIsoEntry,
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500694 std::vector<uint32_t>& removedBMCLogIDs)
695{
696 if (!isOverLimit())
697 {
698 return;
699 }
700
701 auto attributes = getAllPELAttributes(SortOrder::ascending);
702
703 // Make 4 passes on the PELs, stopping as soon as isOverLimit
704 // returns false.
705 // Pass 1: only delete HMC acked PELs
706 // Pass 2: only delete OS acked PELs
707 // Pass 3: only delete PHYP sent PELs
708 // Pass 4: delete all PELs
709 static const std::vector<std::function<bool(const PELAttributes& pel)>>
710 stateChecks{[](const auto& pel) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500711 return pel.hmcState == TransmissionState::acked;
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500712 },
713
714 [](const auto& pel) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500715 return pel.hostState == TransmissionState::acked;
716 },
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500717
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500718 [](const auto& pel) {
719 return pel.hostState == TransmissionState::sent;
720 },
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500721
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500722 [](const auto& /*pel*/) { return true; }};
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500723
724 for (const auto& stateCheck : stateChecks)
725 {
726 for (auto it = attributes.begin(); it != attributes.end();)
727 {
728 const auto& pel = it->get();
729 if (isPELType(pel.second) && stateCheck(pel.second))
730 {
731 auto removedID = pel.first.obmcID.id;
Sumit Kumar027bf282022-01-24 11:25:19 -0600732
733 auto idFound = std::find(idsWithHwIsoEntry.begin(),
734 idsWithHwIsoEntry.end(), removedID);
735 if (idFound != idsWithHwIsoEntry.end())
736 {
737 ++it;
738 continue;
739 }
740
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500741 remove(pel.first);
742
743 removedBMCLogIDs.push_back(removedID);
744
745 attributes.erase(it);
746
747 if (!isOverLimit())
748 {
749 break;
750 }
751 }
752 else
753 {
754 ++it;
755 }
756 }
757
758 if (!isOverLimit())
759 {
760 break;
761 }
762 }
763}
764
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500765void Repository::archivePEL(const PEL& pel)
766{
767 if (pel.valid())
768 {
769 auto path = _archivePath / getPELFilename(pel.id(), pel.commitTime());
770
771 write(pel, path);
772
773 _archiveSize += getFileDiskSize(path);
774 }
775}
776
Matt Spinler89fa0822019-07-17 13:54:30 -0500777} // namespace pels
778} // namespace openpower