ust: collect many channels and start work on pipe listener
[ust.git] / hello / hello.c
CommitLineData
68c1021b
PMF
1#include <stdio.h>
2#include <unistd.h>
b6bf28ec 3#include <sys/mman.h>
9c67dc50
PMF
4#include <stdarg.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
68c1021b 8
59b161cd 9#include "../libmarkers/marker.h"
5f54827b
PMF
10#include "usterr.h"
11#include "tracer.h"
ba6459ba 12#include "marker-control.h"
9c67dc50
PMF
13#include "relay.h"
14
15
16char consumer_stack[10000];
17char trace_name[] = "theusttrace";
18char trace_type[] = "ustrelay";
19
20#define CPRINTF(fmt, args...) safe_printf(fmt "\n", ## args)
21
22int safe_printf(const char *fmt, ...)
23{
24 static char buf[500];
25 va_list ap;
26 int n;
27
28 va_start(ap, fmt);
29
30 n = vsnprintf(buf, sizeof(buf), fmt, ap);
31
32 write(STDOUT_FILENO, buf, n);
33
34 va_end(ap);
35}
36
98963de4
PMF
37struct consumer_channel {
38 int fd;
39 struct ltt_channel_struct *chan;
40};
41
9c67dc50
PMF
42int consumer(void *arg)
43{
44 int result;
9c67dc50 45 int fd;
9c67dc50 46 char str[] = "Hello, this is the consumer.\n";
9c67dc50 47 struct ltt_trace_struct *trace;
98963de4
PMF
48 struct consumer_channel *consumer_channels;
49 int i;
9c67dc50
PMF
50
51 ltt_lock_traces();
52 trace = _ltt_trace_find(trace_name);
53 ltt_unlock_traces();
54
55 if(trace == NULL) {
56 CPRINTF("cannot find trace!");
57 return 1;
58 }
59
98963de4
PMF
60 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
61 if(consumer_channels == NULL) {
62 ERR("malloc returned NULL");
63 return 1;
64 }
9c67dc50 65
98963de4
PMF
66 CPRINTF("opening trace files");
67 for(i=0; i<trace->nr_channels; i++) {
68 char tmp[100];
69 struct ltt_channel_struct *chan = &trace->channels[i];
9c67dc50 70
98963de4 71 consumer_channels[i].chan = chan;
9c67dc50 72
98963de4
PMF
73 snprintf(tmp, sizeof(tmp), "trace/%s", chan->channel_name);
74 result = consumer_channels[i].fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 00644);
75 if(result == -1) {
76 perror("open");
77 return -1;
78 }
79 CPRINTF("\topened trace file %s", tmp);
80
9c67dc50 81 }
98963de4 82 CPRINTF("done opening trace files");
9c67dc50
PMF
83
84 for(;;) {
98963de4
PMF
85 /*wait*/
86
87 for(i=0; i<trace->nr_channels; i++) {
88 struct rchan *rchan = consumer_channels[i].chan->trans_channel_data;
89 struct rchan_buf *rbuf = rchan->buf;
90 struct ltt_channel_buf_struct *lttbuf = consumer_channels[i].chan->buf;
91 long consumed_old;
92
93 result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
94 if(result < 0) {
95 CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
96 }
97 else {
98 CPRINTF("success!");
99
100 result = write(consumer_channels[i].fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
101 ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
102 }
9c67dc50 103 }
9c67dc50
PMF
104
105 sleep(1);
106 }
98963de4
PMF
107
108// CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
109//
110// struct ltt_channel_struct *chan = &trace->channels[0];
111//
112// CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
113
114// struct rchan *rchan = chan->trans_channel_data;
115// struct rchan_buf *rbuf = rchan->buf;
116// struct ltt_channel_buf_struct *lttbuf = chan->buf;
117// long consumed_old;
118//
119// result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
120// if(result == -1) {
121// perror("open");
122// return -1;
123// }
124
125// for(;;) {
126// write(STDOUT_FILENO, str, sizeof(str));
127//
128// result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
129// if(result < 0) {
130// CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
131// }
132// else {
133// CPRINTF("success!");
134//
135// result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
136// ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
137// }
138//
139// //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(&lttbuf->offset), rbuf->chan) - consumed_old);
140// CPRINTF("Commit count %ld", local_read(&lttbuf->commit_count[0]));
141//
142//
143// sleep(1);
144// }
9c67dc50
PMF
145}
146
147void start_consumer(void)
148{
149 int result;
150
151 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM, NULL);
152 if(result == -1) {
153 perror("clone");
154 }
155}
59b161cd
PMF
156
157void probe(const struct marker *mdata,
158 void *probe_private, void *call_private,
159 const char *fmt, va_list *args)
160{
161 printf("In probe\n");
162}
163
8d938dbd
PMF
164void inthandler(int sig)
165{
166 printf("in handler\n");
167 exit(0);
168}
169
170int init_int_handler(void)
171{
172 int result;
173 struct sigaction act;
174
175 result = sigemptyset(&act.sa_mask);
176 if(result == -1) {
177 PERROR("sigemptyset");
178 return -1;
179 }
180
181 act.sa_handler = inthandler;
182 act.sa_flags = SA_RESTART;
183
184 /* Only defer ourselves. Also, try to restart interrupted
185 * syscalls to disturb the traced program as little as possible.
186 */
187 result = sigaction(SIGINT, &act, NULL);
188 if(result == -1) {
189 PERROR("sigaction");
190 return -1;
191 }
192
193 return 0;
194}
195
5f54827b 196int main()
b6bf28ec 197{
5f54827b 198 int result;
98963de4 199 int i;
5f54827b 200
8d938dbd
PMF
201 init_int_handler();
202
5f54827b
PMF
203 init_ustrelay_transport();
204
8d938dbd
PMF
205 printf("page size is %d\n", sysconf(_SC_PAGE_SIZE));
206
ba6459ba
PMF
207 marker_control_init();
208
9c67dc50
PMF
209 //marker_probe_register("abc", "testmark", "", probe, NULL);
210//ust// marker_probe_register("metadata", "core_marker_id", "channel %s name %s event_id %hu int #1u%zu long #1u%zu pointer #1u%zu size_t #1u%zu alignment #1u%u", probe, NULL);
ba6459ba
PMF
211//ust// result = ltt_probe_register(&default_probe);
212//ust// if(result)
213//ust// ERR("ltt_probe_register");
8d938dbd 214
9c67dc50
PMF
215 //result = ltt_marker_connect("metadata", "testev", "default");
216 //if(result)
217 // ERR("ltt_marker_connect");
98963de4
PMF
218 result = ltt_marker_connect("foo", "bar", "default");
219 if(result)
220 ERR("ltt_marker_connect");
b6bf28ec 221
5f54827b
PMF
222 result = ltt_trace_setup(trace_name);
223 if(result < 0) {
224 ERR("ltt_trace_setup failed");
225 return 1;
b6bf28ec
PMF
226 }
227
8d938dbd
PMF
228 result = ltt_trace_set_type(trace_name, trace_type);
229 if(result < 0) {
230 ERR("ltt_trace_set_type failed");
231 return 1;
232 }
b6bf28ec 233
5f54827b
PMF
234 result = ltt_trace_alloc(trace_name);
235 if(result < 0) {
236 ERR("ltt_trace_alloc failed");
237 return 1;
238 }
b6bf28ec 239
8d938dbd
PMF
240 result = ltt_trace_start(trace_name);
241 if(result < 0) {
242 ERR("ltt_trace_start failed");
243 return 1;
244 }
245
9c67dc50 246 start_consumer();
68c1021b 247 printf("Hello, World!\n");
59b161cd 248
9c67dc50 249 sleep(1);
98963de4 250 for(i=0; i<50; i++) {
9c67dc50
PMF
251 //trace_mark(abc, testmark, "", MARK_NOARGS);
252 //trace_mark(metadata, testev, "", MARK_NOARGS);
98963de4
PMF
253 trace_mark(foo, bar, "%s", "FOOBAZ");
254 //trace_mark(metadata, core_marker_id,
255 // "channel %s name %s event_id %hu "
256 // "int #1u%zu long #1u%zu pointer #1u%zu "
257 // "size_t #1u%zu alignment #1u%u",
258 // "abc", "def", (unsigned short)1000,
259 // sizeof(int), sizeof(long), sizeof(void *),
260 // sizeof(size_t), ltt_get_alignment());
9c67dc50 261 usleep(100000);
8d938dbd 262 }
59b161cd 263
98963de4
PMF
264 ltt_trace_stop(trace_name);
265 ltt_trace_destroy(trace_name);
266
68c1021b
PMF
267 scanf("%*s");
268
269 return 0;
270}
This page took 0.033054 seconds and 4 git commands to generate.