blob: 7fd509b9f03096b093edea43c86925edb7d6de57 [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 Spinlerdd325c32020-07-07 11:01:54 -050018#include <sys/stat.h>
19
Matt Spinler89fa0822019-07-17 13:54:30 -050020#include <fstream>
21#include <phosphor-logging/log.hpp>
22#include <xyz/openbmc_project/Common/File/error.hpp>
23
24namespace openpower
25{
26namespace pels
27{
28
29namespace fs = std::filesystem;
30using namespace phosphor::logging;
31namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
32
Matt Spinler7e727a32020-07-07 15:00:17 -050033constexpr size_t warningPercentage = 95;
34
Matt Spinlerdd325c32020-07-07 11:01:54 -050035/**
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 */
44size_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 Spinler8d5f3a22020-07-07 10:30:33 -050061Repository::Repository(const std::filesystem::path& basePath, size_t repoSize,
62 size_t maxNumPELs) :
63 _logPath(basePath / "logs"),
64 _maxRepoSize(repoSize), _maxNumPELs(maxNumPELs)
Matt Spinler89fa0822019-07-17 13:54:30 -050065{
66 if (!fs::exists(_logPath))
67 {
68 fs::create_directories(_logPath);
69 }
Matt Spinler475e5742019-07-18 16:09:49 -050070
71 restore();
72}
73
74void Repository::restore()
75{
76 for (auto& dirEntry : fs::directory_iterator(_logPath))
77 {
78 try
79 {
80 if (!fs::is_regular_file(dirEntry.path()))
81 {
82 continue;
83 }
84
85 std::ifstream file{dirEntry.path()};
86 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
87 std::istreambuf_iterator<char>()};
88 file.close();
89
Matt Spinler07eefc52019-09-26 11:18:26 -050090 PEL pel{data};
Matt Spinler475e5742019-07-18 16:09:49 -050091 if (pel.valid())
92 {
Matt Spinlera3c12a42019-11-21 13:25:32 -060093 // If the host hasn't acked it, reset the host state so
94 // it will get sent up again.
95 if (pel.hostTransmissionState() == TransmissionState::sent)
96 {
97 pel.setHostTransmissionState(TransmissionState::newPEL);
98 try
99 {
100 write(pel, dirEntry.path());
101 }
102 catch (std::exception& e)
103 {
104 log<level::ERR>(
105 "Failed to save PEL after updating host state",
106 entry("PELID=0x%X", pel.id()));
107 }
108 }
109
Matt Spinlerdd325c32020-07-07 11:01:54 -0500110 PELAttributes attributes{dirEntry.path(),
111 getFileDiskSize(dirEntry.path()),
112 pel.privateHeader().creatorID(),
113 pel.userHeader().severity(),
114 pel.userHeader().actionFlags(),
115 pel.hostTransmissionState(),
116 pel.hmcTransmissionState()};
Matt Spinler0ff00482019-11-06 16:19:46 -0600117
Matt Spinler475e5742019-07-18 16:09:49 -0500118 using pelID = LogID::Pel;
119 using obmcID = LogID::Obmc;
Matt Spinler0ff00482019-11-06 16:19:46 -0600120 _pelAttributes.emplace(
Matt Spinler475e5742019-07-18 16:09:49 -0500121 LogID(pelID(pel.id()), obmcID(pel.obmcLogID())),
Matt Spinler0ff00482019-11-06 16:19:46 -0600122 attributes);
Matt Spinlerb188f782020-07-07 11:18:12 -0500123
124 updateRepoStats(attributes, true);
Matt Spinler475e5742019-07-18 16:09:49 -0500125 }
126 else
127 {
128 log<level::ERR>(
129 "Found invalid PEL file while restoring. Removing.",
130 entry("FILENAME=%s", dirEntry.path().c_str()));
131 fs::remove(dirEntry.path());
132 }
133 }
134 catch (std::exception& e)
135 {
136 log<level::ERR>("Hit exception while restoring PEL File",
137 entry("FILENAME=%s", dirEntry.path().c_str()),
138 entry("ERROR=%s", e.what()));
139 }
140 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500141}
142
143std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time)
144{
145 char name[50];
146 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB,
147 time.yearLSB, time.month, time.day, time.hour, time.minutes,
148 time.seconds, time.hundredths, pelID);
149 return std::string{name};
150}
151
152void Repository::add(std::unique_ptr<PEL>& pel)
153{
Matt Spinlerdf43a302019-11-21 13:16:56 -0600154 pel->setHostTransmissionState(TransmissionState::newPEL);
155 pel->setHMCTransmissionState(TransmissionState::newPEL);
156
Matt Spinler89fa0822019-07-17 13:54:30 -0500157 auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600158
159 write(*(pel.get()), path);
160
Matt Spinlerdd325c32020-07-07 11:01:54 -0500161 PELAttributes attributes{path,
162 getFileDiskSize(path),
163 pel->privateHeader().creatorID(),
164 pel->userHeader().severity(),
165 pel->userHeader().actionFlags(),
Matt Spinler346f99a2019-11-21 13:06:35 -0600166 pel->hostTransmissionState(),
167 pel->hmcTransmissionState()};
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600168
169 using pelID = LogID::Pel;
170 using obmcID = LogID::Obmc;
171 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
172 attributes);
173
Matt Spinlerb188f782020-07-07 11:18:12 -0500174 updateRepoStats(attributes, true);
175
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600176 processAddCallbacks(*pel);
177}
178
179void Repository::write(const PEL& pel, const fs::path& path)
180{
Matt Spinler89fa0822019-07-17 13:54:30 -0500181 std::ofstream file{path, std::ios::binary};
182
183 if (!file.good())
184 {
185 // If this fails, the filesystem is probably full so it isn't like
186 // we could successfully create yet another error log here.
187 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500188 fs::remove(path);
189 log<level::ERR>("Unable to open PEL file for writing",
190 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
191 throw file_error::Open();
192 }
193
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600194 auto data = pel.data();
Matt Spinler89fa0822019-07-17 13:54:30 -0500195 file.write(reinterpret_cast<const char*>(data.data()), data.size());
196
197 if (file.fail())
198 {
199 // Same note as above about not being able to create an error log
200 // for this case even if we wanted.
201 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500202 file.close();
203 fs::remove(path);
204 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
205 entry("PATH=%s", path.c_str()));
206 throw file_error::Write();
207 }
Matt Spinler475e5742019-07-18 16:09:49 -0500208}
209
Matt Spinler52602e32020-07-15 12:37:28 -0500210std::optional<Repository::LogID> Repository::remove(const LogID& id)
Matt Spinler475e5742019-07-18 16:09:49 -0500211{
Matt Spinler52602e32020-07-15 12:37:28 -0500212 std::optional<LogID> actualID;
213
Matt Spinler475e5742019-07-18 16:09:49 -0500214 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600215 if (pel != _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500216 {
Matt Spinler52602e32020-07-15 12:37:28 -0500217 actualID = pel->first;
Matt Spinlerb188f782020-07-07 11:18:12 -0500218 updateRepoStats(pel->second, false);
219
Matt Spinler5f5352e2020-03-05 16:23:27 -0600220 log<level::DEBUG>("Removing PEL from repository",
221 entry("PEL_ID=0x%X", pel->first.pelID.id),
222 entry("OBMC_LOG_ID=%d", pel->first.obmcID.id));
Matt Spinler0ff00482019-11-06 16:19:46 -0600223 fs::remove(pel->second.path);
224 _pelAttributes.erase(pel);
Matt Spinler421f6532019-11-06 15:40:45 -0600225
Matt Spinler5f5352e2020-03-05 16:23:27 -0600226 processDeleteCallbacks(pel->first.pelID.id);
227 }
228 else
229 {
230 log<level::DEBUG>("Could not find PEL to remove",
231 entry("PEL_ID=0x%X", id.pelID.id),
232 entry("OBMC_LOG_ID=%d", id.obmcID.id));
Matt Spinler475e5742019-07-18 16:09:49 -0500233 }
Matt Spinler52602e32020-07-15 12:37:28 -0500234
235 return actualID;
Matt Spinler89fa0822019-07-17 13:54:30 -0500236}
237
Matt Spinler2813f362019-07-19 12:45:28 -0500238std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
239{
240 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600241 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500242 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600243 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500244 if (!file.good())
245 {
246 auto e = errno;
247 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600248 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500249 throw file_error::Open();
250 }
251
252 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
253 std::istreambuf_iterator<char>()};
254 return data;
255 }
256
257 return std::nullopt;
258}
259
Matt Spinler6d512242019-12-09 13:44:17 -0600260std::optional<sdbusplus::message::unix_fd> Repository::getPELFD(const LogID& id)
261{
262 auto pel = findPEL(id);
263 if (pel != _pelAttributes.end())
264 {
265 FILE* fp = fopen(pel->second.path.c_str(), "rb");
266
267 if (fp == nullptr)
268 {
269 auto e = errno;
270 log<level::ERR>("Unable to open PEL File", entry("ERRNO=%d", e),
271 entry("PATH=%s", pel->second.path.c_str()));
272 throw file_error::Open();
273 }
274
275 // Must leave the file open here. It will be closed by sdbusplus
276 // when it sends it back over D-Bus.
277
278 return fileno(fp);
279 }
280 return std::nullopt;
281}
282
Matt Spinler1ea78802019-11-01 13:04:59 -0500283void Repository::for_each(ForEachFunc func) const
284{
Matt Spinler0ff00482019-11-06 16:19:46 -0600285 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500286 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600287 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500288
289 if (!file.good())
290 {
291 auto e = errno;
292 log<level::ERR>("Repository::for_each: Unable to open PEL file",
293 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600294 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500295 continue;
296 }
297
298 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
299 std::istreambuf_iterator<char>()};
300 file.close();
301
302 PEL pel{data};
303
304 try
305 {
306 if (func(pel))
307 {
308 break;
309 }
310 }
311 catch (std::exception& e)
312 {
313 log<level::ERR>("Repository::for_each function exception",
314 entry("ERROR=%s", e.what()));
315 }
316 }
317}
318
Matt Spinler421f6532019-11-06 15:40:45 -0600319void Repository::processAddCallbacks(const PEL& pel) const
320{
321 for (auto& [name, func] : _addSubscriptions)
322 {
323 try
324 {
325 func(pel);
326 }
327 catch (std::exception& e)
328 {
329 log<level::ERR>("PEL Repository add callback exception",
330 entry("NAME=%s", name.c_str()),
331 entry("ERROR=%s", e.what()));
332 }
333 }
334}
335
336void Repository::processDeleteCallbacks(uint32_t id) const
337{
338 for (auto& [name, func] : _deleteSubscriptions)
339 {
340 try
341 {
342 func(id);
343 }
344 catch (std::exception& e)
345 {
346 log<level::ERR>("PEL Repository delete callback exception",
347 entry("NAME=%s", name.c_str()),
348 entry("ERROR=%s", e.what()));
349 }
350 }
351}
352
Matt Spinler0ff00482019-11-06 16:19:46 -0600353std::optional<std::reference_wrapper<const Repository::PELAttributes>>
354 Repository::getPELAttributes(const LogID& id) const
355{
356 auto pel = findPEL(id);
357 if (pel != _pelAttributes.end())
358 {
359 return pel->second;
360 }
361
362 return std::nullopt;
363}
364
Matt Spinler29d18c12019-11-21 13:31:27 -0600365void Repository::setPELHostTransState(uint32_t pelID, TransmissionState state)
366{
367 LogID id{LogID::Pel{pelID}};
368 auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
369 [&id](const auto& a) { return a.first == id; });
370
371 if ((attr != _pelAttributes.end()) && (attr->second.hostState != state))
372 {
373 PELUpdateFunc func = [state](PEL& pel) {
374 pel.setHostTransmissionState(state);
375 };
376
377 try
378 {
379 updatePEL(attr->second.path, func);
380
381 attr->second.hostState = state;
382 }
383 catch (std::exception& e)
384 {
385 log<level::ERR>("Unable to update PEL host transmission state",
386 entry("PATH=%s", attr->second.path.c_str()),
387 entry("ERROR=%s", e.what()));
388 }
389 }
390}
391
392void Repository::setPELHMCTransState(uint32_t pelID, TransmissionState state)
393{
394 LogID id{LogID::Pel{pelID}};
395 auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(),
396 [&id](const auto& a) { return a.first == id; });
397
398 if ((attr != _pelAttributes.end()) && (attr->second.hmcState != state))
399 {
400 PELUpdateFunc func = [state](PEL& pel) {
401 pel.setHMCTransmissionState(state);
402 };
403
404 try
405 {
406 updatePEL(attr->second.path, func);
407
408 attr->second.hmcState = state;
409 }
410 catch (std::exception& e)
411 {
412 log<level::ERR>("Unable to update PEL HMC transmission state",
413 entry("PATH=%s", attr->second.path.c_str()),
414 entry("ERROR=%s", e.what()));
415 }
416 }
417}
418
419void Repository::updatePEL(const fs::path& path, PELUpdateFunc updateFunc)
420{
421 std::ifstream file{path};
422 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
423 std::istreambuf_iterator<char>()};
424 file.close();
425
426 PEL pel{data};
427
428 if (pel.valid())
429 {
430 updateFunc(pel);
431
432 write(pel, path);
433 }
434 else
435 {
436 throw std::runtime_error(
437 "Unable to read a valid PEL when trying to update it");
438 }
439}
440
Matt Spinlerb188f782020-07-07 11:18:12 -0500441bool Repository::isServiceableSev(const PELAttributes& pel)
442{
443 auto sevType = static_cast<SeverityType>(pel.severity & 0xF0);
444 auto sevPVEntry =
445 pel_values::findByValue(pel.severity, pel_values::severityValues);
446 std::string sevName = std::get<pel_values::registryNamePos>(*sevPVEntry);
447
448 bool check1 = (sevType == SeverityType::predictive) ||
449 (sevType == SeverityType::unrecoverable) ||
450 (sevType == SeverityType::critical);
451
452 bool check2 = ((sevType == SeverityType::recovered) ||
453 (sevName == "symptom_recovered")) &&
454 !pel.actionFlags.test(hiddenFlagBit);
455
456 bool check3 = (sevName == "symptom_predictive") ||
457 (sevName == "symptom_unrecoverable") ||
458 (sevName == "symptom_critical");
459
460 return check1 || check2 || check3;
461}
462
463void Repository::updateRepoStats(const PELAttributes& pel, bool pelAdded)
464{
465 auto isServiceable = Repository::isServiceableSev(pel);
466 auto bmcPEL = CreatorID::openBMC == static_cast<CreatorID>(pel.creator);
467
468 auto adjustSize = [pelAdded, &pel](auto& runningSize) {
469 if (pelAdded)
470 {
471 runningSize += pel.sizeOnDisk;
472 }
473 else
474 {
475 runningSize = std::max(static_cast<int64_t>(runningSize) -
476 static_cast<int64_t>(pel.sizeOnDisk),
477 static_cast<int64_t>(0));
478 }
479 };
480
481 adjustSize(_sizes.total);
482
483 if (bmcPEL)
484 {
485 adjustSize(_sizes.bmc);
486 if (isServiceable)
487 {
488 adjustSize(_sizes.bmcServiceable);
489 }
490 else
491 {
492 adjustSize(_sizes.bmcInfo);
493 }
494 }
495 else
496 {
497 adjustSize(_sizes.nonBMC);
498 if (isServiceable)
499 {
500 adjustSize(_sizes.nonBMCServiceable);
501 }
502 else
503 {
504 adjustSize(_sizes.nonBMCInfo);
505 }
506 }
507}
508
Matt Spinler7e727a32020-07-07 15:00:17 -0500509bool Repository::sizeWarning() const
510{
511 return (_sizes.total > (_maxRepoSize * warningPercentage / 100)) ||
512 (_pelAttributes.size() > _maxNumPELs);
513}
514
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500515std::vector<Repository::AttributesReference>
516 Repository::getAllPELAttributes(SortOrder order) const
517{
518 std::vector<Repository::AttributesReference> attributes;
519
520 std::for_each(
521 _pelAttributes.begin(), _pelAttributes.end(),
522 [&attributes](auto& pelEntry) { attributes.push_back(pelEntry); });
523
524 std::sort(attributes.begin(), attributes.end(),
525 [order](const auto& left, const auto& right) {
526 if (order == SortOrder::ascending)
527 {
528 return left.get().second.path < right.get().second.path;
529 }
530 return left.get().second.path > right.get().second.path;
531 });
532
533 return attributes;
534}
535
536std::vector<uint32_t> Repository::prune()
537{
538 std::vector<uint32_t> obmcLogIDs;
539 std::string msg = "Pruning PEL repository that takes up " +
540 std::to_string(_sizes.total) + " bytes and has " +
541 std::to_string(_pelAttributes.size()) + " PELs";
542 log<level::INFO>(msg.c_str());
543
544 // Set up the 5 functions to check if the PEL category
545 // is still over its limits.
546
547 // BMC informational PELs should only take up 15%
548 IsOverLimitFunc overBMCInfoLimit = [this]() {
549 return _sizes.bmcInfo > _maxRepoSize * 15 / 100;
550 };
551
552 // BMC non informational PELs should only take up 30%
553 IsOverLimitFunc overBMCNonInfoLimit = [this]() {
554 return _sizes.bmcServiceable > _maxRepoSize * 30 / 100;
555 };
556
557 // Non BMC informational PELs should only take up 15%
558 IsOverLimitFunc overNonBMCInfoLimit = [this]() {
559 return _sizes.nonBMCInfo > _maxRepoSize * 15 / 100;
560 };
561
562 // Non BMC non informational PELs should only take up 15%
563 IsOverLimitFunc overNonBMCNonInfoLimit = [this]() {
564 return _sizes.nonBMCServiceable > _maxRepoSize * 30 / 100;
565 };
566
567 // Bring the total number of PELs down to 80% of the max
568 IsOverLimitFunc tooManyPELsLimit = [this]() {
569 return _pelAttributes.size() > _maxNumPELs * 80 / 100;
570 };
571
572 // Set up the functions to determine which category a PEL is in.
573 // TODO: Return false in these functions if a PEL caused a guard record.
574
575 // A BMC informational PEL
576 IsPELTypeFunc isBMCInfo = [](const PELAttributes& pel) {
577 return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) &&
578 !Repository::isServiceableSev(pel);
579 };
580
581 // A BMC non informational PEL
582 IsPELTypeFunc isBMCNonInfo = [](const PELAttributes& pel) {
583 return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) &&
584 Repository::isServiceableSev(pel);
585 };
586
587 // A non BMC informational PEL
588 IsPELTypeFunc isNonBMCInfo = [](const PELAttributes& pel) {
589 return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) &&
590 !Repository::isServiceableSev(pel);
591 };
592
593 // A non BMC non informational PEL
594 IsPELTypeFunc isNonBMCNonInfo = [](const PELAttributes& pel) {
595 return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) &&
596 Repository::isServiceableSev(pel);
597 };
598
599 // When counting PELs, count every PEL
600 IsPELTypeFunc isAnyPEL = [](const PELAttributes& pel) { return true; };
601
602 // Check all 4 categories, which will result in at most 90%
603 // usage (15 + 30 + 15 + 30).
604 removePELs(overBMCInfoLimit, isBMCInfo, obmcLogIDs);
605 removePELs(overBMCNonInfoLimit, isBMCNonInfo, obmcLogIDs);
606 removePELs(overNonBMCInfoLimit, isNonBMCInfo, obmcLogIDs);
607 removePELs(overNonBMCNonInfoLimit, isNonBMCNonInfo, obmcLogIDs);
608
609 // After the above pruning check if there are still too many PELs,
610 // which can happen depending on PEL sizes.
611 if (_pelAttributes.size() > _maxNumPELs)
612 {
613 removePELs(tooManyPELsLimit, isAnyPEL, obmcLogIDs);
614 }
615
616 if (!obmcLogIDs.empty())
617 {
618 std::string msg = "Number of PELs removed to save space: " +
619 std::to_string(obmcLogIDs.size());
620 log<level::INFO>(msg.c_str());
621 }
622
623 return obmcLogIDs;
624}
625
626void Repository::removePELs(IsOverLimitFunc& isOverLimit,
627 IsPELTypeFunc& isPELType,
628 std::vector<uint32_t>& removedBMCLogIDs)
629{
630 if (!isOverLimit())
631 {
632 return;
633 }
634
635 auto attributes = getAllPELAttributes(SortOrder::ascending);
636
637 // Make 4 passes on the PELs, stopping as soon as isOverLimit
638 // returns false.
639 // Pass 1: only delete HMC acked PELs
640 // Pass 2: only delete OS acked PELs
641 // Pass 3: only delete PHYP sent PELs
642 // Pass 4: delete all PELs
643 static const std::vector<std::function<bool(const PELAttributes& pel)>>
644 stateChecks{[](const auto& pel) {
645 return pel.hmcState == TransmissionState::acked;
646 },
647
648 [](const auto& pel) {
649 return pel.hostState == TransmissionState::acked;
650 },
651
652 [](const auto& pel) {
653 return pel.hostState == TransmissionState::sent;
654 },
655
656 [](const auto& pel) { return true; }};
657
658 for (const auto& stateCheck : stateChecks)
659 {
660 for (auto it = attributes.begin(); it != attributes.end();)
661 {
662 const auto& pel = it->get();
663 if (isPELType(pel.second) && stateCheck(pel.second))
664 {
665 auto removedID = pel.first.obmcID.id;
666 remove(pel.first);
667
668 removedBMCLogIDs.push_back(removedID);
669
670 attributes.erase(it);
671
672 if (!isOverLimit())
673 {
674 break;
675 }
676 }
677 else
678 {
679 ++it;
680 }
681 }
682
683 if (!isOverLimit())
684 {
685 break;
686 }
687 }
688}
689
Matt Spinler89fa0822019-07-17 13:54:30 -0500690} // namespace pels
691} // namespace openpower