rsdCore.cpp revision 2cfe51e7a9eef3dec091ce7c15d2a5a2216e9d3e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsdCore.h"
18#include "rsdBcc.h"
19
20#include <malloc.h>
21#include "rsContext.h"
22
23#include <sys/types.h>
24#include <sys/resource.h>
25#include <sched.h>
26#include <cutils/properties.h>
27#include <cutils/sched_policy.h>
28#include <sys/syscall.h>
29#include <string.h>
30
31using namespace android;
32using namespace android::renderscript;
33
34static void Shutdown(Context *rsc);
35static void SetPriority(const Context *rsc, int32_t priority);
36
37static RsdHalFunctions FunctionTable = {
38    Shutdown,
39    NULL,
40    SetPriority,
41    {
42        rsdScriptInit,
43        rsdScriptInvokeFunction,
44        rsdScriptInvokeRoot,
45        rsdScriptInvokeForEach,
46        rsdScriptInvokeInit,
47        rsdScriptSetGlobalVar,
48        rsdScriptSetGlobalBind,
49        rsdScriptSetGlobalObj,
50        rsdScriptDestroy
51    }
52};
53
54
55
56static void * HelperThreadProc(void *vrsc) {
57    Context *rsc = static_cast<Context *>(vrsc);
58    RsHal *dc = (RsHal *)rsc->mHal.drv;
59
60
61    uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount);
62
63    //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
64
65    dc->mWorkers.mLaunchSignals[idx].init();
66    dc->mWorkers.mNativeThreadId[idx] = gettid();
67
68#if 0
69    typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
70    cpu_set_t cpuset;
71    memset(&cpuset, 0, sizeof(cpuset));
72    cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
73    int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
74              sizeof(cpuset), &cpuset);
75    LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
76#endif
77
78    int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
79    if (status) {
80        LOGE("pthread_setspecific %i", status);
81    }
82
83    while (!dc->mExit) {
84        dc->mWorkers.mLaunchSignals[idx].wait();
85        if (dc->mWorkers.mLaunchCallback) {
86           dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx);
87        }
88        android_atomic_dec(&dc->mWorkers.mRunningCount);
89        dc->mWorkers.mCompleteSignal.set();
90    }
91
92    //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
93    return NULL;
94}
95
96void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) {
97    RsHal *dc = (RsHal *)rsc->mHal.drv;
98
99    dc->mWorkers.mLaunchData = data;
100    dc->mWorkers.mLaunchCallback = cbk;
101    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
102    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
103        dc->mWorkers.mLaunchSignals[ct].set();
104    }
105    while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
106        dc->mWorkers.mCompleteSignal.wait();
107    }
108}
109
110bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
111    rsc->mHal.funcs = FunctionTable;
112
113    RsHal *dc = (RsHal *)calloc(1, sizeof(RsHal));
114    if (!dc) {
115        LOGE("Calloc for driver hal failed.");
116        return false;
117    }
118    rsc->mHal.drv = dc;
119
120
121    int cpu = sysconf(_SC_NPROCESSORS_ONLN);
122    LOGV("RS Launching thread(s), reported CPU count %i", cpu);
123    if (cpu < 2) cpu = 0;
124
125    dc->mWorkers.mCount = (uint32_t)cpu;
126    dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t));
127    dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t));
128    dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount];
129    dc->mWorkers.mLaunchCallback = NULL;
130
131    dc->mWorkers.mCompleteSignal.init();
132
133    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
134    android_atomic_release_store(0, &dc->mWorkers.mLaunchCount);
135
136    int status;
137    pthread_attr_t threadAttr;
138    status = pthread_attr_init(&threadAttr);
139    if (status) {
140        LOGE("Failed to init thread attribute.");
141        return false;
142    }
143
144    for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
145        status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc);
146        if (status) {
147            dc->mWorkers.mCount = ct;
148            LOGE("Created fewer than expected number of RS threads.");
149            break;
150        }
151    }
152    while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
153        usleep(100);
154    }
155
156    pthread_attr_destroy(&threadAttr);
157    return true;
158}
159
160
161void SetPriority(const Context *rsc, int32_t priority) {
162    RsHal *dc = (RsHal *)rsc->mHal.drv;
163    for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
164        setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority);
165    }
166}
167
168void Shutdown(Context *rsc) {
169    RsHal *dc = (RsHal *)rsc->mHal.drv;
170
171    dc->mExit = true;
172    dc->mWorkers.mLaunchData = NULL;
173    dc->mWorkers.mLaunchCallback = NULL;
174    android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
175    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
176        dc->mWorkers.mLaunchSignals[ct].set();
177    }
178    int status;
179    void *res;
180    for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
181        status = pthread_join(dc->mWorkers.mThreadId[ct], &res);
182    }
183    rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0);
184}
185
186
187