forked from zombi-x/android_hardware_nvidia_power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeoutpoker.cpp
297 lines (247 loc) · 7.25 KB
/
timeoutpoker.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
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
/*
* Copyright (c) 2011-2013 NVIDIA Corporation. All Rights Reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation. Any
* use, reproduction, disclosure or distribution of this software and related
* documentation without an express license agreement from NVIDIA Corporation
* is strictly prohibited.
*/
#include "timeoutpoker.h"
#include <fcntl.h>
#undef LOG_TAG
#define LOG_TAG "powerHAL::TimeoutPoker"
enum {
CLIENT_FD,
SERVER_FD
};
TimeoutPoker::TimeoutPoker(Barrier* readyToRun)
{
mPokeHandler = new PokeHandler(this, readyToRun);
}
//Called usually from IPC thread
void TimeoutPoker::pushEvent(QueuedEvent* event)
{
mPokeHandler->sendEventDelayed(0, event);
}
int TimeoutPoker::PokeHandler::createHandleForFd(int fd)
{
int pipefd[2];
int res = pipe(pipefd);
if (res) {
ALOGE("unabled to create handle for fd");
close(fd);
return -1;
}
listenForHandleToCloseFd(pipefd[SERVER_FD], fd);
if (res) {
close(fd);
close(pipefd[SERVER_FD]);
close(pipefd[CLIENT_FD]);
return -1;
}
return pipefd[CLIENT_FD];
}
int TimeoutPoker::PokeHandler::openPmQosNode(const char* filename, int val)
{
int pm_qos_fd = open(filename, O_RDWR);;
if (pm_qos_fd < 0) {
ALOGE("unable to open pm_qos file for %s: %s", filename, strerror(errno));
return -1;
}
write(pm_qos_fd, &val, sizeof(val));
return pm_qos_fd;
}
int TimeoutPoker::PokeHandler::openPmQosNode(const char* filename, int priority, int max, int min)
{
int pm_qos_fd = open(filename, O_RDWR);;
if (pm_qos_fd < 0) {
ALOGE("unable to open pm_qos file for %s: %s", filename, strerror(errno));
return -1;
}
char command[COMMAND_SIZE];
int size = createConstraintCommand((char*)command, COMMAND_SIZE, priority, max, min);
write(pm_qos_fd, command, size);
return pm_qos_fd;
}
int TimeoutPoker::PokeHandler::createHandleForPmQosRequest(const char* filename, int val)
{
int fd = openPmQosNode(filename, val);
if (fd < 0) {
return -1;
}
return createHandleForFd(fd);
}
int TimeoutPoker::PokeHandler::createHandleForPmQosRequest(const char* filename, int priority, int max, int min)
{
int fd = openPmQosNode(filename, priority, max, min);
if (fd < 0) {
return -1;
}
return createHandleForFd(fd);
}
int TimeoutPoker::createPmQosHandle(const char* filename,
int val)
{
Barrier done;
int ret;
pushEvent(new PmQosOpenHandleEvent(
filename, val, &ret, &done));
done.wait();
return ret;
}
void TimeoutPoker::requestPmQosTimed(const char* filename,
int val, nsecs_t timeout)
{
pushEvent(new PmQosOpenTimedEvent(
filename, val, timeout));
}
int TimeoutPoker::requestPmQos(const char* filename, int val)
{
int pm_qos_fd = open(filename, O_RDWR);
if (pm_qos_fd < 0) {
ALOGE("unable to open pm_qos file for %s: %s", filename, strerror(errno));
return -1;
}
write(pm_qos_fd, &val, sizeof(val));
return pm_qos_fd;
}
int TimeoutPoker::createPmQosHandle(const char* filename,
int priority, int max, int min)
{
Barrier done;
int ret;
pushEvent(new PmQosOpenHandleEvent(
filename, priority, max, min, &ret, &done));
done.wait();
return ret;
}
void TimeoutPoker::requestPmQosTimed(const char* filename,
int priority, int max, int min, nsecs_t timeout)
{
pushEvent(new PmQosOpenTimedEvent(
filename, priority, max, min, timeout));
}
int TimeoutPoker::requestPmQos(const char* filename, int priority, int max, int min)
{
int pm_qos_fd = open(filename, O_RDWR);
if (pm_qos_fd < 0) {
ALOGE("unable to open pm_qos file for %s: %s", filename, strerror(errno));
return -1;
}
char command[COMMAND_SIZE];
int size = createConstraintCommand((char*)command, COMMAND_SIZE, priority, max, min);
write(pm_qos_fd, command, size);
return pm_qos_fd;
}
/*
* PokeHandler
*/
void TimeoutPoker::PokeHandler::sendEventDelayed(
nsecs_t delay, TimeoutPoker::QueuedEvent* ev) {
Mutex::Autolock _l(mEvLock);
int key = generateNewKey();
mQueuedEvents.add(key, ev);
mWorker->mLooper->sendMessageDelayed(delay, this, key);
}
TimeoutPoker::QueuedEvent*
TimeoutPoker::PokeHandler::removeEventByKey(
int what) {
Mutex::Autolock _l(mEvLock);
// msg.what contains a key to retrieve the message parameters
TimeoutPoker::QueuedEvent* e =
mQueuedEvents.valueFor(what);
mQueuedEvents.removeItem(what);
return e;
}
int TimeoutPoker::PokeHandler::generateNewKey(void)
{
return mKey++;
}
TimeoutPoker::PokeHandler::PokeHandler(TimeoutPoker* poker, Barrier* readyToRun) :
mPoker(poker),
mKey(0),
mSpamRefresh(false)
{
mWorker = new LooperThread(readyToRun);
mWorker->run("TimeoutPoker::PokeHandler::LooperThread", PRIORITY_FOREGROUND);
readyToRun->wait();
}
void TimeoutPoker::PokeHandler::handleMessage(const Message& msg)
{
assert(!mQueuedEvents->isEmpty());
// msg.what contains a key to retrieve the message parameters
TimeoutPoker::QueuedEvent* e =
removeEventByKey(msg.what);
if (!e)
return;
e->run(this);
delete e;
}
void TimeoutPoker::PokeHandler::openPmQosTimed(const char* filename,
int val, nsecs_t timeout)
{
int fd = openPmQosNode(filename, val);
if (fd < 0) {
return;
}
sendEventDelayed(timeout, new TimeoutEvent(fd));
}
void TimeoutPoker::PokeHandler::openPmQosTimed(const char* filename,
int priority, int max, int min, nsecs_t timeout)
{
int fd = openPmQosNode(filename, priority, max, min);
if (fd < 0) {
return;
}
sendEventDelayed(timeout, new TimeoutEvent(fd));
}
void TimeoutPoker::PokeHandler::timeoutRequest(int fd)
{
close(fd);
}
status_t TimeoutPoker::PokeHandler::LooperThread::readyToRun()
{
mLooper = Looper::prepare(0);
if (mLooper == 0)
return NO_MEMORY;
mReadyToRun->open();
return NO_ERROR;
}
bool TimeoutPoker::PokeHandler::LooperThread::threadLoop()
{
int res = mLooper->pollAll(99999);
if (res == ALOOPER_POLL_ERROR)
ALOGE("Poll returned an error!");
return true;
}
class CallbackContext {
public:
CallbackContext(sp<Looper> looper, int fd) : looper(looper), fd(fd) {}
sp<Looper> looper;
int fd;
};
static int pipeCloseCb(int handle, int events, void* data)
{
CallbackContext* ctx = (CallbackContext*)data;
if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
ctx->looper->removeFd(handle);
close(handle);
close(ctx->fd);
delete ctx;
return 0;
}
return 1;
}
//Reverse arity of result to match call-site usage
int TimeoutPoker::PokeHandler::listenForHandleToCloseFd(int handle, int fd)
{
//This func is threadsafe
return !mWorker->mLooper->addFd(handle, ALOOPER_POLL_CALLBACK,
ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP,
pipeCloseCb, new CallbackContext(mWorker->mLooper, fd));
}
int createConstraintCommand(char* command, int size, int priority, int max, int min) {
snprintf(command, size, "%d %d %d 0", max, min, priority);
return strlen(command);
}