• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <sys/vfs.h>
30 #include <sys/statvfs.h>
31 #include <sys/mman.h>
32 #include <inttypes.h>
33 #include "ioshark.h"
34 #include "ioshark_bench.h"
35 #define _BSD_SOURCE
36 #include <endian.h>
37 
38 extern char *progname;
39 extern int verbose, summary_mode;
40 
41 void *
files_db_create_handle(void)42 files_db_create_handle(void)
43 {
44 	struct files_db_handle *h;
45 	int i;
46 
47 	h = malloc(sizeof(struct files_db_handle));
48 	for (i = 0 ; i < FILE_DB_HASHSIZE ; i++)
49 		h->files_db_buckets[i] = NULL;
50 	return h;
51 }
52 
files_db_lookup_byfileno(void * handle,int fileno)53 void *files_db_lookup_byfileno(void *handle, int fileno)
54 {
55 	u_int32_t	hash;
56 	struct files_db_handle *h = (struct files_db_handle *)handle;
57 	struct files_db_s *db_node;
58 
59 	hash = fileno % FILE_DB_HASHSIZE;
60 	db_node = h->files_db_buckets[hash];
61 	while (db_node != NULL) {
62 		if (db_node->fileno == fileno)
63 			break;
64 		db_node = db_node->next;
65 	}
66 	return db_node;
67 }
68 
files_db_add_byfileno(void * handle,int fileno,int readonly)69 void *files_db_add_byfileno(void *handle, int fileno, int readonly)
70 {
71 	u_int32_t	hash = fileno % FILE_DB_HASHSIZE;
72 	struct files_db_handle *h = (struct files_db_handle *)handle;
73 	struct files_db_s *db_node;
74 
75 	db_node = (struct files_db_s *)
76 		files_db_lookup_byfileno(handle, fileno);
77 	if (db_node == NULL) {
78 		db_node = malloc(sizeof(struct files_db_s));
79 		db_node->fileno = fileno;
80 		db_node->filename = NULL;
81 		db_node->readonly = readonly;
82 		db_node->size = 0;
83 		db_node->fd = -1;
84 		db_node->next = h->files_db_buckets[hash];
85 		h->files_db_buckets[hash] = db_node;
86 	} else {
87 		fprintf(stderr,
88 			"%s: Node to be added already exists fileno = %d\n\n",
89 			__func__, fileno);
90 		exit(EXIT_FAILURE);
91 	}
92 	return db_node;
93 }
94 
95 void
files_db_fsync_discard_files(void * handle)96 files_db_fsync_discard_files(void *handle)
97 {
98 	struct files_db_handle *h = (struct files_db_handle *)handle;
99 	struct files_db_s *db_node;
100 	int i;
101 
102 	for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
103 		db_node = h->files_db_buckets[i];
104 		while (db_node != NULL) {
105 			int do_close = 0;
106 
107 			if (db_node->fd == -1) {
108 				int fd;
109 				int openflags;
110 
111 				/*n
112 				 * File was closed, let's open it so we can
113 				 * fsync and fadvise(DONTNEED) it.
114 				 */
115 				do_close = 1;
116 				if (files_db_readonly(db_node))
117 					openflags = O_RDONLY;
118 				else
119 					openflags = O_RDWR;
120 				fd = open(files_db_get_filename(db_node),
121 					  openflags);
122 				if (fd < 0) {
123 					fprintf(stderr,
124 						"%s: open(%s %x) error %d\n",
125 						progname, db_node->filename,
126 						openflags,
127 						errno);
128 					exit(EXIT_FAILURE);
129 				}
130 				db_node->fd = fd;
131 			}
132 			if (!db_node->readonly && fsync(db_node->fd) < 0) {
133 				fprintf(stderr, "%s: Cannot fsync %s\n",
134 					__func__, db_node->filename);
135 				exit(1);
136 			}
137 			if (posix_fadvise(db_node->fd, 0, 0,
138 					  POSIX_FADV_DONTNEED) < 0) {
139 				fprintf(stderr,
140 					"%s: Cannot fadvise(DONTNEED) %s\n",
141 					__func__, db_node->filename);
142 				exit(1);
143 			}
144 			if (do_close) {
145 				close(db_node->fd);
146 				db_node->fd = -1;
147 			}
148 			db_node = db_node->next;
149 		}
150 	}
151 }
152 
153 void
files_db_update_fd(void * node,int fd)154 files_db_update_fd(void *node, int fd)
155 {
156 	struct files_db_s *db_node = (struct files_db_s *)node;
157 
158 	db_node->fd = fd;
159 }
160 
161 void
files_db_close_fd(void * node)162 files_db_close_fd(void *node)
163 {
164 	struct files_db_s *db_node = (struct files_db_s *)node;
165 
166 	if (db_node->fd != -1)
167 		close(db_node->fd);
168 	db_node->fd = -1;
169 }
170 
171 void
files_db_close_files(void * handle)172 files_db_close_files(void *handle)
173 {
174 	struct files_db_handle *h = (struct files_db_handle *)handle;
175 	struct files_db_s *db_node;
176 	int i;
177 
178 	for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
179 		db_node = h->files_db_buckets[i];
180 		while (db_node != NULL) {
181 			if ((db_node->fd != -1) && close(db_node->fd) < 0) {
182 				fprintf(stderr, "%s: Cannot close %s\n",
183 					__func__, db_node->filename);
184 				exit(1);
185 			}
186 			db_node->fd = -1;
187 			db_node = db_node->next;
188 		}
189 	}
190 }
191 
192 void
files_db_unlink_files(void * handle)193 files_db_unlink_files(void *handle)
194 {
195 	struct files_db_handle *h = (struct files_db_handle *)handle;
196 	struct files_db_s *db_node;
197 	int i;
198 
199 	for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
200 		db_node = h->files_db_buckets[i];
201 		while (db_node != NULL) {
202 			if ((db_node->fd != -1) && close(db_node->fd) < 0) {
203 				fprintf(stderr, "%s: Cannot close %s\n",
204 					__func__, db_node->filename);
205 				exit(1);
206 			}
207 			db_node->fd = -1;
208 			if (is_readonly_mount(db_node->filename, db_node->size) == 0) {
209 				if (unlink(db_node->filename) < 0) {
210 					fprintf(stderr, "%s: Cannot unlink %s:%s\n",
211 						__func__, db_node->filename,
212 						strerror(errno));
213 					exit(EXIT_FAILURE);
214 				}
215 			}
216 			db_node = db_node->next;
217 		}
218 	}
219 }
220 
221 void
files_db_free_memory(void * handle)222 files_db_free_memory(void *handle)
223 {
224 	struct files_db_handle *h = (struct files_db_handle *)handle;
225 	struct files_db_s *db_node, *tmp;
226 	int i;
227 
228 	for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
229 		db_node = h->files_db_buckets[i];
230 		while (db_node != NULL) {
231 			tmp = db_node;
232 			db_node = db_node->next;
233 			free(tmp->filename);
234 			free(tmp);
235 		}
236 	}
237 	free(h);
238 }
239 
240 char *
get_buf(char ** buf,int * buflen,int len,int do_fill)241 get_buf(char **buf, int *buflen, int len, int do_fill __attribute__((unused)))
242 {
243 	if (len == 0 && *buf == NULL) {
244 		/*
245 		 * If we ever get a zero len
246 		 * request, start with MINBUFLEN
247 		 */
248 		if (*buf == NULL)
249 			len = MINBUFLEN / 2;
250 	}
251 	if (*buflen < len) {
252 		*buflen = MAX(MINBUFLEN, len * 2);
253 		if (*buf)
254 			free(*buf);
255 		*buf = malloc(*buflen);
256 		if (do_fill) {
257 			u_int32_t *s;
258 			int count;
259 
260 			s = (u_int32_t *)*buf;
261 			count = *buflen / sizeof(u_int32_t);
262 			while (count > 0) {
263 				*s++ = rand();
264 				count--;
265 			}
266 		}
267 	}
268 	assert(*buf != NULL);
269 	return *buf;
270 }
271 
272 void
create_file(char * path,size_t size,struct rw_bytes_s * rw_bytes)273 create_file(char *path, size_t size, struct rw_bytes_s *rw_bytes)
274 {
275 	int fd, n;
276 	char *buf = NULL;
277 	int buflen = 0;
278 
279 	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
280 	if (fd < 0) {
281 		fprintf(stderr, "%s Cannot create file %s, error = %d\n",
282 			progname, path, errno);
283 		exit(EXIT_FAILURE);
284 	}
285 	while (size > 0) {
286 		n = MIN(size, MINBUFLEN);
287 		buf = get_buf(&buf, &buflen, n, 1);
288 		if (write(fd, buf, n) < n) {
289 			fprintf(stderr,
290 				"%s Cannot write file %s, error = %d\n",
291 				progname, path, errno);
292 			free(buf);
293 			exit(EXIT_FAILURE);
294 		}
295 		rw_bytes->bytes_written += n;
296 		size -= n;
297 	}
298 	free(buf);
299 	if (fsync(fd) < 0) {
300 		fprintf(stderr, "%s Cannot fsync file %s, error = %d\n",
301 			progname, path, errno);
302 		exit(EXIT_FAILURE);
303 	}
304 	if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) < 0) {
305 		fprintf(stderr,
306 			"%s Cannot fadvise(DONTNEED) file %s, error = %d\n",
307 			progname, path, errno);
308 		exit(EXIT_FAILURE);
309 	}
310 	close(fd);
311 }
312 
313 void
print_op_stats(u_int64_t * op_counts)314 print_op_stats(u_int64_t *op_counts)
315 {
316 	int i;
317 	extern char *IO_op[];
318 
319 	printf("IO Operation counts :\n");
320 	for (i = IOSHARK_LSEEK ; i < IOSHARK_MAX_FILE_OP ; i++) {
321 		printf("%s: %ju\n",
322 		       IO_op[i], op_counts[i]);
323 	}
324 }
325 
326 void
print_bytes(char * desc,struct rw_bytes_s * rw_bytes)327 print_bytes(char *desc, struct rw_bytes_s *rw_bytes)
328 {
329 	if (!summary_mode)
330 		printf("%s: Reads = %dMB, Writes = %dMB\n",
331 		       desc,
332 		       (int)(rw_bytes->bytes_read / (1024 * 1024)),
333 		       (int)(rw_bytes->bytes_written / (1024 * 1024)));
334 	else
335 		printf("%d %d ",
336 		       (int)(rw_bytes->bytes_read / (1024 * 1024)),
337 		       (int)(rw_bytes->bytes_written / (1024 * 1024)));
338 }
339 
340 struct cpu_disk_util_stats {
341 	/* CPU util */
342 	u_int64_t user_cpu_ticks;
343 	u_int64_t nice_cpu_ticks;
344 	u_int64_t system_cpu_ticks;
345 	u_int64_t idle_cpu_ticks;
346 	u_int64_t iowait_cpu_ticks;
347 	u_int64_t hardirq_cpu_ticks;
348 	u_int64_t softirq_cpu_ticks;
349 	/* disk util */
350 	unsigned long long uptime;
351 	unsigned int tot_ticks;
352 	unsigned long rd_ios;
353 	unsigned long wr_ios;
354 	unsigned long rd_sec;
355 	unsigned long wr_sec;
356 };
357 
358 static struct cpu_disk_util_stats before;
359 static struct cpu_disk_util_stats after;
360 
361 #define BUFSIZE		8192
362 
363 static int hz;
364 
365 static void
get_HZ(void)366 get_HZ(void)
367 {
368 	if ((hz = sysconf(_SC_CLK_TCK)) == -1)
369 		exit(1);
370 }
371 
372 #if 0
373 static int num_cores;
374 
375 static void
376 get_cores(void)
377 {
378 	if ((num_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
379 		exit(1);
380 }
381 #endif
382 
383 extern char *blockdev_name;
384 
385 static void
get_blockdev_name(char * bdev)386 get_blockdev_name(char *bdev)
387 {
388 	char dev_name[BUFSIZE];
389 	FILE *cmd;
390 
391 	cmd = popen("getprop ro.product.name", "r");
392 	if (cmd == NULL) {
393 		fprintf(stderr, "%s: Cannot popen getprop\n",
394 			progname);
395 		exit(1);
396 	}
397 	if (fgets(dev_name, BUFSIZE, cmd) == NULL) {
398 		fprintf(stderr,
399 			"%s: Bad output from getprop ro.product.name\n",
400 			progname);
401 		exit(1);
402 	}
403 	pclose(cmd);
404 	/* strncmp needed because of the trailing '\n' */
405 	if (strncmp(dev_name, "bullhead", strlen("bullhead")) == 0 ||
406 	    strncmp(dev_name, "angler", strlen("angler")) == 0 ||
407 	    strncmp(dev_name, "shamu", strlen("shamu")) == 0 ||
408 	    strncmp(dev_name, "aosp_gobo", strlen("aosp_gobo")) == 0 ||
409 	    strncmp(dev_name, "full_k37_y33_gms", strlen("full_k37_y33_gms")) == 0 ||
410 	    strncmp(dev_name, "fugu", strlen("fugu")) == 0) {
411 		strcpy(bdev, "mmcblk0");
412 	} else if (strncmp(dev_name, "marlin", strlen("marlin")) == 0 ||
413 		   strncmp(dev_name, "sailfish", strlen("sailfish")) == 0 ||
414 		   strncmp(dev_name, "taimen", strlen("taimen")) == 0 ||
415 		   strncmp(dev_name, "walleye", strlen("walleye")) == 0) {
416 		strcpy(bdev, "sda");
417 	} else if (blockdev_name != NULL) {
418 		strcpy(bdev, blockdev_name);
419 	} else {
420 		fprintf(stderr,
421 			"%s: Unknown device %s, please specify block device name with -b\n",
422 			progname, dev_name);
423 		exit(1);
424 	}
425 }
426 
427 static void
read_disk_util_state(struct cpu_disk_util_stats * state)428 read_disk_util_state(struct cpu_disk_util_stats *state)
429 {
430 	FILE *fp;
431         char line[BUFSIZE], dev_name[BUFSIZE];
432         unsigned int major, minor;
433 	unsigned int ios_pgr;
434 	unsigned int rq_ticks;
435 	unsigned int wr_ticks;
436 	unsigned long rd_ticks;
437 	unsigned long rd_merges;
438 	unsigned long wr_merges;
439 	unsigned long up_sec, up_cent;
440 	char blockdev_name[BUFSIZE];
441 
442 	/* Read and parse /proc/uptime */
443 	fp = fopen("/proc/uptime", "r");
444 	if (fgets(line, sizeof(line), fp) == NULL) {
445 		fprintf(stderr, "%s: Cannot read /proc/uptime\n",
446 			progname);
447 		exit(1);
448 	}
449 	fclose(fp);
450 	sscanf(line, "%lu.%lu", &up_sec, &up_cent);
451 	state->uptime = (unsigned long long) up_sec * hz +
452 		(unsigned long long) up_cent * hz / 100;
453 
454 	/* Read and parse /proc/diskstats */
455 	get_blockdev_name(blockdev_name);
456 	fp = fopen("/proc/diskstats", "r");
457 	while (fgets(line, sizeof(line), fp)) {
458 		sscanf(line,
459 		       "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u %u %u %u",
460 		       &major, &minor, dev_name,
461 		       &state->rd_ios, &rd_merges, &state->rd_sec,
462 		       &rd_ticks, &state->wr_ios, &wr_merges,
463 		       &state->wr_sec, &wr_ticks,
464 		       &ios_pgr, &state->tot_ticks, &rq_ticks);
465                 if (strcmp(dev_name, blockdev_name) == 0) {
466 			/*
467 			 * tot_ticks is "number of milliseconds spent
468 			 * doing I/Os". Look at Documentation/iostats.txt.
469 			 * Or at genhd.c:diskstats_show(), which calls
470 			 * jiffies_to_msecs() on this field before printing
471 			 * it. Convert this to hz, so we can do all our math
472 			 * in ticks.
473 			 */
474 			state->tot_ticks /= 1000; /* to seconds */
475 			state->tot_ticks *= hz;   /* to hz	*/
476 			fclose(fp);
477 			return;
478 		}
479 	}
480         fprintf(stderr, "%s: Did not find device sda in /proc/diskstats\n",
481 		progname);
482 	exit(1);
483 }
484 
485 static void
read_cpu_util_state(struct cpu_disk_util_stats * state)486 read_cpu_util_state(struct cpu_disk_util_stats *state)
487 {
488 	FILE *fp;
489 	char line[BUFSIZE], cpu[BUFSIZE];
490 
491 	/* Read and parse /proc/stat */
492 	fp = fopen("/proc/stat", "r");
493 	if (fgets(line, sizeof(line), fp) == NULL) {
494 		fprintf(stderr, "%s: Cannot read /proc/stat\n",
495 			progname);
496 		exit(1);
497 	}
498 	fclose(fp);
499 	sscanf(line, "%s %ju %ju %ju %ju %ju %ju %ju",
500 	       cpu,
501 	       &state->user_cpu_ticks,
502 	       &state->nice_cpu_ticks,
503 	       &state->system_cpu_ticks,
504 	       &state->idle_cpu_ticks,
505 	       &state->iowait_cpu_ticks,
506 	       &state->hardirq_cpu_ticks,
507 	       &state->softirq_cpu_ticks);
508 }
509 
510 void
capture_util_state_before(void)511 capture_util_state_before(void)
512 {
513 	get_HZ();
514 	read_disk_util_state(&before);
515 	read_cpu_util_state(&before);
516 }
517 
518 void
report_cpu_disk_util(void)519 report_cpu_disk_util(void)
520 {
521         double disk_util, cpu_util;
522 	u_int64_t tot1, tot2, delta1, delta2;
523 
524 	read_disk_util_state(&after);
525 	read_cpu_util_state(&after);
526 	/* CPU Util */
527 	tot2 = after.user_cpu_ticks + after.nice_cpu_ticks +
528 		after.system_cpu_ticks + after.hardirq_cpu_ticks +
529 		after.softirq_cpu_ticks;
530 	tot1 = before.user_cpu_ticks + before.nice_cpu_ticks +
531 		before.system_cpu_ticks + before.hardirq_cpu_ticks +
532 		before.softirq_cpu_ticks;
533 	delta1 = tot2 - tot1;
534 	tot2 += after.iowait_cpu_ticks + after.idle_cpu_ticks;
535 	tot1 += before.iowait_cpu_ticks + before.idle_cpu_ticks;
536 	delta2 = tot2 - tot1;
537 	cpu_util = delta1 * 100.0 / delta2;
538 	if (!summary_mode)
539 		printf("CPU util = %.2f%%\n", cpu_util);
540 	else
541 		printf("%.2f ", cpu_util);
542 	/* Next compute system (incl irq/softirq) and user cpu util */
543 	delta1 = (after.user_cpu_ticks + after.nice_cpu_ticks) -
544 		(before.user_cpu_ticks + before.nice_cpu_ticks);
545 	cpu_util = delta1 * 100.0 / delta2;
546 	if (!summary_mode)
547 		printf("User CPU util = %.2f%%\n", cpu_util);
548 	else
549 		printf("%.2f ", cpu_util);
550 	delta1 = (after.system_cpu_ticks + after.hardirq_cpu_ticks +
551 		  after.softirq_cpu_ticks) -
552 		(before.system_cpu_ticks + before.hardirq_cpu_ticks +
553 		 before.softirq_cpu_ticks);
554 	cpu_util = delta1 * 100.0 / delta2;
555 	if (!summary_mode)
556 		printf("System CPU util = %.2f%%\n", cpu_util);
557 	else
558 		printf("%.2f ", cpu_util);
559 	/* Disk Util */
560 	disk_util = (after.tot_ticks - before.tot_ticks) * 100.0 /
561 		(after.uptime - before.uptime);
562 	if (verbose) {
563 		printf("Reads : nr_ios %lu, MB read %lu\n",
564 	       (after.rd_ios - before.rd_ios),
565 	       (after.rd_sec - before.rd_sec) / 2048);
566 		printf("Writes : nr_ios %lu, MB written %lu\n",
567 	       (after.wr_ios - before.wr_ios),
568 		       (after.wr_sec - before.wr_sec) / 2048);
569 	}
570 	if (!summary_mode)
571 		printf("Disk util = %.2f%%\n", disk_util);
572 	else
573 		printf("%.2f", disk_util);
574 }
575 
576 
577 static struct ioshark_filename_struct *filename_cache;
578 static int filename_cache_num_entries;
579 
580 char *
get_ro_filename(int ix)581 get_ro_filename(int ix)
582 {
583 	if (ix >= filename_cache_num_entries)
584 		return NULL;
585 	return filename_cache[ix].path;
586 }
587 
588 void
init_filename_cache(void)589 init_filename_cache(void)
590 {
591 	int fd;
592 	struct stat st;
593 
594 	fd = open("ioshark_filenames", O_RDONLY);
595 	if (fd < 0) {
596 		fprintf(stderr, "%s Can't open ioshark_filenames file\n",
597 			progname);
598 		exit(EXIT_FAILURE);
599 	}
600 	if (fstat(fd, &st) < 0) {
601 		fprintf(stderr, "%s Can't fstat ioshark_filenames file\n",
602 			progname);
603 		exit(EXIT_FAILURE);
604 	}
605 	filename_cache_num_entries = st.st_size /
606 		sizeof(struct ioshark_filename_struct);
607 	filename_cache = mmap(NULL, st.st_size, PROT_READ,
608 			      MAP_SHARED | MAP_LOCKED | MAP_POPULATE,
609 			      fd, 0);
610 	if (filename_cache == MAP_FAILED) {
611 		fprintf(stderr, "%s Can't fstat ioshark_filenames file: %s\n",
612 			progname, strerror(errno));
613 		exit(EXIT_FAILURE);
614 	}
615 	close(fd);
616 }
617 
618 void
free_filename_cache(void)619 free_filename_cache(void)
620 {
621 	size_t mmap_size;
622 
623 	mmap_size = filename_cache_num_entries *
624 		sizeof(struct ioshark_filename_struct);
625 	munmap(filename_cache, mmap_size);
626 }
627 
628 /*
629  * Is the passed in filename a regular file ? (eg. not a directory).
630  * Second, is it in a read-only partition ?
631  */
632 int
is_readonly_mount(char * filename,size_t size)633 is_readonly_mount(char *filename, size_t size)
634 {
635 	struct statfs statfsbuf;
636 	struct stat statbuf;
637 
638 	if (stat(filename, &statbuf) < 0) {
639 		/* File possibly deleted */
640 		return 0;
641 	}
642 	if (!S_ISREG(statbuf.st_mode)) {
643 		/* Is it a regular file ? */
644 		return 0;
645 	}
646 	if ((size_t)statbuf.st_size < size) {
647 		/* Size of existing file is smaller than we expect */
648 		return 0;
649 	}
650 	if (statfs(filename, &statfsbuf) < 0) {
651 		/* This shouldn't happen */
652 		return 0;
653 	}
654 	if ((statfsbuf.f_flags & ST_RDONLY) == 0)
655 		return 0;
656 	else
657 		return 1;
658 }
659 
660 int
ioshark_read_header(FILE * fp,struct ioshark_header * header)661 ioshark_read_header(FILE *fp, struct ioshark_header *header)
662 {
663 	if (fread(header, sizeof(struct ioshark_header), 1, fp) != 1)
664 		return -1;
665 	header->version = be64toh(header->version);
666 	header->num_files = be64toh(header->num_files);
667 	header->num_io_operations = be64toh(header->num_io_operations);
668 	return 1;
669 }
670 
671 int
ioshark_read_file_state(FILE * fp,struct ioshark_file_state * state)672 ioshark_read_file_state(FILE *fp, struct ioshark_file_state *state)
673 {
674 	if (fread(state, sizeof(struct ioshark_file_state), 1, fp) != 1)
675 		return -1;
676 	state->fileno = be64toh(state->fileno);
677 	state->size = be64toh(state->size);
678 	state->global_filename_ix = be64toh(state->global_filename_ix);
679 	return 1;
680 }
681 
682 int
ioshark_read_file_op(FILE * fp,struct ioshark_file_operation * file_op)683 ioshark_read_file_op(FILE *fp, struct ioshark_file_operation *file_op)
684 {
685 	if (fread(file_op, sizeof(struct ioshark_file_operation), 1, fp) != 1)
686 		return -1;
687 	file_op->delta_us = be64toh(file_op->delta_us);
688 	file_op->op_union.enum_size = be32toh(file_op->op_union.enum_size);
689 	file_op->fileno = be64toh(file_op->fileno);
690 	switch (file_op->ioshark_io_op) {
691 	case IOSHARK_LSEEK:
692 	case IOSHARK_LLSEEK:
693 		file_op->lseek_offset = be64toh(file_op->lseek_offset);
694 		file_op->lseek_action = be32toh(file_op->lseek_action);
695 		break;
696 	case IOSHARK_PREAD64:
697 	case IOSHARK_PWRITE64:
698 		file_op->prw_offset = be64toh(file_op->prw_offset);
699 		file_op->prw_len = be64toh(file_op->prw_len);
700 		break;
701 	case IOSHARK_READ:
702 	case IOSHARK_WRITE:
703 		file_op->rw_len = be64toh(file_op->rw_len);
704 		break;
705 	case IOSHARK_MMAP:
706 	case IOSHARK_MMAP2:
707 		file_op->mmap_offset = be64toh(file_op->mmap_offset);
708 		file_op->mmap_len = be64toh(file_op->mmap_len);
709 		file_op->mmap_prot = be32toh(file_op->mmap_prot);
710 		break;
711 	case IOSHARK_OPEN:
712 		file_op->open_flags = be32toh(file_op->open_flags);
713 		file_op->open_mode = be32toh(file_op->open_mode);
714 		break;
715 	case IOSHARK_FSYNC:
716 	case IOSHARK_FDATASYNC:
717 		break;
718 	case IOSHARK_CLOSE:
719 		break;
720 	default:
721 		fprintf(stderr, "%s: unknown FILE_OP %d\n",
722 			progname, file_op->ioshark_io_op);
723 		exit(EXIT_FAILURE);
724 		break;
725 	}
726 	return 1;
727 }
728