2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
25 #include <sys/errno.h>
30 #include <sys/statvfs.h>
33 #include "ioshark_bench.h"
35 extern char *progname;
36 extern int verbose, summary_mode;
39 files_db_create_handle(void)
41 struct files_db_handle *h;
44 h = malloc(sizeof(struct files_db_handle));
45 for (i = 0 ; i < FILE_DB_HASHSIZE ; i++)
46 h->files_db_buckets[i] = NULL;
50 void *files_db_lookup_byfileno(void *handle, int fileno)
53 struct files_db_handle *h = (struct files_db_handle *)handle;
54 struct files_db_s *db_node;
56 hash = fileno % FILE_DB_HASHSIZE;
57 db_node = h->files_db_buckets[hash];
58 while (db_node != NULL) {
59 if (db_node->fileno == fileno)
61 db_node = db_node->next;
66 void *files_db_add_byfileno(void *handle, int fileno, int readonly)
68 u_int32_t hash = fileno % FILE_DB_HASHSIZE;
69 struct files_db_handle *h = (struct files_db_handle *)handle;
70 struct files_db_s *db_node;
72 db_node = (struct files_db_s *)
73 files_db_lookup_byfileno(handle, fileno);
74 if (db_node == NULL) {
75 db_node = malloc(sizeof(struct files_db_s));
76 db_node->fileno = fileno;
77 db_node->filename = NULL;
78 db_node->readonly = readonly;
81 db_node->next = h->files_db_buckets[hash];
82 h->files_db_buckets[hash] = db_node;
85 "%s: Node to be added already exists fileno = %d\n\n",
93 files_db_fsync_discard_files(void *handle)
95 struct files_db_handle *h = (struct files_db_handle *)handle;
96 struct files_db_s *db_node;
99 for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
100 db_node = h->files_db_buckets[i];
101 while (db_node != NULL) {
104 if (db_node->fd == -1) {
109 * File was closed, let's open it so we can
110 * fsync and fadvise(DONTNEED) it.
113 if (files_db_readonly(db_node))
114 openflags = O_RDONLY;
117 fd = open(files_db_get_filename(db_node),
121 "%s: open(%s %x) error %d\n",
122 progname, db_node->filename,
129 if (!db_node->readonly && fsync(db_node->fd) < 0) {
130 fprintf(stderr, "%s: Cannot fsync %s\n",
131 __func__, db_node->filename);
134 if (posix_fadvise(db_node->fd, 0, 0,
135 POSIX_FADV_DONTNEED) < 0) {
137 "%s: Cannot fadvise(DONTNEED) %s\n",
138 __func__, db_node->filename);
145 db_node = db_node->next;
151 files_db_update_fd(void *node, int fd)
153 struct files_db_s *db_node = (struct files_db_s *)node;
159 files_db_close_fd(void *node)
161 struct files_db_s *db_node = (struct files_db_s *)node;
163 if (db_node->fd != -1)
169 files_db_close_files(void *handle)
171 struct files_db_handle *h = (struct files_db_handle *)handle;
172 struct files_db_s *db_node;
175 for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
176 db_node = h->files_db_buckets[i];
177 while (db_node != NULL) {
178 if ((db_node->fd != -1) && close(db_node->fd) < 0) {
179 fprintf(stderr, "%s: Cannot close %s\n",
180 __func__, db_node->filename);
184 db_node = db_node->next;
190 files_db_unlink_files(void *handle)
192 struct files_db_handle *h = (struct files_db_handle *)handle;
193 struct files_db_s *db_node;
196 for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
197 db_node = h->files_db_buckets[i];
198 while (db_node != NULL) {
199 if ((db_node->fd != -1) && close(db_node->fd) < 0) {
200 fprintf(stderr, "%s: Cannot close %s\n",
201 __func__, db_node->filename);
205 if (is_readonly_mount(db_node->filename, db_node->size) == 0) {
206 if (unlink(db_node->filename) < 0) {
207 fprintf(stderr, "%s: Cannot unlink %s:%s\n",
208 __func__, db_node->filename,
213 db_node = db_node->next;
219 files_db_free_memory(void *handle)
221 struct files_db_handle *h = (struct files_db_handle *)handle;
222 struct files_db_s *db_node, *tmp;
225 for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
226 db_node = h->files_db_buckets[i];
227 while (db_node != NULL) {
229 db_node = db_node->next;
238 get_buf(char **buf, int *buflen, int len, int do_fill __attribute__((unused)))
240 if (len == 0 && *buf == NULL) {
242 * If we ever get a zero len
243 * request, start with MINBUFLEN
249 *buflen = MAX(MINBUFLEN, len * 2);
252 *buf = malloc(*buflen);
257 s = (u_int32_t *)*buf;
258 count = *buflen / sizeof(u_int32_t);
265 assert(*buf != NULL);
270 create_file(char *path, size_t size, struct rw_bytes_s *rw_bytes)
276 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
278 fprintf(stderr, "%s Cannot create file %s, error = %d\n",
279 progname, path, errno);
283 n = MIN(size, MINBUFLEN);
284 buf = get_buf(&buf, &buflen, n, 1);
285 if (write(fd, buf, n) < n) {
287 "%s Cannot write file %s, error = %d\n",
288 progname, path, errno);
291 rw_bytes->bytes_written += n;
295 fprintf(stderr, "%s Cannot fsync file %s, error = %d\n",
296 progname, path, errno);
299 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) < 0) {
301 "%s Cannot fadvise(DONTNEED) file %s, error = %d\n",
302 progname, path, errno);
309 print_op_stats(u_int64_t *op_counts)
312 extern char *IO_op[];
314 printf("IO Operation counts :\n");
315 for (i = IOSHARK_LSEEK ; i < IOSHARK_MAX_FILE_OP ; i++) {
317 IO_op[i], op_counts[i]);
322 print_bytes(char *desc, struct rw_bytes_s *rw_bytes)
325 printf("%s: Reads = %dMB, Writes = %dMB\n",
327 (int)(rw_bytes->bytes_read / (1024 * 1024)),
328 (int)(rw_bytes->bytes_written / (1024 * 1024)));
331 (int)(rw_bytes->bytes_read / (1024 * 1024)),
332 (int)(rw_bytes->bytes_written / (1024 * 1024)));
335 struct cpu_disk_util_stats {
337 u_int64_t user_cpu_ticks;
338 u_int64_t nice_cpu_ticks;
339 u_int64_t system_cpu_ticks;
340 u_int64_t idle_cpu_ticks;
341 u_int64_t iowait_cpu_ticks;
342 u_int64_t hardirq_cpu_ticks;
343 u_int64_t softirq_cpu_ticks;
345 unsigned long long uptime;
346 unsigned int tot_ticks;
347 unsigned long rd_ios;
348 unsigned long wr_ios;
349 unsigned long rd_sec;
350 unsigned long wr_sec;
353 static struct cpu_disk_util_stats before;
354 static struct cpu_disk_util_stats after;
363 if ((hz = sysconf(_SC_CLK_TCK)) == -1)
368 static int num_cores;
373 if ((num_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
379 get_blockdev_name(char *bdev)
381 char dev_name[BUFSIZE];
384 cmd = popen("getprop ro.product.name", "r");
386 fprintf(stderr, "%s: Cannot popen getprop\n",
390 if (fgets(dev_name, BUFSIZE, cmd) == NULL) {
392 "%s: Bad output from getprop ro.product.name\n",
397 /* strncmp needed because of the trailing '\n' */
398 if (strncmp(dev_name, "bullhead", strlen("bullhead")) == 0 ||
399 strncmp(dev_name, "angler", strlen("angler")) == 0 ||
400 strncmp(dev_name, "shamu", strlen("shamu")) == 0) {
401 strcpy(bdev, "mmcblk0");
402 } else if (strncmp(dev_name, "marlin", strlen("marlin")) == 0 ||
403 strncmp(dev_name, "sailfish", strlen("sailfish")) == 0) {
407 "%s: Unknown device %s\n",
414 read_disk_util_state(struct cpu_disk_util_stats *state)
417 char line[BUFSIZE], dev_name[BUFSIZE];
418 unsigned int major, minor;
419 unsigned int ios_pgr;
420 unsigned int rq_ticks;
421 unsigned int wr_ticks;
422 unsigned long rd_ticks;
423 unsigned long rd_merges;
424 unsigned long wr_merges;
425 unsigned long up_sec, up_cent;
426 char blockdev_name[BUFSIZE];
428 /* Read and parse /proc/uptime */
429 fp = fopen("/proc/uptime", "r");
430 if (fgets(line, sizeof(line), fp) == NULL) {
431 fprintf(stderr, "%s: Cannot read /proc/uptime\n",
436 sscanf(line, "%lu.%lu", &up_sec, &up_cent);
437 state->uptime = (unsigned long long) up_sec * hz +
438 (unsigned long long) up_cent * hz / 100;
440 /* Read and parse /proc/diskstats */
441 get_blockdev_name(blockdev_name);
442 fp = fopen("/proc/diskstats", "r");
443 while (fgets(line, sizeof(line), fp)) {
445 "%u %u %s %lu %lu %lu %lu %lu %lu %lu %u %u %u %u",
446 &major, &minor, dev_name,
447 &state->rd_ios, &rd_merges, &state->rd_sec,
448 &rd_ticks, &state->wr_ios, &wr_merges,
449 &state->wr_sec, &wr_ticks,
450 &ios_pgr, &state->tot_ticks, &rq_ticks);
451 if (strcmp(dev_name, blockdev_name) == 0) {
453 * tot_ticks is "number of milliseconds spent
454 * doing I/Os". Look at Documentation/iostats.txt.
455 * Or at genhd.c:diskstats_show(), which calls
456 * jiffies_to_msecs() on this field before printing
457 * it. Convert this to hz, so we can do all our math
460 state->tot_ticks /= 1000; /* to seconds */
461 state->tot_ticks *= hz; /* to hz */
466 fprintf(stderr, "%s: Did not find device sda in /proc/diskstats\n",
472 read_cpu_util_state(struct cpu_disk_util_stats *state)
475 char line[BUFSIZE], cpu[BUFSIZE];
477 /* Read and parse /proc/stat */
478 fp = fopen("/proc/stat", "r");
479 if (fgets(line, sizeof(line), fp) == NULL) {
480 fprintf(stderr, "%s: Cannot read /proc/stat\n",
485 sscanf(line, "%s %ju %ju %ju %ju %ju %ju %ju",
487 &state->user_cpu_ticks,
488 &state->nice_cpu_ticks,
489 &state->system_cpu_ticks,
490 &state->idle_cpu_ticks,
491 &state->iowait_cpu_ticks,
492 &state->hardirq_cpu_ticks,
493 &state->softirq_cpu_ticks);
497 capture_util_state_before(void)
500 read_disk_util_state(&before);
501 read_cpu_util_state(&before);
505 report_cpu_disk_util(void)
507 double disk_util, cpu_util;
508 u_int64_t tot1, tot2, delta1, delta2;
510 read_disk_util_state(&after);
511 read_cpu_util_state(&after);
513 tot2 = after.user_cpu_ticks + after.nice_cpu_ticks +
514 after.system_cpu_ticks + after.hardirq_cpu_ticks +
515 after.softirq_cpu_ticks;
516 tot1 = before.user_cpu_ticks + before.nice_cpu_ticks +
517 before.system_cpu_ticks + before.hardirq_cpu_ticks +
518 before.softirq_cpu_ticks;
519 delta1 = tot2 - tot1;
520 tot2 += after.iowait_cpu_ticks + after.idle_cpu_ticks;
521 tot1 += before.iowait_cpu_ticks + before.idle_cpu_ticks;
522 delta2 = tot2 - tot1;
523 cpu_util = delta1 * 100.0 / delta2;
525 printf("CPU util = %.2f%%\n", cpu_util);
527 printf("%.2f ", cpu_util);
528 /* Next compute system (incl irq/softirq) and user cpu util */
529 delta1 = (after.user_cpu_ticks + after.nice_cpu_ticks) -
530 (before.user_cpu_ticks + before.nice_cpu_ticks);
531 cpu_util = delta1 * 100.0 / delta2;
533 printf("User CPU util = %.2f%%\n", cpu_util);
535 printf("%.2f ", cpu_util);
536 delta1 = (after.system_cpu_ticks + after.hardirq_cpu_ticks +
537 after.softirq_cpu_ticks) -
538 (before.system_cpu_ticks + before.hardirq_cpu_ticks +
539 before.softirq_cpu_ticks);
540 cpu_util = delta1 * 100.0 / delta2;
542 printf("System CPU util = %.2f%%\n", cpu_util);
544 printf("%.2f ", cpu_util);
546 disk_util = (after.tot_ticks - before.tot_ticks) * 100.0 /
547 (after.uptime - before.uptime);
549 printf("Reads : nr_ios %lu, MB read %lu\n",
550 (after.rd_ios - before.rd_ios),
551 (after.rd_sec - before.rd_sec) / 2048);
552 printf("Writes : nr_ios %lu, MB written %lu\n",
553 (after.wr_ios - before.wr_ios),
554 (after.wr_sec - before.wr_sec) / 2048);
557 printf("Disk util = %.2f%%\n", disk_util);
559 printf("%.2f", disk_util);
563 static struct ioshark_filename_struct *filename_cache;
564 static int filename_cache_num_entries;
567 get_ro_filename(int ix)
569 if (ix >= filename_cache_num_entries)
571 return filename_cache[ix].path;
575 init_filename_cache(void)
580 fd = open("ioshark_filenames", O_RDONLY);
582 fprintf(stderr, "%s Can't open ioshark_filenames file\n",
586 if (fstat(fd, &st) < 0) {
587 fprintf(stderr, "%s Can't fstat ioshark_filenames file\n",
591 filename_cache_num_entries = st.st_size /
592 sizeof(struct ioshark_filename_struct);
593 filename_cache = mmap(NULL, st.st_size, PROT_READ,
594 MAP_SHARED | MAP_LOCKED | MAP_POPULATE,
596 if (filename_cache == MAP_FAILED) {
597 fprintf(stderr, "%s Can't fstat ioshark_filenames file: %s\n",
598 progname, strerror(errno));
605 free_filename_cache(void)
609 mmap_size = filename_cache_num_entries *
610 sizeof(struct ioshark_filename_struct);
611 munmap(filename_cache, mmap_size);
615 * Is the passed in filename a regular file ? (eg. not a directory).
616 * Second, is it in a read-only partition ?
619 is_readonly_mount(char *filename, size_t size)
621 struct statfs statfsbuf;
624 if (stat(filename, &statbuf) < 0) {
625 /* File possibly deleted */
628 if (!S_ISREG(statbuf.st_mode)) {
629 /* Is it a regular file ? */
632 if ((size_t)statbuf.st_size < size) {
633 /* Size of existing file is smaller than we expect */
636 if (statfs(filename, &statfsbuf) < 0) {
637 /* This shouldn't happen */
640 if ((statfsbuf.f_flags & ST_RDONLY) == 0)