Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 793ef705 authored by Ady Abraham's avatar Ady Abraham Committed by Android (Google) Code Review
Browse files

Merge changes I55af2ec2,I4aa5b86c

* changes:
  SurfaceFlinger: add thread name to OneShotTimer
  SurfaceFlinger: fix OneShotTimer timespec overflow
parents 2d6b7349 db3dfeec
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ int32_t getUpdateTimeout() {
PowerAdvisor::PowerAdvisor()
      : mUseUpdateImminentTimer(getUpdateTimeout() > 0),
        mUpdateImminentTimer(
                OneShotTimer::Interval(getUpdateTimeout()),
                "UpdateImminentTimer", OneShotTimer::Interval(getUpdateTimeout()),
                /* resetCallback */ [this] { mSendUpdateImminent.store(false); },
                /* timeoutCallback */ [this] { mSendUpdateImminent.store(true); }) {
    if (mUseUpdateImminentTimer) {
+1 −0
Original line number Diff line number Diff line
@@ -175,6 +175,7 @@ RegionSamplingThread::RegionSamplingThread(SurfaceFlinger& flinger, Scheduler& s
        mScheduler(scheduler),
        mTunables(tunables),
        mIdleTimer(
                "RegionSamplingIdleTimer",
                std::chrono::duration_cast<std::chrono::milliseconds>(
                        mTunables.mSamplingTimerTimeout),
                [] {}, [this] { checkForStaleLuma(); }),
+35 −11
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "OneShotTimer.h"

#include <utils/Log.h>
#include <utils/Timers.h>
#include <chrono>
#include <sstream>
#include <thread>
@@ -29,25 +31,30 @@ constexpr int64_t kNsToSeconds = std::chrono::duration_cast<std::chrono::nanosec
// (tv_sec) is the whole count of seconds. The second (tv_nsec) is the
// nanosecond part of the count. This function takes care of translation.
void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) {
    clock_gettime(CLOCK_MONOTONIC, spec);
    spec->tv_sec += static_cast<__kernel_time_t>(timestamp.count() / kNsToSeconds);
    spec->tv_nsec += timestamp.count() % kNsToSeconds;
    const nsecs_t timeout = systemTime(CLOCK_MONOTONIC) + timestamp.count();
    spec->tv_sec = static_cast<__kernel_time_t>(timeout / kNsToSeconds);
    spec->tv_nsec = timeout % kNsToSeconds;
}
} // namespace

namespace android {
namespace scheduler {

OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
OneShotTimer::OneShotTimer(std::string name, const Interval& interval,
                           const ResetCallback& resetCallback,
                           const TimeoutCallback& timeoutCallback)
      : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {}
      : mName(std::move(name)),
        mInterval(interval),
        mResetCallback(resetCallback),
        mTimeoutCallback(timeoutCallback) {}

OneShotTimer::~OneShotTimer() {
    stop();
}

void OneShotTimer::start() {
    sem_init(&mSemaphore, 0, 0);
    int result = sem_init(&mSemaphore, 0, 0);
    LOG_ALWAYS_FATAL_IF(result, "sem_init failed");

    if (!mThread.joinable()) {
        // Only create thread if it has not been created.
@@ -57,15 +64,21 @@ void OneShotTimer::start() {

void OneShotTimer::stop() {
    mStopTriggered = true;
    sem_post(&mSemaphore);
    int result = sem_post(&mSemaphore);
    LOG_ALWAYS_FATAL_IF(result, "sem_post failed");

    if (mThread.joinable()) {
        mThread.join();
        sem_destroy(&mSemaphore);
        result = sem_destroy(&mSemaphore);
        LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed");
    }
}

void OneShotTimer::loop() {
    if (pthread_setname_np(pthread_self(), mName.c_str())) {
        ALOGW("Failed to set thread name on dispatch thread");
    }

    TimerState state = TimerState::RESET;
    while (true) {
        bool triggerReset = false;
@@ -77,7 +90,12 @@ void OneShotTimer::loop() {
        }

        if (state == TimerState::IDLE) {
            sem_wait(&mSemaphore);
            int result = sem_wait(&mSemaphore);
            if (result && errno != EINTR) {
                std::stringstream ss;
                ss << "sem_wait failed (" << errno << ")";
                LOG_ALWAYS_FATAL("%s", ss.str().c_str());
            }
            continue;
        }

@@ -101,7 +119,12 @@ void OneShotTimer::loop() {
            // Wait for mInterval time for semaphore signal.
            struct timespec ts;
            calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts);
            sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts);
            int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts);
            if (result && errno != ETIMEDOUT && errno != EINTR) {
                std::stringstream ss;
                ss << "sem_clockwait failed (" << errno << ")";
                LOG_ALWAYS_FATAL("%s", ss.str().c_str());
            }

            state = checkForResetAndStop(state);
            if (state == TimerState::RESET) {
@@ -135,7 +158,8 @@ OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) {

void OneShotTimer::reset() {
    mResetTriggered = true;
    sem_post(&mSemaphore);
    int result = sem_post(&mSemaphore);
    LOG_ALWAYS_FATAL_IF(result, "sem_post failed");
}

std::string OneShotTimer::dump() const {
+4 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public:
    using ResetCallback = std::function<void()>;
    using TimeoutCallback = std::function<void()>;

    OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
    OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback,
                 const TimeoutCallback& timeoutCallback);
    ~OneShotTimer();

@@ -81,6 +81,9 @@ private:
    // Semaphore to keep mThread synchronized.
    sem_t mSemaphore;

    // Timer's name.
    std::string mName;

    // Interval after which timer expires.
    const Interval mInterval;

+3 −3
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCal
        const auto callback = mOptions.supportKernelTimer ? &Scheduler::kernelIdleTimerCallback
                                                          : &Scheduler::idleTimerCallback;
        mIdleTimer.emplace(
                std::chrono::milliseconds(millis),
                "IdleTimer", std::chrono::milliseconds(millis),
                [this, callback] { std::invoke(callback, this, TimerState::Reset); },
                [this, callback] { std::invoke(callback, this, TimerState::Expired); });
        mIdleTimer->start();
@@ -144,7 +144,7 @@ Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCal
    if (const int64_t millis = set_touch_timer_ms(0); millis > 0) {
        // Touch events are coming to SF every 100ms, so the timer needs to be higher than that
        mTouchTimer.emplace(
                std::chrono::milliseconds(millis),
                "TouchTimer", std::chrono::milliseconds(millis),
                [this] { touchTimerCallback(TimerState::Reset); },
                [this] { touchTimerCallback(TimerState::Expired); });
        mTouchTimer->start();
@@ -152,7 +152,7 @@ Scheduler::Scheduler(const scheduler::RefreshRateConfigs& configs, ISchedulerCal

    if (const int64_t millis = set_display_power_timer_ms(0); millis > 0) {
        mDisplayPowerTimer.emplace(
                std::chrono::milliseconds(millis),
                "DisplayPowerTimer", std::chrono::milliseconds(millis),
                [this] { displayPowerTimerCallback(TimerState::Reset); },
                [this] { displayPowerTimerCallback(TimerState::Expired); });
        mDisplayPowerTimer->start();
Loading