blob: 738438196d9adbfd7bbdaf08b6ff849dfac80192 [file] [log] [blame]
Jiri Olsaf7add552012-07-22 14:14:40 +02001#include "util.h"
2
3#include <stdlib.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02004#include <linux/types.h>
Jiri Olsaf7add552012-07-22 14:14:40 +02005#include <sys/stat.h>
6#include <fcntl.h>
7#include <string.h>
8
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03009#include "machine.h"
Jiri Olsaf7add552012-07-22 14:14:40 +020010#include "symbol.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010011#include "tests.h"
Jiri Olsaf7add552012-07-22 14:14:40 +020012
Jiri Olsaf7add552012-07-22 14:14:40 +020013static char *test_file(int size)
14{
Jiri Olsa822c45d2014-05-04 13:51:46 +020015#define TEMPL "/tmp/perf-test-XXXXXX"
16 static char buf_templ[sizeof(TEMPL)];
Jiri Olsaf7add552012-07-22 14:14:40 +020017 char *templ = buf_templ;
18 int fd, i;
19 unsigned char *buf;
20
Jiri Olsa822c45d2014-05-04 13:51:46 +020021 strcpy(buf_templ, TEMPL);
22#undef TEMPL
23
Irina Tirdea7b45f212012-09-08 03:43:21 +030024 fd = mkstemp(templ);
Jiri Olsaf95e0812012-11-10 01:46:52 +010025 if (fd < 0) {
26 perror("mkstemp failed");
27 return NULL;
28 }
Jiri Olsaf7add552012-07-22 14:14:40 +020029
30 buf = malloc(size);
31 if (!buf) {
32 close(fd);
33 return NULL;
34 }
35
36 for (i = 0; i < size; i++)
37 buf[i] = (unsigned char) ((int) i % 10);
38
39 if (size != write(fd, buf, size))
40 templ = NULL;
41
Felipe Pena1df92972013-10-09 23:00:38 -030042 free(buf);
Jiri Olsaf7add552012-07-22 14:14:40 +020043 close(fd);
44 return templ;
45}
46
47#define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
48
49struct test_data_offset {
50 off_t offset;
51 u8 data[10];
52 int size;
53};
54
55struct test_data_offset offsets[] = {
56 /* Fill first cache page. */
57 {
58 .offset = 10,
59 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
60 .size = 10,
61 },
62 /* Read first cache page. */
63 {
64 .offset = 10,
65 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
66 .size = 10,
67 },
68 /* Fill cache boundary pages. */
69 {
70 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
71 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
72 .size = 10,
73 },
74 /* Read cache boundary pages. */
75 {
76 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
77 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
78 .size = 10,
79 },
80 /* Fill final cache page. */
81 {
82 .offset = TEST_FILE_SIZE - 10,
83 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
84 .size = 10,
85 },
86 /* Read final cache page. */
87 {
88 .offset = TEST_FILE_SIZE - 10,
89 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
90 .size = 10,
91 },
92 /* Read final cache page. */
93 {
94 .offset = TEST_FILE_SIZE - 3,
95 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
96 .size = 3,
97 },
98};
99
Jiri Olsac81251e2012-11-10 01:46:51 +0100100int test__dso_data(void)
Jiri Olsaf7add552012-07-22 14:14:40 +0200101{
102 struct machine machine;
103 struct dso *dso;
104 char *file = test_file(TEST_FILE_SIZE);
105 size_t i;
106
107 TEST_ASSERT_VAL("No test file", file);
108
109 memset(&machine, 0, sizeof(machine));
110
111 dso = dso__new((const char *)file);
112
113 /* Basic 10 bytes tests. */
114 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
115 struct test_data_offset *data = &offsets[i];
116 ssize_t size;
117 u8 buf[10];
118
119 memset(buf, 0, 10);
120 size = dso__data_read_offset(dso, &machine, data->offset,
121 buf, 10);
122
123 TEST_ASSERT_VAL("Wrong size", size == data->size);
124 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
125 }
126
127 /* Read cross multiple cache pages. */
128 {
129 ssize_t size;
130 int c;
131 u8 *buf;
132
133 buf = malloc(TEST_FILE_SIZE);
134 TEST_ASSERT_VAL("ENOMEM\n", buf);
135
136 /* First iteration to fill caches, second one to read them. */
137 for (c = 0; c < 2; c++) {
138 memset(buf, 0, TEST_FILE_SIZE);
139 size = dso__data_read_offset(dso, &machine, 10,
140 buf, TEST_FILE_SIZE);
141
142 TEST_ASSERT_VAL("Wrong size",
143 size == (TEST_FILE_SIZE - 10));
144
145 for (i = 0; i < (size_t)size; i++)
146 TEST_ASSERT_VAL("Wrong data",
147 buf[i] == (i % 10));
148 }
149
150 free(buf);
151 }
152
153 dso__delete(dso);
154 unlink(file);
155 return 0;
156}