clock_gettime-2.patch

Akira Tanaka, 07/20/2013 07:39 PM

Download (7.58 KB)

View differences:

process.c (working copy)
6627 6627
#define rb_proc_times rb_f_notimplement
6628 6628
#endif
6629 6629

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

  
6695
    rb_scan_args(argc, argv, "11", &clk_id, &unit);
6696

  
6697
    c = NUM2CLOCKID(clk_id);
6698
    ret = clock_gettime(c, &ts);
6699
    if (ret == -1)
6700
	rb_sys_fail(0);
6701

  
6702
    if (unit == ID2SYM(rb_intern("nanoseconds"))) {
6703
        factor = 1000000000;
6704
        goto return_integer;
6705
    }
6706
    else if (unit == ID2SYM(rb_intern("microseconds"))) {
6707
        factor = 1000000;
6708
        goto return_integer;
6709
    }
6710
    else if (unit == ID2SYM(rb_intern("milliseconds"))) {
6711
        factor = 1000;
6712
        goto return_integer;
6713
    }
6714
    else if (unit == ID2SYM(rb_intern("float_microseconds"))) {
6715
        factor = 1000000;
6716
        goto return_float;
6717
    }
6718
    else if (unit == ID2SYM(rb_intern("float_milliseconds"))) {
6719
        factor = 1000;
6720
        goto return_float;
6721
    }
6722
    else if (NIL_P(unit) || unit == ID2SYM(rb_intern("float_seconds"))) {
6723
        factor = 1;
6724
        goto return_float;
6725
    }
6726
    else {
6727
        rb_raise(rb_eArgError, "unexpected unit: %"PRIsVALUE, unit);
6728
    }
6729

  
6730
  return_float:
6731
    return DBL2NUM((ts.tv_sec + 1e-9 * (double)ts.tv_nsec) / factor);
6732

  
6733
  return_integer:
6734
#if defined(HAVE_LONG_LONG)
6735
    if (!MUL_OVERFLOW_SIGNED_INTEGER_P(factor, (LONG_LONG)ts.tv_sec,
6736
                LLONG_MIN, LLONG_MAX-(factor-1))) {
6737
        return LL2NUM(ts.tv_nsec/(1000000000/factor) + factor * (LONG_LONG)ts.tv_sec);
6738
    }
6739
#endif
6740
    return rb_funcall(LONG2FIX(ts.tv_nsec/(1000000000/factor)), '+', 1,
6741
            rb_funcall(LONG2FIX(factor), '*', 1, TIMET2NUM(ts.tv_sec)));
6742
}
6743
#else
6744
#define rb_clock_gettime rb_f_notimplement
6745
#endif
6746

  
6630 6747
VALUE rb_mProcess;
6631 6748
VALUE rb_mProcUID;
6632 6749
VALUE rb_mProcGID;
......
6885 7002

  
6886 7003
    rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0);
6887 7004

  
7005
#ifdef CLOCK_REALTIME
7006
    rb_define_const(rb_mProcess, "CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
7007
#endif
7008
#ifdef CLOCK_MONOTONIC
7009
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
7010
#endif
7011
#ifdef CLOCK_PROCESS_CPUTIME_ID
7012
    rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
7013
#endif
7014
#ifdef CLOCK_THREAD_CPUTIME_ID
7015
    rb_define_const(rb_mProcess, "CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
7016
#endif
7017
#ifdef CLOCK_VIRTUAL
7018
    rb_define_const(rb_mProcess, "CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
7019
#endif
7020
#ifdef CLOCK_PROF
7021
    rb_define_const(rb_mProcess, "CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
7022
#endif
7023
#ifdef CLOCK_REALTIME_FAST
7024
    rb_define_const(rb_mProcess, "CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
7025
#endif
7026
#ifdef CLOCK_REALTIME_PRECISE
7027
    rb_define_const(rb_mProcess, "CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
7028
#endif
7029
#ifdef CLOCK_MONOTONIC_FAST
7030
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
7031
#endif
7032
#ifdef CLOCK_MONOTONIC_PRECISE
7033
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
7034
#endif
7035
#ifdef CLOCK_MONOTONIC_RAW
7036
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
7037
#endif
7038
#ifdef CLOCK_UPTIME
7039
    rb_define_const(rb_mProcess, "CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
7040
#endif
7041
#ifdef CLOCK_UPTIME_FAST
7042
    rb_define_const(rb_mProcess, "CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
7043
#endif
7044
#ifdef CLOCK_UPTIME_PRECISE
7045
    rb_define_const(rb_mProcess, "CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
7046
#endif
7047
#ifdef CLOCK_SECOND
7048
    rb_define_const(rb_mProcess, "CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
7049
#endif
7050
    rb_define_module_function(rb_mProcess, "clock_gettime", rb_clock_gettime, -1);
7051

  
6888 7052
#if defined(HAVE_TIMES) || defined(_WIN32)
6889 7053
    rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", NULL);
6890 7054
#endif
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);],
test/ruby/test_process.rb (working copy)
1658 1658
    end
1659 1659
  end if windows?
1660 1660

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

  
1671
    t1 = Time.now
1672
    t1 = t1.tv_sec * 1000000000 + t1.tv_nsec
1673
    t2 = Process.clock_gettime(Process::CLOCK_REALTIME, :nanoseconds)
1674
    assert_operator(t1, :<=, t2)
1675
  end
1676

  
1661 1677
end