Add a timeout option in shell subscriber.

Test: cts added
Change-Id: I0fe854fcfd5535ed03e502a4cad3f57079b45381
This commit is contained in:
Yao Chen
2019-01-03 16:49:14 -08:00
parent 20a1afd998
commit 35cb8d6537
5 changed files with 20 additions and 5 deletions

View File

@@ -332,6 +332,8 @@ java_library {
"src/stats_log.proto",
"src/statsd_config.proto",
"src/atoms.proto",
"src/shell/shell_config.proto",
"src/shell/shell_data.proto",
],
static_libs: [

View File

@@ -360,7 +360,11 @@ status_t StatsService::command(int in, int out, int err, Vector<String8>& args,
if (mShellSubscriber == nullptr) {
mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager);
}
mShellSubscriber->startNewSubscription(in, out, resultReceiver);
int timeoutSec = -1;
if (argCount >= 2) {
timeoutSec = atoi(args[1].c_str());
}
mShellSubscriber->startNewSubscription(in, out, resultReceiver, timeoutSec);
return NO_ERROR;
}
}

View File

@@ -30,7 +30,8 @@ namespace statsd {
const static int FIELD_ID_ATOM = 1;
void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver> resultReceiver) {
void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver> resultReceiver,
int timeoutSec) {
VLOG("start new shell subscription");
{
std::lock_guard<std::mutex> lock(mMutex);
@@ -50,11 +51,18 @@ void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver>
// Read config forever until EOF is reached. Clients may send multiple configs -- each new
// config replace the previous one.
readConfig(in);
VLOG("timeout : %d", timeoutSec);
// Now we have read an EOF we now wait for the semaphore until the client exits.
VLOG("Now wait for client to exit");
std::unique_lock<std::mutex> lk(mMutex);
mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; });
if (timeoutSec > 0) {
mShellDied.wait_for(lk, timeoutSec * 1s,
[this, resultReceiver] { return mResultReceiver != resultReceiver; });
} else {
mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; });
}
}
void ShellSubscriber::updateConfig(const ShellSubscription& config) {

View File

@@ -65,7 +65,8 @@ public:
/**
* Start a new subscription.
*/
void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver);
void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver,
int timeoutSec);
void binderDied(const wp<IBinder>& who);

View File

@@ -83,7 +83,7 @@ void runShellTest(ShellSubscription config, sp<MockUidMap> uidMap,
// mimic a binder thread that a shell subscriber runs on. it would block.
std::thread reader([&resultReceiver, &fds_config, &fds_data, &shellClient] {
shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver);
shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver, -1);
});
reader.detach();