md: use resync_max_sectors for reshape as well as resync.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / util.c
blob8109a907841e63edb56020abd6e03aca8c463eb7
1 #include "../perf.h"
2 #include "util.h"
3 #include <sys/mman.h>
5 /*
6 * XXX We need to find a better place for these things...
7 */
8 bool perf_host = true;
9 bool perf_guest = false;
11 void event_attr_init(struct perf_event_attr *attr)
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);
21 int mkdir_p(char *path, mode_t mode)
23 struct stat st;
24 int err;
25 char *d = path;
27 if (*d != '/')
28 return -1;
30 if (stat(path, &st) == 0)
31 return 0;
33 while (*++d == '/');
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;
44 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
47 static int slow_copyfile(const char *from, const char *to)
49 int err = 0;
50 char *line = NULL;
51 size_t n;
52 FILE *from_fp = fopen(from, "r"), *to_fp;
54 if (from_fp == NULL)
55 goto out;
57 to_fp = fopen(to, "w");
58 if (to_fp == NULL)
59 goto out_fclose_from;
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;
74 int copyfile(const char *from, const char *to)
76 int fromfd, tofd;
77 struct stat st;
78 void *addr;
79 int err = -1;
81 if (stat(from, &st))
82 goto out;
84 if (st.st_size == 0) /* /proc? do it slowly... */
85 return slow_copyfile(from, to);
87 fromfd = open(from, O_RDONLY);
88 if (fromfd < 0)
89 goto out;
91 tofd = creat(to, 0755);
92 if (tofd < 0)
93 goto out_close_from;
95 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
96 if (addr == MAP_FAILED)
97 goto out_close_to;
99 if (write(tofd, addr, st.st_size) == st.st_size)
100 err = 0;
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;
113 unsigned long convert_unit(unsigned long value, char *unit)
115 *unit = ' ';
117 if (value > 1000) {
118 value /= 1000;
119 *unit = 'K';
122 if (value > 1000) {
123 value /= 1000;
124 *unit = 'M';
127 if (value > 1000) {
128 value /= 1000;
129 *unit = 'G';
132 return value;
135 int readn(int fd, void *buf, size_t n)
137 void *buf_start = buf;
139 while (n) {
140 int ret = read(fd, buf, n);
142 if (ret <= 0)
143 return ret;
145 n -= ret;
146 buf += ret;
149 return buf - buf_start;