Matt Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | */ |
| 16 | #include "record_manager.hpp" |
| 17 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 18 | #include <math.h> |
| 19 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 20 | #include <phosphor-logging/log.hpp> |
| 21 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 22 | #include <chrono> |
| 23 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 24 | namespace phosphor |
Matt Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 25 | { |
| 26 | namespace power |
| 27 | { |
| 28 | namespace history |
| 29 | { |
| 30 | |
Matt Spinler | 28fa1b6 | 2018-01-18 13:15:02 -0600 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 33 | bool RecordManager::add(const std::vector<uint8_t>& rawRecord) |
| 34 | { |
| 35 | if (rawRecord.size() == 0) |
| 36 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 37 | // The PS has no data - either the power supply just started up, |
| 38 | // or it just got a SYNC. Clear the history. |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 39 | records.clear(); |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | try |
| 44 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 45 | // Peek at the ID to see if more processing is needed. |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 46 | auto id = getRawRecordID(rawRecord); |
| 47 | |
| 48 | if (!records.empty()) |
| 49 | { |
| 50 | auto previousID = std::get<recIDPos>(records.front()); |
| 51 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 52 | // Already have this record. Done. |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 53 | if (previousID == id) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 58 | // Check that the sequence ID is in order. |
| 59 | // If not, clear out current list. |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 60 | if ((previousID + 1) != id) |
| 61 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 62 | // If it just rolled over from 0xFF to 0x00, then no |
| 63 | // need to clear. If we see a 0 seemingly out of nowhere, |
| 64 | // then it was a sync so clear the old records. |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 65 | auto rolledOver = (previousID == lastSequenceID) && |
| 66 | (id == FIRST_SEQUENCE_ID); |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 67 | |
| 68 | if (!rolledOver) |
| 69 | { |
| 70 | if (id != FIRST_SEQUENCE_ID) |
| 71 | { |
| 72 | log<level::INFO>( |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 73 | "Noncontiguous INPUT_HISTORY sequence ID " |
| 74 | "found. Clearing old entries", |
| 75 | entry("OLD_ID=%ld", previousID), |
| 76 | entry("NEW_ID=%ld", id)); |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 77 | } |
| 78 | records.clear(); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Jayanth Othayoth | f9886b9 | 2024-12-07 01:26:08 -0600 | [diff] [blame^] | 83 | #pragma GCC diagnostic push |
| 84 | #pragma GCC diagnostic ignored "-Wpessimizing-move" |
| 85 | #ifdef __clang__ |
| 86 | #pragma clang diagnostic ignored "-Wpessimizing-move" |
| 87 | #endif |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 88 | records.push_front(std::move(createRecord(rawRecord))); |
Jayanth Othayoth | f9886b9 | 2024-12-07 01:26:08 -0600 | [diff] [blame^] | 89 | #pragma GCC diagnostic pop |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 90 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 91 | // If no more should be stored, prune the oldest |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 92 | if (records.size() > maxRecords) |
| 93 | { |
| 94 | records.pop_back(); |
| 95 | } |
| 96 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 97 | catch (const InvalidRecordException& e) |
Matt Spinler | 8168d28 | 2018-01-18 13:24:06 -0600 | [diff] [blame] | 98 | { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
Matt Spinler | c341438 | 2018-01-18 13:49:16 -0600 | [diff] [blame] | 105 | auto RecordManager::getAverageRecords() -> DBusRecordList |
| 106 | { |
| 107 | DBusRecordList list; |
| 108 | |
| 109 | for (const auto& r : records) |
| 110 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 111 | list.emplace_back(std::get<recTimePos>(r), std::get<recAvgPos>(r)); |
Matt Spinler | c341438 | 2018-01-18 13:49:16 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return list; |
| 115 | } |
| 116 | |
| 117 | auto RecordManager::getMaximumRecords() -> DBusRecordList |
| 118 | { |
| 119 | DBusRecordList list; |
| 120 | |
| 121 | for (const auto& r : records) |
| 122 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 123 | list.emplace_back(std::get<recTimePos>(r), std::get<recMaxPos>(r)); |
Matt Spinler | c341438 | 2018-01-18 13:49:16 -0600 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return list; |
| 127 | } |
| 128 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 129 | size_t RecordManager::getRawRecordID(const std::vector<uint8_t>& data) const |
Matt Spinler | 28fa1b6 | 2018-01-18 13:15:02 -0600 | [diff] [blame] | 130 | { |
| 131 | if (data.size() != RAW_RECORD_SIZE) |
| 132 | { |
| 133 | log<level::ERR>("Invalid INPUT_HISTORY size", |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 134 | entry("SIZE=%d", data.size())); |
Matt Spinler | 28fa1b6 | 2018-01-18 13:15:02 -0600 | [diff] [blame] | 135 | throw InvalidRecordException{}; |
| 136 | } |
| 137 | |
| 138 | return data[RAW_RECORD_ID_OFFSET]; |
| 139 | } |
| 140 | |
| 141 | Record RecordManager::createRecord(const std::vector<uint8_t>& data) |
| 142 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 143 | // The raw record format is: |
Matt Spinler | 28fa1b6 | 2018-01-18 13:15:02 -0600 | [diff] [blame] | 144 | // 0xAABBCCDDEE |
| 145 | // |
| 146 | // where: |
| 147 | // 0xAA = sequence ID |
| 148 | // 0xBBCC = average power in linear format (0xCC = MSB) |
| 149 | // 0xDDEE = maximum power in linear format (0xEE = MSB) |
| 150 | auto id = getRawRecordID(data); |
| 151 | |
| 152 | auto time = std::chrono::duration_cast<std::chrono::milliseconds>( |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 153 | std::chrono::system_clock::now().time_since_epoch()) |
| 154 | .count(); |
Matt Spinler | 28fa1b6 | 2018-01-18 13:15:02 -0600 | [diff] [blame] | 155 | |
| 156 | auto val = static_cast<uint16_t>(data[2]) << 8 | data[1]; |
| 157 | auto averagePower = linearToInteger(val); |
| 158 | |
| 159 | val = static_cast<uint16_t>(data[4]) << 8 | data[3]; |
| 160 | auto maxPower = linearToInteger(val); |
| 161 | |
| 162 | return Record{id, time, averagePower, maxPower}; |
| 163 | } |
| 164 | |
Matt Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame] | 165 | int64_t RecordManager::linearToInteger(uint16_t data) |
| 166 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 167 | // The exponent is the first 5 bits, followed by 11 bits of mantissa. |
Matt Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame] | 168 | int8_t exponent = (data & 0xF800) >> 11; |
| 169 | int16_t mantissa = (data & 0x07FF); |
| 170 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 171 | // If exponent's MSB on, then it's negative. |
| 172 | // Convert from two's complement. |
Matt Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame] | 173 | if (exponent & 0x10) |
| 174 | { |
| 175 | exponent = (~exponent) & 0x1F; |
| 176 | exponent = (exponent + 1) * -1; |
| 177 | } |
| 178 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 179 | // If mantissa's MSB on, then it's negative. |
| 180 | // Convert from two's complement. |
Matt Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame] | 181 | if (mantissa & 0x400) |
| 182 | { |
| 183 | mantissa = (~mantissa) & 0x07FF; |
| 184 | mantissa = (mantissa + 1) * -1; |
| 185 | } |
| 186 | |
| 187 | auto value = static_cast<float>(mantissa) * pow(2, exponent); |
| 188 | return value; |
| 189 | } |
Matt Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 190 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 191 | } // namespace history |
| 192 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 193 | } // namespace phosphor |