From c791a836a06f4b7d3883e7930f2a730ee02afe4b Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sun, 12 Apr 2020 16:52:26 +0200 Subject: [PATCH] Don't factor out Tms. --- src/crystal/system/process.cr | 4 ++-- src/crystal/system/unix/process.cr | 12 ++++++------ src/process.cr | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/crystal/system/process.cr b/src/crystal/system/process.cr index d66292ae84bc..2dda1b26afc1 100644 --- a/src/crystal/system/process.cr +++ b/src/crystal/system/process.cr @@ -42,8 +42,8 @@ struct Crystal::System::Process # Whether the process identified by *pid* is still registered in the system. # def self.exists?(pid : Int) : Bool - # Measures CPU times and returns valid arguments for `Tms.new`. - # def self.times : NamedTuple + # Measures CPU times. + # def self.times : ::Process::Tms # Duplicates the current process. # def self.fork : ProcessInformation diff --git a/src/crystal/system/unix/process.cr b/src/crystal/system/unix/process.cr index 52b61f166a40..018ecd7cf3f3 100644 --- a/src/crystal/system/unix/process.cr +++ b/src/crystal/system/unix/process.cr @@ -68,12 +68,12 @@ struct Crystal::System::Process LibC.getrusage(LibC::RUSAGE_SELF, out usage) LibC.getrusage(LibC::RUSAGE_CHILDREN, out child) - { - utime: usage.ru_utime.tv_sec.to_f64 + usage.ru_utime.tv_usec.to_f64 / 1e6, - stime: usage.ru_stime.tv_sec.to_f64 + usage.ru_stime.tv_usec.to_f64 / 1e6, - cutime: child.ru_utime.tv_sec.to_f64 + child.ru_utime.tv_usec.to_f64 / 1e6, - cstime: child.ru_stime.tv_sec.to_f64 + child.ru_stime.tv_usec.to_f64 / 1e6, - } + ::Process::Tms.new( + usage.ru_utime.tv_sec.to_f64 + usage.ru_utime.tv_usec.to_f64 / 1e6, + usage.ru_stime.tv_sec.to_f64 + usage.ru_stime.tv_usec.to_f64 / 1e6, + child.ru_utime.tv_sec.to_f64 + child.ru_utime.tv_usec.to_f64 / 1e6, + child.ru_stime.tv_sec.to_f64 + child.ru_stime.tv_usec.to_f64 / 1e6, + ) end def self.fork(*, will_exec = false) diff --git a/src/process.cr b/src/process.cr index 4603aecbfbd5..bbcf790670bf 100644 --- a/src/process.cr +++ b/src/process.cr @@ -1,3 +1,12 @@ +# A struct representing the CPU current times of the process, +# in fractions of seconds. +# +# * *utime*: CPU time a process spent in userland. +# * *stime*: CPU time a process spent in the kernel. +# * *cutime*: CPU time a processes terminated children (and their terminated children) spent in the userland. +# * *cstime*: CPU time a processes terminated children (and their terminated children) spent in the kernel. +record Process::Tms, utime : Float64, stime : Float64, cutime : Float64, cstime : Float64 + require "crystal/system/process" class Process @@ -50,19 +59,10 @@ class Process Crystal::System::Process.exists?(pid) end - # A struct representing the CPU current times of the process, - # in fractions of seconds. - # - # * *utime*: CPU time a process spent in userland. - # * *stime*: CPU time a process spent in the kernel. - # * *cutime*: CPU time a processes terminated children (and their terminated children) spent in the userland. - # * *cstime*: CPU time a processes terminated children (and their terminated children) spent in the kernel. - record Tms, utime : Float64, stime : Float64, cutime : Float64, cstime : Float64 - # Returns a `Tms` for the current process. For the children times, only those # of terminated children are returned. def self.times : Tms - Tms.new(**Crystal::System::Process.times) + Crystal::System::Process.times end # Runs the given block inside a new process and