-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCollectionInspectionWorkerThread.cpp
503 lines (474 loc) · 22.6 KB
/
CollectionInspectionWorkerThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "CollectionInspectionWorkerThread.h"
#include "CANDataTypes.h"
#include "LoggingModule.h"
#include "QueueTypes.h"
#include "SignalTypes.h"
#include "TraceModule.h"
#include <algorithm>
#include <array>
#include <string>
#include <utility>
#include <vector>
#ifdef FWE_FEATURE_STORE_AND_FORWARD
#include "StreamManager.h"
#include <unordered_map>
#endif
namespace Aws
{
namespace IoTFleetWise
{
bool
CollectionInspectionWorkerThread::init( const std::shared_ptr<SignalBuffer> &inputSignalBuffer,
const std::shared_ptr<DataSenderQueue> &outputCollectedData,
uint32_t idleTimeMs,
std::shared_ptr<RawData::BufferManager> rawBufferManager
#ifdef FWE_FEATURE_STORE_AND_FORWARD
,
std::shared_ptr<Aws::IoTFleetWise::Store::StreamForwarder> streamForwarder,
std::shared_ptr<Store::StreamManager> streamManager
#endif
)
{
mInputSignalBuffer = inputSignalBuffer;
mOutputCollectedData = outputCollectedData;
if ( idleTimeMs != 0 )
{
mIdleTimeMs = idleTimeMs;
}
mCollectionInspectionEngine.setRawDataBufferManager( rawBufferManager );
mRawBufferManager = std::move( rawBufferManager );
#ifdef FWE_FEATURE_STORE_AND_FORWARD
mStreamForwarder = std::move( streamForwarder );
mStreamManager = std::move( streamManager );
#endif
return true;
}
bool
CollectionInspectionWorkerThread::start()
{
if ( ( mInputSignalBuffer == nullptr ) || ( mOutputCollectedData == nullptr ) )
{
FWE_LOG_ERROR( "Collection Engine cannot be started without correct configurations" );
return false;
}
// Prevent concurrent stop/init
std::lock_guard<std::mutex> lock( mThreadMutex );
// On multi core systems the shared variable mShouldStop must be updated for
// all cores before starting the thread otherwise thread will directly end
mShouldStop.store( false );
if ( !mThread.create( doWork, this ) )
{
FWE_LOG_TRACE( "Inspection Thread failed to start" );
}
else
{
FWE_LOG_TRACE( "Inspection Thread started" );
mThread.setThreadName( "fwDICollInsEng" );
}
return mThread.isActive() && mThread.isValid();
}
bool
CollectionInspectionWorkerThread::stop()
{
if ( ( !mThread.isValid() ) || ( !mThread.isActive() ) )
{
return true;
}
std::lock_guard<std::mutex> lock( mThreadMutex );
mShouldStop.store( true, std::memory_order_relaxed );
FWE_LOG_TRACE( "Request stop" );
mWait.notify();
mThread.release();
FWE_LOG_TRACE( "Stop finished" );
mShouldStop.store( false, std::memory_order_relaxed );
return !mThread.isActive();
}
bool
CollectionInspectionWorkerThread::shouldStop() const
{
return mShouldStop.load( std::memory_order_relaxed );
}
void
CollectionInspectionWorkerThread::onChangeInspectionMatrix(
const std::shared_ptr<const InspectionMatrix> &inspectionMatrix )
{
std::lock_guard<std::mutex> lock( mInspectionMatrixMutex );
mUpdatedInspectionMatrix = inspectionMatrix;
mUpdatedInspectionMatrixAvailable = true;
FWE_LOG_TRACE( "New inspection matrix handed over" );
// Wake up the thread.
mWait.notify();
}
void
CollectionInspectionWorkerThread::onNewDataAvailable()
{
mWait.notify();
}
void
CollectionInspectionWorkerThread::doWork( void *data )
{
CollectionInspectionWorkerThread *consumer = static_cast<CollectionInspectionWorkerThread *>( data );
TimePoint lastTimeEvaluated = { 0, 0 };
Timestamp lastTraceOutput = 0;
uint32_t statisticInputMessagesProcessed = 0;
uint32_t statisticDataSentOut = 0;
uint32_t activations = 0;
while ( true )
{
activations++;
if ( consumer->mUpdatedInspectionMatrixAvailable )
{
std::shared_ptr<const InspectionMatrix> newInspectionMatrix;
{
std::lock_guard<std::mutex> lock( consumer->mInspectionMatrixMutex );
consumer->mUpdatedInspectionMatrixAvailable = false;
newInspectionMatrix = consumer->mUpdatedInspectionMatrix;
}
consumer->mCollectionInspectionEngine.onChangeInspectionMatrix( newInspectionMatrix,
consumer->mClock->timeSinceEpoch() );
}
// Only run the main inspection loop if there is an inspection matrix
// Otherwise, go to sleep.
if ( consumer->mUpdatedInspectionMatrix )
{
std::array<uint8_t, MAX_CAN_FRAME_BYTE_SIZE> buf = {};
CollectedCanRawFrame inputCANFrame( 0, 0, 0, buf, 0 );
TimePoint currentTime = consumer->mClock->timeSinceEpoch();
uint32_t waitTimeMs = consumer->mIdleTimeMs;
// Consume any new signals and pass them over to the inspection Engine
auto consumeSignalGroups = [&]( const CollectedDataFrame &dataFrame ) {
TraceModule::get().incrementVariable( TraceVariable::CE_PROCESSED_DATA_FRAMES );
if ( !dataFrame.mCollectedSignals.empty() )
{
for ( auto &inputSignal : dataFrame.mCollectedSignals )
{
TraceModule::get().decrementAtomicVariable(
TraceAtomicVariable::QUEUE_CONSUMER_TO_INSPECTION_SIGNALS );
TraceModule::get().incrementVariable( TraceVariable::CE_PROCESSED_SIGNALS );
auto signalValue = inputSignal.getValue();
switch ( signalValue.getType() )
{
case SignalType::UINT8:
consumer->mCollectionInspectionEngine.addNewSignal<uint8_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint8Val );
break;
case SignalType::INT8:
consumer->mCollectionInspectionEngine.addNewSignal<int8_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.int8Val );
break;
case SignalType::UINT16:
consumer->mCollectionInspectionEngine.addNewSignal<uint16_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint16Val );
break;
case SignalType::INT16:
consumer->mCollectionInspectionEngine.addNewSignal<int16_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.int16Val );
break;
case SignalType::UINT32:
consumer->mCollectionInspectionEngine.addNewSignal<uint32_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint32Val );
break;
case SignalType::INT32:
consumer->mCollectionInspectionEngine.addNewSignal<int32_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.int32Val );
break;
case SignalType::UINT64:
consumer->mCollectionInspectionEngine.addNewSignal<uint64_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint64Val );
break;
case SignalType::INT64:
consumer->mCollectionInspectionEngine.addNewSignal<int64_t>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.int64Val );
break;
case SignalType::FLOAT:
consumer->mCollectionInspectionEngine.addNewSignal<float>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.floatVal );
break;
case SignalType::DOUBLE:
consumer->mCollectionInspectionEngine.addNewSignal<double>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.doubleVal );
break;
case SignalType::BOOLEAN:
consumer->mCollectionInspectionEngine.addNewSignal<bool>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.boolVal );
break;
case SignalType::STRING:
consumer->mCollectionInspectionEngine.addNewSignal<RawData::BufferHandle>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint32Val );
if ( consumer->mRawBufferManager != nullptr )
{
consumer->mRawBufferManager->decreaseHandleUsageHint(
inputSignal.signalID,
signalValue.value.uint32Val,
RawData::BufferHandleUsageStage::COLLECTED_NOT_IN_HISTORY_BUFFER );
}
break;
case SignalType::UNKNOWN:
FWE_LOG_WARN( "UNKNOWN signal [signal id: " + std::to_string( inputSignal.signalID ) +
" ] should not be processed" );
break;
#ifdef FWE_FEATURE_VISION_SYSTEM_DATA
case SignalType::COMPLEX_SIGNAL:
consumer->mCollectionInspectionEngine.addNewSignal<RawData::BufferHandle>(
inputSignal.signalID,
inputSignal.fetchRequestID,
calculateMonotonicTime( currentTime, inputSignal.receiveTime ),
currentTime.monotonicTimeMs,
signalValue.value.uint32Val );
if ( consumer->mRawBufferManager != nullptr )
{
consumer->mRawBufferManager->decreaseHandleUsageHint(
inputSignal.signalID,
signalValue.value.uint32Val,
RawData::BufferHandleUsageStage::COLLECTED_NOT_IN_HISTORY_BUFFER );
}
break;
#endif
}
statisticInputMessagesProcessed++;
}
}
if ( dataFrame.mCollectedCanRawFrame != nullptr )
{
// Consume any raw frames
TraceModule::get().decrementAtomicVariable( TraceAtomicVariable::QUEUE_CONSUMER_TO_INSPECTION_CAN );
TraceModule::get().incrementVariable( TraceVariable::CE_PROCESSED_CAN_FRAMES );
consumer->mCollectionInspectionEngine.addNewRawCanFrame(
dataFrame.mCollectedCanRawFrame->frameID,
dataFrame.mCollectedCanRawFrame->channelId,
calculateMonotonicTime( currentTime, dataFrame.mCollectedCanRawFrame->receiveTime ),
dataFrame.mCollectedCanRawFrame->data,
dataFrame.mCollectedCanRawFrame->size );
statisticInputMessagesProcessed++;
}
// Consume any Active DTCs
// We could check if the DTCs have changed here, but not necessary
// as we are looking at only the latest known DTCs.
// We only pop one item from the Buffer for a reason : DTCs represent
// the health of all ECUs in the network. The Inspection Engine does
// not need to know that topology and thus counts on the OBD Module
// to aggregate all DTCs from all ECUs in one single Item.
if ( dataFrame.mActiveDTCs != nullptr )
{
TraceModule::get().decrementAtomicVariable(
TraceAtomicVariable::QUEUE_CONSUMER_TO_INSPECTION_DTCS );
TraceModule::get().incrementVariable( TraceVariable::CE_PROCESSED_DTCS );
consumer->mCollectionInspectionEngine.setActiveDTCs( *dataFrame.mActiveDTCs.get() );
statisticInputMessagesProcessed++;
}
lastTimeEvaluated = consumer->mClock->timeSinceEpoch();
consumer->mCollectionInspectionEngine.evaluateConditions( lastTimeEvaluated );
// Initiate data collection and upload after every condition evaluation
statisticDataSentOut += consumer->collectDataAndUpload();
};
auto consumed = consumer->mInputSignalBuffer->consumeAll( consumeSignalGroups );
// If nothing was consumed and at least the evaluate interval has elapsed, evaluate the
// conditions to check heartbeat campaigns:
if ( ( consumed == 0 ) && ( ( consumer->mClock->monotonicTimeSinceEpochMs() -
lastTimeEvaluated.monotonicTimeMs ) >= EVALUATE_INTERVAL_MS ) )
{
lastTimeEvaluated = consumer->mClock->timeSinceEpoch();
consumer->mCollectionInspectionEngine.evaluateConditions( lastTimeEvaluated );
statisticDataSentOut += consumer->collectDataAndUpload();
}
// Nothing is in the ring buffer to consume. Go to idle mode for some time.
uint32_t timeToWait = std::min( waitTimeMs, consumer->mIdleTimeMs );
// Print only every THREAD_IDLE_TIME_MS to avoid console spam
if ( consumer->mClock->monotonicTimeSinceEpochMs() >
( lastTraceOutput + LoggingModule::LOG_AGGREGATION_TIME_MS ) )
{
FWE_LOG_TRACE( "Activations: " + std::to_string( activations ) +
". Waiting for some data to come. Idling for: " + std::to_string( timeToWait ) +
" ms or until notify. Since last idling processed " +
std::to_string( statisticInputMessagesProcessed ) +
" incoming data packages and sent out " + std::to_string( statisticDataSentOut ) +
" packages out" );
activations = 0;
statisticInputMessagesProcessed = 0;
statisticDataSentOut = 0;
lastTraceOutput = consumer->mClock->monotonicTimeSinceEpochMs();
}
consumer->mWait.wait( timeToWait );
}
else
{
// No inspection Matrix available. Wait for it from the CollectionScheme manager
consumer->mWait.wait( Signal::WaitWithPredicate );
}
if ( consumer->shouldStop() )
{
break;
}
}
}
uint32_t
CollectionInspectionWorkerThread::collectDataAndUpload()
{
uint32_t collectedDataPackages = 0;
uint32_t waitTimeMs = this->mIdleTimeMs;
#ifdef FWE_FEATURE_STORE_AND_FORWARD
// TODO consider priority, queue is shared between collecting and forwarding
if ( this->mStreamForwarder != nullptr )
{
for ( const auto &campaign : this->mCollectionInspectionEngine.forwardConditionForCampaignPartitions() )
{
for ( Aws::IoTFleetWise::Store::PartitionID pID = 0; pID < campaign.second.size(); ++pID )
{
if ( campaign.second[pID] )
{
this->mStreamForwarder->beginForward(
campaign.first, pID, Store::StreamForwarder::Source::CONDITION );
}
else
{
this->mStreamForwarder->cancelForward(
campaign.first, pID, Store::StreamForwarder::Source::CONDITION );
}
}
}
}
#endif
auto collectedData =
this->mCollectionInspectionEngine.collectNextDataToSend( this->mClock->timeSinceEpoch(), waitTimeMs );
while ( ( ( collectedData.triggeredCollectionSchemeData != nullptr )
#ifdef FWE_FEATURE_VISION_SYSTEM_DATA
|| ( collectedData.triggeredVisionSystemData != nullptr )
#endif
) &&
( !this->shouldStop() ) )
{
TraceModule::get().incrementVariable( TraceVariable::CE_TRIGGERS );
if ( collectedData.triggeredCollectionSchemeData != nullptr )
{
#ifdef FWE_FEATURE_STORE_AND_FORWARD
auto result = Store::StreamManager::ReturnCode::STREAM_NOT_FOUND;
if ( mStreamManager != nullptr )
{
result = mStreamManager->appendToStreams( *collectedData.triggeredCollectionSchemeData );
}
if ( result == Store::StreamManager::ReturnCode::SUCCESS )
{
// Successfully appended
}
else if ( result == Store::StreamManager::ReturnCode::EMPTY_DATA )
{
FWE_LOG_INFO(
"The trigger for Campaign: " + collectedData.triggeredCollectionSchemeData->metadata.campaignArn +
" activated eventID: " + std::to_string( collectedData.triggeredCollectionSchemeData->eventID ) +
" but no data is available to ingest" );
}
else if ( result != Store::StreamManager::ReturnCode::STREAM_NOT_FOUND )
{
FWE_LOG_ERROR( "Failed to store FWE data with eventID " +
std::to_string( collectedData.triggeredCollectionSchemeData->eventID ) );
}
else
#endif
{
if ( this->mOutputCollectedData->push( collectedData.triggeredCollectionSchemeData ) )
{
collectedDataPackages++;
}
else
{
FWE_LOG_WARN( "Collected data output buffer is full" );
}
}
}
#ifdef FWE_FEATURE_VISION_SYSTEM_DATA
if ( collectedData.triggeredVisionSystemData != nullptr )
{
if ( this->mOutputCollectedData->push( collectedData.triggeredVisionSystemData ) )
{
collectedDataPackages++;
}
else
{
FWE_LOG_WARN( "Collected data output buffer is full, Vision System Data could not be pushed" );
}
}
#endif
collectedData =
this->mCollectionInspectionEngine.collectNextDataToSend( this->mClock->timeSinceEpoch(), waitTimeMs );
}
return collectedDataPackages;
}
TimePoint
CollectionInspectionWorkerThread::calculateMonotonicTime( const TimePoint &currTime, Timestamp systemTimeMs )
{
TimePoint convertedTime = timePointFromSystemTime( currTime, systemTimeMs );
if ( ( convertedTime.systemTimeMs == 0 ) && ( convertedTime.monotonicTimeMs == 0 ) )
{
FWE_LOG_ERROR( "The system time " + std::to_string( systemTimeMs ) +
" corresponds to a time in the past before the monotonic" +
" clock started ticking. Current system time: " + std::to_string( currTime.systemTimeMs ) +
". Current monotonic time: " + std::to_string( currTime.monotonicTimeMs ) );
return TimePoint{ systemTimeMs, 0 };
}
return convertedTime;
}
bool
CollectionInspectionWorkerThread::isAlive()
{
return mThread.isValid() && mThread.isActive();
}
CollectionInspectionWorkerThread::~CollectionInspectionWorkerThread()
{
// To make sure the thread stops during teardown of tests.
if ( isAlive() )
{
stop();
}
}
} // namespace IoTFleetWise
} // namespace Aws