ust: collect many channels and start work on pipe listener
[ust.git] / hello / hello.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/mman.h>
4 #include <stdarg.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 #include "../libmarkers/marker.h"
10 #include "usterr.h"
11 #include "tracer.h"
12 #include "marker-control.h"
13 #include "relay.h"
14
15
16 char consumer_stack[10000];
17 char trace_name[] = "theusttrace";
18 char trace_type[] = "ustrelay";
19
20 #define CPRINTF(fmt, args...) safe_printf(fmt "\n", ## args)
21
22 int 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
37 struct consumer_channel {
38 int fd;
39 struct ltt_channel_struct *chan;
40 };
41
42 int consumer(void *arg)
43 {
44 int result;
45 int fd;
46 char str[] = "Hello, this is the consumer.\n";
47 struct ltt_trace_struct *trace;
48 struct consumer_channel *consumer_channels;
49 int i;
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
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 }
65
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];
70
71 consumer_channels[i].chan = chan;
72
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
81 }
82 CPRINTF("done opening trace files");
83
84 for(;;) {
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 }
103 }
104
105 sleep(1);
106 }
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 // }
145 }
146
147 void 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 }
156
157 void 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
164 void inthandler(int sig)
165 {
166 printf("in handler\n");
167 exit(0);
168 }
169
170 int 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
196 int main()
197 {
198 int result;
199 int i;
200
201 init_int_handler();
202
203 init_ustrelay_transport();
204
205 printf("page size is %d\n", sysconf(_SC_PAGE_SIZE));
206
207 marker_control_init();
208
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);
211 //ust// result = ltt_probe_register(&default_probe);
212 //ust// if(result)
213 //ust// ERR("ltt_probe_register");
214
215 //result = ltt_marker_connect("metadata", "testev", "default");
216 //if(result)
217 // ERR("ltt_marker_connect");
218 result = ltt_marker_connect("foo", "bar", "default");
219 if(result)
220 ERR("ltt_marker_connect");
221
222 result = ltt_trace_setup(trace_name);
223 if(result < 0) {
224 ERR("ltt_trace_setup failed");
225 return 1;
226 }
227
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 }
233
234 result = ltt_trace_alloc(trace_name);
235 if(result < 0) {
236 ERR("ltt_trace_alloc failed");
237 return 1;
238 }
239
240 result = ltt_trace_start(trace_name);
241 if(result < 0) {
242 ERR("ltt_trace_start failed");
243 return 1;
244 }
245
246 start_consumer();
247 printf("Hello, World!\n");
248
249 sleep(1);
250 for(i=0; i<50; i++) {
251 //trace_mark(abc, testmark, "", MARK_NOARGS);
252 //trace_mark(metadata, testev, "", MARK_NOARGS);
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());
261 usleep(100000);
262 }
263
264 ltt_trace_stop(trace_name);
265 ltt_trace_destroy(trace_name);
266
267 scanf("%*s");
268
269 return 0;
270 }
This page took 0.033925 seconds and 4 git commands to generate.