sryanyuan commented on code in PR #3379:
URL: https://github.com/apache/kvrocks/pull/3379#discussion_r2870171430
##########
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:
Thanks for the suggestion. Agreed - originally used -1.0 to explicitly flag
errors, but given:
* Monitoring context makes 0.0 values statistically rare
* Any 0.0 result can serve as valid error indicator
* Simpler to treat 0 as "no measurable CPU time"
Will change to return 0.0 for:
* Invalid thread_id
* System call failures
This maintains functionality while simplifying error handling. Good catch!
--
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]