cpu_utilization.cc 6.7 KB
Newer Older
C
chenjian 已提交
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/platform/profiler/cpu_utilization.h"

namespace paddle {
namespace platform {

#ifdef _MSC_VER
static uint64_t FileTimeToUint64(FILETIME time) {
  uint64_t low_part = time.dwLowDateTime;
  uint64_t high_part = time.dwHighDateTime;
  uint64_t result = (high_part << 32) | low_part;
  return result;
}
#endif

void CpuUtilization::RecordBeginTimeInfo() {
#if defined(_MSC_VER)
  HANDLE process_handle = GetCurrentProcess();
  GetSystemTimeAsFileTime(&start_);
  GetSystemTimes(&system_idle_time_start_, &system_kernel_time_start_,
                 &system_user_time_start_);
  GetProcessTimes(process_handle, &process_creation_time_, &process_exit_time_,
                  &process_kernel_time_start_, &process_user_time_start_);

#elif defined(__linux__)
  start_ = times(&process_tms_start_);
#define proc_path_size 1024
  static char proc_stat_path[proc_path_size] = "/proc/stat";
  FILE *stat_file = fopen(proc_stat_path, "r");
  if (stat_file != nullptr) {
    char temp_str[200];
    uint64_t temp_lu;
C
chenjian 已提交
57 58 59 60 61 62 63 64 65 66
    int retval = fscanf(
        stat_file, "%s %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64
                   "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64,
        temp_str, &system_tms_start_.tms_utime, &nice_time_start_,
        &system_tms_start_.tms_stime, &idle_start_, &iowait_start_, &irq_start_,
        &softirq_start_, &steal_start_, &temp_lu, &temp_lu);
    if (retval != 11) {
      LOG(WARNING)
          << "Failed to read cpu utilization information at record beginning."
          << std::endl;
C
chenjian 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }
    fclose(stat_file);
  }
#else
#endif
}

void CpuUtilization::RecordEndTimeInfo() {
#if defined(_MSC_VER)
  HANDLE process_handle = GetCurrentProcess();
  GetSystemTimeAsFileTime(&end_);
  GetSystemTimes(&system_idle_time_end_, &system_kernel_time_end_,
                 &system_user_time_end_);
  GetProcessTimes(process_handle, &process_creation_time_, &process_exit_time_,
                  &process_kernel_time_end_, &process_user_time_end_);
#elif defined(__linux__)
  end_ = times(&process_tms_end_);
#define proc_path_size 1024
  static char proc_stat_path[proc_path_size] = "/proc/stat";
  FILE *stat_file = fopen(proc_stat_path, "r");
  if (stat_file != nullptr) {
    char temp_str[200];
    uint64_t temp_lu;
C
chenjian 已提交
90 91 92 93 94 95 96 97 98 99 100
    int retval = fscanf(
        stat_file, "%s %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64
                   "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64,
        temp_str, &system_tms_end_.tms_utime, &nice_time_end_,
        &system_tms_end_.tms_stime, &idle_end_, &iowait_end_, &irq_end_,
        &softirq_end_, &steal_end_, &temp_lu, &temp_lu);

    if (retval != 11) {
      LOG(WARNING)
          << "Failed to read cpu utilization information at record end."
          << std::endl;
C
chenjian 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }
    fclose(stat_file);
  }
#else
#endif
}

float CpuUtilization::GetCpuUtilization() {
  float cpu_utilization = 0.0;
#if defined(_MSC_VER)
  uint64_t system_user_time_start = FileTimeToUint64(system_user_time_start_);
  uint64_t system_user_time_end = FileTimeToUint64(system_user_time_end_);
  uint64_t system_kernel_time_start =
      FileTimeToUint64(system_kernel_time_start_);
  uint64_t system_kernel_time_end = FileTimeToUint64(system_kernel_time_end_);
  uint64_t system_idle_time_start = FileTimeToUint64(system_idle_time_start_);
  uint64_t system_idle_time_end = FileTimeToUint64(system_idle_time_end_);
  float busy_time = (system_kernel_time_end - system_kernel_time_start) +
                    (system_user_time_end - system_user_time_start);
  float idle_time = system_idle_time_end - system_idle_time_start;
C
chenjian 已提交
121 122 123
  if (busy_time + idle_time != 0) {
    cpu_utilization = busy_time / (busy_time + idle_time);
  }
C
chenjian 已提交
124 125 126 127 128 129 130
#elif defined(__linux__)
  float busy_time = (system_tms_end_.tms_utime - system_tms_start_.tms_utime) +
                    (system_tms_end_.tms_stime - system_tms_start_.tms_stime) +
                    (nice_time_end_ - nice_time_start_) +
                    (irq_end_ - irq_start_) + (softirq_end_ - softirq_start_) +
                    (steal_end_ - steal_start_);
  float idle_time = (idle_end_ - idle_start_) + (iowait_end_ - iowait_start_);
C
chenjian 已提交
131 132 133
  if (busy_time + idle_time != 0) {
    cpu_utilization = busy_time / (busy_time + idle_time);
  }
C
chenjian 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#else
  LOG(WARNING)
      << "Current System is not supported to get system cpu utilization"
      << cpu_utilization << std::endl;
#endif
  return cpu_utilization;
}

float CpuUtilization::GetCpuCurProcessUtilization() {
  float cpu_process_utilization = 0.0;
#ifdef _MSC_VER
  uint64_t process_user_time_start = FileTimeToUint64(process_user_time_start_);
  uint64_t process_user_time_end = FileTimeToUint64(process_user_time_end_);
  uint64_t process_kernel_time_start =
      FileTimeToUint64(process_kernel_time_start_);
  uint64_t process_kernel_time_end = FileTimeToUint64(process_kernel_time_end_);
  uint64_t start = FileTimeToUint64(start_);
  uint64_t end = FileTimeToUint64(end_);
  float busy_time = (process_kernel_time_end - process_kernel_time_start) +
                    (process_user_time_end - process_user_time_start);
C
chenjian 已提交
154 155 156
  if (end - start != 0) {
    cpu_process_utilization = busy_time / (end - start);
  }
C
chenjian 已提交
157 158 159 160
#elif defined(__linux__)
  float busy_time =
      (process_tms_end_.tms_utime - process_tms_start_.tms_utime) +
      (process_tms_end_.tms_stime - process_tms_start_.tms_stime);
C
chenjian 已提交
161 162 163
  if (end_ - start_ != 0) {
    cpu_process_utilization = busy_time / (end_ - start_);
  }
C
chenjian 已提交
164 165 166 167 168 169 170 171 172 173
#else
  LOG(WARNING)
      << "Current System is not supported to get process cpu utilization"
      << cpu_process_utilization << std::endl;
#endif
  return cpu_process_utilization;
}

}  // namespace platform
}  // namespace paddle