Skip to content

Commit

Permalink
Shift event number from priority-based to global-based (#13021)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Sep 7, 2023
1 parent 534faa6 commit 1356080
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 531 deletions.
4 changes: 1 addition & 3 deletions src/app/EventLoggingTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ class EventOptions
struct EventLoadOutContext
{
EventLoadOutContext(TLV::TLVWriter & aWriter, PriorityLevel aPriority, EventNumber aStartingEventNumber) :
mWriter(aWriter), mPriority(aPriority), mStartingEventNumber(aStartingEventNumber), mCurrentEventNumber(0), mFirst(true),
mSamePriorityEvent(false)
mWriter(aWriter), mPriority(aPriority), mStartingEventNumber(aStartingEventNumber), mCurrentEventNumber(0), mFirst(true)
{}

TLV::TLVWriter & mWriter;
Expand All @@ -162,7 +161,6 @@ struct EventLoadOutContext
size_t mEventCount = 0;
ClusterInfo * mpInterestedEventPaths = nullptr;
bool mFirst = true;
bool mSamePriorityEvent = false;
};
} // namespace app
} // namespace chip
306 changes: 110 additions & 196 deletions src/app/EventManagement.cpp

Large diffs are not rendered by default.

116 changes: 28 additions & 88 deletions src/app/EventManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,6 @@ class CircularEventBuffer : public TLV::CHIPCircularTLVBuffer
*/
bool IsFinalDestinationForPriority(PriorityLevel aPriority) const;

/**
* @brief
* Allocate a new event Number based on the event priority, and advance the counter
* if we have one.
*
* @return EventNumber Event Number for this priority.
*/
EventNumber VendEventNumber();

/**
* @brief
* Remove the number of event
*
* @param[in] aNumEvents the number of the event.
*/
void RemoveEvent(EventNumber aNumEvents);

/**
* @brief
* Given a timestamp of an event, compute the delta time to store in the log
*
* @param aEventTimestamp The event timestamp.
*
*/
void UpdateFirstLastEventTime(Timestamp aEventTimestamp);

void InitCounter(MonotonicallyIncreasingCounter * apEventNumberCounter)
{
if (apEventNumberCounter == nullptr)
{
mNonPersistedCounter.Init(1);
mpEventNumberCounter = &(mNonPersistedCounter);
return;
}
mpEventNumberCounter = apEventNumberCounter;
mFirstEventNumber = mpEventNumberCounter->GetValue();
}

PriorityLevel GetPriority() { return mPriority; }

CircularEventBuffer * GetPreviousCircularEventBuffer() { return mpPrev; }
Expand All @@ -134,14 +96,6 @@ class CircularEventBuffer : public TLV::CHIPCircularTLVBuffer
void SetRequiredSpaceforEvicted(size_t aRequiredSpace) { mRequiredSpaceForEvicted = aRequiredSpace; }
size_t GetRequiredSpaceforEvicted() { return mRequiredSpaceForEvicted; }

EventNumber GetFirstEventNumber() { return mFirstEventNumber; }
EventNumber GetLastEventNumber() { return mLastEventNumber; }

uint64_t GetFirstEventTimestamp() { return mFirstEventTimestamp.mValue; }
void SetFirstEventTimestamp(uint64_t aValue) { mLastEventTimestamp.mValue = aValue; }

uint64_t GetLastEventTimestamp() { return mLastEventTimestamp.mValue; }

virtual ~CircularEventBuffer() = default;

private:
Expand All @@ -158,10 +112,6 @@ class CircularEventBuffer : public TLV::CHIPCircularTLVBuffer
MonotonicallyIncreasingCounter mNonPersistedCounter;

size_t mRequiredSpaceForEvicted = 0; ///< Required space for previous buffer to evict event to new buffer
EventNumber mFirstEventNumber = 0; ///< First event Number stored in the logging subsystem for this priority
EventNumber mLastEventNumber = 0; ///< Last event Number vended for this priority
Timestamp mFirstEventTimestamp; ///< The timestamp of the first event in this buffer
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer
};

class CircularEventReader;
Expand Down Expand Up @@ -207,22 +157,8 @@ struct LogStorageResources
nullptr; // Buffer to be used as a storage at the particular priority level and shared with more important events.
// Must not be nullptr. Must be large enough to accommodate the largest event emitted by the system.
uint32_t mBufferSize = 0; ///< The size, in bytes, of the `mBuffer`.
Platform::PersistedStorage::Key * mCounterKey =
nullptr; // Name of the key naming persistent counter for events of this priority. When NULL, the persistent
// counters will not be used for this priority level.
uint32_t mCounterEpoch = 0; // The interval used in incrementing persistent counters. When 0, the persistent counters will not
// be used for this priority level.
PersistedCounter * mpCounterStorage = nullptr; // application provided storage for persistent counter for this priority level.
PriorityLevel mPriority =
PriorityLevel::Invalid; // Log priority level associated with the resources provided in this structure.
PersistedCounter * InitializeCounter() const
{
if (mpCounterStorage != nullptr && mCounterKey != nullptr && mCounterEpoch != 0)
{
return (mpCounterStorage->Init(*mCounterKey, mCounterEpoch) == CHIP_NO_ERROR) ? mpCounterStorage : nullptr;
}
return nullptr;
}
};

/**
Expand Down Expand Up @@ -259,7 +195,11 @@ class EventManagement
*
*/
void Init(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers, CircularEventBuffer * apCircularEventBuffer,
const LogStorageResources * const apLogStorageResources);
const LogStorageResources * const apLogStorageResources, Platform::PersistedStorage::Key * apCounterKey,
uint32_t aCounterEpoch, PersistedCounter * apPersistedCounter);

void InitializeCounter(Platform::PersistedStorage::Key * apCounterKey, uint32_t aCounterEpoch,
PersistedCounter * apPersistedCounter);

static EventManagement & GetInstance();

Expand All @@ -285,7 +225,9 @@ class EventManagement
*/
static void CreateEventManagement(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers,
CircularEventBuffer * apCircularEventBuffer,
const LogStorageResources * const apLogStorageResources);
const LogStorageResources * const apLogStorageResources,
Platform::PersistedStorage::Key * apCounterKey, uint32_t aCounterEpoch,
PersistedCounter * apPersistedCounter);

static void DestroyEventManagement();

Expand Down Expand Up @@ -367,19 +309,17 @@ class EventManagement
* A function to retrieve events of specified priority since a specified event ID.
*
* Given a TLV::TLVWriter, an priority type, and an event ID, the
* function will fetch events of specified priority since the
* specified event. The function will continue fetching events until
* function will fetch events since the
* specified event number. The function will continue fetching events until
* it runs out of space in the TLV::TLVWriter or in the log. The function
* will terminate the event writing on event boundary.
* will terminate the event writing on event boundary. The function would filter out event based upon interested path
* specified by read/subscribe request.
*
* @param[in] aWriter The writer to use for event storage
* @param[in] apClusterInfolist the interested cluster info list with event path inside
* @param[in] aPriority The priority of events to be fetched
*
* @param[in,out] aEventNumber On input, the Event number immediately
* prior to the one we're fetching. On
* completion, the event number of the last event
* fetched.
* @param[in,out] aEventMin On input, the Event number is the one we're fetching. On
* completion, the event number of the next one we plan to fetch.
*
* @param[out] aEventCount The number of fetched event
* @retval #CHIP_END_OF_TLV The function has reached the end of the
Expand All @@ -395,8 +335,8 @@ class EventManagement
* available.
*
*/
CHIP_ERROR FetchEventsSince(chip::TLV::TLVWriter & aWriter, ClusterInfo * apClusterInfolist, PriorityLevel aPriority,
EventNumber & aEventNumber, size_t & aEventCount);
CHIP_ERROR FetchEventsSince(chip::TLV::TLVWriter & aWriter, ClusterInfo * apClusterInfolist, EventNumber & aEventMin,
size_t & aEventCount);

/**
* @brief
Expand All @@ -406,17 +346,7 @@ class EventManagement
*
* @return EventNumber most recently vended event Number for that event priority
*/
EventNumber GetLastEventNumber(PriorityLevel aPriority);

/**
* @brief
* Fetch the first event Number currently stored for a particular priority level
*
* @param aPriority Priority level
*
* @return EventNumber First currently stored event Number for that event priority
*/
EventNumber GetFirstEventNumber(PriorityLevel aPriority);
EventNumber GetLastEventNumber() { return mLastEventNumber; }

/**
* @brief
Expand All @@ -427,9 +357,10 @@ class EventManagement
/**
* Logger would save last logged event number for each logger buffer into schedule event number array
*/
void SetScheduledEventEndpoint(EventNumber * aEventEndpoints);
void SetScheduledEventNumber(EventNumber & aEventNumber);

private:
void VendEventNumber();
CHIP_ERROR CalculateEventSize(EventLoggingDelegate * apDelegate, const EventOptions * apOptions, uint32_t & requiredSize);
/**
* @brief Helper function for writing event header and data according to event
Expand Down Expand Up @@ -540,6 +471,15 @@ class EventManagement
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
System::Mutex mAccessLock;
#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING

// The counter we're going to actually use.
MonotonicallyIncreasingCounter * mpEventNumberCounter = nullptr;

// The backup counter to use if no counter is provided for us.
MonotonicallyIncreasingCounter mNonPersistedCounter;

EventNumber mLastEventNumber = 0; ///< Last event Number vended for this priority
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer
};
} // namespace app
} // namespace chip
21 changes: 11 additions & 10 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ CHIP_ERROR ReadClient::ProcessEventReportIBs(TLV::TLVReader & aEventReportIBsRea
header.mTimestamp = mEventTimestamp;
ReturnErrorOnFailure(data.DecodeEventHeader(header));
mEventTimestamp = header.mTimestamp;

mEventMin = header.mEventNumber + 1;
ReturnErrorOnFailure(data.GetData(&dataReader));

mpCallback->OnEventData(this, header, &dataReader, nullptr);
Expand Down Expand Up @@ -681,16 +681,17 @@ CHIP_ERROR ReadClient::SendSubscribeRequest(ReadPrepareParams & aReadPreparePara

if (aReadPrepareParams.mEventNumber != 0)
{
// EventNumber is optional
EventFilterIBs::Builder & eventFilters = request.CreateEventFilters();
SuccessOrExit(err = request.GetError());
EventFilterIB::Builder & eventFilter = eventFilters.CreateEventFilter();
SuccessOrExit(err = eventFilters.GetError());
eventFilter.EventMin(aReadPrepareParams.mEventNumber).EndOfEventFilterIB();
SuccessOrExit(err = eventFilter.GetError());
eventFilters.EndOfEventFilters();
SuccessOrExit(err = eventFilters.GetError());
mEventMin = aReadPrepareParams.mEventNumber;
}

EventFilterIBs::Builder & eventFilters = request.CreateEventFilters();
SuccessOrExit(err = request.GetError());
EventFilterIB::Builder & eventFilter = eventFilters.CreateEventFilter();
SuccessOrExit(err = eventFilters.GetError());
eventFilter.EventMin(mEventMin).EndOfEventFilterIB();
SuccessOrExit(err = eventFilter.GetError());
eventFilters.EndOfEventFilters();
SuccessOrExit(err = eventFilters.GetError());
}

request.IsFabricFiltered(false).EndOfSubscribeRequestMessage();
Expand Down
1 change: 1 addition & 0 deletions src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class ReadClient : public Messaging::ExchangeDelegate
FabricIndex mFabricIndex = kUndefinedFabricIndex;
InteractionType mInteractionType = InteractionType::Read;
Timestamp mEventTimestamp;
EventNumber mEventMin = 0;
};

}; // namespace app
Expand Down
Loading

0 comments on commit 1356080

Please sign in to comment.