c++sysinfo

Is gutil's sysinfo.cc missing a closing bracket when calculating num_cpus?


I have gutil's sysinfo.cc file, indentical to this one: https://github.com/cloudera/kudu/blob/master/src/kudu/gutil/sysinfo.cc#L248 I would like to know if the InitializeSystemInfo() function works properly for Linux, given I'm not too familiar with C++.

I can't find a closing bracket for this do statement on line 248, so would it calculate numcpus properly still?

 do {   // we'll exit when the last read didn't read anything
    // Move the next line to the beginning of the buffer
    const int oldlinelen = strlen(line);
    if (sizeof(line) == oldlinelen + 1)    // oldlinelen took up entire line
      line[0] = '\0';
    else                                   // still other lines left to save
      memmove(line, line + oldlinelen+1, sizeof(line) - (oldlinelen+1));
    // Terminate the new line, reading more if we can't find the newline
    char* newline = strchr(line, '\n');
    if (newline == NULL) {
      const int linelen = strlen(line);
      const int bytes_to_read = sizeof(line)-1 - linelen;
      CHECK_GT(bytes_to_read, 0);  // because the memmove recovered >=1 bytes
      chars_read = read(fd, line + linelen, bytes_to_read);
      line[linelen + chars_read] = '\0';
      newline = strchr(line, '\n');
    }
    if (newline != NULL)
      *newline = '\0';

Solution

  • The closing brace is in line 305.

    The programming style in this file is hard to read since the function is several screens long and uses several #ifdef branches. It is necessary though. This source file hides the operating-system-specific parts from the rest of the code, therefore it may look this complicated.

    The other code can just access the cpuinfo_cycles_per_second variable, which makes a really simple API.