9
  1. Is it safe to use lseek(fd,0) and then read(fd,buf) for /proc/stat file instead of reopening it to get updated contents of this file next time?
  2. And what does the mmap() call after opening this file really do (see below)?

The problem I am encountering is that top reports way too low CPU usage (10% vs 100% for software interrupts). The strace tool indicates that top does not reopen this file but instead lseeks to beginning and reads it once again. And somehow the contents which are read from this file next time does not match with what I would get when I run cat for /proc/stat file alone.

Also If I run top and cat /proc/stat in a loop at the same time then top starts to report correct CPU-Usage.

One more difference I spot is that top uses mmap() call right after opening the /proc/stat file, while cat does not do that. I am not sure if this also could be related with my problem (because filesdes=-1 here):

mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7b37649000

I am using Ubuntu 10.04.1 Desktop edition with 2.6.32-27-server image. The CPU is Intel Q6600.

2 Answers 2

4

It's very interesting what you ask... I start checking in my machine but I don't see differences between cat /proc/stat and execute top. Anyway, I'm at work and I'm not totally 'free' to make tests.

The way you describe to 'refresh' the opened file to read new data is correct... in case of a [f|l]seek() call to the end and then to the beginning of the file will update the EOF and new data will be read.

I don't think that the mmap() call will cause the problem you mentioned, it could make the read faster, but nothing else (I'm not 100% sure).

I would suggest you to make a small app in C that open /proc/stat, read it, seek it and read it again to see how it's updated, also if you have some stress test to do could be useful.

Now, answering your real questions:

  1. Yes, AFAIK it's sure because you will be 'waiting' for new data on the file and it should be better than open and close the file all the time.

  2. It maps a file to the process address space, here are some info and examples:

http://www.gnu.org/s/libc/manual/html_node/Memory_002dmapped-I_002fO.html http://www.linuxquestions.org/questions/programming-9/mmap-tutorial-c-c-511265/

1
  • That is true about lseek. It seems that htop is implemented to reopen /proc/stat file each time before reading it, but in the meantime htop also reports invalid cpu-usage. So we can probably rule out lseek(). Jan 26, 2011 at 17:56
0

As explained in the answers to this question, it is not possible to memory map a file from the /proc virtual file system. You would get an EIO error from mmap().
The optimal way to avoid a systematic open/read/close, is to simply open the file once and seek to the beginning to read its content again. Updated data are returned.
Here is an example program reading /proc/meminfo every second:

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define MEMINFO_FNAME "/proc/meminfo"

int read_file(int fd)
{
  int rc;
  char buffer[512];
  off_t off;

  off = lseek(fd, 0, SEEK_SET);
  if (off == (off_t)-1) {
    fprintf(stderr, "lseek(%s): '%m' (%d)\n", MEMINFO_FNAME, errno);
    return -1;
  }

  do {

    rc = read(fd, buffer, sizeof(buffer));
    if (rc > 0) {
      buffer[rc] = '\0';
      printf("%s", buffer);
    }
    
  } while(rc > 0);

  return rc;
}

int main(void)
{
  int fd;
  int rc;

  // Open the file
  fd = open(MEMINFO_FNAME, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open(%s): '%m' (%d)\n", MEMINFO_FNAME, errno);
    return 1;
  }

  do {
    printf("==========\n");
    rc = read_file(fd);
    sleep(1);
  } while(rc == 0);

  close(fd);

  return 0;
}

The execution shows varying values after each reads in the file:

$ gcc meminfo.c -o meminfo
$ ./meminfo
==========
MemTotal:       131927796 kB
MemFree:         9305304 kB
MemAvailable:   125498348 kB
Buffers:        53300112 kB
[...]
==========
MemTotal:       131927796 kB
MemFree:         9323984 kB
MemAvailable:   125517048 kB
Buffers:        53300112 kB
[...]
==========
MemTotal:       131927796 kB
MemFree:         9324236 kB
MemAvailable:   125517300 kB
Buffers:        53300120 kB
[...]

N.B.: Depending on the average size of the file which is monitored, the size of the read buffer can be adapted to have only one call to read().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.