Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
adb: initialize mDNS asynchronously.
Browse files Browse the repository at this point in the history
Use fdevent_run_on_main_thread to initialize mDNS in a thread and
register an fdevent from the main thread upon success.

This reduces the startup time of `adb server` by ~3 seconds when mDNS
can't be successfully started. With an already running adb server,
`time adb server nodaemon` goes from:

    adb server nodaemon  0.00s user 0.16s system 4% cpu 3.817 total

to:

    adb server nodaemon  0.00s user 0.01s system 1% cpu 0.665 total

Bug: http://b/37869663
Test: `adb server nodaemon` with an existing adb server
Change-Id: Ia5a1a2a138610f3bf6792400050ca68f95ae3734
  • Loading branch information
jmgao committed May 4, 2017
1 parent 4c93639 commit 6f46e6b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions adb/transport_mdns.cpp
Expand Up @@ -24,6 +24,8 @@
#include <arpa/inet.h>
#endif

#include <thread>

#include <android-base/stringprintf.h>
#include <dns_sd.h>

Expand Down Expand Up @@ -262,19 +264,22 @@ static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
}
}

void init_mdns_transport_discovery(void) {
DNSServiceErrorType errorCode =
DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, nullptr,
register_mdns_transport, nullptr);
void init_mdns_transport_discovery_thread(void) {
DNSServiceErrorType errorCode = DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, nullptr,
register_mdns_transport, nullptr);

if (errorCode != kDNSServiceErr_NoError) {
D("Got %d initiating mDNS browse.", errorCode);
return;
}

fdevent_install(&service_ref_fde,
adb_DNSServiceRefSockFD(service_ref),
pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);
fdevent_run_on_main_thread([]() {
fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);
});
}

void init_mdns_transport_discovery(void) {
std::thread(init_mdns_transport_discovery_thread).detach();
}

0 comments on commit 6f46e6b

Please sign in to comment.