diff --git a/fboss/qsfp_service/module/I2cLogBuffer.cpp b/fboss/qsfp_service/module/I2cLogBuffer.cpp index 018fdfa6d2c30..8b5c5138f289a 100644 --- a/fboss/qsfp_service/module/I2cLogBuffer.cpp +++ b/fboss/qsfp_service/module/I2cLogBuffer.cpp @@ -280,30 +280,29 @@ std::pair I2cLogBuffer::dumpToFile() { return std::make_pair(hdrSize, logCount); } -TransceiverAccessParameter I2cLogBuffer::getParam(std::stringstream& ss) { +TransceiverAccessParameter I2cLogBuffer::getParam(const std::string& str) { TransceiverAccessParameter param(0, 0, 0); - std::string token; - ss >> token; - if (token != kEmptyOptional) { - param.i2cAddress = folly::to(token); + std::vector fields; + folly::split(' ', str, fields, true); + if (fields.size() < kNumParamFields) { + throw std::out_of_range("Invalie param fields:" + str); } - ss >> token; - if (token != kEmptyOptional) { - param.page = folly::to(token); + if (fields.at(0) != kEmptyOptional) { + param.i2cAddress = folly::to(fields.at(0)); + }; + if (fields.at(1) != kEmptyOptional) { + param.page = folly::to(fields.at(1)); } - ss >> token; - if (token != kEmptyOptional) { - param.bank = folly::to(token); + if (fields.at(2) != kEmptyOptional) { + param.bank = folly::to(fields.at(2)); } - ss >> param.offset; - ss >> param.len; + param.offset = folly::to(fields.at(3)); + param.len = folly::to(fields.at(4)); return param; } -I2cLogBuffer::Operation I2cLogBuffer::getOp(std::stringstream& ss) { - char c; - ss >> c; - switch (c) { +I2cLogBuffer::Operation I2cLogBuffer::getOp(const char op) { + switch (op) { case 'R': return Operation::Read; break; @@ -311,7 +310,7 @@ I2cLogBuffer::Operation I2cLogBuffer::getOp(std::stringstream& ss) { return Operation::Write; break; default: - throw std::invalid_argument(fmt::format("Invalid Operation :{}", c)); + throw std::invalid_argument(fmt::format("Invalid Operation :{}", op)); break; } } @@ -379,10 +378,10 @@ std::vector I2cLogBuffer::loadFromLog( } while (std::getline(file, line)) { - ss = std::stringstream(getField(line, '<', '>')); - auto param = getParam(ss); - auto op = getOp(ss); - auto str = getField(line, '[', ']'); + auto str = getField(line, '<', '>'); + auto param = getParam(str); + auto op = getOp(str.back()); + str = getField(line, '[', ']'); auto data = getData(str); auto delay = getDelay(line); str = getField(line, '>', '['); diff --git a/fboss/qsfp_service/module/I2cLogBuffer.h b/fboss/qsfp_service/module/I2cLogBuffer.h index 156161a47dbf0..21028004ab3d0 100644 --- a/fboss/qsfp_service/module/I2cLogBuffer.h +++ b/fboss/qsfp_service/module/I2cLogBuffer.h @@ -31,6 +31,9 @@ namespace facebook::fboss { constexpr int kMaxI2clogDataSize = 128; constexpr size_t kI2cFieldNameLength = 16; +// Number of address fields in TransceiverAccessParameter +constexpr int kNumParamFields = 5; + class I2cLogBuffer { using TimePointSteady = std::chrono::steady_clock::time_point; using TimePointSystem = std::chrono::system_clock::time_point; @@ -176,8 +179,8 @@ class I2cLogBuffer { // Operations to re-construct I2cReplayEntry from a log file. static size_t getHeader(std::stringstream& ss, const I2cLogHeader& info); static std::string getField(const std::string& line, char left, char right); - static TransceiverAccessParameter getParam(std::stringstream& ss); - static I2cLogBuffer::Operation getOp(std::stringstream& ss); + static TransceiverAccessParameter getParam(const std::string& str); + static I2cLogBuffer::Operation getOp(const char op); static std::array getData(std::string str); static uint64_t getDelay(const std::string& str); static bool getSuccess(const std::string& str);