From: Evan Phoenix Date: 2011-01-19T05:24:40+09:00 Subject: [ruby-core:34568] [PATCH] Add thread timing to Struct::Tms --Apple-Mail-5--752510550 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Because 1.9 now uses real threads, being able to find out how much time = the current thread is using is quite useful, especially for = benchmarking. The attached diff adds tutime and tstime to Struct::Tms. tutime is for = thread user time and tstime is for thread system time. The second diff also changes benchmark.rb to also use tutime out of = Struct::Tms instead of utime. This provides more relevant timing of the = block than using utime. --Apple-Mail-5--752510550 Content-Disposition: attachment; filename=thread-timing.diff Content-Type: application/octet-stream; name="thread-timing.diff" Content-Transfer-Encoding: 7bit Index: process.c =================================================================== --- process.c (revision 30597) +++ process.c (working copy) @@ -122,6 +122,10 @@ #endif #endif +#if defined(__MacOS_X__) +#include +#endif + #if SIZEOF_RLIM_T == SIZEOF_INT # define RLIM2NUM(v) UINT2NUM(v) # define NUM2RLIM(v) NUM2UINT(v) @@ -5504,6 +5508,16 @@ #if defined(HAVE_TIMES) + +static double to_dbl(long sec, long msec) { + return (double)sec + ((double)msec / 1000000.0); +} + +static double tv_to_dbl(struct timeval* tv) { + return (double)tv->tv_sec + ((double)tv->tv_usec / 1000000.0); +} + + /* * call-seq: * Process.times -> aStructTms @@ -5533,14 +5547,60 @@ HZ; #endif struct tms buf; - volatile VALUE utime, stime, cutime, sctime; + volatile VALUE utime, stime, cutime, sctime, tutime, tstime; times(&buf); - return rb_struct_new(rb_cProcessTms, - utime = DBL2NUM(buf.tms_utime / hertz), - stime = DBL2NUM(buf.tms_stime / hertz), - cutime = DBL2NUM(buf.tms_cutime / hertz), - sctime = DBL2NUM(buf.tms_cstime / hertz)); + utime = DBL2NUM(buf.tms_utime / hertz); + stime = DBL2NUM(buf.tms_stime / hertz); + cutime = DBL2NUM(buf.tms_cutime / hertz); + sctime = DBL2NUM(buf.tms_cstime / hertz); + +#if defined(__MacOS_X__) + { + mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; + thread_basic_info_data_t info; + kern_return_t err; + + mach_port_t port = mach_thread_self(); + err = thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count); + mach_port_deallocate(mach_task_self(), port); + + if(err == KERN_SUCCESS) { + tutime = DBL2NUM(to_dbl( + info.user_time.seconds, info.user_time.microseconds)); + tstime = DBL2NUM(to_dbl( + info.system_time.seconds, info.system_time.microseconds)); + } else { + tutime = utime; + tstime = stime; + } + } +#elif defined(RUSAGE_THREAD) + { + struct rusage buf; + + getrusage(RUSAGE_THREAD, &buf); + tutime = DBL2NUM(tv_to_dbl(&buf.ru_utime)); + tstime = DBL2NUM(tv_to_dbl(&buf.ru_stime)); + } +#elif defined(_WIN32) + { + FILETIME unused, unused2; + FILETIME sys, usr; + + GetThreadTimes(GetCurrentThread(), &unused, &unused2, &sys, &user); + + tutime = DBL2NUM(((double)usr) / 10000); + tstime = DBL2NUM(((double)sys) / 10000); + } +#else + tutime = utime; + tstime = stime; +#endif + + return rb_struct_new(rb_cProcessTms, utime, stime, + cutime, sctime, + tutime, tstime); } #else #define rb_proc_times rb_f_notimplement @@ -5727,7 +5787,7 @@ rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0); #if defined(HAVE_TIMES) || defined(_WIN32) - rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", NULL); + rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", "tutime", "tstime", NULL); #endif SAVED_USER_ID = geteuid(); --Apple-Mail-5--752510550 Content-Disposition: attachment; filename=benchmark-thread.diff Content-Type: application/octet-stream; name="benchmark-thread.diff" Content-Transfer-Encoding: 7bit Index: lib/benchmark.rb =================================================================== --- lib/benchmark.rb (revision 30597) +++ lib/benchmark.rb (working copy) @@ -293,8 +293,8 @@ t0, r0 = Benchmark.times, Time.now yield t1, r1 = Benchmark.times, Time.now - Benchmark::Tms.new(t1.utime - t0.utime, - t1.stime - t0.stime, + Benchmark::Tms.new(t1.tutime - t0.tutime, + t1.tstime - t0.tstime, t1.cutime - t0.cutime, t1.cstime - t0.cstime, r1.to_f - r0.to_f, --Apple-Mail-5--752510550--