PragmaTwice commented on code in PR #3379:
URL: https://github.com/apache/kvrocks/pull/3379#discussion_r2868261992
##########
src/common/thread_util.cc:
##########
@@ -33,6 +38,43 @@ void ThreadSetName(const char *name) {
#endif
}
+#ifdef __APPLE__
+double ThreadGetCPUTime(std::thread::native_handle_type thread_id) {
+ if (!thread_id) {
+ return -1.0;
+ }
+
+ mach_port_t mach_thread = pthread_mach_thread_np(thread_id);
+
+ thread_basic_info_data_t info;
+ mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
+
+ if (thread_info(mach_thread, THREAD_BASIC_INFO, (thread_info_t)&info,
&count) != KERN_SUCCESS) {
+ return -1.0;
+ }
+
+ return (static_cast<double>(info.user_time.seconds) +
static_cast<double>(info.user_time.microseconds) / 1e6) +
+ (static_cast<double>(info.system_time.seconds) +
static_cast<double>(info.system_time.microseconds) / 1e6);
+}
+#else
+double ThreadGetCPUTime(std::thread::native_handle_type thread_id) {
+ if (!thread_id) {
+ return -1.0;
Review Comment:
why -1.0 instead of 0?
##########
src/server/server.cc:
##########
@@ -1402,10 +1402,18 @@ Server::InfoEntries Server::GetCpuInfo() { //
NOLINT(readability-convert-member
rusage self_ru;
getrusage(RUSAGE_SELF, &self_ru);
- entries.emplace_back("used_cpu_sys",
static_cast<float>(self_ru.ru_stime.tv_sec) +
-
static_cast<float>(self_ru.ru_stime.tv_usec / 1000000));
+ entries.emplace_back(
+ "used_cpu_sys", static_cast<float>(self_ru.ru_stime.tv_sec) +
static_cast<float>(self_ru.ru_stime.tv_usec) / 1e6);
entries.emplace_back("used_cpu_user",
static_cast<float>(self_ru.ru_utime.tv_sec) +
-
static_cast<float>(self_ru.ru_utime.tv_usec / 1000000));
+
static_cast<float>(self_ru.ru_utime.tv_usec) / 1e6);
+
+ std::vector<double> thread_cpu_times(worker_threads_.size());
+ for (std::size_t i{0}; i < worker_threads_.size(); ++i) {
+ thread_cpu_times[i] =
util::ThreadGetCPUTime(worker_threads_[i]->GetNativeHandle());
+ }
+ entries.emplace_back(
+ "worker_cpu_time",
Review Comment:
```suggestion
fmt::format("worker_cpu_time#{}", worker_threads_[i]->GetId()),
```
something like that?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]