diff options
Diffstat (limited to 'tools/perf/util/util.c')
-rw-r--r-- | tools/perf/util/util.c | 112 |
1 files changed, 64 insertions, 48 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index ead9509835d2..b7766c577b01 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #include <limits.h> | 14 | #include <limits.h> |
15 | #include <byteswap.h> | 15 | #include <byteswap.h> |
16 | #include <linux/kernel.h> | 16 | #include <linux/kernel.h> |
17 | #include <linux/log2.h> | ||
17 | #include <unistd.h> | 18 | #include <unistd.h> |
18 | #include "callchain.h" | 19 | #include "callchain.h" |
19 | #include "strlist.h" | 20 | #include "strlist.h" |
@@ -507,54 +508,6 @@ int parse_callchain_record(const char *arg, struct callchain_param *param) | |||
507 | return ret; | 508 | return ret; |
508 | } | 509 | } |
509 | 510 | ||
510 | int filename__read_str(const char *filename, char **buf, size_t *sizep) | ||
511 | { | ||
512 | size_t size = 0, alloc_size = 0; | ||
513 | void *bf = NULL, *nbf; | ||
514 | int fd, n, err = 0; | ||
515 | char sbuf[STRERR_BUFSIZE]; | ||
516 | |||
517 | fd = open(filename, O_RDONLY); | ||
518 | if (fd < 0) | ||
519 | return -errno; | ||
520 | |||
521 | do { | ||
522 | if (size == alloc_size) { | ||
523 | alloc_size += BUFSIZ; | ||
524 | nbf = realloc(bf, alloc_size); | ||
525 | if (!nbf) { | ||
526 | err = -ENOMEM; | ||
527 | break; | ||
528 | } | ||
529 | |||
530 | bf = nbf; | ||
531 | } | ||
532 | |||
533 | n = read(fd, bf + size, alloc_size - size); | ||
534 | if (n < 0) { | ||
535 | if (size) { | ||
536 | pr_warning("read failed %d: %s\n", errno, | ||
537 | strerror_r(errno, sbuf, sizeof(sbuf))); | ||
538 | err = 0; | ||
539 | } else | ||
540 | err = -errno; | ||
541 | |||
542 | break; | ||
543 | } | ||
544 | |||
545 | size += n; | ||
546 | } while (n > 0); | ||
547 | |||
548 | if (!err) { | ||
549 | *sizep = size; | ||
550 | *buf = bf; | ||
551 | } else | ||
552 | free(bf); | ||
553 | |||
554 | close(fd); | ||
555 | return err; | ||
556 | } | ||
557 | |||
558 | const char *get_filename_for_perf_kvm(void) | 511 | const char *get_filename_for_perf_kvm(void) |
559 | { | 512 | { |
560 | const char *filename; | 513 | const char *filename; |
@@ -691,3 +644,66 @@ out: | |||
691 | 644 | ||
692 | return tip; | 645 | return tip; |
693 | } | 646 | } |
647 | |||
648 | bool is_regular_file(const char *file) | ||
649 | { | ||
650 | struct stat st; | ||
651 | |||
652 | if (stat(file, &st)) | ||
653 | return false; | ||
654 | |||
655 | return S_ISREG(st.st_mode); | ||
656 | } | ||
657 | |||
658 | int fetch_current_timestamp(char *buf, size_t sz) | ||
659 | { | ||
660 | struct timeval tv; | ||
661 | struct tm tm; | ||
662 | char dt[32]; | ||
663 | |||
664 | if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm)) | ||
665 | return -1; | ||
666 | |||
667 | if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm)) | ||
668 | return -1; | ||
669 | |||
670 | scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000); | ||
671 | |||
672 | return 0; | ||
673 | } | ||
674 | |||
675 | void print_binary(unsigned char *data, size_t len, | ||
676 | size_t bytes_per_line, print_binary_t printer, | ||
677 | void *extra) | ||
678 | { | ||
679 | size_t i, j, mask; | ||
680 | |||
681 | if (!printer) | ||
682 | return; | ||
683 | |||
684 | bytes_per_line = roundup_pow_of_two(bytes_per_line); | ||
685 | mask = bytes_per_line - 1; | ||
686 | |||
687 | printer(BINARY_PRINT_DATA_BEGIN, 0, extra); | ||
688 | for (i = 0; i < len; i++) { | ||
689 | if ((i & mask) == 0) { | ||
690 | printer(BINARY_PRINT_LINE_BEGIN, -1, extra); | ||
691 | printer(BINARY_PRINT_ADDR, i, extra); | ||
692 | } | ||
693 | |||
694 | printer(BINARY_PRINT_NUM_DATA, data[i], extra); | ||
695 | |||
696 | if (((i & mask) == mask) || i == len - 1) { | ||
697 | for (j = 0; j < mask-(i & mask); j++) | ||
698 | printer(BINARY_PRINT_NUM_PAD, -1, extra); | ||
699 | |||
700 | printer(BINARY_PRINT_SEP, i, extra); | ||
701 | for (j = i & ~mask; j <= i; j++) | ||
702 | printer(BINARY_PRINT_CHAR_DATA, data[j], extra); | ||
703 | for (j = 0; j < mask-(i & mask); j++) | ||
704 | printer(BINARY_PRINT_CHAR_PAD, i, extra); | ||
705 | printer(BINARY_PRINT_LINE_END, -1, extra); | ||
706 | } | ||
707 | } | ||
708 | printer(BINARY_PRINT_DATA_END, -1, extra); | ||
709 | } | ||