Skip to content

Commit 7d37ea3

Browse files
committed
Fix pivot behavior when starved.
1 parent 4d3d517 commit 7d37ea3

6 files changed

Lines changed: 25 additions & 42 deletions

File tree

SerialPrograms/Source/CommonTools/Async/InferenceSession.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ InferenceSession::InferenceSession(
2525
, m_overlays(stream.overlay())
2626
, m_triggered(nullptr)
2727
{
28+
WallClock start_time = current_time();
2829
try{
2930
for (size_t c = 0; c < callbacks.size(); c++){
3031
const PeriodicInferenceCallback& callback = callbacks[c];
@@ -40,7 +41,8 @@ InferenceSession::InferenceSession(
4041
stream.video_inference_pivot().add_callback(
4142
scope, &m_triggered,
4243
visual_callback,
43-
callback.period > std::chrono::milliseconds(0) ? callback.period : default_video_period
44+
callback.period > std::chrono::milliseconds(0) ? callback.period : default_video_period,
45+
start_time
4446
);
4547
visual_callback.make_overlays(m_overlays);
4648
break;
@@ -49,7 +51,8 @@ InferenceSession::InferenceSession(
4951
stream.audio_inference_pivot().add_callback(
5052
scope, &m_triggered,
5153
static_cast<AudioInferenceCallback&>(*callback.callback),
52-
callback.period > std::chrono::milliseconds(0) ? callback.period : default_audio_period
54+
callback.period > std::chrono::milliseconds(0) ? callback.period : default_audio_period,
55+
start_time
5356
);
5457
break;
5558
}

SerialPrograms/Source/CommonTools/InferenceCallbacks/InferenceCallback.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,7 @@ struct PeriodicInferenceCallback{
6767
)
6868
: callback(&p_callback)
6969
, period(p_period)
70-
{
71-
#if 0
72-
if (period > std::chrono::milliseconds(0)){
73-
return;
74-
}
75-
switch (callback->type()){
76-
case InferenceType::VISUAL:
77-
period = std::chrono::milliseconds(50);
78-
break;
79-
case InferenceType::AUDIO:
80-
period = std::chrono::milliseconds(20);
81-
break;
82-
}
83-
#endif
84-
}
70+
{}
8571
};
8672

8773

SerialPrograms/Source/CommonTools/InferencePivots/AudioInferencePivot.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct AudioInferencePivot::PeriodicCallback{
2222
std::atomic<InferenceCallback*>* set_when_triggered;
2323
AudioInferenceCallback& callback;
2424
std::chrono::milliseconds period;
25+
WallClock start_time;
2526

2627
uint64_t last_seqnum = ~(uint64_t)0;
2728

@@ -31,12 +32,14 @@ struct AudioInferencePivot::PeriodicCallback{
3132
Cancellable& p_scope,
3233
std::atomic<InferenceCallback*>* p_set_when_triggered,
3334
AudioInferenceCallback& p_callback,
34-
std::chrono::milliseconds p_period
35+
std::chrono::milliseconds p_period,
36+
WallClock m_start_time
3537
)
3638
: scope(p_scope)
3739
, set_when_triggered(p_set_when_triggered)
3840
, callback(p_callback)
3941
, period(p_period)
42+
, start_time(m_start_time)
4043
{}
4144
};
4245

@@ -55,7 +58,8 @@ void AudioInferencePivot::add_callback(
5558
Cancellable& scope,
5659
std::atomic<InferenceCallback*>* set_when_triggered,
5760
AudioInferenceCallback& callback,
58-
std::chrono::milliseconds period
61+
std::chrono::milliseconds period,
62+
WallClock start_time
5963
){
6064
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
6165
auto iter = m_map.find(&callback);
@@ -65,7 +69,7 @@ void AudioInferencePivot::add_callback(
6569
iter = m_map.emplace(
6670
std::piecewise_construct,
6771
std::forward_as_tuple(&callback),
68-
std::forward_as_tuple(scope, set_when_triggered, callback, period)
72+
std::forward_as_tuple(scope, set_when_triggered, callback, period, start_time)
6973
).first;
7074
try{
7175
PeriodicRunner::add_event(&iter->second, period);

SerialPrograms/Source/CommonTools/InferencePivots/AudioInferencePivot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class AudioInferencePivot final : public PeriodicRunner, public OverlayStat{
3232
Cancellable& scope,
3333
std::atomic<InferenceCallback*>* set_when_triggered,
3434
AudioInferenceCallback& callback,
35-
std::chrono::milliseconds period
35+
std::chrono::milliseconds period,
36+
WallClock start_time
3637
);
3738

3839
// Returns the latency stats for the callback. Units are microseconds.

SerialPrograms/Source/CommonTools/InferencePivots/VisualInferencePivot.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ struct VisualInferencePivot::PeriodicCallback{
2222
std::atomic<InferenceCallback*>* set_when_triggered;
2323
VisualInferenceCallback& callback;
2424
std::chrono::milliseconds period;
25-
StatAccumulatorI32 stats;
2625
WallClock last_timestamp;
26+
StatAccumulatorI32 stats;
2727

2828
PeriodicCallback(
2929
Cancellable& p_scope,
3030
std::atomic<InferenceCallback*>* p_set_when_triggered,
3131
VisualInferenceCallback& p_callback,
32-
std::chrono::milliseconds p_period
32+
std::chrono::milliseconds p_period,
33+
WallClock p_start_time
3334
)
3435
: scope(p_scope)
3536
, set_when_triggered(p_set_when_triggered)
3637
, callback(p_callback)
3738
, period(p_period)
38-
, last_timestamp(WallClock::min())
39+
, last_timestamp(p_start_time)
3940
{}
4041
};
4142

@@ -55,7 +56,8 @@ void VisualInferencePivot::add_callback(
5556
Cancellable& scope,
5657
std::atomic<InferenceCallback*>* set_when_triggered,
5758
VisualInferenceCallback& callback,
58-
std::chrono::milliseconds period
59+
std::chrono::milliseconds period,
60+
WallClock start_time
5961
){
6062
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
6163
auto iter = m_map.find(&callback);
@@ -65,7 +67,7 @@ void VisualInferencePivot::add_callback(
6567
iter = m_map.emplace(
6668
std::piecewise_construct,
6769
std::forward_as_tuple(&callback),
68-
std::forward_as_tuple(scope, set_when_triggered, callback, period)
70+
std::forward_as_tuple(scope, set_when_triggered, callback, period, start_time)
6971
).first;
7072
try{
7173
PeriodicRunner::add_event(&iter->second, period);
@@ -90,21 +92,7 @@ void VisualInferencePivot::run(void* event, bool is_back_to_back) noexcept{
9092
try{
9193
// Reuse the cached screenshot.
9294
if (!is_back_to_back || callback.last_timestamp == m_last.timestamp){
93-
// cout << "back-to-back" << endl;
94-
// m_last = m_feed.snapshot();
95-
96-
WallClock min_time = callback.last_timestamp;
97-
if (min_time == WallClock::min()){
98-
min_time = current_time() - 2 * callback.period;
99-
}
100-
101-
// TODO: Destructing "m_last" is really slow so this is not the
102-
// best place to do it.
103-
// WallClock start = current_time();
104-
// cout << "m_feed.snapshot_recent_nonblocking() - start" << endl;
105-
m_last = m_feed.snapshot_recent_nonblocking(min_time); // Implied destruction.
106-
// WallClock end = current_time();
107-
// cout << "m_feed.snapshot_recent_nonblocking() - end" << std::chrono::duration_cast<Milliseconds>(end - start).count() << endl;
95+
m_last = m_feed.snapshot_recent_nonblocking(callback.last_timestamp);
10896
}
10997

11098
if (!m_last){

SerialPrograms/Source/CommonTools/InferencePivots/VisualInferencePivot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class VisualInferencePivot final : public PeriodicRunner, public OverlayStat{
3333
Cancellable& scope,
3434
std::atomic<InferenceCallback*>* set_when_triggered,
3535
VisualInferenceCallback& callback,
36-
std::chrono::milliseconds period
36+
std::chrono::milliseconds period,
37+
WallClock start_time
3738
);
3839

3940
// Returns the latency stats for the callback. Units are microseconds.

0 commit comments

Comments
 (0)