Commit | Line | Data |
---|---|---|
b1acd2b3 JD |
1 | #include "babeltrace/ctf/types.h" |
2 | #include "babeltrace/ctf/metadata.h" | |
3 | ||
4 | void ctf_move_mmap_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) | |
5 | { | |
6 | struct ctf_file_stream *file_stream = | |
7 | container_of(pos, struct ctf_file_stream, pos); | |
8 | int ret, err, i; | |
9 | off_t off; | |
10 | struct packet_index *index; | |
11 | int len_index; | |
12 | struct mmap_stream *iter, *tmp; | |
13 | struct mmap_stream tmp_snapshot; | |
14 | ||
15 | /* first time we have to mmap the region */ | |
16 | if (pos->mmap_len == 0) { | |
17 | /* get the len of the mmap region */ | |
18 | ret = kernctl_get_mmap_len(pos->fd, &pos->mmap_len); | |
19 | if (ret != 0) { | |
20 | ret = errno; | |
21 | perror("kernctl_get_mmap_len"); | |
22 | goto end; | |
23 | } | |
24 | pos->mmap_real_base = mmap(NULL, pos->mmap_len, PROT_READ, MAP_PRIVATE, pos->fd, 0); | |
25 | if (pos->mmap_real_base == MAP_FAILED) { | |
26 | perror("Error mmaping"); | |
27 | ret = -1; | |
28 | } | |
29 | } | |
30 | ||
31 | if (pos->base) { | |
32 | /* FIXME : put_subbuf should work but fails after nb_subbuf get + put */ | |
33 | ret = kernctl_put_next_subbuf(pos->fd); | |
34 | if (ret != 0) { | |
35 | ret = errno; | |
36 | perror("kernctl_put_subbuf"); | |
37 | } | |
38 | pos->base = NULL; | |
39 | } | |
40 | ||
41 | next_snapshot: | |
42 | tmp_snapshot.kconsumerd_fd = 0; | |
43 | for (i = 0; i < available_snapshots->len; i++) { | |
44 | tmp = g_ptr_array_index(available_snapshots, 0); | |
45 | if (tmp->kconsumerd_fd->wait_fd == pos->fd) { | |
46 | tmp_snapshot.last_pos = tmp->last_pos; | |
47 | tmp_snapshot.kconsumerd_fd = tmp->kconsumerd_fd; | |
48 | g_ptr_array_remove_index(available_snapshots, i); | |
49 | free(tmp); | |
50 | break; | |
51 | } | |
52 | } | |
53 | if (tmp_snapshot.kconsumerd_fd == 0) { | |
54 | pos->offset = EOF; | |
55 | return; | |
56 | } | |
57 | // fprintf(stderr,"READING FROM SNAPSHOT ON FD %d at %lu\n", | |
58 | // tmp_snapshot.kconsumerd_fd->wait_fd, tmp_snapshot.last_pos); | |
59 | ret = kernctl_get_subbuf(tmp_snapshot.kconsumerd_fd->wait_fd, &tmp_snapshot.last_pos); | |
60 | if (ret != 0) { | |
61 | ret = errno; | |
62 | perror("kernctl_get_subbuf"); | |
63 | goto next_snapshot; | |
64 | } | |
65 | ||
66 | ret = kernctl_get_mmap_read_offset(pos->fd, &(pos->mmap_offset)); | |
67 | if (ret != 0) { | |
68 | ret = errno; | |
69 | perror("kernctl_get_mmap_read_offset"); | |
70 | goto end; | |
71 | } | |
72 | ||
73 | /* read only the data in the subbuffer */ | |
74 | err = kernctl_get_subbuf_size(pos->fd, &pos->content_size); | |
75 | if (err != 0) { | |
76 | ret = errno; | |
77 | perror("Getting sub-buffer len failed."); | |
78 | goto end; | |
79 | } | |
80 | /* bits vs bytes */ | |
81 | pos->content_size *= CHAR_BIT; | |
82 | /* read the whole subbuffer */ | |
83 | err = kernctl_get_padded_subbuf_size(pos->fd, &pos->packet_size); | |
84 | if (err != 0) { | |
85 | ret = errno; | |
86 | perror("Getting sub-buffer len failed."); | |
87 | goto end; | |
88 | } | |
89 | /* bits vs bytes */ | |
90 | pos->packet_size *= CHAR_BIT; | |
91 | ||
92 | pos->offset = 0; /* will read headers */ | |
93 | ||
94 | /* map new base. Need mapping length from header. */ | |
95 | pos->base = pos->mmap_real_base + pos->mmap_offset; | |
96 | ||
97 | /* update trace_packet_header and stream_packet_context */ | |
98 | if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) { | |
99 | /* Read packet header */ | |
100 | ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p); | |
101 | assert(!ret); | |
102 | } | |
103 | if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) { | |
104 | /* Read packet context */ | |
105 | ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); | |
106 | assert(!ret); | |
107 | } | |
108 | ||
109 | /* read timestamp begin from header */ | |
110 | len_index = struct_declaration_lookup_field_index( | |
111 | file_stream->parent.stream_packet_context->declaration, | |
112 | g_quark_from_static_string("timestamp_begin")); | |
113 | if (len_index >= 0) { | |
114 | struct definition_integer *defint; | |
115 | struct definition *field; | |
116 | ||
117 | field = struct_definition_get_field_from_index( | |
118 | file_stream->parent.stream_packet_context, len_index); | |
119 | assert(field->declaration->id == CTF_TYPE_INTEGER); | |
120 | defint = container_of(field, struct definition_integer, p); | |
121 | assert(defint->declaration->signedness == FALSE); | |
122 | file_stream->parent.timestamp = defint->value._unsigned; | |
123 | // fprintf(stderr, "READ TIMESTAMP : %lu\n", file_stream->parent.timestamp); | |
124 | } | |
125 | ||
126 | ||
127 | end: | |
128 | return; | |
129 | } | |
130 |