I have a client-side gui application which records data received periodically to a file.
There is one such file per component, and there can be ~400 components.
Limits:
I would have thought this would fit well within the soft limits, but it's hitting the limit:
$ grep "Limit\|files" /proc/7469/limits
Limit Soft Limit Hard Limit Units
Max open files 1024 4096 files
$ ls -l /proc/7469/fd | wc -l
1025
Showing the open file descriptors with lsof
shows that in fact there are 8 file descriptors opened per std::ofstream
, half of which seem to be related to gtk and Qt, and the other half directly from my app.
$ lsof | grep comp1.data
gui_app 7469 steve 746w REG 8,2 99024 1049235 /var/log/gui_app/comp1.data
gui_app 7469 7477 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
QXcbEvent 7469 7480 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
gdbus 7469 7481 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
gmain 7469 7482 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
gui_app 7469 7486 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
QDBusConn 7469 7487 steve 746w REG 8,2 99008 1049235 /var/log/gui_app/comp1.data
gui_app 7469 7490 steve 746w REG 8,2 99024 1049235 /var/log/gui_app/comp1.data
Design:
There is only a single reference to each ofstream
in the client code.
_ofs = std::make_unique<std::ofstream>(_filename, std::ofstream::out | std::ios_base::app | std::ios_base::binary);
I made the decision to keep each file descriptor open for the duration of the app, since I write to it every second, and made the (perhaps naive) assumption that opening/closing 400 file descriptors every second wasn't the best approach.
Questions:
- What are all the extra file descriptors for?
- Is there any way I can cut down on the number without closing/reopening the file descriptor every time I need it?
- Is opening the fd, writing, closing the fd for 400 different files, each once per second, feasible?