clock_gettime-3.patch

Akira Tanaka, 08/01/2013 09:10 PM

Download (8.3 KB)

View differences:

test/ruby/test_process.rb (working copy)
1658 1658
    end
1659 1659
  end if windows?
1660 1660

  
1661
  def test_clock_gettime
1662
    t1 = Process.clock_gettime(Process::CLOCK_REALTIME, :nanoseconds)
1663
    t2 = Time.now; t2 = t2.tv_sec * 1000000000 + t2.tv_nsec
1664
    t3 = Process.clock_gettime(Process::CLOCK_REALTIME, :nanoseconds)
1665
    assert_operator(t1, :<=, t2)
1666
    assert_operator(t2, :<=, t3)
1667
  end
1668

  
1661 1669
end
configure.in (working copy)
1262 1262
@%:@include <sys/resource.h>
1263 1263
])
1264 1264
RUBY_REPLACE_TYPE(off_t, [], OFFT)
1265
RUBY_REPLACE_TYPE(clockid_t, [], CLOCKID)
1265 1266

  
1266 1267
AC_CACHE_CHECK(for prototypes, rb_cv_have_prototypes,
1267 1268
  [AC_TRY_COMPILE([int foo(int x) { return 0; }], [return foo(10);],
process.c (working copy)
68 68
# include "nacl/unistd.h"
69 69
#endif
70 70

  
71

  
71
#ifdef HAVE_SYS_TIME_H
72
#include <sys/time.h>
73
#endif
72 74
#ifdef HAVE_SYS_TIMES_H
73 75
#include <sys/times.h>
74 76
#endif
......
6627 6629
#define rb_proc_times rb_f_notimplement
6628 6630
#endif
6629 6631

  
6632
/*
6633
 *  call-seq:
6634
 *     Process.clock_gettime(clk_id [, unit])   -> number
6635
 *
6636
 *  Returns a time returned by POSIX clock_gettime() function.
6637
 *
6638
 *  _clk_id_ specifies a kind of clock.
6639
 *  It is specifed as a constant which begins with <code>Process::CLOCK_</code>
6640
 *  such like <code>Process::CLOCK_REALTIME</code> and
6641
 *  <code>Process::CLOCK_MONOTONIC</code>.
6642
 *  The supported constants depends on OS and version.
6643
 *  Ruby provides following type of _clk_id_ if available.
6644
 *
6645
 *  [CLOCK_REALTIME] SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1
6646
 *  [CLOCK_MONOTONIC] SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4
6647
 *  [CLOCK_PROCESS_CPUTIME_ID] SUSv3 to 4, Linux 2.5.63
6648
 *  [CLOCK_THREAD_CPUTIME_ID] SUSv3 to 4, Linux 2.5.63
6649
 *  [CLOCK_VIRTUAL] FreeBSD 3.0, OpenBSD 2.1
6650
 *  [CLOCK_PROF] FreeBSD 3.0, OpenBSD 2.1
6651
 *  [CLOCK_REALTIME_FAST] FreeBSD 8.1
6652
 *  [CLOCK_REALTIME_PRECISE] FreeBSD 8.1
6653
 *  [CLOCK_MONOTONIC_FAST] FreeBSD 8.1
6654
 *  [CLOCK_MONOTONIC_PRECISE] FreeBSD 8.1
6655
 *  [CLOCK_MONOTONIC_RAW] Linux 2.6.28
6656
 *  [CLOCK_UPTIME] FreeBSD 7.0
6657
 *  [CLOCK_UPTIME_FAST] FreeBSD 8.1
6658
 *  [CLOCK_UPTIME_PRECISE] FreeBSD 8.1
6659
 *  [CLOCK_SECOND] FreeBSD 8.1
6660
 *
6661
 *  _unit_ specifies a type of the return value.
6662
 *
6663
 *  [:float_seconds] number of seconds as a float (default)
6664
 *  [:float_milliseconds] number of milliseconds as a float
6665
 *  [:float_microseconds] number of microseconds as a float
6666
 *  [:milliseconds] number of milliseconds as an integer
6667
 *  [:microseconds] number of microseconds as an integer
6668
 *  [:nanoseconds] number of nanoseconds as an integer
6669
 *
6670
 *  The underlying function, clock_gettime(), returns a number of nanoseconds.
6671
 *  Float object (IEEE 754 double) is not enough to represent
6672
 *  the return value for CLOCK_REALTIME.
6673
 *  If the exact nanoseconds value is required, use :nanoseconds as _unit_.
6674
 *
6675
 *  The origin (zero) of the returned value varies.
6676
 *  For example, system start up time, process start up time, the Epoch, etc.
6677
 *
6678
 *  The origin in CLOCK_REALTIME is defined as the Epoch
6679
 *  (1970-01-01 00:00:00 UTC).
6680
 *  But some systems count leap seconds and others doesn't.
6681
 *  So the result can be interpreted differently across systems.
6682
 *
6683
 *    p Process.clock_gettime(Process::CLOCK_MONOTONIC)
6684
 *    #=> 2684827897652283
6685
 *
6686
 */
6687
VALUE
6688
rb_clock_gettime(int argc, VALUE *argv)
6689
{
6690
    struct timespec ts;
6691
    VALUE clk_id, unit;
6692
    int ret;
6693
    long factor;
6694

  
6695
#if defined(HAVE_CLOCK_GETTIME)
6696
    clockid_t c;
6697
#endif
6698

  
6699
    rb_scan_args(argc, argv, "11", &clk_id, &unit);
6700

  
6701
#ifndef CLOCK_REALTIME
6702
#define RUBY_CLOCK_REALTIME ID2SYM(rb_intern("EMULATE_CLOCK_REALTIME_BY_GETTIMEOFDAY"))
6703
    if (clk_id == RUBY_CLOCK_REALTIME) {
6704
        struct timeval tv;
6705
        ret = gettimeofday(&tv, 0);
6706
        if (ret != 0)
6707
            rb_sys_fail("gettimeofday");
6708
        ts.tv_sec = tv.tv_sec;
6709
        ts.tv_nsec = tv.tv_usec * 1000;
6710
        goto success;
6711
    }
6712
#endif
6713

  
6714
#if defined(HAVE_CLOCK_GETTIME)
6715
    c = NUM2CLOCKID(clk_id);
6716
    ret = clock_gettime(c, &ts);
6717
    if (ret == -1)
6718
	rb_sys_fail("clock_gettime");
6719
    goto success;
6720
#else
6721
    /* EINVAL emulates clock_gettime behavior when clock_id is invalid. */
6722
    errno = EINVAL;
6723
    rb_sys_fail(0);
6724
#endif
6725

  
6726
  success:
6727
    if (unit == ID2SYM(rb_intern("nanoseconds"))) {
6728
        factor = 1000000000;
6729
        goto return_integer;
6730
    }
6731
    else if (unit == ID2SYM(rb_intern("microseconds"))) {
6732
        factor = 1000000;
6733
        goto return_integer;
6734
    }
6735
    else if (unit == ID2SYM(rb_intern("milliseconds"))) {
6736
        factor = 1000;
6737
        goto return_integer;
6738
    }
6739
    else if (unit == ID2SYM(rb_intern("float_microseconds"))) {
6740
        factor = 1000000;
6741
        goto return_float;
6742
    }
6743
    else if (unit == ID2SYM(rb_intern("float_milliseconds"))) {
6744
        factor = 1000;
6745
        goto return_float;
6746
    }
6747
    else if (NIL_P(unit) || unit == ID2SYM(rb_intern("float_seconds"))) {
6748
        factor = 1;
6749
        goto return_float;
6750
    }
6751
    else {
6752
        rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit);
6753
    }
6754

  
6755
  return_float:
6756
    return DBL2NUM((ts.tv_sec + 1e-9 * (double)ts.tv_nsec) / factor);
6757

  
6758
  return_integer:
6759
#if defined(HAVE_LONG_LONG)
6760
    if (!MUL_OVERFLOW_SIGNED_INTEGER_P(factor, (LONG_LONG)ts.tv_sec,
6761
                LLONG_MIN, LLONG_MAX-(factor-1))) {
6762
        return LL2NUM(ts.tv_nsec/(1000000000/factor) + factor * (LONG_LONG)ts.tv_sec);
6763
    }
6764
#endif
6765
    return rb_funcall(LONG2FIX(ts.tv_nsec/(1000000000/factor)), '+', 1,
6766
            rb_funcall(LONG2FIX(factor), '*', 1, TIMET2NUM(ts.tv_sec)));
6767
}
6768

  
6630 6769
VALUE rb_mProcess;
6631 6770
VALUE rb_mProcUID;
6632 6771
VALUE rb_mProcGID;
......
6885 7024

  
6886 7025
    rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0);
6887 7026

  
7027
#ifdef RUBY_CLOCK_REALTIME
7028
    rb_define_const(rb_mProcess, "CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
7029
#elif defined(CLOCK_REALTIME)
7030
    rb_define_const(rb_mProcess, "CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
7031
#endif
7032
#ifdef CLOCK_MONOTONIC
7033
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
7034
#endif
7035
#ifdef CLOCK_PROCESS_CPUTIME_ID
7036
    rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
7037
#endif
7038
#ifdef CLOCK_THREAD_CPUTIME_ID
7039
    rb_define_const(rb_mProcess, "CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
7040
#endif
7041
#ifdef CLOCK_VIRTUAL
7042
    rb_define_const(rb_mProcess, "CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
7043
#endif
7044
#ifdef CLOCK_PROF
7045
    rb_define_const(rb_mProcess, "CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
7046
#endif
7047
#ifdef CLOCK_REALTIME_FAST
7048
    rb_define_const(rb_mProcess, "CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
7049
#endif
7050
#ifdef CLOCK_REALTIME_PRECISE
7051
    rb_define_const(rb_mProcess, "CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
7052
#endif
7053
#ifdef CLOCK_MONOTONIC_FAST
7054
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
7055
#endif
7056
#ifdef CLOCK_MONOTONIC_PRECISE
7057
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
7058
#endif
7059
#ifdef CLOCK_MONOTONIC_RAW
7060
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
7061
#endif
7062
#ifdef CLOCK_UPTIME
7063
    rb_define_const(rb_mProcess, "CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
7064
#endif
7065
#ifdef CLOCK_UPTIME_FAST
7066
    rb_define_const(rb_mProcess, "CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
7067
#endif
7068
#ifdef CLOCK_UPTIME_PRECISE
7069
    rb_define_const(rb_mProcess, "CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
7070
#endif
7071
#ifdef CLOCK_SECOND
7072
    rb_define_const(rb_mProcess, "CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
7073
#endif
7074
    rb_define_module_function(rb_mProcess, "clock_gettime", rb_clock_gettime, -1);
7075

  
6888 7076
#if defined(HAVE_TIMES) || defined(_WIN32)
6889 7077
    rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", NULL);
6890 7078
#endif