xref: /openbmc/linux/tools/perf/util/util.c (revision 37185b33)
1 #include "../perf.h"
2 #include "util.h"
3 #include <sys/mman.h>
4 
5 /*
6  * XXX We need to find a better place for these things...
7  */
8 bool perf_host  = true;
9 bool perf_guest = false;
10 
11 void event_attr_init(struct perf_event_attr *attr)
12 {
13 	if (!perf_host)
14 		attr->exclude_host  = 1;
15 	if (!perf_guest)
16 		attr->exclude_guest = 1;
17 	/* to capture ABI version */
18 	attr->size = sizeof(*attr);
19 }
20 
21 int mkdir_p(char *path, mode_t mode)
22 {
23 	struct stat st;
24 	int err;
25 	char *d = path;
26 
27 	if (*d != '/')
28 		return -1;
29 
30 	if (stat(path, &st) == 0)
31 		return 0;
32 
33 	while (*++d == '/');
34 
35 	while ((d = strchr(d, '/'))) {
36 		*d = '\0';
37 		err = stat(path, &st) && mkdir(path, mode);
38 		*d++ = '/';
39 		if (err)
40 			return -1;
41 		while (*d == '/')
42 			++d;
43 	}
44 	return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
45 }
46 
47 static int slow_copyfile(const char *from, const char *to)
48 {
49 	int err = 0;
50 	char *line = NULL;
51 	size_t n;
52 	FILE *from_fp = fopen(from, "r"), *to_fp;
53 
54 	if (from_fp == NULL)
55 		goto out;
56 
57 	to_fp = fopen(to, "w");
58 	if (to_fp == NULL)
59 		goto out_fclose_from;
60 
61 	while (getline(&line, &n, from_fp) > 0)
62 		if (fputs(line, to_fp) == EOF)
63 			goto out_fclose_to;
64 	err = 0;
65 out_fclose_to:
66 	fclose(to_fp);
67 	free(line);
68 out_fclose_from:
69 	fclose(from_fp);
70 out:
71 	return err;
72 }
73 
74 int copyfile(const char *from, const char *to)
75 {
76 	int fromfd, tofd;
77 	struct stat st;
78 	void *addr;
79 	int err = -1;
80 
81 	if (stat(from, &st))
82 		goto out;
83 
84 	if (st.st_size == 0) /* /proc? do it slowly... */
85 		return slow_copyfile(from, to);
86 
87 	fromfd = open(from, O_RDONLY);
88 	if (fromfd < 0)
89 		goto out;
90 
91 	tofd = creat(to, 0755);
92 	if (tofd < 0)
93 		goto out_close_from;
94 
95 	addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
96 	if (addr == MAP_FAILED)
97 		goto out_close_to;
98 
99 	if (write(tofd, addr, st.st_size) == st.st_size)
100 		err = 0;
101 
102 	munmap(addr, st.st_size);
103 out_close_to:
104 	close(tofd);
105 	if (err)
106 		unlink(to);
107 out_close_from:
108 	close(fromfd);
109 out:
110 	return err;
111 }
112 
113 unsigned long convert_unit(unsigned long value, char *unit)
114 {
115 	*unit = ' ';
116 
117 	if (value > 1000) {
118 		value /= 1000;
119 		*unit = 'K';
120 	}
121 
122 	if (value > 1000) {
123 		value /= 1000;
124 		*unit = 'M';
125 	}
126 
127 	if (value > 1000) {
128 		value /= 1000;
129 		*unit = 'G';
130 	}
131 
132 	return value;
133 }
134 
135 int readn(int fd, void *buf, size_t n)
136 {
137 	void *buf_start = buf;
138 
139 	while (n) {
140 		int ret = read(fd, buf, n);
141 
142 		if (ret <= 0)
143 			return ret;
144 
145 		n -= ret;
146 		buf += ret;
147 	}
148 
149 	return buf - buf_start;
150 }
151 
152 size_t hex_width(u64 v)
153 {
154 	size_t n = 1;
155 
156 	while ((v >>= 4))
157 		++n;
158 
159 	return n;
160 }
161