0

I had a File IO interface that was implemented using stdlib fopen/fclose functions and it was working well until we had to change it for synchronous implementation for preventing data loss(one of the scenario). So I replaced all the stdlib file IO calls with system calls (open()/close() etc.). with these changes I started seeing the performance issues. I understand that some performance degradation is expected but I don't know how much impact I should expect and happily allow.

According to my understanding, this degradation in performance is majorly due to the fact that stdlib file IO interfaces provide caching in our process space for fast read writes; but still, I am not sure HOW caching is provided by stdlib file io interfaces.

Once I have this clarity I think I would be able to understand the performance degradation and hence would understand its quantum.

Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70

1 Answers1

1

I guess that the main reason of performance degradation is the increased number of system calls.

I'd recommend to explore the classic "The C programming language book by K&R". You'll need to find a chapter about UNIX I/O (probably, chapter 7). There are examples of implementations: "fopen", "fread", etc

After that I'd recommend to explore glibc implementation .

dshil
  • 381
  • 2
  • 10
  • I am referring https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/stdio/fread.c file for understanding the behaviour but I am no curious that even if I use no buffer flag in stdlib FIO then also during fflush it is not writing to discs instead only doing write and not fsync().. See this: https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/stdio/fflush.c – Bhupesh Pant Feb 26 '18 at 14:04
  • I got the book and I will take a look, btw its chapter 8 :). Also can you please point me out to the system call functions open/write/read definition? – Bhupesh Pant Feb 26 '18 at 14:05
  • Well, you can look at my earlier answers related to "system calls": [1](https://stackoverflow.com/questions/11905934/how-to-switch-from-user-mode-to-kernel-mode/45750079#45750079), [2](https://stackoverflow.com/questions/23846097/difference-between-user-vs-kernel-system-call/45728287#45728287). Also you should see the corresponding man page, e.g. `man 2 open`. – dshil Feb 26 '18 at 16:36
  • 1
    @BhupeshPant, "if I use no buffer flag in stdlib FIO". It only means that you change the buffering strategy from, e.g. line-buffered strategy to unbuffered. `write(2), read(2)` will be called for each symbol during `printf, putchar, getchar, etc` – dshil Feb 28 '18 at 02:41