Reputation: 3716
I'm writing a C++ application that is going to be doing performance monitoring. I was thinking of just wrapping calls to iostat inside of an exe. (There's technical reasons why it must be done this way, I'd rather not get into that).
My question is, if I wrap calls to iostat inside of an exe, is that incredibly stupid? (In terms of performance)
example:
while (true) {
every 200ms
make system call to iostat, store results in my_data_structure
do some math on my_data_structure
}
stupid? Or is there a better way?
Edit: nevermind, I can get what I need by reading /proc/diskstats and other files.
Upvotes: 1
Views: 365
Reputation: 18572
My question is, if I wrap calls to iostat inside of an exe, is that incredibly stupid?
I wouldn't call it "incredibly stupid", but it certainly won't get you Nobel Prize. I've seen much worse. There are two objectionable things with this idea:
You say that there are reasons that you don't want to get into why you need to be using a executable (compiled from C/C++). I have absolutely no idea what those reasons could be because if you want to be doing what you want to do (calling iostat), you need to invoke the system's shell interpreter (usually "bash") from your C++ program. So, what's the difference between running a program (C++) that invokes bash to run a command (iostat), and invoking bash to run a script that runs a command (iostat). There is nothing that I can think of that would make the one possible and the other not. In any case, this is not the end of the world, you can follow these instructions about invoking a command and retrieving its output using popen().
The second objection is in the fact that iostat is very simple (as seen here or here). The iostat program really does nothing more that read data from standard system monitoring pseudo-files in folders like /proc
, as listed in the manual:
/proc/stat contains system statistics.
/proc/uptime contains system uptime.
/proc/diskstats contains disks statistics.
/sys contains statistics for block devices.
/proc/self/mountstats contains statistics for network filesystems.
/dev/disk contains persistent device names.
This means that depending on your specific problem, it is very likely that you could simply read off what you need from those files directly (or, grab some code from iostat's source code to do so). The point of iostat is to read data from those files and make them "human-readable" and formatted as requested. I doubt that the string-parsing that you will have to do within your program to parse the output of iostat will be much simpler than the parsing necessary to retrieve the data you want from those system files directly.
Nevertheless, doing what you propose might not be so terrible after all.
Upvotes: 3