os_info.cc 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 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. */

#include "paddle/fluid/platform/os_info.h"
L
liutiexing 已提交
16 17
#include <functional>
#include <mutex>
18
#include <sstream>
L
liutiexing 已提交
19 20
#include <thread>
#include <vector>
L
liutiexing 已提交
21 22 23 24 25 26 27
#if defined(__linux__)
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#elif defined(_MSC_VER)
#include <processthreadsapi.h>
#endif
L
liutiexing 已提交
28
#include "paddle/fluid/platform/macros.h"  // import DISABLE_COPY_AND_ASSIGN
29 30 31

namespace paddle {
namespace platform {
L
liutiexing 已提交
32
namespace internal {
33

L
liutiexing 已提交
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
static uint64_t main_tid =
    std::hash<std::thread::id>()(std::this_thread::get_id());

template <typename T>
class ThreadDataRegistry {
  class ThreadDataHolder;

 public:
  // Singleton
  static ThreadDataRegistry& GetInstance() {
    static ThreadDataRegistry instance;
    return instance;
  }

  const T& GetCurrentThreadData() { return CurrentThreadData(); }

  void SetCurrentThreadData(const T& val) {
    std::lock_guard<std::mutex> lock(lock_);
    CurrentThreadData() = val;
  }

  // Returns current snapshot of all threads. Make sure there is no thread
  // create/destory when using it.
  template <typename = std::enable_if_t<std::is_copy_constructible<T>::value>>
  std::unordered_map<uint64_t, T> GetAllThreadDataByValue() {
    std::unordered_map<uint64_t, T> data_copy;
    std::lock_guard<std::mutex> lock(lock_);
    data_copy.reserve(tid_map_.size());
    for (auto& kv : tid_map_) {
      data_copy.emplace(kv.first, kv.second->GetData());
    }
    return std::move(data_copy);
  }

  void RegisterData(uint64_t tid, ThreadDataHolder* tls_obj) {
    std::lock_guard<std::mutex> lock(lock_);
    tid_map_[tid] = tls_obj;
  }

  void UnregisterData(uint64_t tid) {
    if (tid == main_tid) {
      return;
    }
    std::lock_guard<std::mutex> lock(lock_);
    tid_map_.erase(tid);
  }

 private:
  class ThreadDataHolder {
   public:
    ThreadDataHolder() {
      tid_ = std::hash<std::thread::id>()(std::this_thread::get_id());
      ThreadDataRegistry::GetInstance().RegisterData(tid_, this);
    }

    ~ThreadDataHolder() {
      ThreadDataRegistry::GetInstance().UnregisterData(tid_);
    }

    T& GetData() { return data_; }

   private:
    uint64_t tid_;
    T data_;
  };

  ThreadDataRegistry() = default;

  DISABLE_COPY_AND_ASSIGN(ThreadDataRegistry);

  T& CurrentThreadData() {
    static thread_local ThreadDataHolder thread_data;
    return thread_data.GetData();
  }

  std::mutex lock_;
  std::unordered_map<uint64_t, ThreadDataHolder*> tid_map_;  // not owned
};

class InternalThreadId {
 public:
  InternalThreadId();

  const ThreadId& GetTid() const { return id_; }

 private:
  ThreadId id_;
};

InternalThreadId::InternalThreadId() {
L
liutiexing 已提交
124
  // C++ std tid
L
liutiexing 已提交
125
  id_.std_tid = std::hash<std::thread::id>()(std::this_thread::get_id());
L
liutiexing 已提交
126 127
// system tid
#if defined(__linux__)
L
liutiexing 已提交
128
  id_.sys_tid = static_cast<uint64_t>(syscall(SYS_gettid));
L
liutiexing 已提交
129
#elif defined(_MSC_VER)
L
liutiexing 已提交
130 131 132
  id_.sys_tid = static_cast<uint64_t>(::GetCurrentThreadId());
#else  // unsupported platforms, use std_tid
  id_.sys_tid = id_.std_tid;
L
liutiexing 已提交
133 134
#endif
  // cupti tid
135 136
  std::stringstream ss;
  ss << std::this_thread::get_id();
L
liutiexing 已提交
137 138 139 140 141 142 143 144 145 146
  id_.cupti_tid = static_cast<uint32_t>(std::stoull(ss.str()));
}

}  // namespace internal

uint64_t GetCurrentThreadSysId() {
  return internal::ThreadDataRegistry<internal::InternalThreadId>::GetInstance()
      .GetCurrentThreadData()
      .GetTid()
      .sys_tid;
147 148
}

L
liutiexing 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
uint64_t GetCurrentThreadStdId() {
  return internal::ThreadDataRegistry<internal::InternalThreadId>::GetInstance()
      .GetCurrentThreadData()
      .GetTid()
      .std_tid;
}

ThreadId GetCurrentThreadId() {
  return internal::ThreadDataRegistry<internal::InternalThreadId>::GetInstance()
      .GetCurrentThreadData()
      .GetTid();
}

std::unordered_map<uint64_t, ThreadId> GetAllThreadIds() {
  auto tids =
      internal::ThreadDataRegistry<internal::InternalThreadId>::GetInstance()
          .GetAllThreadDataByValue();
  std::unordered_map<uint64_t, ThreadId> res;
  for (const auto& kv : tids) {
    res[kv.first] = kv.second.GetTid();
169
  }
L
liutiexing 已提交
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
  return res;
}

static constexpr const char* kDefaultThreadName = "unset";

std::string GetCurrentThreadName() {
  const auto& thread_name =
      internal::ThreadDataRegistry<std::string>::GetInstance()
          .GetCurrentThreadData();
  return thread_name.empty() ? kDefaultThreadName : thread_name;
}

std::unordered_map<uint64_t, std::string> GetAllThreadNames() {
  return internal::ThreadDataRegistry<std::string>::GetInstance()
      .GetAllThreadDataByValue();
}

bool SetCurrentThreadName(const std::string& name) {
  auto& instance = internal::ThreadDataRegistry<std::string>::GetInstance();
  const auto& cur_name = instance.GetCurrentThreadData();
  if (!cur_name.empty() || cur_name == kDefaultThreadName) {
    return false;
  }
  instance.SetCurrentThreadData(name);
  return true;
}

uint32_t GetProcessId() {
#if defined(_MSC_VER)
  return static_cast<uint32_t>(GetCurrentProcessId());
#else
  return static_cast<uint32_t>(getpid());
#endif
203 204 205 206
}

}  // namespace platform
}  // namespace paddle