[lxc-devel] Clean up include time functions and header

Stéphane Graber stgraber at ubuntu.com
Thu Apr 23 14:34:42 UTC 2015


On Thu, Apr 23, 2015 at 05:19:54PM +0900, Gyeongmin Kim wrote:
> Dear, All
> 
> In project, use both gettimeofday() and clock_gettime(). It's needless.
> I think to use clock_gettime() better than gettimeofday(). becaues,
> gettimeofday() function will be obsoletecent. (
> http://pubs.opengroup.org/stage7tc1/functions/gettimeofday.html)
> and "time.h" is used only (Now, using "sys/time.h", "time.h")
> 
> also, clock_gettime() use to timespec structrue.
> If function use to timeval structure, timespec structure also support to
> function. But, different slightly name of function. such as, select() and
> pselect ()
> 
> int select(int nfds, fd_set *readfds, fd_set *writefds,
>            fd_set *exceptfds, struct timeval *timeout);
> 
> int pselect(int nfds, fd_set *readfds, fd_set *writefds,
>             fd_set *exceptfds, const struct timespec *timeout,
>             const sigset_t *sigmask

One reason to prefer gettimeofday to clock_gettime is that on quite a
few architecture it's implemented using a VDSO which avoids the need for
a kernel roundtrip. However unless we call it very frequently in some
kind of loop, that's unlikely to be a significant performance loss.

> 
> Signed-off-by: GyeongminKim <gyeongmintwo at gmail.com>
> 
> # On branch master
> # Changes to be committed:
> #   (use "git reset HEAD <file>..." to unstage)
> #
> # modified:   src/lxc/conf.c
> # modified:   src/lxc/confile.c
> # modified:   src/lxc/log.c
> # modified:   src/lxc/log.h
> # modified:   src/lxc/lxc_stop.c
> # modified:   src/lxc/lxclock.h
> # modified:   src/lxc/monitor.c
> # modified:   src/lxc/state.c
> #
> 
> diff --git a/src/lxc/conf.c b/src/lxc/conf.c
> index d4855fd..d47907d 100644
> --- a/src/lxc/conf.c
> +++ b/src/lxc/conf.c
> @@ -35,7 +35,6 @@
> #include <sys/types.h>
> #include <pwd.h>
> #include <grp.h>
> -#include <time.h>
> #ifdef HAVE_STATVFS
> #include <sys/statvfs.h>
> #endif
> diff --git a/src/lxc/confile.c b/src/lxc/confile.c
> index e3be6a6..dc12edf 100644
> --- a/src/lxc/confile.c
> +++ b/src/lxc/confile.c
> @@ -36,7 +36,6 @@
> #include <arpa/inet.h>
> #include <netinet/in.h>
> #include <net/if.h>
> -#include <time.h>
> #include <dirent.h>
> 
> #include "parse.h"
> diff --git a/src/lxc/log.c b/src/lxc/log.c
> index 6633e62..0230724 100644
> --- a/src/lxc/log.c
> +++ b/src/lxc/log.c
> @@ -89,7 +89,7 @@ static int log_append_logfile(const struct
> lxc_log_appender *appender,
>         if (lxc_log_fd == -1)
>                 return 0;
> 
> -       ms = event->timestamp.tv_usec / 1000;
> +       ms = event->timestamp.tv_nsec / 1000000;
>         n = snprintf(buffer, sizeof(buffer),
>                      "%15s %10ld.%03d %-8s %s - %s:%s:%d - ",
>                      log_prefix,
> diff --git a/src/lxc/log.h b/src/lxc/log.h
> index b47f120..bccdf5f 100644
> --- a/src/lxc/log.h
> +++ b/src/lxc/log.h
> @@ -28,7 +28,7 @@
> 
> #include <stdarg.h>
> #include <stdio.h>
> -#include <sys/time.h>
> +#include <time.h>
> #include <string.h>
> #include <strings.h>
> #include <stdbool.h>
> @@ -79,7 +79,7 @@ struct lxc_log_locinfo {
> struct lxc_log_event {
>         const char*             category;
>         int                     priority;
> -       struct timeval          timestamp;
> +       struct timespec         timestamp;
>         struct lxc_log_locinfo  *locinfo;
>         const char              *fmt;
>         va_list                 *vap;
> @@ -203,7 +203,7 @@ ATTR_UNUSED static inline void LXC_##PRIORITY(struct
> lxc_log_locinfo* locinfo,      \
>                 };                                                      \
>                 va_list va_ref;                                         \
>                                                                         \
> -               gettimeofday(&evt.timestamp, NULL);                     \
> +               clock_gettime(CLOCK_REALTIME, &evt.timestamp);          \
>                                                                         \
>                 va_start(va_ref, format);                               \
>                 evt.vap = &va_ref;                                      \
> diff --git a/src/lxc/lxc_stop.c b/src/lxc/lxc_stop.c
> index 7054532..3b891a6 100644
> --- a/src/lxc/lxc_stop.c
> +++ b/src/lxc/lxc_stop.c
> @@ -104,14 +104,14 @@ static int do_reboot_and_check(struct lxc_arguments
> *a, struct lxc_container *c)
>                 /* can we use c-> wait for this, assuming it will
>                  * re-enter RUNNING?  For now just sleep */
>                 int elapsed_time, curtime = 0;
> -               struct timeval tv;
> +               struct timespec tv;
> 
>                 newpid = c->init_pid(c);
>                 if (newpid != -1 && newpid != pid)
>                         return 0;
> 
>                 if (timeout != -1) {
> -                       ret = gettimeofday(&tv, NULL);
> +                       ret = clock_gettime(CLOCK_REALTIME, &tv);
>                         if (ret)
>                                 break;
>                         curtime = tv.tv_sec;
> @@ -119,7 +119,7 @@ static int do_reboot_and_check(struct lxc_arguments *a,
> struct lxc_container *c)
> 
>                 sleep(1);
>                 if (timeout != -1) {
> -                       ret = gettimeofday(&tv, NULL);
> +                       ret = clock_gettime(CLOCK_REALTIME, &tv);
>                         if (ret)
>                                 break;
>                         elapsed_time = tv.tv_sec - curtime;
> diff --git a/src/lxc/lxclock.h b/src/lxc/lxclock.h
> index e00dd8a..74ea83f 100644
> --- a/src/lxc/lxclock.h
> +++ b/src/lxc/lxclock.h
> @@ -28,7 +28,6 @@
> #include <sys/file.h>
> #include <semaphore.h>
> #include <string.h>
> -#include <time.h>
> 
> #define LXC_LOCK_ANON_SEM 1 /*!< Anonymous semaphore lock */
> #define LXC_LOCK_FLOCK    2 /*!< flock(2) lock */
> diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c
> index 1e1c094..9f68af8 100644
> --- a/src/lxc/monitor.c
> +++ b/src/lxc/monitor.c
> @@ -222,16 +222,16 @@ err1:
> int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg,
>                            int timeout)
> {
> -       struct timeval tval,*tv = NULL;
> +       struct timespec tspec,*tv = NULL;
>         int ret,i;
> 
>         if (timeout != -1) {
> -               tv = &tval;
> +               tv = &tspec;
>                 tv->tv_sec = timeout;
> -               tv->tv_usec = 0;
> +               tv->tv_nsec = 0;
>         }
> 
> -       ret = select(nfds, rfds, NULL, NULL, tv);
> +       ret = pselect(nfds, rfds, NULL, NULL, tv, NULL);
>         if (ret == -1)
>                 return -1;
>         else if (ret == 0)
> diff --git a/src/lxc/state.c b/src/lxc/state.c
> index db833b0..a5e9d6f 100644
> --- a/src/lxc/state.c
> +++ b/src/lxc/state.c
> @@ -134,12 +134,12 @@ extern int lxc_wait(const char *lxcname, const char
> *states, int timeout, const
> 
>         for (;;) {
>                 int elapsed_time, curtime = 0;
> -               struct timeval tv;
> +               struct timespec tv;
>                 int stop = 0;
>                 int retval;
> 
>                 if (timeout != -1) {
> -                       retval = gettimeofday(&tv, NULL);
> +                       retval = clock_gettime(CLOCK_REALTIME, &tv);
>                         if (retval)
>                                 goto out_close;
>                         curtime = tv.tv_sec;
> @@ -151,7 +151,7 @@ extern int lxc_wait(const char *lxcname, const char
> *states, int timeout, const
>                 }
> 
>                 if (timeout != -1) {
> -                       retval = gettimeofday(&tv, NULL);
> +                       retval = clock_gettime(CLOCK_REALTIME, &tv);
>                         if (retval)
>                                 goto out_close;
>                         elapsed_time = tv.tv_sec - curtime;

> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel


-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20150423/e4e84a2e/attachment.sig>


More information about the lxc-devel mailing list