+#ifdef IOSTATS
+static int process_parse_io(struct process *process)
+{
+ static const char *read_bytes_str="read_bytes:";
+ static const char *write_bytes_str="write_bytes:";
+
+ char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN];
+ int ps;
+ int rc;
+ char *pos, *endpos;
+ unsigned long long read_bytes, write_bytes;
+
+ snprintf(filename, sizeof(filename), PROCFS_TEMPLATE_IO, process->pid);
+
+ ps = open(filename, O_RDONLY);
+ if (ps < 0) {
+ /* The process must have finished in the last few jiffies!
+ * Or, the kernel doesn't support I/O accounting.
+ */
+ return 1;
+ }
+
+ rc = read(ps, line, sizeof(line));
+ close(ps);
+ if (rc < 0) {
+ return 1;
+ }
+
+ pos = strstr(line, read_bytes_str);
+ if (pos == NULL) {
+ /* these should not happen (unless the format of the file changes) */
+ return 1;
+ }
+ pos += strlen(read_bytes_str);
+ process->read_bytes = strtoull(pos, &endpos, 10);
+ if (endpos == pos) {
+ return 1;
+ }
+
+ pos = strstr(line, write_bytes_str);
+ if (pos == NULL) {
+ return 1;
+ }
+ pos += strlen(write_bytes_str);
+ process->write_bytes = strtoull(pos, &endpos, 10);
+ if (endpos == pos) {
+ return 1;
+ }
+
+ if (process->previous_read_bytes == ULLONG_MAX) {
+ process->previous_read_bytes = process->read_bytes;
+ }
+ if (process->previous_write_bytes == ULLONG_MAX) {
+ process->previous_write_bytes = process->write_bytes;
+ }
+
+ /* store the difference of the byte counts */
+ read_bytes = process->read_bytes - process->previous_read_bytes;
+ write_bytes = process->write_bytes - process->previous_write_bytes;
+
+ /* backup the counts for next time around */
+ process->previous_read_bytes = process->read_bytes;
+ process->previous_write_bytes = process->write_bytes;
+
+ /* store only the difference here... */
+ process->read_bytes = read_bytes;
+ process->write_bytes = write_bytes;
+
+ return 0;
+}
+#endif
+