Tests: remove declaration already present in utils.sh
[lttng-tools.git] / tests / regression / tools / live / live_test.c
1 /*
2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <common/compat/time.h>
25 #include <sys/types.h>
26 #include <inttypes.h>
27 #include <stdlib.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <fcntl.h>
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34
35 #include <tap/tap.h>
36 #include <lttng/lttng.h>
37
38 #include <urcu/list.h>
39 #include <bin/lttng-sessiond/session.h>
40 #include <common/common.h>
41
42 #include <bin/lttng-relayd/lttng-viewer-abi.h>
43 #include <common/index/ctf-index.h>
44
45 #include <common/compat/endian.h>
46
47 #define SESSION1 "test1"
48 #define RELAYD_URL "net://localhost"
49 #define LIVE_TIMER 2000000
50
51 /* Number of TAP tests in this file */
52 #define NUM_TESTS 11
53 #define mmap_size 524288
54
55 int ust_consumerd32_fd;
56 int ust_consumerd64_fd;
57
58 static int control_sock;
59 struct live_session *session;
60
61 static int first_packet_offset;
62 static int first_packet_len;
63 static int first_packet_stream_id = -1;
64
65 struct viewer_stream {
66 uint64_t id;
67 uint64_t ctf_trace_id;
68 void *mmap_base;
69 int fd;
70 int metadata_flag;
71 int first_read;
72 char path[PATH_MAX];
73 };
74
75 struct live_session {
76 struct viewer_stream *streams;
77 uint64_t live_timer_interval;
78 uint64_t stream_count;
79 };
80
81 static
82 ssize_t lttng_live_recv(int fd, void *buf, size_t len)
83 {
84 ssize_t ret;
85 size_t copied = 0, to_copy = len;
86
87 do {
88 ret = recv(fd, buf + copied, to_copy, 0);
89 if (ret > 0) {
90 assert(ret <= to_copy);
91 copied += ret;
92 to_copy -= ret;
93 }
94 } while ((ret > 0 && to_copy > 0)
95 || (ret < 0 && errno == EINTR));
96 if (ret > 0)
97 ret = copied;
98 /* ret = 0 means orderly shutdown, ret < 0 is error. */
99 return ret;
100 }
101
102 static
103 ssize_t lttng_live_send(int fd, const void *buf, size_t len)
104 {
105 ssize_t ret;
106
107 do {
108 ret = send(fd, buf, len, MSG_NOSIGNAL);
109 } while (ret < 0 && errno == EINTR);
110 return ret;
111 }
112
113 static
114 int connect_viewer(char *hostname)
115 {
116 struct hostent *host;
117 struct sockaddr_in server_addr;
118 int ret;
119
120 host = gethostbyname(hostname);
121 if (!host) {
122 ret = -1;
123 goto end;
124 }
125
126 if ((control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
127 PERROR("Socket");
128 ret = -1;
129 goto end;
130 }
131
132 server_addr.sin_family = AF_INET;
133 server_addr.sin_port = htons(5344);
134 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
135 bzero(&(server_addr.sin_zero), 8);
136
137 if (connect(control_sock, (struct sockaddr *) &server_addr,
138 sizeof(struct sockaddr)) == -1) {
139 PERROR("Connect");
140 ret = -1;
141 goto end;
142 }
143
144 server_addr.sin_family = AF_INET;
145 server_addr.sin_port = htons(5345);
146 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
147 bzero(&(server_addr.sin_zero), 8);
148
149 ret = 0;
150
151 end:
152 return ret;
153 }
154
155 int establish_connection(void)
156 {
157 struct lttng_viewer_cmd cmd;
158 struct lttng_viewer_connect connect;
159 ssize_t ret_len;
160
161 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
162 cmd.data_size = htobe64(sizeof(connect));
163 cmd.cmd_version = htobe32(0);
164
165 memset(&connect, 0, sizeof(connect));
166 connect.major = htobe32(VERSION_MAJOR);
167 connect.minor = htobe32(VERSION_MINOR);
168 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
169
170 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
171 if (ret_len < 0) {
172 diag("Error sending cmd");
173 goto error;
174 }
175 ret_len = lttng_live_send(control_sock, &connect, sizeof(connect));
176 if (ret_len < 0) {
177 diag("Error sending version");
178 goto error;
179 }
180
181 ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect));
182 if (ret_len == 0) {
183 diag("[error] Remote side has closed connection");
184 goto error;
185 }
186 if (ret_len < 0) {
187 diag("Error receiving version");
188 goto error;
189 }
190 return 0;
191
192 error:
193 return -1;
194 }
195
196 /*
197 * Returns the number of sessions, should be 1 during the unit test.
198 */
199 int list_sessions(uint64_t *session_id)
200 {
201 struct lttng_viewer_cmd cmd;
202 struct lttng_viewer_list_sessions list;
203 struct lttng_viewer_session lsession;
204 int i;
205 ssize_t ret_len;
206 int first_session = 0;
207
208 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
209 cmd.data_size = htobe64(0);
210 cmd.cmd_version = htobe32(0);
211
212 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
213 if (ret_len < 0) {
214 diag("Error sending cmd");
215 goto error;
216 }
217
218 ret_len = lttng_live_recv(control_sock, &list, sizeof(list));
219 if (ret_len == 0) {
220 diag("[error] Remote side has closed connection");
221 goto error;
222 }
223 if (ret_len < 0) {
224 diag("Error receiving session list");
225 goto error;
226 }
227
228 for (i = 0; i < be32toh(list.sessions_count); i++) {
229 ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession));
230 if (ret_len < 0) {
231 diag("Error receiving session");
232 goto error;
233 }
234 if (lsession.streams > 0 && first_session <= 0) {
235 first_session = be64toh(lsession.id);
236 *session_id = first_session;
237 }
238 }
239
240 return be32toh(list.sessions_count);
241
242 error:
243 return -1;
244 }
245
246 int create_viewer_session(void)
247 {
248 struct lttng_viewer_cmd cmd;
249 struct lttng_viewer_create_session_response resp;
250 ssize_t ret_len;
251
252 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
253 cmd.data_size = htobe64(0);
254 cmd.cmd_version = htobe32(0);
255
256 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
257 if (ret_len < 0) {
258 diag("[error] Error sending cmd");
259 goto error;
260 }
261 assert(ret_len == sizeof(cmd));
262
263 ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));
264 if (ret_len == 0) {
265 diag("[error] Remote side has closed connection");
266 goto error;
267 }
268 if (ret_len < 0) {
269 diag("[error] Error receiving create session reply");
270 goto error;
271 }
272 assert(ret_len == sizeof(resp));
273
274 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
275 diag("[error] Error creating viewer session");
276 goto error;
277 }
278 return 0;
279
280 error:
281 return -1;
282 }
283
284 int attach_session(uint64_t id)
285 {
286 struct lttng_viewer_cmd cmd;
287 struct lttng_viewer_attach_session_request rq;
288 struct lttng_viewer_attach_session_response rp;
289 struct lttng_viewer_stream stream;
290 int i;
291 ssize_t ret_len;
292
293 session = zmalloc(sizeof(struct live_session));
294 if (!session) {
295 goto error;
296 }
297
298 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
299 cmd.data_size = htobe64(sizeof(rq));
300 cmd.cmd_version = htobe32(0);
301
302 memset(&rq, 0, sizeof(rq));
303 rq.session_id = htobe64(id);
304 rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
305
306 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
307 if (ret_len < 0) {
308 diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION");
309 goto error;
310 }
311 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
312 if (ret_len < 0) {
313 diag("Error sending attach request");
314 goto error;
315 }
316
317 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
318 if (ret_len == 0) {
319 diag("[error] Remote side has closed connection");
320 goto error;
321 }
322 if (ret_len < 0) {
323 diag("Error receiving attach response");
324 goto error;
325 }
326 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
327 goto error;
328 }
329
330 session->stream_count = be32toh(rp.streams_count);
331 if (session->stream_count == 0) {
332 diag("Got session stream count == 0");
333 goto error;
334 }
335 session->streams = zmalloc(session->stream_count *
336 sizeof(struct viewer_stream));
337 if (!session->streams) {
338 goto error;
339 }
340
341 for (i = 0; i < be32toh(rp.streams_count); i++) {
342 ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream));
343 if (ret_len == 0) {
344 diag("[error] Remote side has closed connection");
345 goto error;
346 }
347 if (ret_len < 0) {
348 diag("Error receiving stream");
349 goto error;
350 }
351 session->streams[i].id = be64toh(stream.id);
352
353 session->streams[i].ctf_trace_id = be64toh(stream.ctf_trace_id);
354 session->streams[i].first_read = 1;
355 session->streams[i].mmap_base = mmap(NULL, mmap_size,
356 PROT_READ | PROT_WRITE,
357 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
358 if (session->streams[i].mmap_base == MAP_FAILED) {
359 diag("mmap error");
360 goto error;
361 }
362
363 if (be32toh(stream.metadata_flag)) {
364 session->streams[i].metadata_flag = 1;
365 }
366 }
367 return session->stream_count;
368
369 error:
370 return -1;
371 }
372
373 int get_metadata(void)
374 {
375 struct lttng_viewer_cmd cmd;
376 struct lttng_viewer_get_metadata rq;
377 struct lttng_viewer_metadata_packet rp;
378 ssize_t ret_len;
379 int ret;
380 uint64_t i;
381 char *data = NULL;
382 uint64_t len = 0;
383 int metadata_stream_id = -1;
384
385 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
386 cmd.data_size = htobe64(sizeof(rq));
387 cmd.cmd_version = htobe32(0);
388
389 for (i = 0; i < session->stream_count; i++) {
390 if (session->streams[i].metadata_flag) {
391 metadata_stream_id = i;
392 break;
393 }
394 }
395
396 if (metadata_stream_id < 0) {
397 diag("No metadata stream found");
398 goto error;
399 }
400
401 rq.stream_id = htobe64(session->streams[metadata_stream_id].id);
402
403 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
404 if (ret_len < 0) {
405 diag("Error sending cmd");
406 goto error;
407 }
408 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
409 if (ret_len < 0) {
410 diag("Error sending get_metadata request");
411 goto error;
412 }
413 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
414 if (ret_len == 0) {
415 diag("[error] Remote side has closed connection");
416 goto error;
417 }
418 if (ret_len < 0) {
419 diag("Error receiving metadata response");
420 goto error;
421 }
422 switch (be32toh(rp.status)) {
423 case LTTNG_VIEWER_METADATA_OK:
424 break;
425 case LTTNG_VIEWER_NO_NEW_METADATA:
426 diag("Got LTTNG_VIEWER_NO_NEW_METADATA:");
427 ret = 0;
428 goto end;
429 case LTTNG_VIEWER_METADATA_ERR:
430 diag("Got LTTNG_VIEWER_METADATA_ERR:");
431 goto error;
432 default:
433 diag("Got unknown status during LTTNG_VIEWER_GET_METADATA");
434 goto error;
435 }
436
437 len = be64toh(rp.len);
438 if (len <= 0) {
439 goto error;
440 }
441
442 data = zmalloc(len);
443 if (!data) {
444 PERROR("relay data zmalloc");
445 goto error;
446 }
447 ret_len = lttng_live_recv(control_sock, data, len);
448 if (ret_len == 0) {
449 diag("[error] Remote side has closed connection");
450 goto error_free_data;
451 }
452 if (ret_len < 0) {
453 diag("Error receiving trace packet");
454 goto error_free_data;
455 }
456 free(data);
457 ret = len;
458 end:
459 return ret;
460
461 error_free_data:
462 free(data);
463 error:
464 return -1;
465 }
466
467 int get_next_index(void)
468 {
469 struct lttng_viewer_cmd cmd;
470 struct lttng_viewer_get_next_index rq;
471 struct lttng_viewer_index rp;
472 ssize_t ret_len;
473 int id;
474
475 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
476 cmd.data_size = htobe64(sizeof(rq));
477 cmd.cmd_version = htobe32(0);
478
479 for (id = 0; id < session->stream_count; id++) {
480 if (session->streams[id].metadata_flag) {
481 continue;
482 }
483 memset(&rq, 0, sizeof(rq));
484 rq.stream_id = htobe64(session->streams[id].id);
485
486 retry:
487 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
488 if (ret_len < 0) {
489 diag("Error sending cmd");
490 goto error;
491 }
492 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
493 if (ret_len < 0) {
494 diag("Error sending get_next_index request");
495 goto error;
496 }
497 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
498 if (ret_len == 0) {
499 diag("[error] Remote side has closed connection");
500 goto error;
501 }
502 if (ret_len < 0) {
503 diag("Error receiving index response");
504 goto error;
505 }
506
507 rp.flags = be32toh(rp.flags);
508
509 switch (be32toh(rp.status)) {
510 case LTTNG_VIEWER_INDEX_INACTIVE:
511 /* Skip this stream. */
512 diag("Got LTTNG_VIEWER_INDEX_INACTIVE");
513 continue;
514 case LTTNG_VIEWER_INDEX_OK:
515 break;
516 case LTTNG_VIEWER_INDEX_RETRY:
517 sleep(1);
518 goto retry;
519 case LTTNG_VIEWER_INDEX_HUP:
520 diag("Got LTTNG_VIEWER_INDEX_HUP");
521 session->streams[id].id = -1ULL;
522 session->streams[id].fd = -1;
523 goto error;
524 case LTTNG_VIEWER_INDEX_ERR:
525 diag("Got LTTNG_VIEWER_INDEX_ERR");
526 goto error;
527 default:
528 diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp.status));
529 goto error;
530 }
531 if (first_packet_stream_id < 0) {
532 /*
533 * Initialize the first packet stream id. That is,
534 * the first active stream encoutered.
535 */
536 first_packet_offset = be64toh(rp.offset);
537 first_packet_len = be64toh(rp.packet_size) / CHAR_BIT;
538 first_packet_stream_id = id;
539 diag("Got first packet index with offset %d and len %d",
540 first_packet_offset, first_packet_len);
541 }
542 }
543 return 0;
544
545 error:
546 return -1;
547 }
548
549 static
550 int get_data_packet(int id, uint64_t offset,
551 uint64_t len)
552 {
553 struct lttng_viewer_cmd cmd;
554 struct lttng_viewer_get_packet rq;
555 struct lttng_viewer_trace_packet rp;
556 ssize_t ret_len;
557
558 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
559 cmd.data_size = htobe64(sizeof(rq));
560 cmd.cmd_version = htobe32(0);
561
562 memset(&rq, 0, sizeof(rq));
563 rq.stream_id = htobe64(session->streams[id].id);
564 /* Already in big endian. */
565 rq.offset = offset;
566 rq.len = htobe32(len);
567
568 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
569 if (ret_len < 0) {
570 diag("Error sending cmd");
571 goto error;
572 }
573 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
574 if (ret_len < 0) {
575 diag("Error sending get_data_packet request");
576 goto error;
577 }
578 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
579 if (ret_len == 0) {
580 diag("[error] Remote side has closed connection");
581 goto error;
582 }
583 if (ret_len < 0) {
584 diag("Error receiving data response");
585 goto error;
586 }
587 rp.flags = be32toh(rp.flags);
588
589 switch (be32toh(rp.status)) {
590 case LTTNG_VIEWER_GET_PACKET_OK:
591 len = be32toh(rp.len);
592 if (len == 0) {
593 diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0");
594 goto error;
595 }
596 break;
597 case LTTNG_VIEWER_GET_PACKET_RETRY:
598 diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:");
599 goto error;
600 case LTTNG_VIEWER_GET_PACKET_ERR:
601 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
602 diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag");
603 goto end;
604 }
605 diag("Got LTTNG_VIEWER_GET_PACKET_ERR:");
606 goto error;
607 default:
608 diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET");
609 goto error;
610 }
611
612 if (len > mmap_size) {
613 diag("mmap_size not big enough");
614 goto error;
615 }
616
617 ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len);
618 if (ret_len == 0) {
619 diag("[error] Remote side has closed connection");
620 goto error;
621 }
622 if (ret_len < 0) {
623 diag("Error receiving trace packet");
624 goto error;
625 }
626 end:
627 return 0;
628 error:
629 return -1;
630 }
631
632 int detach_viewer_session(uint64_t id)
633 {
634 struct lttng_viewer_cmd cmd;
635 struct lttng_viewer_detach_session_response resp;
636 struct lttng_viewer_detach_session_request rq;
637 int ret;
638 ssize_t ret_len;
639
640 cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION);
641 cmd.data_size = htobe64(sizeof(rq));
642 cmd.cmd_version = htobe32(0);
643
644 memset(&rq, 0, sizeof(rq));
645 rq.session_id = htobe64(id);
646
647 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
648 if (ret_len < 0) {
649 fprintf(stderr, "[error] Error sending cmd\n");
650 ret = ret_len;
651 goto error;
652 }
653
654 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
655 if (ret_len < 0) {
656 fprintf(stderr, "Error sending attach request\n");
657 ret = ret_len;
658 goto error;
659 }
660
661 ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));
662 if (ret_len < 0) {
663 fprintf(stderr, "[error] Error receiving detach session reply\n");
664 ret = ret_len;
665 goto error;
666 }
667
668 if (be32toh(resp.status) != LTTNG_VIEWER_DETACH_SESSION_OK) {
669 fprintf(stderr, "[error] Error detaching viewer session\n");
670 ret = -1;
671 goto error;
672 }
673 ret = 0;
674
675 error:
676 return ret;
677 }
678
679 int main(int argc, char **argv)
680 {
681 int ret;
682 uint64_t session_id;
683
684 plan_tests(NUM_TESTS);
685
686 diag("Live unit tests");
687
688 ret = connect_viewer("localhost");
689 ok(ret == 0, "Connect viewer to relayd");
690
691 ret = establish_connection();
692 ok(ret == 0, "Established connection and version check with %d.%d",
693 VERSION_MAJOR, VERSION_MINOR);
694
695 ret = list_sessions(&session_id);
696 ok(ret > 0, "List sessions : %d session(s)", ret);
697
698 ret = create_viewer_session();
699 ok(ret == 0, "Create viewer session");
700
701 ret = attach_session(session_id);
702 ok(ret > 0, "Attach to session, %d stream(s) received", ret);
703
704 ret = get_metadata();
705 ok(ret > 0, "Get metadata, received %d bytes", ret);
706
707 ret = get_next_index();
708 ok(ret == 0, "Get one index per stream");
709
710 ret = get_data_packet(first_packet_stream_id, first_packet_offset,
711 first_packet_len);
712 ok(ret == 0,
713 "Get one data packet for stream %d, offset %d, len %d",
714 first_packet_stream_id, first_packet_offset,
715 first_packet_len);
716
717 ret = detach_viewer_session(session_id);
718 ok(ret == 0, "Detach viewer session");
719
720 ret = list_sessions(&session_id);
721 ok(ret > 0, "List sessions : %d session(s)", ret);
722
723 ret = attach_session(session_id);
724 ok(ret > 0, "Attach to session, %d streams received", ret);
725
726 return exit_status();
727 }
This page took 0.044265 seconds and 4 git commands to generate.