Fix CPU count calculation for one-CPU systems
authorAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Tue, 4 Sep 2018 15:31:44 +0000 (18:31 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Tue, 4 Sep 2018 19:28:19 +0000 (22:28 +0300)
proc_stat.c

index 3a6a366..1fc53f2 100644 (file)
@@ -80,15 +80,29 @@ static void write_proc_stat(FILE *stat_file_out, int pid, int timeout_usec, int
                uninit_write_proc_stat_context(&c);
                return;
        }
-       int tmp, ncpu;
-       if (fscanf(c.cpuf, "%d-%d\n", &tmp, &ncpu) < 2) {
-               log_system_error("fscanf(cpuf) failed");
-               uninit_write_proc_stat_context(&c);
-               return;
+       unsigned int tmp = -1;
+       unsigned int ncpu = -1;
+       errno = 0;
+
+       if (fscanf(c.cpuf, "%u-%u\n", &tmp, &ncpu) == 2) {
+               ncpu = ncpu - tmp + 1;
+       } else {
+               rewind(c.cpuf);
+               if (fscanf(c.cpuf, "%u\n", &ncpu) == 1) {
+                       ncpu = ncpu + 1;
+               } else {
+                       if (errno) {
+                               log_system_error("fscanf(cpuf) failed");
+                       } else {
+                               log_error("fscanf(cpuf) parsing error");
+                       }
+                       uninit_write_proc_stat_context(&c);
+                       return;
+               }
        }
+
        fclose(c.cpuf);
        c.cpuf = NULL;
-       ncpu = ncpu - tmp + 1;
 
        long psize = sysconf(_SC_PAGESIZE);
        if (fprintf(stat_file_out, "psize %ld ncpu %d\n", psize, ncpu) < 0) {