os_info.cc 3.6 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
#include <functional>
17
#include <sstream>
L
liutiexing 已提交
18 19
#include <thread>
#include <vector>
L
liutiexing 已提交
20 21 22 23 24 25
#if defined(__linux__)
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#elif defined(_MSC_VER)
#include <processthreadsapi.h>
26 27
#else
#include <unistd.h>
L
liutiexing 已提交
28
#endif
29
#include "paddle/fluid/framework/new_executor/workqueue/thread_data_registry.h"
L
liutiexing 已提交
30
#include "paddle/fluid/platform/macros.h"  // import DISABLE_COPY_AND_ASSIGN
31 32 33

namespace paddle {
namespace platform {
L
liutiexing 已提交
34
namespace internal {
35

36
using framework::ThreadDataRegistry;
L
liutiexing 已提交
37 38 39 40 41 42 43 44 45 46 47 48

class InternalThreadId {
 public:
  InternalThreadId();

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

 private:
  ThreadId id_;
};

InternalThreadId::InternalThreadId() {
L
liutiexing 已提交
49
  // C++ std tid
L
liutiexing 已提交
50
  id_.std_tid = std::hash<std::thread::id>()(std::this_thread::get_id());
L
liutiexing 已提交
51 52
// system tid
#if defined(__linux__)
L
liutiexing 已提交
53
  id_.sys_tid = static_cast<uint64_t>(syscall(SYS_gettid));
L
liutiexing 已提交
54
#elif defined(_MSC_VER)
L
liutiexing 已提交
55 56 57
  id_.sys_tid = static_cast<uint64_t>(::GetCurrentThreadId());
#else  // unsupported platforms, use std_tid
  id_.sys_tid = id_.std_tid;
L
liutiexing 已提交
58 59
#endif
  // cupti tid
60 61
  std::stringstream ss;
  ss << std::this_thread::get_id();
L
liutiexing 已提交
62 63 64 65 66 67 68 69 70 71
  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;
72 73
}

L
liutiexing 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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();
94
  }
L
liutiexing 已提交
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
  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
128 129 130 131
}

}  // namespace platform
}  // namespace paddle