ehds commented on code in PR #3602:
URL: https://github.com/apache/brpc/pull/3602#discussion_r4267136486


##########
src/bvar/default_variables.cpp:
##########
@@ -107,25 +110,76 @@ static bool read_proc_status(ProcStat &stat) {
     }
     return true;
 #elif defined(OS_MACOSX)
-    // TODO(zhujiashun): get remaining state in MacOS.
-    memset(&stat, 0, sizeof(stat));
-    static pid_t pid = getpid();
-    std::ostringstream oss;
-    char cmdbuf[128];
-    snprintf(cmdbuf, sizeof(cmdbuf),
-            "ps -p %ld -o pid,ppid,pgid,sess"
-            ",tpgid,flags,pri,nice | tail -n1", (long)pid);
-    if (butil::read_command_output(oss, cmdbuf) != 0) {
-        LOG(ERROR) << "Fail to read stat";
-        return false;
+    const pid_t pid = getpid();
+    stat.pid = pid;
+    stat.ppid = getppid();
+    stat.pgrp = getpgrp();
+    const pid_t session = getsid(0);
+    stat.session = session >= 0 ? session : 0;
+    errno = 0;
+    const int nice = getpriority(PRIO_PROCESS, 0);
+    if (errno == 0)
+      stat.nice = nice;
+    struct proc_bsdinfo bsd = {};

Review Comment:
   stat is already value-initialized at the beginning of read_proc_status() via 
stat = ProcStat().



##########
src/bvar/default_variables.cpp:
##########
@@ -232,24 +286,19 @@ static bool read_proc_memory(ProcMemory &m) {
     return true;
 #elif defined(OS_MACOSX)
     // TODO(zhujiashun): get remaining memory info in MacOS.
-    memset(&m, 0, sizeof(m));
-    static pid_t pid = getpid();
-    static int64_t pagesize = getpagesize();
-    std::ostringstream oss;
-    char cmdbuf[128];
-    snprintf(cmdbuf, sizeof(cmdbuf), "ps -p %ld -o rss=,vsz=", (long)pid);
-    if (butil::read_command_output(oss, cmdbuf) != 0) {
-        LOG(ERROR) << "Fail to read memory state";
+    mach_task_basic_info_data_t info = {};
+    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
+    const kern_return_t result = task_info(
+        mach_task_self(), MACH_TASK_BASIC_INFO,
+        reinterpret_cast<task_info_t>(&info), &count);
+    if (result != KERN_SUCCESS) {
+        LOG_ONCE(WARNING) << "Cannot read own task memory, Mach error=" << 
result;
         return false;
     }

Review Comment:
    `m` has been zero-initialized at the beginning of ` m = ProcMemory()`, it 
won’t be partially assigned when return `false` here.



##########
src/bvar/default_variables.cpp:
##########
@@ -232,24 +286,19 @@ static bool read_proc_memory(ProcMemory &m) {
     return true;
 #elif defined(OS_MACOSX)
     // TODO(zhujiashun): get remaining memory info in MacOS.
-    memset(&m, 0, sizeof(m));
-    static pid_t pid = getpid();
-    static int64_t pagesize = getpagesize();
-    std::ostringstream oss;
-    char cmdbuf[128];
-    snprintf(cmdbuf, sizeof(cmdbuf), "ps -p %ld -o rss=,vsz=", (long)pid);
-    if (butil::read_command_output(oss, cmdbuf) != 0) {
-        LOG(ERROR) << "Fail to read memory state";
+    mach_task_basic_info_data_t info = {};
+    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
+    const kern_return_t result = task_info(
+        mach_task_self(), MACH_TASK_BASIC_INFO,
+        reinterpret_cast<task_info_t>(&info), &count);
+    if (result != KERN_SUCCESS) {
+        LOG_ONCE(WARNING) << "Cannot read own task memory, Mach error=" << 
result;

Review Comment:
   Added.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to