Skip to content

Commit

Permalink
use folly::synchronized for I2cControllerStats
Browse files Browse the repository at this point in the history
Summary: We were running into race conditions with these stats, make the I2cControllerStats object synchronized

Reviewed By: harshitgulati18

Differential Revision: D64145306

fbshipit-source-id: 1615c2ef25f93446ad5018b1aee3f48b2ce88bf3
  • Loading branch information
Chet Powers authored and facebook-github-bot committed Oct 10, 2024
1 parent 50da808 commit ca8bc77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions fboss/lib/i2c/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cpp_library(
"//fboss/agent:utils",
"//folly:format",
"//folly:range",
"//folly:synchronized",
"//folly/io/async:async_base",
"//folly/lang:bits",
"//folly/logging:logging",
Expand Down
34 changes: 18 additions & 16 deletions fboss/lib/i2c/I2cController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
#pragma once

#include <folly/Synchronized.h>
#include <stdint.h>
#include "fboss/lib/i2c/gen-cpp2/i2c_controller_stats_types.h"

Expand All @@ -18,50 +19,51 @@ namespace facebook::fboss {
*/
class I2cController {
public:
I2cController(std::string name) {
*i2cControllerPlatformStats_.controllerName_() = name;
explicit I2cController(std::string name) {
*i2cControllerPlatformStats_.wlock()->controllerName_() = name;
}
virtual ~I2cController() {}

// Reset all i2c stats
void resetStats() {
*i2cControllerPlatformStats_.readTotal_() = 0;
*i2cControllerPlatformStats_.readFailed_() = 0;
*i2cControllerPlatformStats_.readBytes_() = 0;
*i2cControllerPlatformStats_.writeTotal_() = 0;
*i2cControllerPlatformStats_.writeFailed_() = 0;
*i2cControllerPlatformStats_.writeBytes_() = 0;
auto stats = i2cControllerPlatformStats_.wlock();
*stats->readTotal_() = 0;
*stats->readFailed_() = 0;
*stats->readBytes_() = 0;
*stats->writeTotal_() = 0;
*stats->writeFailed_() = 0;
*stats->writeBytes_() = 0;
}
// Total number of reads
void incrReadTotal(uint32_t count = 1) {
*i2cControllerPlatformStats_.readTotal_() += count;
*i2cControllerPlatformStats_.wlock()->readTotal_() += count;
}
// Total Read failures
void incrReadFailed(uint32_t count = 1) {
*i2cControllerPlatformStats_.readFailed_() += count;
*i2cControllerPlatformStats_.wlock()->readFailed_() += count;
}
// Number of bytes read
void incrReadBytes(uint32_t count = 1) {
*i2cControllerPlatformStats_.readBytes_() += count;
*i2cControllerPlatformStats_.wlock()->readBytes_() += count;
}
// Total number of writes
void incrWriteTotal(uint32_t count = 1) {
*i2cControllerPlatformStats_.writeTotal_() += count;
*i2cControllerPlatformStats_.wlock()->writeTotal_() += count;
}
// Total write failures
void incrWriteFailed(uint32_t count = 1) {
*i2cControllerPlatformStats_.writeFailed_() += count;
*i2cControllerPlatformStats_.wlock()->writeFailed_() += count;
}
// Number of bytes written
void incrWriteBytes(uint32_t count = 1) {
*i2cControllerPlatformStats_.writeBytes_() += count;
*i2cControllerPlatformStats_.wlock()->writeBytes_() += count;
}

/* Get the I2c transaction stats from the i2c controller
*/
const I2cControllerStats getI2cControllerPlatformStats() const {
// return the structre reference of latest i2c transaction stats
return i2cControllerPlatformStats_;
return *i2cControllerPlatformStats_.rlock();
}

virtual void i2cTimeProfilingStart() {}
Expand All @@ -72,7 +74,7 @@ class I2cController {

private:
// Platform i2c controller stats
I2cControllerStats i2cControllerPlatformStats_;
folly::Synchronized<I2cControllerStats> i2cControllerPlatformStats_;
};

} // namespace facebook::fboss

0 comments on commit ca8bc77

Please sign in to comment.