2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
37 #include <lttng/lttng.h>
39 #include <urcu/list.h>
40 #include <bin/lttng-sessiond/session.h>
41 #include <common/common.h>
43 #include <bin/lttng-relayd/lttng-viewer-abi.h>
44 #include <common/index/ctf-index.h>
46 #define SESSION1 "test1"
47 #define RELAYD_URL "net://localhost"
48 #define LIVE_TIMER 2000000
50 /* Number of TAP tests in this file */
52 #define mmap_size 524288
54 int ust_consumerd32_fd
;
55 int ust_consumerd64_fd
;
57 static int control_sock
;
58 struct live_session
*session
;
60 static int first_packet_offset
;
61 static int first_packet_len
;
62 static int first_packet_stream_id
;
64 struct viewer_stream
{
66 uint64_t ctf_trace_id
;
75 struct viewer_stream
*streams
;
76 uint64_t live_timer_interval
;
77 uint64_t stream_count
;
81 int connect_viewer(char *hostname
)
84 struct sockaddr_in server_addr
;
87 host
= gethostbyname(hostname
);
93 if ((control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
99 server_addr
.sin_family
= AF_INET
;
100 server_addr
.sin_port
= htons(5344);
101 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
102 bzero(&(server_addr
.sin_zero
), 8);
104 if (connect(control_sock
, (struct sockaddr
*) &server_addr
,
105 sizeof(struct sockaddr
)) == -1) {
111 server_addr
.sin_family
= AF_INET
;
112 server_addr
.sin_port
= htons(5345);
113 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
114 bzero(&(server_addr
.sin_zero
), 8);
122 int establish_connection(void)
124 struct lttng_viewer_cmd cmd
;
125 struct lttng_viewer_connect connect
;
128 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
129 cmd
.data_size
= sizeof(connect
);
132 memset(&connect
, 0, sizeof(connect
));
133 connect
.major
= htobe32(VERSION_MAJOR
);
134 connect
.minor
= htobe32(VERSION_MINOR
);
135 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
138 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
139 } while (ret
< 0 && errno
== EINTR
);
141 fprintf(stderr
, "Error sending cmd\n");
145 ret
= send(control_sock
, &connect
, sizeof(connect
), 0);
146 } while (ret
< 0 && errno
== EINTR
);
148 fprintf(stderr
, "Error sending version\n");
153 ret
= recv(control_sock
, &connect
, sizeof(connect
), 0);
154 } while (ret
< 0 && errno
== EINTR
);
156 fprintf(stderr
, "Error receiving version\n");
166 * Returns the number of sessions, should be 1 during the unit test.
168 int list_sessions(int *session_id
)
170 struct lttng_viewer_cmd cmd
;
171 struct lttng_viewer_list_sessions list
;
172 struct lttng_viewer_session lsession
;
174 int first_session
= 0;
176 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
181 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
182 } while (ret
< 0 && errno
== EINTR
);
184 fprintf(stderr
, "Error sending cmd\n");
189 ret
= recv(control_sock
, &list
, sizeof(list
), 0);
190 } while (ret
< 0 && errno
== EINTR
);
192 fprintf(stderr
, "Error receiving session list\n");
196 for (i
= 0; i
< be32toh(list
.sessions_count
); i
++) {
198 ret
= recv(control_sock
, &lsession
, sizeof(lsession
), 0);
199 } while (ret
< 0 && errno
== EINTR
);
201 fprintf(stderr
, "Error receiving session\n");
204 if (lsession
.streams
> 0 && first_session
<= 0) {
205 first_session
= be64toh(lsession
.id
);
206 *session_id
= first_session
;
210 ret
= be32toh(list
.sessions_count
);
216 int create_viewer_session()
218 struct lttng_viewer_cmd cmd
;
219 struct lttng_viewer_create_session_response resp
;
223 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
228 ret_len
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
229 } while (ret_len
< 0 && errno
== EINTR
);
231 fprintf(stderr
, "[error] Error sending cmd\n");
235 assert(ret_len
== sizeof(cmd
));
238 ret_len
= recv(control_sock
, &resp
, sizeof(resp
), 0);
239 } while (ret_len
< 0 && errno
== EINTR
);
241 fprintf(stderr
, "[error] Error receiving create session reply\n");
245 assert(ret_len
== sizeof(resp
));
247 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
248 fprintf(stderr
, "[error] Error creating viewer session\n");
258 int attach_session(int id
)
260 struct lttng_viewer_cmd cmd
;
261 struct lttng_viewer_attach_session_request rq
;
262 struct lttng_viewer_attach_session_response rp
;
263 struct lttng_viewer_stream stream
;
266 session
= zmalloc(sizeof(struct live_session
));
272 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
273 cmd
.data_size
= sizeof(rq
);
276 memset(&rq
, 0, sizeof(rq
));
277 rq
.session_id
= htobe64(id
);
278 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_BEGINNING
);
281 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
282 } while (ret
< 0 && errno
== EINTR
);
284 fprintf(stderr
, "Error sending cmd\n");
288 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
289 } while (ret
< 0 && errno
== EINTR
);
291 fprintf(stderr
, "Error sending attach request\n");
296 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
297 } while (ret
< 0 && errno
== EINTR
);
299 fprintf(stderr
, "Error receiving attach response\n");
302 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
307 session
->stream_count
= be32toh(rp
.streams_count
);
308 session
->streams
= zmalloc(session
->stream_count
*
309 sizeof(struct viewer_stream
));
310 if (!session
->streams
) {
315 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
317 ret
= recv(control_sock
, &stream
, sizeof(stream
), 0);
318 } while (ret
< 0 && errno
== EINTR
);
320 fprintf(stderr
, "Error receiving stream\n");
323 session
->streams
[i
].id
= be64toh(stream
.id
);
325 session
->streams
[i
].ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
326 session
->streams
[i
].first_read
= 1;
327 session
->streams
[i
].mmap_base
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
328 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
329 if (session
->streams
[i
].mmap_base
== MAP_FAILED
) {
330 fprintf(stderr
, "mmap error\n");
335 if (be32toh(stream
.metadata_flag
)) {
336 session
->streams
[i
].metadata_flag
= 1;
339 ret
= session
->stream_count
;
346 int get_metadata(void)
348 struct lttng_viewer_cmd cmd
;
349 struct lttng_viewer_get_metadata rq
;
350 struct lttng_viewer_metadata_packet rp
;
355 int metadata_stream_id
= -1;
357 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
358 cmd
.data_size
= sizeof(rq
);
361 for (i
= 0; i
< session
->stream_count
; i
++) {
362 if (session
->streams
[i
].metadata_flag
) {
363 metadata_stream_id
= i
;
368 if (metadata_stream_id
< 0) {
369 fprintf(stderr
, "No metadata stream found\n");
374 rq
.stream_id
= htobe64(session
->streams
[metadata_stream_id
].id
);
377 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
378 } while (ret
< 0 && errno
== EINTR
);
380 fprintf(stderr
, "Error sending cmd\n");
384 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
385 } while (ret
< 0 && errno
== EINTR
);
387 fprintf(stderr
, "Error sending get_metadata request\n");
391 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
392 } while (ret
< 0 && errno
== EINTR
);
394 fprintf(stderr
, "Error receiving metadata response\n");
397 switch (be32toh(rp
.status
)) {
398 case LTTNG_VIEWER_METADATA_OK
:
400 case LTTNG_VIEWER_NO_NEW_METADATA
:
401 fprintf(stderr
, "NO NEW\n");
404 case LTTNG_VIEWER_METADATA_ERR
:
405 fprintf(stderr
, "ERR\n");
409 fprintf(stderr
, "UNKNOWN\n");
414 len
= be64toh(rp
.len
);
421 perror("relay data zmalloc");
425 ret
= recv(control_sock
, data
, len
, MSG_WAITALL
);
426 } while (ret
< 0 && errno
== EINTR
);
428 fprintf(stderr
, "Error receiving trace packet\n");
440 int get_next_index(void)
442 struct lttng_viewer_cmd cmd
;
443 struct lttng_viewer_get_next_index rq
;
444 struct lttng_viewer_index rp
;
448 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
449 cmd
.data_size
= sizeof(rq
);
452 for (id
= 0; id
< session
->stream_count
; id
++) {
453 if (session
->streams
[id
].metadata_flag
) {
456 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
460 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
461 } while (ret
< 0 && errno
== EINTR
);
463 fprintf(stderr
, "Error sending cmd\n");
467 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
468 } while (ret
< 0 && errno
== EINTR
);
470 fprintf(stderr
, "Error sending get_next_index request\n");
474 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
475 } while (ret
< 0 && errno
== EINTR
);
477 fprintf(stderr
, "Error receiving index response\n");
481 rp
.flags
= be32toh(rp
.flags
);
483 switch (be32toh(rp
.status
)) {
484 case LTTNG_VIEWER_INDEX_INACTIVE
:
485 fprintf(stderr
, "(INACTIVE)\n");
487 case LTTNG_VIEWER_INDEX_OK
:
489 case LTTNG_VIEWER_INDEX_RETRY
:
492 case LTTNG_VIEWER_INDEX_HUP
:
493 fprintf(stderr
, "(HUP)\n");
494 session
->streams
[id
].id
= -1ULL;
495 session
->streams
[id
].fd
= -1;
497 case LTTNG_VIEWER_INDEX_ERR
:
498 fprintf(stderr
, "(ERR)\n");
502 fprintf(stderr
, "SHOULD NOT HAPPEN\n");
506 if (!first_packet_stream_id
) {
507 first_packet_offset
= be64toh(rp
.offset
);
508 first_packet_len
= be64toh(rp
.packet_size
) / CHAR_BIT
;
509 first_packet_stream_id
= id
;
519 int get_data_packet(int id
, uint64_t offset
,
522 struct lttng_viewer_cmd cmd
;
523 struct lttng_viewer_get_packet rq
;
524 struct lttng_viewer_trace_packet rp
;
527 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
528 cmd
.data_size
= sizeof(rq
);
531 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
532 /* Already in big endian. */
534 rq
.len
= htobe32(len
);
537 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
538 } while (ret
< 0 && errno
== EINTR
);
540 fprintf(stderr
, "Error sending cmd\n");
544 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
545 } while (ret
< 0 && errno
== EINTR
);
547 fprintf(stderr
, "Error sending get_data_packet request\n");
551 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
552 } while (ret
< 0 && errno
== EINTR
);
554 fprintf(stderr
, "Error receiving data response\n");
557 rp
.flags
= be32toh(rp
.flags
);
559 switch (be32toh(rp
.status
)) {
560 case LTTNG_VIEWER_GET_PACKET_OK
:
562 case LTTNG_VIEWER_GET_PACKET_RETRY
:
563 fprintf(stderr
, "RETRY\n");
566 case LTTNG_VIEWER_GET_PACKET_ERR
:
567 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
568 fprintf(stderr
, "NEW_METADATA\n");
572 fprintf(stderr
, "ERR\n");
576 fprintf(stderr
, "UNKNOWN\n");
581 len
= be32toh(rp
.len
);
586 if (len
> mmap_size
) {
587 fprintf(stderr
, "mmap_size not big enough\n");
593 ret
= recv(control_sock
, session
->streams
[id
].mmap_base
, len
, MSG_WAITALL
);
594 } while (ret
< 0 && errno
== EINTR
);
596 fprintf(stderr
, "Error receiving trace packet\n");
606 int main(int argc
, char **argv
)
611 plan_tests(NUM_TESTS
);
613 diag("Live unit tests");
615 ret
= connect_viewer("localhost");
616 ok(ret
== 0, "Connect viewer to relayd");
618 ret
= establish_connection();
619 ok(ret
== 0, "Established connection and version check with %d.%d",
620 VERSION_MAJOR
, VERSION_MINOR
);
622 ret
= list_sessions(&session_id
);
623 ok(ret
> 0, "List sessions : %d session(s)", ret
);
625 ret
= create_viewer_session();
626 ok(ret
== 0, "Create viewer session");
628 ret
= attach_session(session_id
);
629 ok(ret
> 0, "Attach to session, %d streams received", ret
);
631 ret
= get_metadata();
632 ok(ret
> 0, "Get metadata, received %d bytes", ret
);
634 ret
= get_next_index();
635 ok(ret
== 0, "Get one index per stream");
637 ret
= get_data_packet(first_packet_stream_id
, first_packet_offset
,
639 ok(ret
== first_packet_len
,
640 "Get one data packet for stream %d, offset %d, len %d",
641 first_packet_stream_id
, first_packet_offset
,
644 return exit_status();