| 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 |
|
| 31 |
static double time_double(struct timeval* tv) { |
| 32 |
return tv->tv_sec + 1e-6 * tv->tv_usec; |
| 33 |
} |
| 34 |
#endif |
| 35 |
|
| 36 |
UsageTimer::UsageTimer() : start_(Sample()) {} |
| 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 |
|
| 79 |
UsageTimer::Result UsageTimer::Sample() { |
| 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 |
|
| 89 |
UsageTimer::Result UsageTimer::Mark() const { |
| 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 |
} |