Projects
Browse Source     Search     Timeline     Wiki

Changeset 23471

Show
Ignore:
Timestamp:
12/19/07 11:48:57 (12 months ago)
Author:
zarzycki@…
Message:

Both Mach and Unix time concepts are weird.

We now use an 'int64_t' instead of 'struct timeval' for wall clock code.
We now wrap the 'timebase info' logic required by Mach absolute time.

Location:
trunk/launchd/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/launchd/src/launchd.c

    r23470 r23471  
    246246launchd_shutdown(void) 
    247247{ 
    248         struct timeval tvnow; 
     248        int64_t now; 
    249249 
    250250        if (shutdown_in_progress) { 
     
    265265        runtime_log_push(); 
    266266 
    267         if (launchd_assumes(gettimeofday(&tvnow, NULL) != -1)) { 
    268                 runtime_syslog(LOG_NOTICE, "Shutdown began at: %lu.%06u", tvnow.tv_sec, tvnow.tv_usec); 
    269         } 
     267        now = runtime_get_wall_time(); 
     268 
     269        runtime_syslog(LOG_NOTICE, "Shutdown began at: %lld.%06llu", now / USEC_PER_SEC, now % USEC_PER_SEC); 
    270270 
    271271        launchd_assert(jobmgr_shutdown(root_jobmgr) != NULL); 
  • trunk/launchd/src/launchd_core_logic.c

    r23470 r23471  
    2424#include <mach/mach.h> 
    2525#include <mach/mach_error.h> 
    26 #include <mach/mach_time.h> 
    2726#include <mach/boolean.h> 
    2827#include <mach/message.h> 
     
    453452static void extract_rcsid_substr(const char *i, char *o, size_t osz); 
    454453static void do_first_per_user_launchd_hack(void); 
    455 static void do_file_init(void) __attribute__((constructor)); 
    456454static void do_unmounts(void); 
    457455 
     
    464462static jobmgr_t background_jobmgr; 
    465463static job_t workaround_5477111; 
    466 static mach_timebase_info_data_t tbi; 
    467464 
    468465/* process wide globals */ 
     
    540537 
    541538        job_assumes(j, runtime_kill(j->p, SIGTERM) != -1); 
    542         j->sent_sigterm_time = mach_absolute_time(); 
     539        j->sent_sigterm_time = runtime_get_opaque_time(); 
    543540 
    544541        if (j->exit_timeout) { 
     
    21512148 
    21522149        if (j->sent_sigterm_time) { 
    2153                 uint64_t td_sec, td_usec, td = (mach_absolute_time() - j->sent_sigterm_time) * tbi.numer / tbi.denom; 
     2150                uint64_t td_sec, td_usec, td = runtime_opaque_time_to_nano(runtime_get_opaque_time() - j->sent_sigterm_time); 
    21542151 
    21552152                td_sec = td / NSEC_PER_SEC; 
     
    24332430        } else if (&j->exit_timeout == ident) { 
    24342431                if (j->sent_sigkill) { 
    2435                         uint64_t td = (mach_absolute_time() - j->sent_sigterm_time) * tbi.numer / tbi.denom; 
     2432                        uint64_t td = runtime_opaque_time_to_nano(runtime_get_opaque_time() - j->sent_sigterm_time); 
    24362433 
    24372434                        td /= NSEC_PER_SEC; 
     
    24822479jobmgr_callback(void *obj, struct kevent *kev) 
    24832480{ 
    2484         struct timeval tvnow; 
    24852481        jobmgr_t jm = obj; 
    24862482        job_t ji; 
     
    25052501                        runtime_closelog(); /* HACK -- force 'start' time to be set */ 
    25062502 
    2507                         if (getpid() == 1 && jobmgr_assumes(jm, gettimeofday(&tvnow, NULL) != -1)) { 
    2508                                 jobmgr_log(jm, LOG_NOTICE, "Anticipatory shutdown began at: %lu.%06u", tvnow.tv_sec, tvnow.tv_usec); 
     2503                        if (getpid() == 1) { 
     2504                                int64_t now = runtime_get_wall_time(); 
     2505 
     2506                                jobmgr_log(jm, LOG_NOTICE, "Anticipatory shutdown began at: %lld.%06llu", now / USEC_PER_SEC, now % USEC_PER_SEC); 
     2507 
    25092508                                LIST_FOREACH(ji, &root_jobmgr->jobs, sle) { 
    25102509                                        if (ji->per_user && ji->p) { 
     
    25632562job_start(job_t j) 
    25642563{ 
    2565         uint64_t td, tnow = mach_absolute_time(); 
     2564        uint64_t td, tnow = runtime_get_opaque_time(); 
    25662565        int spair[2]; 
    25672566        int execspair[2]; 
     
    25852584        /* 
    25862585         * Some users adjust the wall-clock and then expect software to not notice. 
    2587          * Therefore, launchd must use an absolute clock instead of gettimeofday() 
    2588          * or time() wherever possible. 
     2586         * Therefore, launchd must use an absolute clock instead of the wall clock 
     2587         * wherever possible. 
    25892588         */ 
    2590         td = (tnow - j->start_time) * tbi.numer / tbi.denom; 
     2589        td = runtime_opaque_time_to_nano(tnow - j->start_time); 
    25912590        td /= NSEC_PER_SEC; 
    25922591 
     
    67386737 
    67396738void 
    6740 do_file_init(void) 
    6741 { 
    6742         launchd_assert(mach_timebase_info(&tbi) == 0); 
    6743  
    6744 } 
    6745  
    6746 void 
    67476739do_unmounts(void) 
    67486740{ 
  • trunk/launchd/src/launchd_runtime.c

    r23470 r23471  
    3434#include <mach/host_info.h> 
    3535#include <mach/mach_host.h> 
     36#include <mach/mach_time.h> 
    3637#include <mach/exception.h> 
    3738#include <sys/types.h> 
     
    110111static void logmsg_remove(struct logmsg_s *lm); 
    111112 
     113static void do_file_init(void) __attribute__((constructor)); 
     114static mach_timebase_info_data_t tbi; 
    112115 
    113116static const int sigigns[] = { SIGHUP, SIGINT, SIGPIPE, SIGALRM, SIGTERM, 
     
    12101213        data_off = lm->data; 
    12111214 
    1212         launchd_assumes(gettimeofday(&lm->when, NULL) != -1); 
     1215        lm->when = runtime_get_wall_time(); 
    12131216        lm->from_pid = attr->from_pid; 
    12141217        lm->about_pid = attr->about_pid; 
     
    13081311{ 
    13091312        static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; 
    1310         static struct timeval shutdown_start; 
    1311         struct timeval tvd; 
     1313        static int64_t shutdown_start, log_delta; 
    13121314        mach_msg_type_number_t outvalCnt; 
    13131315        struct logmsg_s *lm; 
     
    13301332        } 
    13311333 
    1332         if (shutdown_start.tv_sec == 0) { 
    1333                 gettimeofday(&shutdown_start, NULL); 
     1334        if (shutdown_start == 0) { 
     1335                shutdown_start = runtime_get_wall_time(); 
    13341336                launchd_log_vm_stats(); 
    13351337        } 
     
    13501352 
    13511353        while ((lm = STAILQ_FIRST(&logmsg_queue))) { 
    1352                 timersub(&lm->when, &shutdown_start, &tvd); 
    1353  
    1354                 /* don't ask */ 
    1355                 if (tvd.tv_sec < 0) { 
    1356                         tvd.tv_sec = 0; 
    1357                         tvd.tv_usec = 0; 
    1358                 } 
    1359  
    1360                 fprintf(ourlogfile, "%3ld.%06d%6u %-40s%6u %-40s %s\n", tvd.tv_sec, tvd.tv_usec, 
     1354                log_delta = lm->when - shutdown_start; 
     1355 
     1356                fprintf(ourlogfile, "%8lld%6u %-40s%6u %-40s %s\n", log_delta, 
    13611357                                lm->from_pid, lm->from_name, lm->about_pid, lm->about_name, lm->msg); 
    13621358 
     
    15511547        return (apple_internal_logging == 0); 
    15521548} 
     1549 
     1550int64_t 
     1551runtime_get_wall_time(void) 
     1552{ 
     1553        struct timeval tv; 
     1554        int64_t r; 
     1555 
     1556        launchd_assumes(gettimeofday(&tv, NULL) != -1); 
     1557 
     1558        r = tv.tv_sec; 
     1559        r *= USEC_PER_SEC; 
     1560        r += tv.tv_usec; 
     1561 
     1562        return r; 
     1563} 
     1564 
     1565uint64_t 
     1566runtime_get_opaque_time(void) 
     1567{ 
     1568        return mach_absolute_time(); 
     1569} 
     1570 
     1571uint64_t 
     1572runtime_opaque_time_to_nano(uint64_t o) 
     1573{ 
     1574#if defined(__i386__) 
     1575        if (unlikely(tbi.numer != tbi.denom)) { 
     1576#elif defined(__ppc__) 
     1577        if (likely(tbi.numer != tbi.denom)) { 
     1578#else 
     1579        if (tbi.numer != tbi.denom) { 
     1580#endif 
     1581                if (o < INT32_MAX) { 
     1582                        o *= tbi.numer; 
     1583                        o /= tbi.denom; 
     1584                } else { 
     1585                        double d = o; 
     1586                        d *= tbi.numer; 
     1587                        d /= tbi.denom; 
     1588                        o = d; 
     1589                } 
     1590        } 
     1591 
     1592        return o; 
     1593} 
     1594 
     1595void 
     1596do_file_init(void) 
     1597{ 
     1598        launchd_assert(mach_timebase_info(&tbi) == 0); 
     1599} 
     1600 
  • trunk/launchd/src/launchd_runtime.h

    r23470 r23471  
    7171#endif 
    7272 
     73#define likely(x)       __builtin_expect((bool)(x), true) 
     74#define unlikely(x)     __builtin_expect((bool)(x), false) 
    7375 
    7476struct ldcred { 
     
    152154void runtime_log_push(void); 
    153155 
     156int64_t runtime_get_wall_time(void); 
     157uint64_t runtime_get_opaque_time(void); 
     158uint64_t runtime_opaque_time_to_nano(uint64_t o); 
     159 
    154160 
    155161kern_return_t launchd_set_bport(mach_port_t name); 
  • trunk/launchd/src/libvproc_internal.h

    r23297 r23471  
    6060struct logmsg_s { 
    6161        STAILQ_ENTRY(logmsg_s) sqe; 
    62         struct timeval when; 
     62        int64_t when; 
    6363        pid_t from_pid; 
    6464        pid_t about_pid;