Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed InferredSpans interrupt timing (#1089) #1090

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/native/ext/lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ void elasticApmRequestInit()

ELASTIC_APM_LOG_DEBUG("resuming inferred spans thread with sampling interval %zums", interval.count());
ELASTICAPM_G(globals)->inferredSpans_->setInterval(interval);
ELASTICAPM_G(globals)->inferredSpans_->reset();
ELASTICAPM_G(globals)->periodicTaskExecutor_->setInterval(interval);
ELASTICAPM_G(globals)->periodicTaskExecutor_->resumePeriodicTasks();
}
Expand Down
12 changes: 9 additions & 3 deletions agent/native/libcommon/code/InferredSpans.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,36 @@ class InferredSpans {
}

std::unique_lock lock(mutex_);

if (now > lastInterruptRequestTick_ + samplingInterval_) {
lastInterruptRequestTick_ = now;

interruptedRequested_ = true;
lock.unlock();
interrupt_(); // set interrupt for user space functions
}

}


void setInterval(std::chrono::milliseconds interval) {
std::lock_guard lock(mutex_);
samplingInterval_ = interval;
}

void reset() {
std::lock_guard lock(mutex_);
lastInterruptRequestTick_ = std::chrono::time_point_cast<time_point_t::duration>(clock_t::now());
}

private:
bool checkAndResetInterruptFlag() {
bool interrupted = true;
return interruptedRequested_.compare_exchange_strong(interrupted, false, std::memory_order_release, std::memory_order_acquire);
}

std::atomic_bool interruptedRequested_;
std::atomic_bool interruptedRequested_ = false;
std::chrono::milliseconds samplingInterval_ = std::chrono::milliseconds(20);
time_point_t lastInterruptRequestTick_{};
time_point_t lastInterruptRequestTick_ = std::chrono::time_point_cast<time_point_t::duration>(clock_t::now());
std::mutex mutex_;
interruptFunc_t interrupt_;
attachInferredSpansOnPhp_t attachInferredSpansOnPhp_;
Expand Down
14 changes: 11 additions & 3 deletions agent/native/libcommon/test/InferredSpansTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class InferredSpansTest : public ::testing::Test {

TEST_F(InferredSpansTest, TryInterrupt) {
inferredSpans_.setInterval(1ms);
std::this_thread::sleep_for(2ms);

EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(1));

Expand All @@ -55,6 +56,7 @@ TEST_F(InferredSpansTest, TryInterrupt) {

TEST_F(InferredSpansTest, TryInterruptOnlyOnce) {
inferredSpans_.setInterval(1ms);
std::this_thread::sleep_for(2ms);

EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(1));

Expand All @@ -64,21 +66,26 @@ TEST_F(InferredSpansTest, TryInterruptOnlyOnce) {
}

TEST_F(InferredSpansTest, DontInterruptBeforeInterval) {
inferredSpans_.setInterval(10min);
inferredSpans_.setInterval(1ms);
std::this_thread::sleep_for(2ms);

::testing::InSequence s;
EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(1));
inferredSpans_.tryRequestInterrupt(std::chrono::time_point_cast<std::chrono::milliseconds>(InferredSpans::clock_t::now()));

inferredSpans_.setInterval(10min);

EXPECT_CALL(attachInferredSpansFuncMock_, attachInferredSpansOnPhp(::testing::_, ::testing::_)).Times(::testing::Exactly(1));
inferredSpans_.attachBacktraceIfInterrupted();

EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(0));
inferredSpans_.tryRequestInterrupt(std::chrono::time_point_cast<std::chrono::milliseconds>(InferredSpans::clock_t::now()));
EXPECT_CALL(attachInferredSpansFuncMock_, attachInferredSpansOnPhp(::testing::_, ::testing::_)).Times(::testing::Exactly(0));

}

TEST_F(InferredSpansTest, InterruptAfterInterval) {
inferredSpans_.setInterval(5ms);
std::this_thread::sleep_for(6ms);

EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(1));
inferredSpans_.tryRequestInterrupt(std::chrono::time_point_cast<std::chrono::milliseconds>(InferredSpans::clock_t::now()));
Expand All @@ -94,7 +101,8 @@ TEST_F(InferredSpansTest, InterruptAfterInterval) {

TEST_F(InferredSpansTest, DontInterruptAfterIntervalIfSpansNotAttachedYet) {
inferredSpans_.setInterval(5ms);

std::this_thread::sleep_for(6ms);

EXPECT_CALL(interruptFuncMock_, interruptFunction()).Times(::testing::Exactly(1));
inferredSpans_.tryRequestInterrupt(std::chrono::time_point_cast<std::chrono::milliseconds>(InferredSpans::clock_t::now()));

Expand Down
Loading