Merge "Switch adb over to <chrono>."

am: 59826ddee5

Change-Id: I673ce3c9f4a104df3197ec7120a126155e18918e
This commit is contained in:
Elliott Hughes 2016-11-16 20:14:20 +00:00 committed by android-build-merger
commit 2278b50f24
12 changed files with 65 additions and 47 deletions

View file

@ -30,7 +30,9 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <chrono>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#include <android-base/errors.h> #include <android-base/errors.h>
@ -51,6 +53,7 @@
#include <sys/capability.h> #include <sys/capability.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <android-base/properties.h> #include <android-base/properties.h>
using namespace std::chrono_literals;
#endif #endif
std::string adb_version() { std::string adb_version() {
@ -375,7 +378,7 @@ void handle_packet(apacket *p, atransport *t)
adbd_auth_verified(t); adbd_auth_verified(t);
t->failed_auth_attempts = 0; t->failed_auth_attempts = 0;
} else { } else {
if (t->failed_auth_attempts++ > 256) adb_sleep_ms(1000); if (t->failed_auth_attempts++ > 256) std::this_thread::sleep_for(1s);
send_auth_request(t); send_auth_request(t);
} }
break; break;

View file

@ -28,7 +28,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <chrono>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
@ -188,8 +190,8 @@ int adb_connect(const std::string& service, std::string* error) {
} else { } else {
fprintf(stdout,"* daemon started successfully *\n"); fprintf(stdout,"* daemon started successfully *\n");
} }
/* give the server some time to start properly and detect devices */ // Give the server some time to start properly and detect devices.
adb_sleep_ms(3000); std::this_thread::sleep_for(std::chrono::seconds(3));
// fall through to _adb_connect // fall through to _adb_connect
} else { } else {
// If a server is already running, check its version matches. // If a server is already running, check its version matches.
@ -234,7 +236,7 @@ int adb_connect(const std::string& service, std::string* error) {
} }
/* XXX can we better detect its death? */ /* XXX can we better detect its death? */
adb_sleep_ms(2000); std::this_thread::sleep_for(std::chrono::seconds(2));
goto start_server; goto start_server;
} }
} }

View file

@ -20,6 +20,9 @@
#include <unistd.h> #include <unistd.h>
#include <chrono>
#include <thread>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include "adb.h" #include "adb.h"
@ -104,7 +107,7 @@ bool WriteFdExactly(int fd, const void* buf, size_t len) {
if (r == -1) { if (r == -1) {
D("writex: fd=%d error %d: %s", fd, errno, strerror(errno)); D("writex: fd=%d error %d: %s", fd, errno, strerror(errno));
if (errno == EAGAIN) { if (errno == EAGAIN) {
adb_sleep_ms(1); // just yield some cpu time std::this_thread::yield();
continue; continue;
} else if (errno == EPIPE) { } else if (errno == EPIPE) {
D("writex: fd=%d disconnected", fd); D("writex: fd=%d disconnected", fd);

View file

@ -31,8 +31,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <chrono>
#include <memory> #include <memory>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#include <android-base/file.h> #include <android-base/file.h>
@ -1080,7 +1082,7 @@ static bool adb_root(const char* command) {
// Give adbd some time to kill itself and come back up. // Give adbd some time to kill itself and come back up.
// We can't use wait-for-device because devices (e.g. adb over network) might not come back. // We can't use wait-for-device because devices (e.g. adb over network) might not come back.
adb_sleep_ms(3000); std::this_thread::sleep_for(std::chrono::seconds(3));
return true; return true;
} }

View file

@ -19,9 +19,11 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <array> #include <array>
#include <chrono>
#include <limits> #include <limits>
#include <queue> #include <queue>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#include <unistd.h> #include <unistd.h>
@ -44,7 +46,7 @@ static void FdEventThreadFunc(void*) {
fdevent_loop(); fdevent_loop();
} }
const size_t SLEEP_FOR_FDEVENT_IN_MS = 100; constexpr auto SLEEP_FOR_FDEVENT = std::chrono::milliseconds(100);
TEST_F(LocalSocketTest, smoke) { TEST_F(LocalSocketTest, smoke) {
// Join two socketpairs with a chain of intermediate socketpairs. // Join two socketpairs with a chain of intermediate socketpairs.
@ -101,7 +103,7 @@ TEST_F(LocalSocketTest, smoke) {
ASSERT_EQ(0, adb_close(last[1])); ASSERT_EQ(0, adb_close(last[1]));
// Wait until the local sockets are closed. // Wait until the local sockets are closed.
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
TerminateThread(thread); TerminateThread(thread);
} }
@ -154,12 +156,12 @@ TEST_F(LocalSocketTest, close_socket_with_packet) {
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
&arg, &thread)); &arg, &thread));
// Wait until the fdevent_loop() starts. // Wait until the fdevent_loop() starts.
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(0, adb_close(cause_close_fd[0])); ASSERT_EQ(0, adb_close(cause_close_fd[0]));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0])); ASSERT_EQ(0, adb_close(socket_fd[0]));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
TerminateThread(thread); TerminateThread(thread);
} }
@ -179,9 +181,9 @@ TEST_F(LocalSocketTest, read_from_closing_socket) {
ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc), ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseWithPacketThreadFunc),
&arg, &thread)); &arg, &thread));
// Wait until the fdevent_loop() starts. // Wait until the fdevent_loop() starts.
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(0, adb_close(cause_close_fd[0])); ASSERT_EQ(0, adb_close(cause_close_fd[0]));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
// Verify if we can read successfully. // Verify if we can read successfully.
@ -190,7 +192,7 @@ TEST_F(LocalSocketTest, read_from_closing_socket) {
ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size()));
ASSERT_EQ(0, adb_close(socket_fd[0])); ASSERT_EQ(0, adb_close(socket_fd[0]));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
TerminateThread(thread); TerminateThread(thread);
} }
@ -214,11 +216,11 @@ TEST_F(LocalSocketTest, write_error_when_having_packets) {
&arg, &thread)); &arg, &thread));
// Wait until the fdevent_loop() starts. // Wait until the fdevent_loop() starts.
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
ASSERT_EQ(0, adb_close(socket_fd[0])); ASSERT_EQ(0, adb_close(socket_fd[0]));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
TerminateThread(thread); TerminateThread(thread);
} }
@ -229,7 +231,7 @@ static void ClientThreadFunc() {
std::string error; std::string error;
int fd = network_loopback_client(5038, SOCK_STREAM, &error); int fd = network_loopback_client(5038, SOCK_STREAM, &error);
ASSERT_GE(fd, 0) << error; ASSERT_GE(fd, 0) << error;
adb_sleep_ms(200); std::this_thread::sleep_for(std::chrono::milliseconds(200));
ASSERT_EQ(0, adb_close(fd)); ASSERT_EQ(0, adb_close(fd));
} }
@ -265,13 +267,13 @@ TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
&arg, &thread)); &arg, &thread));
// Wait until the fdevent_loop() starts. // Wait until the fdevent_loop() starts.
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());
// Wait until the client closes its socket. // Wait until the client closes its socket.
ASSERT_TRUE(adb_thread_join(client_thread)); ASSERT_TRUE(adb_thread_join(client_thread));
adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); std::this_thread::sleep_for(SLEEP_FOR_FDEVENT);
ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
TerminateThread(thread); TerminateThread(thread);
} }

View file

@ -248,11 +248,6 @@ extern int unix_open(const char* path, int options, ...);
int unix_isatty(int fd); int unix_isatty(int fd);
#define isatty ___xxx_isatty #define isatty ___xxx_isatty
static __inline__ void adb_sleep_ms( int mseconds )
{
Sleep( mseconds );
}
int network_loopback_client(int port, int type, std::string* error); int network_loopback_client(int port, int type, std::string* error);
int network_loopback_server(int port, int type, std::string* error); int network_loopback_server(int port, int type, std::string* error);
int network_inaddr_any_server(int port, int type, std::string* error); int network_inaddr_any_server(int port, int type, std::string* error);
@ -766,11 +761,6 @@ static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
#define poll ___xxx_poll #define poll ___xxx_poll
static __inline__ void adb_sleep_ms( int mseconds )
{
usleep( mseconds*1000 );
}
static __inline__ int adb_mkdir(const std::string& path, int mode) static __inline__ int adb_mkdir(const std::string& path, int mode)
{ {
return mkdir(path.c_str(), mode); return mkdir(path.c_str(), mode);

View file

@ -16,14 +16,17 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <unistd.h> #include <unistd.h>
#include <atomic> #include <atomic>
#include <chrono>
#include <condition_variable> #include <condition_variable>
#include <thread>
#include "adb_io.h" #include "adb_io.h"
#include "sysdeps.h" #include "sysdeps.h"
static void increment_atomic_int(void* c) { static void increment_atomic_int(void* c) {
sleep(1); std::this_thread::sleep_for(std::chrono::seconds(1));
reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1); reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
} }
@ -34,7 +37,7 @@ TEST(sysdeps_thread, smoke) {
ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter));
} }
sleep(2); std::this_thread::sleep_for(std::chrono::seconds(2));
ASSERT_EQ(100, counter.load()); ASSERT_EQ(100, counter.load());
} }
@ -255,15 +258,15 @@ TEST(sysdeps_mutex, mutex_smoke) {
ASSERT_FALSE(m.try_lock()); ASSERT_FALSE(m.try_lock());
m.lock(); m.lock();
finished.store(true); finished.store(true);
adb_sleep_ms(200); std::this_thread::sleep_for(std::chrono::milliseconds(200));
m.unlock(); m.unlock();
}, nullptr); }, nullptr);
ASSERT_FALSE(finished.load()); ASSERT_FALSE(finished.load());
adb_sleep_ms(100); std::this_thread::sleep_for(std::chrono::milliseconds(100));
ASSERT_FALSE(finished.load()); ASSERT_FALSE(finished.load());
m.unlock(); m.unlock();
adb_sleep_ms(100); std::this_thread::sleep_for(std::chrono::milliseconds(100));
m.lock(); m.lock();
ASSERT_TRUE(finished.load()); ASSERT_TRUE(finished.load());
m.unlock(); m.unlock();
@ -279,13 +282,13 @@ TEST(sysdeps_mutex, recursive_mutex_smoke) {
adb_thread_create([](void*) { adb_thread_create([](void*) {
ASSERT_FALSE(m.try_lock()); ASSERT_FALSE(m.try_lock());
m.lock(); m.lock();
adb_sleep_ms(500); std::this_thread::sleep_for(std::chrono::milliseconds(500));
m.unlock(); m.unlock();
}, nullptr); }, nullptr);
adb_sleep_ms(100); std::this_thread::sleep_for(std::chrono::milliseconds(100));
m.unlock(); m.unlock();
adb_sleep_ms(100); std::this_thread::sleep_for(std::chrono::milliseconds(100));
ASSERT_FALSE(m.try_lock()); ASSERT_FALSE(m.try_lock());
m.lock(); m.lock();
m.unlock(); m.unlock();

View file

@ -25,8 +25,10 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <chrono>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <thread>
#include <vector> #include <vector>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
@ -144,7 +146,7 @@ static void PollAllLocalPortsForEmulator() {
// Retry the disconnected local port for 60 times, and sleep 1 second between two retries. // Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60; constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000; constexpr auto LOCAL_PORT_RETRY_INTERVAL = std::chrono::seconds(1);
struct RetryPort { struct RetryPort {
int port; int port;
@ -173,7 +175,7 @@ static void client_socket_thread(void* x) {
// Sleep here instead of the end of loop, because if we immediately try to reconnect // Sleep here instead of the end of loop, because if we immediately try to reconnect
// the emulator just kicked, the adbd on the emulator may not have time to remove the // the emulator just kicked, the adbd on the emulator may not have time to remove the
// just kicked transport. // just kicked transport.
adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS); std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
// Try connecting retry ports. // Try connecting retry ports.
std::vector<RetryPort> next_ports; std::vector<RetryPort> next_ports;
@ -214,7 +216,7 @@ static void server_socket_thread(void* arg) {
serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
if(serverfd < 0) { if(serverfd < 0) {
D("server: cannot bind socket yet: %s", error.c_str()); D("server: cannot bind socket yet: %s", error.c_str());
adb_sleep_ms(1000); std::this_thread::sleep_for(std::chrono::seconds(1));
continue; continue;
} }
close_on_exec(serverfd); close_on_exec(serverfd);

View file

@ -38,6 +38,7 @@
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
@ -46,6 +47,7 @@
#include "adb.h" #include "adb.h"
#include "transport.h" #include "transport.h"
using namespace std::chrono_literals;
using namespace std::literals; using namespace std::literals;
/* usb scan debugging is waaaay too verbose */ /* usb scan debugging is waaaay too verbose */
@ -577,7 +579,7 @@ static void device_poll_thread(void*) {
// TODO: Use inotify. // TODO: Use inotify.
find_usb_device("/dev/bus/usb", register_device); find_usb_device("/dev/bus/usb", register_device);
kick_disconnected_devices(); kick_disconnected_devices();
sleep(1); std::this_thread::sleep_for(1s);
} }
} }

View file

@ -31,8 +31,10 @@
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <chrono>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <thread>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/properties.h> #include <android-base/properties.h>
@ -40,6 +42,8 @@
#include "adb.h" #include "adb.h"
#include "transport.h" #include "transport.h"
using namespace std::chrono_literals;
#define MAX_PACKET_SIZE_FS 64 #define MAX_PACKET_SIZE_FS 64
#define MAX_PACKET_SIZE_HS 512 #define MAX_PACKET_SIZE_HS 512
#define MAX_PACKET_SIZE_SS 1024 #define MAX_PACKET_SIZE_SS 1024
@ -268,7 +272,7 @@ static void usb_adb_open_thread(void* x) {
fd = unix_open("/dev/android", O_RDWR); fd = unix_open("/dev/android", O_RDWR);
} }
if (fd < 0) { if (fd < 0) {
adb_sleep_ms(1000); std::this_thread::sleep_for(1s);
} }
} while (fd < 0); } while (fd < 0);
D("[ opening device succeeded ]"); D("[ opening device succeeded ]");
@ -476,7 +480,7 @@ static void usb_ffs_open_thread(void* x) {
if (init_functionfs(usb)) { if (init_functionfs(usb)) {
break; break;
} }
adb_sleep_ms(1000); std::this_thread::sleep_for(1s);
} }
android::base::SetProperty("sys.usb.ffs.ready", "1"); android::base::SetProperty("sys.usb.ffs.ready", "1");

View file

@ -30,8 +30,10 @@
#include <stdio.h> #include <stdio.h>
#include <atomic> #include <atomic>
#include <chrono>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <thread>
#include <vector> #include <vector>
#include <android-base/logging.h> #include <android-base/logging.h>
@ -40,6 +42,8 @@
#include "adb.h" #include "adb.h"
#include "transport.h" #include "transport.h"
using namespace std::chrono_literals;
struct usb_handle struct usb_handle
{ {
UInt8 bulkIn; UInt8 bulkIn;
@ -411,7 +415,7 @@ static void RunLoopThread(void* unused) {
} }
// Signal the parent that we are running // Signal the parent that we are running
usb_inited_flag = true; usb_inited_flag = true;
adb_sleep_ms(1000); std::this_thread::sleep_for(1s);
} }
VLOG(USB) << "RunLoopThread done"; VLOG(USB) << "RunLoopThread done";
} }
@ -436,7 +440,7 @@ void usb_init() {
// Wait for initialization to finish // Wait for initialization to finish
while (!usb_inited_flag) { while (!usb_inited_flag) {
adb_sleep_ms(100); std::this_thread::sleep_for(100ms);
} }
initialized = true; initialized = true;

View file

@ -28,6 +28,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <mutex> #include <mutex>
#include <thread>
#include <adb_api.h> #include <adb_api.h>
@ -176,9 +177,9 @@ void device_poll_thread(void*) {
adb_thread_setname("Device Poll"); adb_thread_setname("Device Poll");
D("Created device thread"); D("Created device thread");
while(1) { while (true) {
find_devices(); find_devices();
adb_sleep_ms(1000); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
} }