Skip to content

Commit

Permalink
[debug] Use wrapper for sleep() calls. MER#1774
Browse files Browse the repository at this point in the history
Calling the likes of sleep() function blocks usb-moded mainloop. Whether the
sleeps add up to something significant is difficult to tell.

When in verbose mode, log all blocking sleeps made by usb-moded.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Mar 24, 2017
1 parent 2b01382 commit c2d824d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/usb_moded-modesetting.c
Expand Up @@ -223,7 +223,7 @@ umount: command = g_strconcat("mount | grep ", mountpath, NULL);
if(try != 3)
{
try++;
sleep(1);
usb_moded_sleep(1);
log_err("Umount failed. Retrying\n");
report_mass_storage_blocker(mount, 1);
goto umount;
Expand All @@ -243,7 +243,7 @@ umount: command = g_strconcat("mount | grep ", mountpath, NULL);
}

/* activate mounts after sleeping 1s to be sure enumeration happened and autoplay will work in windows*/
sleep(1);
usb_moded_sleep(1);
for(i=0 ; mounts[i] != NULL; i++)
{

Expand Down Expand Up @@ -482,7 +482,7 @@ int set_dynamic_mode(void)
if(data->appsync && !ret)
{
/* let's sleep for a bit (350ms) to allow interfaces to settle before running postsync */
usleep(350000);
usb_moded_msleep(350);
activate_sync_post(data->mode_name);
}

Expand Down
4 changes: 2 additions & 2 deletions src/usb_moded-modules.c
Expand Up @@ -201,7 +201,7 @@ int usb_moded_module_cleanup(const char *module)
if(!strcmp(module, MODULE_NONE))
goto END;
/* wait a bit for all components listening on dbus to clean up their act
sleep(2); */
usb_moded_sleep(2); */
/* check if things were not reconnected in that timespan
if(get_usb_connection_state())
return(0);
Expand All @@ -224,7 +224,7 @@ int usb_moded_module_cleanup(const char *module)
while(failure)
{
/* module did not get unloaded. We will wait a bit and try again */
sleep(1);
usb_moded_sleep(1);
/* send the REALLY disconnect message */
usb_moded_send_signal(USB_REALLY_DISCONNECT);
failure = usb_moded_unload_module(module);
Expand Down
9 changes: 3 additions & 6 deletions src/usb_moded-network.c
Expand Up @@ -491,10 +491,7 @@ gboolean connman_set_tethering(const char *path, gboolean on)
{
if (i>0)
{
struct timespec tv;
tv.tv_sec = 0;
tv.tv_nsec = 200000000;
nanosleep(&tv, NULL);
usb_moded_msleep(200);
}
if (connman_try_set_tethering(connection, path, on))
{
Expand Down Expand Up @@ -711,7 +708,7 @@ static int connman_set_cellular_online(DBusConnection *dbus_conn_connman, const
/* we don't care for the reply, which is empty anyway if all goes well */
ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
/* sleep for the connection to come up */
sleep(5);
usb_moded_sleep(5);
/* make sure the message is sent before cleaning up and closing the connection */
dbus_connection_flush(dbus_conn_connman);
dbus_message_unref(msg);
Expand Down Expand Up @@ -976,7 +973,7 @@ int usb_network_up(struct mode_list_elem *data)
//usb_moded_system("/sbin/ifconfig rndis0 up");

log_debug("waiting for connman to pick up interface\n");
sleep(1);
usb_moded_sleep(1);
dbus_error_init(&error);

if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
Expand Down
30 changes: 29 additions & 1 deletion src/usb_moded.c
Expand Up @@ -191,7 +191,7 @@ void set_usb_connected(gboolean connected)
static gboolean set_disconnected(gpointer data)
{
/* let usb settle */
sleep(1);
usb_moded_sleep(1);
/* only disconnect for real if we are actually still disconnected */
if(!get_usb_connection_state())
{
Expand Down Expand Up @@ -1147,6 +1147,34 @@ usb_moded_system_(const char *file, int line, const char *func,
return rc;
}

/** Wrapper to give visibility to blocking sleeps usb-moded is making
*/
void
usb_moded_usleep_(const char *file, int line, const char *func,
useconds_t usec)
{
struct timespec ts = {
.tv_sec = (usec / 1000000),
.tv_nsec = (usec % 1000000) * 1000
};

long ms = (ts.tv_nsec + 1000000 - 1) / 1000000;

if( !ms ) {
log_debug("SLEEP %ld seconds; from %s:%d: %s()",
(long)ts.tv_sec, file, line, func);
}
else if( ts.tv_sec ) {
log_debug("SLEEP %ld.%03ld seconds; from %s:%d: %s()",
(long)ts.tv_sec, ms, file, line, func);
}
else {
log_debug("SLEEP %ld milliseconds; from %s:%d: %s()",
ms, file, line, func);
}

do { } while( nanosleep(&ts, &ts) == -1 && errno != EINTR );
}

int main(int argc, char* argv[])
{
Expand Down
5 changes: 5 additions & 0 deletions src/usb_moded.h
Expand Up @@ -99,4 +99,9 @@ void usb_moded_stop(int exitcode);
int usb_moded_system_(const char *file, int line, const char *func, const char *command);
#define usb_moded_system(command) usb_moded_system_(__FILE__,__LINE__,__FUNCTION__,(command))

void usb_moded_usleep_(const char *file, int line, const char *func, useconds_t usec);
#define usb_moded_usleep(usec) usb_moded_usleep_(__FILE__,__LINE__,__FUNCTION__,(usec))
#define usb_moded_msleep(msec) usb_moded_usleep_(__FILE__,__LINE__,__FUNCTION__,(msec)*1000)
#define usb_moded_sleep(sec) usb_moded_usleep_(__FILE__,__LINE__,__FUNCTION__,(sec)*1000000)

#endif /* USB_MODED_H */

0 comments on commit c2d824d

Please sign in to comment.