File: monotonic_time.rb

package info (click to toggle)
ruby-statsd 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: ruby: 915; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 936 bytes parent folder | download
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
class Statsd
  # = MonotonicTime: a helper for getting monotonic time
  #
  # @example
  #   MonotonicTime.time_in_ms #=> 287138801.144576

  # MonotonicTime guarantees that the time is strictly linearly
  # increasing (unlike realtime).
  # @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
  module MonotonicTime
    class << self
      # @return [Integer] current monotonic time in milliseconds
      def time_in_ms
        time_in_nanoseconds / (10.0 ** 6)
      end

      private

      if defined?(Process::CLOCK_MONOTONIC)
        def time_in_nanoseconds
          Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
        end
      elsif RUBY_ENGINE == 'jruby'
        def time_in_nanoseconds
          java.lang.System.nanoTime
        end
      else
        def time_in_nanoseconds
          t = Time.now
          t.to_i * (10 ** 9) + t.nsec
        end
      end
    end
  end
end