| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SERVER_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SERVER_H_ |
| |
| #include <cstdint> |
| #include <memory> |
| #include <string> |
| #include <string_view> |
| #include <utility> |
| |
| #include "absl/base/thread_annotations.h" |
| #include "absl/container/flat_hash_map.h" |
| #include "absl/flags/declare.h" |
| #include "absl/status/statusor.h" |
| #include "absl/synchronization/mutex.h" |
| #include "absl/time/time.h" |
| #include "tlbmc/hal/shared_mem/metrics.h" |
| #include "tlbmc/hal/shared_mem/segment_manager.h" |
| #include "tlbmc/hal/shared_mem/sensors.h" |
| |
| namespace milotic_tlbmc { |
| |
| class IpcServer { |
| public: |
| virtual ~IpcServer() = default; |
| // Returns the sensor value and timestamp given the sensor name. |
| virtual absl::StatusOr<std::pair<float, uint64_t>> ReadSensorValue( |
| const std::string& sensor_name) = 0; |
| // copybara:strip_begin(g3-shared-libs) |
| virtual void UpdateMetricsRequestCount(bool is_tlbmc_request) = 0; |
| virtual void UpdateMetricsResponse(absl::Duration response_time, |
| int status_code, |
| std::string_view resource_url) = 0; |
| // copybara:strip_end |
| virtual const TlbmcMetrics* GetMetrics() const = 0; |
| }; |
| |
| // The class is thread-safe. |
| // Typical usage (sensor): |
| // SharedMemoryServer::Initialize(); |
| // IpcServer& server = SharedMemoryServer::GetInstance(); |
| // while (true) { |
| // server.ReadSensorValue("sensor001"); |
| // time.sleep(1ms); |
| // } |
| // Typical usage (metrics): |
| // SharedMemoryServer::Initialize(); |
| // IpcServer& server = SharedMemoryServer::GetInstance(); |
| // while (true) { |
| // server.UpdateMetricsXxx(); |
| // time.sleep(1ms); |
| // } |
| class SharedMemoryServer : public IpcServer { |
| private: |
| class Token; |
| |
| public: |
| static void Initialize(); |
| static IpcServer& GetInstance(); |
| // Avoid permission issues in /run/. |
| static void SetUpInstanceForUnitTest(); |
| virtual ~SharedMemoryServer() = default; |
| |
| // Sensor APIs. |
| absl::StatusOr<std::pair<float, uint64_t>> ReadSensorValue( |
| const std::string& sensor_name) override; |
| |
| // Metrics APIs. |
| const TlbmcMetrics* GetMetrics() const override { return metrics_; } |
| // copybara:strip_begin(g3-shared-libs) |
| void UpdateMetricsRequestCount(bool is_tlbmc_request) override; |
| void UpdateMetricsResponse(absl::Duration response_time, int status_code, |
| std::string_view resource_url) override; |
| // copybara:strip_end |
| |
| SharedMemoryServer(Token token, const std::string& initialized_file_path, |
| std::unique_ptr<SegmentManager> segment_manager); |
| |
| static void Initialize(const std::string& initialized_file_path); |
| |
| protected: |
| SharedMemoryServer(const std::string& initialized_file_path, |
| std::unique_ptr<SegmentManager> segment_manager); |
| |
| SharedMemoryServer() = default; |
| IpcSensor* GetSensor(const std::string& sensor_name) |
| ABSL_LOCKS_EXCLUDED(sensors_mutex_); |
| |
| // For testing only. Provides the ability to set `initialized_file_path` which |
| // works in test environment. |
| static IpcServer& GetInstance(const std::string& initialized_file_path); |
| |
| private: |
| class Token { |
| private: |
| explicit Token() = default; |
| friend SharedMemoryServer; |
| }; |
| const std::string initialized_file_path_; |
| const std::unique_ptr<SegmentManager> segment_manager_; |
| absl::Mutex sensors_mutex_; |
| absl::flat_hash_map<std::string, IpcSensor*> sensors_ |
| ABSL_GUARDED_BY(sensors_mutex_); |
| TlbmcMetrics* const metrics_ = nullptr; |
| }; |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SHARED_MEM_SERVER_H_ |