File: rp_measure_process_time.c

package info (click to toggle)
ruby-prof 0.17.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,656 kB
  • sloc: ruby: 5,043; ansic: 2,175; makefile: 6
file content (71 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Copyright (C) 2005-2013 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
   Please see the LICENSE file for copyright and distribution information */

#include "ruby_prof.h"
#include <time.h>

static VALUE cMeasureProcessTime;

static double
measure_process_time()
{
#if defined(__linux__)
    struct timespec clock;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID , &clock);
    return clock.tv_sec + (clock.tv_nsec/1000000000.0);
#elif defined(_win32)
	FILETIME createTime;
	FILETIME exitTime;
	FILETIME sysTime;
	FILETIME cpuTime;

	ULARGE_INTEGER sysTimeInt;
	ULARGE_INTEGER cpuTimeInt;
	ULONGLONG totalTime;

	GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &sysTime, &cpuTime); 

	/* Doing this based on MSFT's recommendation in the FILETIME structure documentation at
	  http://msdn.microsoft.com/en-us/library/ms724284%28VS.85%29.aspx*/

	sysTimeInt.LowPart = sysTime.dwLowDateTime;
	sysTimeInt.HighPart = sysTime.dwHighDateTime;
	cpuTimeInt.LowPart = cpuTime.dwLowDateTime;
	cpuTimeInt.HighPart = cpuTime.dwHighDateTime;

	totalTime = sysTimeInt.QuadPart + cpuTimeInt.QuadPart;

	// Times are in 100-nanosecond time units.  So instead of 10-9 use 10-7
	return totalTime / 10000000.0;
#else
    return ((double)clock()) / CLOCKS_PER_SEC;
#endif
}

/* call-seq:
   measure_process_time -> float

Returns the process time.*/
static VALUE
prof_measure_process_time(VALUE self)
{
    return rb_float_new(measure_process_time());
}

prof_measurer_t* prof_measurer_process_time()
{
  prof_measurer_t* measure = ALLOC(prof_measurer_t);
  measure->measure = measure_process_time;
  return measure;
}


void rp_init_measure_process_time()
{
    rb_define_const(mProf, "CLOCKS_PER_SEC", INT2NUM(CLOCKS_PER_SEC));
    rb_define_const(mProf, "PROCESS_TIME", INT2NUM(MEASURE_PROCESS_TIME));
	rb_define_const(mProf, "PROCESS_TIME_ENABLED", Qtrue);

    cMeasureProcessTime = rb_define_class_under(mMeasure, "ProcessTime", rb_cObject);
    rb_define_singleton_method(cMeasureProcessTime, "measure", prof_measure_process_time, 0);
}