]> granicus.if.org Git - sysstat/commitdiff
Fix #154: pidstat: Don't stop if /proc/#/schedstat files not found
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 23 Jun 2017 10:00:01 +0000 (12:00 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 23 Jun 2017 10:00:01 +0000 (12:00 +0200)
Commit a41b24a added %wait field to pidstat CPU statistics. This field
is calculated for each task using the contents of /proc/#/schedstat
file. Yet pidstat should not stop if this file cannot be found: It
should display the other CPU statistics as usual and display 0.00 for
this particular field. This is what this patch does.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
pidstat.c

index 990944efec513263b920f32aaa1a9be0f08d0717..876635c69de705333d70fbf7521b495c6cb0b07d 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -431,10 +431,10 @@ int read_proc_pid_stat(unsigned int pid, struct pid_stats *pst,
 int read_proc_pid_sched(unsigned int pid, struct pid_stats *pst,
                       unsigned int *thread_nr, unsigned int tgid)
 {
-       int fd, sz, rc;
+       int fd, sz, rc = 0;
        char filename[128];
        static char buffer[1024 + 1];
-       unsigned long long wtime;
+       unsigned long long wtime = 0;
 
        if (tgid) {
                sprintf(filename, TASK_SCHED, tgid, pid);
@@ -443,28 +443,26 @@ int read_proc_pid_sched(unsigned int pid, struct pid_stats *pst,
                sprintf(filename, PID_SCHED, pid);
        }
 
-       if ((fd = open(filename, O_RDONLY)) < 0)
-               /* No such process */
-               return 1;
+       if ((fd = open(filename, O_RDONLY)) >= 0) {
+               /* schedstat file found for process */
+               sz = read(fd, buffer, 1024);
+               close(fd);
+               if (sz > 0) {
+                       buffer[sz] = '\0';
 
-       sz = read(fd, buffer, 1024);
-       close(fd);
-       if (sz <= 0)
-               return 1;
-       buffer[sz] = '\0';
-
-       rc = sscanf(buffer,
-                   "%*u %llu %*d\n",
-                   &wtime);
-
-       if (rc < 1)
-               return 1;
+                       rc = sscanf(buffer, "%*u %llu %*d\n", &wtime);
+               }
+       }
 
        /* Convert ns to jiffies */
        pst->wtime = wtime * HZ / 1000000000;
 
        pst->pid = pid;
        pst->tgid = tgid;
+
+       if (rc < 1)
+               return 1;
+
        return 0;
 }
 
@@ -785,8 +783,11 @@ int read_pid_stats(unsigned int pid, struct pid_stats *pst,
        if (read_proc_pid_stat(pid, pst, thread_nr, tgid))
                return 1;
 
-       if (read_proc_pid_sched(pid, pst, thread_nr, tgid))
-               return 1;
+       /*
+        * No need to test the return code here: Not finding
+        * the schedstat files shouldn't make pidstat stop.
+        */
+       read_proc_pid_sched(pid, pst, thread_nr, tgid);
 
        if (DISPLAY_CMDLINE(pidflag)) {
                if (read_proc_pid_cmdline(pid, pst, tgid))