Stat.cpp 6.3 KB
Newer Older
Z
zhangjinchao01 已提交
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */

#include "Stat.h"

#include <sys/syscall.h>  // for syscall()
#include <sys/types.h>
#include <iomanip>
#include <algorithm>

namespace paddle {

// return the thread id used by glog
pid_t getTID() {
#ifndef __NR_gettid
#define __NR_gettid 224
#endif
  pid_t tid = syscall(__NR_gettid);
  CHECK_NE(tid, -1);
  return tid;
}

StatSet globalStat("GlobalStatInfo");

void Stat::addSample(uint64_t value) {
  StatInfo* statInfo = statInfo_.get(false);
  if (!statInfo) {
    statInfo = new StatInfo(this);
    statInfo_.set(statInfo);
    std::lock_guard<std::mutex> guard(lock_);
    threadLocalBuf_.push_back({statInfo, getTID()});
  }
  if (value > statInfo->max_) {
    statInfo->max_ = value;
  }
  if (value < statInfo->min_) {
    statInfo->min_ = value;
  }
  statInfo->total_ += value;
  statInfo->count_++;
}

void Stat::mergeThreadStat(StatInfo& allThreadStat) {
  allThreadStat = destructStat_;
  for (auto& buf : threadLocalBuf_) {
    if (buf.first->max_ > allThreadStat.max_) {
      allThreadStat.max_ = buf.first->max_;
    }
    if (buf.first->min_ < allThreadStat.min_) {
      allThreadStat.min_ = buf.first->min_;
    }
    allThreadStat.total_ += buf.first->total_;
    allThreadStat.count_ += buf.first->count_;
  }
}

void Stat::reset() {
  std::lock_guard<std::mutex> guard(lock_);
  for (auto& buf : threadLocalBuf_) {
    buf.first->reset();
  }
}

std::ostream& operator<<(std::ostream& outPut, const Stat& stat) {
  std::lock_guard<std::mutex> guard(const_cast<Stat&>(stat).lock_);
  auto showStat = [&](const StatInfo* info, pid_t tid, bool isFirst = true) {
    uint64_t average = 0;
    if (info->count_ > 0) {
      if (!isFirst) {
        outPut << std::setw(42) << " ";
      }
      average = info->total_ / info->count_;
      outPut << "Stat=" << std::setw(30) << stat.getName();
      if (tid) {
        outPut << " TID=" << std::setw(6) << tid;
      }
      outPut << " total=" << std::setw(10) << info->total_ * 0.001
             << " avg=" << std::setw(10) << average * 0.001
             << " max=" << std::setw(10) << info->max_ * 0.001
             << " min=" << std::setw(10) << info->min_ * 0.001
             << " count=" << std::setw(10) << info->count_ << std::endl;
    }
  };
  if (!stat.getThreadInfo()) {
    StatInfo infoVarTmp;
    const_cast<Stat&>(stat).mergeThreadStat(infoVarTmp);
    showStat(&infoVarTmp, 0);
  } else {
    bool isFirst = true;
    for (auto& buf : stat.threadLocalBuf_) {
      showStat(buf.first, buf.second, isFirst);
      if (isFirst) isFirst = false;
    }
    showStat(&stat.destructStat_, 0);
  }

  return outPut;
}

BarrierStatPtr StatSet::getStat(uint16_t numConnThreads,
                                const std::string& name,
                                BarrierStatType bType) {
  {
    ReadLockGuard guard(lock_);
    auto it = barrierStatSet_.find(name);
    if (it != barrierStatSet_.end()) {
      return it->second;
    }
  }

  std::lock_guard<RWLock> guard(lock_);
  // test again with lock_guard
  auto it = barrierStatSet_.find(name);
  if (it != barrierStatSet_.end()) {
    return it->second;
  }

  BarrierStatPtr stat;
  if (bType == BARRIER_END) {
    stat = std::make_shared<BarrierEndStat>(numConnThreads, name);
  } else if (bType == BARRIER_DELTA) {
    stat = std::make_shared<BarrierDeltaStat>(numConnThreads, name);
  }
  auto ret = barrierStatSet_.insert(std::make_pair(name, stat));
  return ret.first->second;
}

void StatSet::printSegTimerStatus() {
  ReadLockGuard guard(lock_);
  LOG(INFO) << std::setiosflags(std::ios::left) << std::setfill(' ')
            << "======= StatSet: [" << name_ << "] status ======" << std::endl;
  for (auto& stat : statSet_) {
    LOG(INFO) << std::setiosflags(std::ios::left) << std::setfill(' ')
              << *(stat.second);
  }
}

void StatSet::printBarrierTimerStatus() {
  ReadLockGuard guard(lock_);
  // control barrierAbstact in runtime, so enable compliation
  LOG(INFO) << std::setiosflags(std::ios::left) << std::setfill(' ')
            << "======= BarrierStatSet status ======" << std::endl;
  for (auto& stat : barrierStatSet_) {
    LOG(INFO) << std::setiosflags(std::ios::left) << std::setfill(' ')
              << *(stat.second);
  }
}

void StatSet::printAllStatus() {
#ifndef PADDLE_DISABLE_TIMER
  printSegTimerStatus();
#endif
  printBarrierTimerStatus();
  LOG(INFO) << std::setiosflags(std::ios::left)
            << "--------------------------------------------------"
            << std::endl;
}

void StatSet::printStatus(const std::string& name) {
  ReadLockGuard guard(lock_);
  auto iter = statSet_.find(name);
  CHECK(iter != statSet_.end()) << name << " is not registed in " << name_;
  LOG(INFO) << *(iter->second);
}

void StatSet::reset(bool clearRawData) {
  ReadLockGuard guard(lock_);
  for (auto& stat : statSet_) {
    stat.second->reset();
  }
  // reset barrierStat
  for (auto& stat : barrierStatSet_) {
    stat.second->reset(clearRawData);
  }
}

void StatSet::setThreadInfo(const std::string& name, bool flag) {
  ReadLockGuard guard(lock_);
  auto iter = statSet_.find(name);
  CHECK(iter != statSet_.end()) << name << " is not registed in " << name_;
  iter->second->setThreadInfo(flag);
}

void StatSet::deleteStat(const std::string& name) {
  std::lock_guard<RWLock> guard(lock_);
  auto iter = statSet_.find(name);
  CHECK(iter != statSet_.end()) << name << " is not registed in " << name_;
  statSet_.erase(iter);
}

StatInfo::~StatInfo() {
  if (stat_) {
    std::lock_guard<std::mutex> guard(stat_->lock_);
    if (stat_->destructStat_.max_ < this->max_) {
      stat_->destructStat_.max_ = this->max_;
    }
    if (stat_->destructStat_.min_ > this->min_) {
      stat_->destructStat_.min_ = this->min_;
    }
    stat_->destructStat_.total_ += this->total_;
    stat_->destructStat_.count_ += this->count_;
    stat_->threadLocalBuf_.remove({this, getTID()});
  }
}

}  // namespace paddle