-
Notifications
You must be signed in to change notification settings - Fork 15
/
watchdog.cpp
123 lines (97 loc) · 3.05 KB
/
watchdog.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
/*
* Copyright 2021 Google, Inc
*
* 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.
*/
#define LOG_TAG "lowmemorykiller"
#include <errno.h>
#include <log/log.h>
#include <string.h>
#include <processgroup/processgroup.h>
#include "watchdog.h"
static void* watchdog_main(void* param) {
Watchdog *watchdog = static_cast<Watchdog*>(param);
sigset_t sigset;
int signum;
// Ensure the thread does not use little cores
if (!SetTaskProfiles(gettid(), {"CPUSET_SP_FOREGROUND"}, true)) {
ALOGE("Failed to assign cpuset to the watchdog thread");
}
if (!watchdog->create_timer(sigset)) {
ALOGE("Watchdog timer creation failed!");
return NULL;
}
while (true) {
if (sigwait(&sigset, &signum) == -1) {
ALOGE("sigwait failed: %s", strerror(errno));
}
watchdog->bite();
}
return NULL;
}
bool Watchdog::init() {
pthread_t thread;
if (pthread_create(&thread, NULL, watchdog_main, this)) {
ALOGE("pthread_create failed: %s", strerror(errno));
return false;
}
if (pthread_setname_np(thread, "lmkd_watchdog")) {
ALOGW("pthread_setname_np failed: %s", strerror(errno));
}
return true;
}
bool Watchdog::start() {
// Start the timer and keep it active until it's disarmed
struct itimerspec new_timer;
if (!timer_created_) {
return false;
}
new_timer.it_value.tv_sec = timeout_;
new_timer.it_value.tv_nsec = 0;
new_timer.it_interval.tv_sec = timeout_;
new_timer.it_interval.tv_nsec = 0;
if (timer_settime(timer_, 0, &new_timer, NULL)) {
ALOGE("timer_settime failed: %s", strerror(errno));
return false;
}
return true;
}
bool Watchdog::stop() {
struct itimerspec new_timer = {};
if (!timer_created_) {
return false;
}
if (timer_settime(timer_, 0, &new_timer, NULL)) {
ALOGE("timer_settime failed: %s", strerror(errno));
return false;
}
return true;
}
bool Watchdog::create_timer(sigset_t &sigset) {
struct sigevent sevent;
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM);
if (sigprocmask(SIG_BLOCK, &sigset, NULL)) {
ALOGE("sigprocmask failed: %s", strerror(errno));
return false;
}
sevent.sigev_notify = SIGEV_THREAD_ID;
sevent.sigev_notify_thread_id = gettid();
sevent.sigev_signo = SIGALRM;
if (timer_create(CLOCK_MONOTONIC, &sevent, &timer_)) {
ALOGE("timer_create failed: %s", strerror(errno));
return false;
}
timer_created_ = true;
return true;
}