summaryrefslogtreecommitdiff
path: root/suspend/1.0/default/SystemSuspend.cpp
blob: 2262c58aa070be4b111b78c231d6e31318875a68 (plain)
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
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SystemSuspend.h"

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <fcntl.h>
#include <hidl/Status.h>
#include <hwbinder/IPCThreadState.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string>
#include <thread>

using ::android::base::Error;
using ::android::base::ReadFdToString;
using ::android::base::WriteStringToFd;
using ::android::hardware::Void;
using ::std::string;

namespace android {
namespace system {
namespace suspend {
namespace V1_0 {

struct SuspendTime {
    std::chrono::nanoseconds suspendOverhead;
    std::chrono::nanoseconds suspendTime;
};

static const char kSleepState[] = "mem";
// TODO(b/128923994): we only need /sys/power/wake_[un]lock to export debugging info via
// /sys/kernel/debug/wakeup_sources.
static constexpr char kSysPowerWakeLock[] = "/sys/power/wake_lock";
static constexpr char kSysPowerWakeUnlock[] = "/sys/power/wake_unlock";
static constexpr char kUnknownWakeup[] = "unknown";

// This function assumes that data in fd is small enough that it can be read in one go.
// We use this function instead of the ones available in libbase because it doesn't block
// indefinitely when reading from socket streams which are used for testing.
string readFd(int fd) {
    char buf[BUFSIZ];
    ssize_t n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)));
    if (n < 0) return "";
    return string{buf, static_cast<size_t>(n)};
}

static inline int getCallingPid() {
    return ::android::hardware::IPCThreadState::self()->getCallingPid();
}

static std::vector<std::string> readWakeupReasons(int fd) {
    std::vector<std::string> wakeupReasons;
    std::string reasonlines;

    lseek(fd, 0, SEEK_SET);
    if (!ReadFdToString(fd, &reasonlines) || reasonlines.empty()) {
        PLOG(ERROR) << "failed to read wakeup reasons";
        // Return unknown wakeup reason if we fail to read
        return {kUnknownWakeup};
    }

    std::stringstream ss(reasonlines);
    for (std::string reasonline; std::getline(ss, reasonline);) {
        reasonline = ::android::base::Trim(reasonline);

        // Only include non-empty reason lines
        if (!reasonline.empty()) {
            wakeupReasons.push_back(reasonline);
        }
    }

    // Empty wakeup reason found. Record as unknown wakeup
    if (wakeupReasons.empty()) {
        wakeupReasons.push_back(kUnknownWakeup);
    }

    return wakeupReasons;
}

// reads the suspend overhead and suspend time
// Returns 0s if reading the sysfs node fails (unlikely)
static struct SuspendTime readSuspendTime(int fd) {
    std::string content;

    lseek(fd, 0, SEEK_SET);
    if (!ReadFdToString(fd, &content)) {
        LOG(ERROR) << "failed to read suspend time";
        return {0ns, 0ns};
    }

    double suspendOverhead, suspendTime;
    std::stringstream ss(content);
    if (!(ss >> suspendOverhead) || !(ss >> suspendTime)) {
        LOG(ERROR) << "failed to parse suspend time " << content;
        return {0ns, 0ns};
    }

    return {std::chrono::duration_cast<std::chrono::nanoseconds>(
                std::chrono::duration<double>(suspendOverhead)),
            std::chrono::duration_cast<std::chrono::nanoseconds>(
                std::chrono::duration<double>(suspendTime))};
}

WakeLock::WakeLock(SystemSuspend* systemSuspend, const string& name, int pid)
    : mReleased(), mSystemSuspend(systemSuspend), mName(name), mPid(pid) {
    mSystemSuspend->incSuspendCounter(mName);
}

WakeLock::~WakeLock() {
    releaseOnce();
}

Return<void> WakeLock::release() {
    releaseOnce();
    return Void();
}

void WakeLock::releaseOnce() {
    std::call_once(mReleased, [this]() {
        mSystemSuspend->decSuspendCounter(mName);
        mSystemSuspend->updateWakeLockStatOnRelease(mName, mPid, getTimeNow());
    });
}

SystemSuspend::SystemSuspend(unique_fd wakeupCountFd, unique_fd stateFd, unique_fd suspendStatsFd,
                             size_t maxStatsEntries, unique_fd kernelWakelockStatsFd,
                             unique_fd wakeupReasonsFd, unique_fd suspendTimeFd,
                             const SleepTimeConfig& sleepTimeConfig,
                             const sp<SuspendControlService>& controlService,
                             const sp<SuspendControlServiceInternal>& controlServiceInternal,
                             bool useSuspendCounter)
    : mSuspendCounter(0),
      mWakeupCountFd(std::move(wakeupCountFd)),
      mStateFd(std::move(stateFd)),
      mSuspendStatsFd(std::move(suspendStatsFd)),
      mSuspendTimeFd(std::move(suspendTimeFd)),
      kSleepTimeConfig(sleepTimeConfig),
      mSleepTime(sleepTimeConfig.baseSleepTime),
      mNumConsecutiveBadSuspends(0),
      mControlService(controlService),
      mControlServiceInternal(controlServiceInternal),
      mStatsList(maxStatsEntries, std::move(kernelWakelockStatsFd)),
      mWakeupList(maxStatsEntries),
      mUseSuspendCounter(useSuspendCounter),
      mWakeLockFd(-1),
      mWakeUnlockFd(-1),
      mWakeupReasonsFd(std::move(wakeupReasonsFd)) {
    mControlServiceInternal->setSuspendService(this);

    if (!mUseSuspendCounter) {
        mWakeLockFd.reset(TEMP_FAILURE_RETRY(open(kSysPowerWakeLock, O_CLOEXEC | O_RDWR)));
        if (mWakeLockFd < 0) {
            PLOG(ERROR) << "error opening " << kSysPowerWakeLock;
        }
        mWakeUnlockFd.reset(TEMP_FAILURE_RETRY(open(kSysPowerWakeUnlock, O_CLOEXEC | O_RDWR)));
        if (mWakeUnlockFd < 0) {
            PLOG(ERROR) << "error opening " << kSysPowerWakeUnlock;
        }
    }
}

bool SystemSuspend::enableAutosuspend() {
    if (mAutosuspendEnabled.test_and_set()) {
        LOG(ERROR) << "Autosuspend already started.";
        return false;
    }

    initAutosuspend();
    return true;
}

bool SystemSuspend::forceSuspend() {
    //  We are forcing the system to suspend. This particular call ignores all
    //  existing wakelocks (full or partial). It does not cancel the wakelocks
    //  or reset mSuspendCounter, it just ignores them.  When the system
    //  returns from suspend, the wakelocks and SuspendCounter will not have
    //  changed.
    auto counterLock = std::unique_lock(mCounterLock);
    bool success = WriteStringToFd(kSleepState, mStateFd);
    counterLock.unlock();

    if (!success) {
        PLOG(VERBOSE) << "error writing to /sys/power/state for forceSuspend";
    }
    return success;
}

Return<sp<IWakeLock>> SystemSuspend::acquireWakeLock(WakeLockType /* type */,
                                                     const hidl_string& name) {
    auto pid = getCallingPid();
    auto timeNow = getTimeNow();
    IWakeLock* wl = new WakeLock{this, name, pid};
    mControlService->notifyWakelock(name, true);
    mStatsList.updateOnAcquire(name, pid, timeNow);
    return wl;
}

void SystemSuspend::incSuspendCounter(const string& name) {
    auto l = std::lock_guard(mCounterLock);
    if (mUseSuspendCounter) {
        mSuspendCounter++;
    } else {
        if (!WriteStringToFd(name, mWakeLockFd)) {
            PLOG(ERROR) << "error writing " << name << " to " << kSysPowerWakeLock;
        }
    }
}

void SystemSuspend::decSuspendCounter(const string& name) {
    auto l = std::lock_guard(mCounterLock);
    if (mUseSuspendCounter) {
        if (--mSuspendCounter == 0) {
            mCounterCondVar.notify_one();
        }
    } else {
        if (!WriteStringToFd(name, mWakeUnlockFd)) {
            PLOG(ERROR) << "error writing " << name << " to " << kSysPowerWakeUnlock;
        }
    }
}

unique_fd SystemSuspend::reopenFileUsingFd(const int pid, const int fd, const int permission) {
    string filePath = android::base::StringPrintf("/proc/%d/fd/%d", pid, fd);

    unique_fd tempFd{TEMP_FAILURE_RETRY(open(filePath.c_str(), permission))};
    if (tempFd < 0) {
        PLOG(ERROR) << "SystemSuspend: Error opening file, using path: " << filePath;
        return unique_fd(-1);
    }
    return tempFd;
}

void SystemSuspend::initAutosuspend() {
    std::thread autosuspendThread([this] {
        while (true) {
            std::this_thread::sleep_for(mSleepTime);
            lseek(mWakeupCountFd, 0, SEEK_SET);
            const string wakeupCount = readFd(mWakeupCountFd);
            if (wakeupCount.empty()) {
                PLOG(ERROR) << "error reading from /sys/power/wakeup_count";
                continue;
            }

            auto counterLock = std::unique_lock(mCounterLock);
            mCounterCondVar.wait(counterLock, [this] { return mSuspendCounter == 0; });
            // The mutex is locked and *MUST* remain locked until we write to /sys/power/state.
            // Otherwise, a WakeLock might be acquired after we check mSuspendCounter and before we
            // write to /sys/power/state.

            if (!WriteStringToFd(wakeupCount, mWakeupCountFd)) {
                PLOG(VERBOSE) << "error writing from /sys/power/wakeup_count";
                continue;
            }
            bool success = WriteStringToFd(kSleepState, mStateFd);
            counterLock.unlock();

            if (!success) {
                PLOG(VERBOSE) << "error writing to /sys/power/state";
            }

            struct SuspendTime suspendTime = readSuspendTime(mSuspendTimeFd);
            updateSleepTime(success, suspendTime);

            std::vector<std::string> wakeupReasons = readWakeupReasons(mWakeupReasonsFd);
            if (wakeupReasons == std::vector<std::string>({kUnknownWakeup})) {
                LOG(INFO) << "Unknown/empty wakeup reason. Re-opening wakeup_reason file.";

                mWakeupReasonsFd = std::move(reopenFileUsingFd(
                    getCallingPid(), mWakeupReasonsFd.get(), O_CLOEXEC | O_RDONLY));
            }
            mWakeupList.update(wakeupReasons);

            mControlService->notifyWakeup(success, wakeupReasons);
        }
    });
    autosuspendThread.detach();
    LOG(INFO) << "automatic system suspend enabled";
}

/**
 * Updates sleep time depending on the result of suspend attempt.
 * Time (in milliseconds) between suspend attempts is described the formula
 * t[n] = { B, 0 < n <= N
 *        { min(B * (S**(n - N)), M), n > N
 * where:
 *   n is the number of consecutive bad suspend attempts,
 *   B = kBaseSleepTime,
 *   N = kSuspendBackoffThreshold,
 *   S = kSleepTimeScaleFactor,
 *   M = kMaxSleepTime
 *
 * kFailedSuspendBackoffEnabled determines whether a failed suspend is counted as a bad suspend
 *
 * kShortSuspendBackoffEnabled determines whether a suspend whose duration
 * t < kShortSuspendThreshold is counted as a bad suspend
 */
void SystemSuspend::updateSleepTime(bool success, const struct SuspendTime& suspendTime) {
    std::scoped_lock lock(mSuspendInfoLock);
    mSuspendInfo.suspendAttemptCount++;
    mSuspendInfo.sleepTimeMillis +=
        std::chrono::round<std::chrono::milliseconds>(mSleepTime).count();

    bool shortSuspend = success && (suspendTime.suspendTime > 0ns) &&
                        (suspendTime.suspendTime < kSleepTimeConfig.shortSuspendThreshold);

    bool badSuspend = (kSleepTimeConfig.failedSuspendBackoffEnabled && !success) ||
                      (kSleepTimeConfig.shortSuspendBackoffEnabled && shortSuspend);

    auto suspendTimeMillis =
        std::chrono::round<std::chrono::milliseconds>(suspendTime.suspendTime).count();
    auto suspendOverheadMillis =
        std::chrono::round<std::chrono::milliseconds>(suspendTime.suspendOverhead).count();

    if (success) {
        mSuspendInfo.suspendOverheadTimeMillis += suspendOverheadMillis;
        mSuspendInfo.suspendTimeMillis += suspendTimeMillis;
    } else {
        mSuspendInfo.failedSuspendCount++;
        mSuspendInfo.failedSuspendOverheadTimeMillis += suspendOverheadMillis;
    }

    if (shortSuspend) {
        mSuspendInfo.shortSuspendCount++;
        mSuspendInfo.shortSuspendTimeMillis += suspendTimeMillis;
    }

    if (!badSuspend) {
        mNumConsecutiveBadSuspends = 0;
        mSleepTime = kSleepTimeConfig.baseSleepTime;
        return;
    }

    // Suspend attempt was bad (failed or short suspend)
    if (mNumConsecutiveBadSuspends >= kSleepTimeConfig.backoffThreshold) {
        if (mNumConsecutiveBadSuspends == kSleepTimeConfig.backoffThreshold) {
            mSuspendInfo.newBackoffCount++;
        } else {
            mSuspendInfo.backoffContinueCount++;
        }

        mSleepTime = std::min(std::chrono::round<std::chrono::milliseconds>(
                                  mSleepTime * kSleepTimeConfig.sleepTimeScaleFactor),
                              kSleepTimeConfig.maxSleepTime);
    }

    mNumConsecutiveBadSuspends++;
}

void SystemSuspend::updateWakeLockStatOnRelease(const std::string& name, int pid,
                                                TimestampType timeNow) {
    mControlService->notifyWakelock(name, false);
    mStatsList.updateOnRelease(name, pid, timeNow);
}

const WakeLockEntryList& SystemSuspend::getStatsList() const {
    return mStatsList;
}

void SystemSuspend::updateStatsNow() {
    mStatsList.updateNow();
}

void SystemSuspend::getSuspendInfo(SuspendInfo* info) {
    std::scoped_lock lock(mSuspendInfoLock);

    *info = mSuspendInfo;
}

const WakeupList& SystemSuspend::getWakeupList() const {
    return mWakeupList;
}

/**
 * Returns suspend stats.
 */
Result<SuspendStats> SystemSuspend::getSuspendStats() {
    SuspendStats stats;
    std::unique_ptr<DIR, decltype(&closedir)> dp(fdopendir(dup(mSuspendStatsFd.get())), &closedir);
    if (!dp) {
        return stats;
    }

    // rewinddir, else subsequent calls will not get any suspend_stats
    rewinddir(dp.get());

    struct dirent* de;

    // Grab a wakelock before reading suspend stats,
    // to ensure a consistent snapshot.
    sp<IWakeLock> suspendStatsLock = acquireWakeLock(WakeLockType::PARTIAL, "suspend_stats_lock");

    while ((de = readdir(dp.get()))) {
        std::string statName(de->d_name);
        if ((statName == ".") || (statName == "..")) {
            continue;
        }

        unique_fd statFd{TEMP_FAILURE_RETRY(
            openat(mSuspendStatsFd.get(), statName.c_str(), O_CLOEXEC | O_RDONLY))};
        if (statFd < 0) {
            return Error() << "Failed to open " << statName;
        }

        std::string valStr;
        if (!ReadFdToString(statFd.get(), &valStr)) {
            return Error() << "Failed to read " << statName;
        }

        // Trim newline
        valStr.erase(std::remove(valStr.begin(), valStr.end(), '\n'), valStr.end());

        if (statName == "last_failed_dev") {
            stats.lastFailedDev = valStr;
        } else if (statName == "last_failed_step") {
            stats.lastFailedStep = valStr;
        } else {
            int statVal = std::stoi(valStr);
            if (statName == "success") {
                stats.success = statVal;
            } else if (statName == "fail") {
                stats.fail = statVal;
            } else if (statName == "failed_freeze") {
                stats.failedFreeze = statVal;
            } else if (statName == "failed_prepare") {
                stats.failedPrepare = statVal;
            } else if (statName == "failed_suspend") {
                stats.failedSuspend = statVal;
            } else if (statName == "failed_suspend_late") {
                stats.failedSuspendLate = statVal;
            } else if (statName == "failed_suspend_noirq") {
                stats.failedSuspendNoirq = statVal;
            } else if (statName == "failed_resume") {
                stats.failedResume = statVal;
            } else if (statName == "failed_resume_early") {
                stats.failedResumeEarly = statVal;
            } else if (statName == "failed_resume_noirq") {
                stats.failedResumeNoirq = statVal;
            } else if (statName == "last_failed_errno") {
                stats.lastFailedErrno = statVal;
            }
        }
    }

    return stats;
}

std::chrono::milliseconds SystemSuspend::getSleepTime() const {
    return mSleepTime;
}

}  // namespace V1_0
}  // namespace suspend
}  // namespace system
}  // namespace android