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 | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | #include "pel.hpp" |
| 17 | |
| 18 | #include "bcd_time.hpp" |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 19 | #include "extended_user_header.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 20 | #include "failing_mtms.hpp" |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 21 | #include "json_utils.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 22 | #include "log_id.hpp" |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 23 | #include "pel_rules.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 24 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 25 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 26 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 27 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 28 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 29 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 30 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 33 | namespace openpower |
| 34 | { |
| 35 | namespace pels |
| 36 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 37 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 38 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 39 | using namespace phosphor::logging; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 40 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 41 | constexpr auto unknownValue = "Unknown"; |
| 42 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 43 | PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 44 | phosphor::logging::Entry::Level severity, |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame^] | 45 | const AdditionalData& additionalData, const PelFFDC& ffdcFiles, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 46 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 47 | { |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 48 | _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID, |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 49 | timestamp); |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 50 | _uh = std::make_unique<UserHeader>(regEntry, severity); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 51 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 52 | auto src = std::make_unique<SRC>(regEntry, additionalData, dataIface); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 53 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 54 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 55 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 56 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 57 | _optionalSections.push_back(std::move(euh)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 58 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 59 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 60 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 62 | auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface); |
| 63 | _optionalSections.push_back(std::move(ud)); |
| 64 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 65 | // Create a UserData section from AdditionalData. |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 66 | if (!additionalData.empty()) |
| 67 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 68 | ud = util::makeADUserDataSection(additionalData); |
Matt Spinler | 6d66382 | 2020-01-22 14:50:46 -0600 | [diff] [blame] | 69 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 70 | // Shrink the section if necessary. |
| 71 | if (size() + ud->header().size > _maxPELSize) |
| 72 | { |
| 73 | if (ud->shrink(_maxPELSize - size())) |
| 74 | { |
| 75 | _optionalSections.push_back(std::move(ud)); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | log<level::WARNING>( |
| 80 | "Dropping AdditionalData UserData section", |
| 81 | entry("SECTION_SIZE=%d\n", ud->header().size)); |
| 82 | } |
| 83 | } |
| 84 | else |
Matt Spinler | 6d66382 | 2020-01-22 14:50:46 -0600 | [diff] [blame] | 85 | { |
| 86 | _optionalSections.push_back(std::move(ud)); |
| 87 | } |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame^] | 90 | // Add any FFDC files into UserData sections |
| 91 | for (const auto& file : ffdcFiles) |
| 92 | { |
| 93 | ud = util::makeFFDCuserDataSection(regEntry.componentID, file); |
| 94 | if (!ud) |
| 95 | { |
| 96 | log<level::WARNING>( |
| 97 | "Could not make PEL FFDC UserData section from file", |
| 98 | entry("COMPONENT_ID=0x%02X", regEntry.componentID), |
| 99 | entry("SUBTYPE=0x%X", file.subType), |
| 100 | entry("VERSION=0x%X", file.version)); |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | // Shrink it if necessary |
| 105 | if (size() + ud->header().size > _maxPELSize) |
| 106 | { |
| 107 | if (!ud->shrink(_maxPELSize - size())) |
| 108 | { |
| 109 | log<level::WARNING>( |
| 110 | "Could not shrink FFDC UserData section", |
| 111 | entry("COMPONENT_ID=0x%02X", regEntry.componentID), |
| 112 | entry("SUBTYPE=0x%X", file.subType), |
| 113 | entry("VERSION=0x%X", file.version)); |
| 114 | |
| 115 | // Give up adding FFDC |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | _optionalSections.push_back(std::move(ud)); |
| 121 | } |
| 122 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 123 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 124 | |
| 125 | checkRulesAndFix(); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 126 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 127 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 128 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 129 | { |
| 130 | } |
| 131 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 132 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 133 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 134 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 137 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 138 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 139 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 140 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 141 | if (obmcLogID != 0) |
| 142 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 143 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 147 | |
| 148 | // Use the section factory to create the rest of the objects |
| 149 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 150 | { |
| 151 | auto section = section_factory::create(pelData); |
| 152 | _optionalSections.push_back(std::move(section)); |
| 153 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool PEL::valid() const |
| 157 | { |
| 158 | bool valid = _ph->valid(); |
| 159 | |
| 160 | if (valid) |
| 161 | { |
| 162 | valid = _uh->valid(); |
| 163 | } |
| 164 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 165 | if (valid) |
| 166 | { |
| 167 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 168 | [](const auto& section) { return section->valid(); })) |
| 169 | { |
| 170 | valid = false; |
| 171 | } |
| 172 | } |
| 173 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 174 | return valid; |
| 175 | } |
| 176 | |
| 177 | void PEL::setCommitTime() |
| 178 | { |
| 179 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 180 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void PEL::assignID() |
| 184 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 185 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 188 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 189 | { |
| 190 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 191 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 192 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 193 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 194 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 197 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 198 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 199 | |
| 200 | for (auto& section : _optionalSections) |
| 201 | { |
| 202 | section->flatten(pelData); |
| 203 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 206 | std::vector<uint8_t> PEL::data() const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 207 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 208 | std::vector<uint8_t> pelData; |
| 209 | flatten(pelData); |
| 210 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 213 | size_t PEL::size() const |
| 214 | { |
| 215 | size_t size = 0; |
| 216 | |
| 217 | if (_ph) |
| 218 | { |
| 219 | size += _ph->header().size; |
| 220 | } |
| 221 | |
| 222 | if (_uh) |
| 223 | { |
| 224 | size += _uh->header().size; |
| 225 | } |
| 226 | |
| 227 | for (const auto& section : _optionalSections) |
| 228 | { |
| 229 | size += section->header().size; |
| 230 | } |
| 231 | |
| 232 | return size; |
| 233 | } |
| 234 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 235 | std::optional<SRC*> PEL::primarySRC() const |
| 236 | { |
| 237 | auto src = std::find_if( |
| 238 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 239 | return section->header().id == |
| 240 | static_cast<uint16_t>(SectionID::primarySRC); |
| 241 | }); |
| 242 | if (src != _optionalSections.end()) |
| 243 | { |
| 244 | return static_cast<SRC*>(src->get()); |
| 245 | } |
| 246 | |
| 247 | return std::nullopt; |
| 248 | } |
| 249 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 250 | void PEL::checkRulesAndFix() |
| 251 | { |
| 252 | auto [actionFlags, eventType] = |
| 253 | pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity()); |
| 254 | |
| 255 | _uh->setActionFlags(actionFlags); |
| 256 | _uh->setEventType(eventType); |
| 257 | } |
| 258 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 259 | void PEL::printSectionInJSON(const Section& section, std::string& buf, |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 260 | std::map<uint16_t, size_t>& pluralSections, |
| 261 | message::Registry& registry) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 262 | { |
| 263 | char tmpB[5]; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 264 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 265 | static_cast<uint8_t>(section.header().id)}; |
| 266 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 267 | std::string sectionID(tmpB); |
| 268 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 269 | ? pv::sectionTitles.at(sectionID) |
| 270 | : "Unknown Section"; |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 271 | |
| 272 | // Add a count if there are multiple of this type of section |
| 273 | auto count = pluralSections.find(section.header().id); |
| 274 | if (count != pluralSections.end()) |
| 275 | { |
| 276 | sectionName += " " + std::to_string(count->second); |
| 277 | count->second++; |
| 278 | } |
| 279 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 280 | if (section.valid()) |
| 281 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 282 | auto json = (sectionID == "PS" || sectionID == "SS") |
| 283 | ? section.getJSON(registry) |
| 284 | : section.getJSON(); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 285 | |
| 286 | buf += "\"" + sectionName + "\": {\n"; |
| 287 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 288 | if (json) |
| 289 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 290 | buf += *json + "\n},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 291 | } |
| 292 | else |
| 293 | { |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 294 | jsonInsert(buf, pv::sectionVer, |
| 295 | getNumberString("%d", section.header().version), 1); |
| 296 | jsonInsert(buf, pv::subSection, |
| 297 | getNumberString("%d", section.header().subType), 1); |
| 298 | jsonInsert(buf, pv::createdBy, |
| 299 | getNumberString("0x%X", section.header().componentID), |
| 300 | 1); |
| 301 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 302 | std::vector<uint8_t> data; |
| 303 | Stream s{data}; |
| 304 | section.flatten(s); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 305 | std::string dstr = |
| 306 | dumpHex(std::data(data) + SectionHeader::flattenedSize(), |
| 307 | data.size(), 2); |
| 308 | |
| 309 | std::string jsonIndent(indentLevel, 0x20); |
| 310 | buf += jsonIndent + "\"Data\": [\n"; |
| 311 | buf += dstr; |
| 312 | buf += jsonIndent + "]\n"; |
| 313 | buf += "},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 314 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 318 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 322 | std::map<uint16_t, size_t> PEL::getPluralSections() const |
| 323 | { |
| 324 | std::map<uint16_t, size_t> sectionCounts; |
| 325 | |
| 326 | for (const auto& section : optionalSections()) |
| 327 | { |
| 328 | if (sectionCounts.find(section->header().id) == sectionCounts.end()) |
| 329 | { |
| 330 | sectionCounts[section->header().id] = 1; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | sectionCounts[section->header().id]++; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | std::map<uint16_t, size_t> sections; |
| 339 | for (const auto& [id, count] : sectionCounts) |
| 340 | { |
| 341 | if (count > 1) |
| 342 | { |
| 343 | // Start with 0 here and printSectionInJSON() |
| 344 | // will increment it as it goes. |
| 345 | sections.emplace(id, 0); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return sections; |
| 350 | } |
| 351 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 352 | void PEL::toJSON(message::Registry& registry) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 353 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 354 | auto sections = getPluralSections(); |
| 355 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 356 | std::string buf = "{\n"; |
| 357 | printSectionInJSON(*(_ph.get()), buf, sections, registry); |
| 358 | printSectionInJSON(*(_uh.get()), buf, sections, registry); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 359 | for (auto& section : this->optionalSections()) |
| 360 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 361 | printSectionInJSON(*(section.get()), buf, sections, registry); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 362 | } |
| 363 | buf += "}"; |
| 364 | std::size_t found = buf.rfind(","); |
| 365 | if (found != std::string::npos) |
| 366 | buf.replace(found, 1, ""); |
| 367 | std::cout << buf << std::endl; |
| 368 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 369 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 370 | namespace util |
| 371 | { |
| 372 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 373 | std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json) |
| 374 | { |
| 375 | auto jsonString = json.dump(); |
| 376 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 377 | |
| 378 | // Pad to a 4 byte boundary |
| 379 | while ((jsonData.size() % 4) != 0) |
| 380 | { |
| 381 | jsonData.push_back(0); |
| 382 | } |
| 383 | |
| 384 | return std::make_unique<UserData>( |
| 385 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 386 | static_cast<uint8_t>(UserDataFormat::json), |
| 387 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 388 | } |
| 389 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 390 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 391 | { |
| 392 | assert(!ad.empty()); |
| 393 | nlohmann::json json; |
| 394 | |
| 395 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 396 | if (ad.getValue("ESEL")) |
| 397 | { |
| 398 | auto newAD = ad; |
| 399 | newAD.remove("ESEL"); |
| 400 | json = newAD.toJSON(); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | json = ad.toJSON(); |
| 405 | } |
| 406 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 407 | return makeJSONUserDataSection(json); |
| 408 | } |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 409 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 410 | void addProcessNameToJSON(nlohmann::json& json, |
| 411 | const std::optional<std::string>& pid, |
| 412 | const DataInterfaceBase& dataIface) |
| 413 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 414 | std::string name{unknownValue}; |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 415 | |
| 416 | try |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 417 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 418 | if (pid) |
| 419 | { |
| 420 | auto n = dataIface.getProcessName(*pid); |
| 421 | if (n) |
| 422 | { |
| 423 | name = *n; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | catch (std::exception& e) |
| 428 | { |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 429 | } |
| 430 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 431 | json["Process Name"] = std::move(name); |
| 432 | } |
| 433 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 434 | void addBMCFWVersionIDToJSON(nlohmann::json& json, |
| 435 | const DataInterfaceBase& dataIface) |
| 436 | { |
| 437 | auto id = dataIface.getBMCFWVersionID(); |
| 438 | if (id.empty()) |
| 439 | { |
| 440 | id = unknownValue; |
| 441 | } |
| 442 | |
| 443 | json["BMC Version ID"] = std::move(id); |
| 444 | } |
| 445 | |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 446 | std::string lastSegment(char separator, std::string data) |
| 447 | { |
| 448 | auto pos = data.find_last_of(separator); |
| 449 | if (pos != std::string::npos) |
| 450 | { |
| 451 | data = data.substr(pos + 1); |
| 452 | } |
| 453 | |
| 454 | return data; |
| 455 | } |
| 456 | |
| 457 | void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 458 | { |
| 459 | json["BMCState"] = lastSegment('.', dataIface.getBMCState()); |
| 460 | json["ChassisState"] = lastSegment('.', dataIface.getChassisState()); |
| 461 | json["HostState"] = lastSegment('.', dataIface.getHostState()); |
| 462 | } |
| 463 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 464 | std::unique_ptr<UserData> |
| 465 | makeSysInfoUserDataSection(const AdditionalData& ad, |
| 466 | const DataInterfaceBase& dataIface) |
| 467 | { |
| 468 | nlohmann::json json; |
| 469 | |
| 470 | addProcessNameToJSON(json, ad.getValue("_PID"), dataIface); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 471 | addBMCFWVersionIDToJSON(json, dataIface); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 472 | addStatesToJSON(json, dataIface); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 473 | |
| 474 | return makeJSONUserDataSection(json); |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 475 | } |
| 476 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame^] | 477 | std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID, |
| 478 | const PelFFDCfile& file) |
| 479 | { |
| 480 | return std::unique_ptr<UserData>(); |
| 481 | } |
| 482 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 483 | } // namespace util |
| 484 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 485 | } // namespace pels |
| 486 | } // namespace openpower |