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