usage_timer.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27 #ifdef __linux__
28 #include <sys/resource.h>
29 #include <sys/time.h>
30 
31 static double time_double(struct timeval* tv) {
32  return tv->tv_sec + 1e-6 * tv->tv_usec;
33 }
34 #endif
35 
37 
38 double UsageTimer::Now() {
39  auto ts = gpr_now(GPR_CLOCK_REALTIME);
40  return ts.tv_sec + 1e-9 * ts.tv_nsec;
41 }
42 
43 static void get_resource_usage(double* utime, double* stime) {
44 #ifdef __linux__
45  struct rusage usage;
46  getrusage(RUSAGE_SELF, &usage);
47  *utime = time_double(&usage.ru_utime);
48  *stime = time_double(&usage.ru_stime);
49 #else
50  *utime = 0;
51  *stime = 0;
52 #endif
53 }
54 
55 static void get_cpu_usage(unsigned long long* total_cpu_time,
56  unsigned long long* idle_cpu_time) {
57 #ifdef __linux__
58  std::ifstream proc_stat("/proc/stat");
59  proc_stat.ignore(5);
60  std::string cpu_time_str;
61  std::string first_line;
62  std::getline(proc_stat, first_line);
63  std::stringstream first_line_s(first_line);
64  for (int i = 0; i < 10; ++i) {
65  std::getline(first_line_s, cpu_time_str, ' ');
66  *total_cpu_time += std::stol(cpu_time_str);
67  if (i == 3) {
68  *idle_cpu_time = std::stol(cpu_time_str);
69  }
70  }
71 #else
72  // Use the parameters to avoid unused-parameter warning
73  (void)total_cpu_time;
74  (void)idle_cpu_time;
75  gpr_log(GPR_INFO, "get_cpu_usage(): Non-linux platform is not supported.");
76 #endif
77 }
78 
80  Result r;
81  r.wall = Now();
82  get_resource_usage(&r.user, &r.system);
83  r.total_cpu_time = 0;
84  r.idle_cpu_time = 0;
85  get_cpu_usage(&r.total_cpu_time, &r.idle_cpu_time);
86  return r;
87 }
88 
90  Result s = Sample();
91  Result r;
92  r.wall = s.wall - start_.wall;
93  r.user = s.user - start_.user;
94  r.system = s.system - start_.system;
95  r.total_cpu_time = s.total_cpu_time - start_.total_cpu_time;
96  r.idle_cpu_time = s.idle_cpu_time - start_.idle_cpu_time;
97 
98  return r;
99 }
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
get_resource_usage
static void get_resource_usage(double *utime, double *stime)
Definition: usage_timer.cc:43
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
UsageTimer::Sample
static Result Sample()
Definition: usage_timer.cc:79
time.h
UsageTimer::Now
static double Now()
Definition: usage_timer.cc:38
start_
const char * start_
Definition: abseil-cpp/absl/strings/internal/str_format/arg.cc:175
absl::FormatConversionChar::e
@ e
UsageTimer::Result::wall
double wall
Definition: usage_timer.h:27
get_cpu_usage
static void get_cpu_usage(unsigned long long *total_cpu_time, unsigned long long *idle_cpu_time)
Definition: usage_timer.cc:55
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
UsageTimer::Mark
Result Mark() const
Definition: usage_timer.cc:89
UsageTimer::Result::total_cpu_time
unsigned long long total_cpu_time
Definition: usage_timer.h:30
UsageTimer::Result::idle_cpu_time
unsigned long long idle_cpu_time
Definition: usage_timer.h:31
UsageTimer::Result
Definition: usage_timer.h:26
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
UsageTimer::UsageTimer
UsageTimer()
Definition: usage_timer.cc:36
UsageTimer::Result::user
double user
Definition: usage_timer.h:28
UsageTimer::start_
const Result start_
Definition: usage_timer.h:41
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
bloaty::usage
const char usage[]
Definition: bloaty.cc:1843
timeval::tv_usec
long tv_usec
Definition: setup_once.h:122
timeval
Definition: setup_once.h:113
fix_build_deps.r
r
Definition: fix_build_deps.py:491
absl::container_internal::Sample
HashtablezInfoHandle Sample(size_t inline_element_size ABSL_ATTRIBUTE_UNUSED)
Definition: abseil-cpp/absl/container/internal/hashtablez_sampler.h:251
UsageTimer::Result::system
double system
Definition: usage_timer.h:29
usage_timer.h
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:48