• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "test/cpp/qps/usage_timer.h"
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 
time_double(struct timeval * tv)31 static double time_double(struct timeval* tv) {
32   return tv->tv_sec + 1e-6 * tv->tv_usec;
33 }
34 #endif
35 
UsageTimer()36 UsageTimer::UsageTimer() : start_(Sample()) {}
37 
Now()38 double UsageTimer::Now() {
39   auto ts = gpr_now(GPR_CLOCK_REALTIME);
40   return ts.tv_sec + 1e-9 * ts.tv_nsec;
41 }
42 
get_resource_usage(double * utime,double * stime)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 
get_cpu_usage(unsigned long long * total_cpu_time,unsigned long long * idle_cpu_time)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   gpr_log(GPR_INFO, "get_cpu_usage(): Non-linux platform is not supported.");
73 #endif
74 }
75 
Sample()76 UsageTimer::Result UsageTimer::Sample() {
77   Result r;
78   r.wall = Now();
79   get_resource_usage(&r.user, &r.system);
80   r.total_cpu_time = 0;
81   r.idle_cpu_time = 0;
82   get_cpu_usage(&r.total_cpu_time, &r.idle_cpu_time);
83   return r;
84 }
85 
Mark() const86 UsageTimer::Result UsageTimer::Mark() const {
87   Result s = Sample();
88   Result r;
89   r.wall = s.wall - start_.wall;
90   r.user = s.user - start_.user;
91   r.system = s.system - start_.system;
92   r.total_cpu_time = s.total_cpu_time - start_.total_cpu_time;
93   r.idle_cpu_time = s.idle_cpu_time - start_.idle_cpu_time;
94 
95   return r;
96 }
97