ust: cleanup hello.c
[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 60 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
fbd8191b 61if(consumer_channels == NULL) {
98963de4
PMF
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
fbd8191b 151 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL);
9c67dc50
PMF
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
98963de4
PMF
203 result = ltt_marker_connect("foo", "bar", "default");
204 if(result)
205 ERR("ltt_marker_connect");
b6bf28ec 206
5f54827b
PMF
207 result = ltt_trace_setup(trace_name);
208 if(result < 0) {
209 ERR("ltt_trace_setup failed");
210 return 1;
b6bf28ec
PMF
211 }
212
8d938dbd
PMF
213 result = ltt_trace_set_type(trace_name, trace_type);
214 if(result < 0) {
215 ERR("ltt_trace_set_type failed");
216 return 1;
217 }
b6bf28ec 218
5f54827b
PMF
219 result = ltt_trace_alloc(trace_name);
220 if(result < 0) {
221 ERR("ltt_trace_alloc failed");
222 return 1;
223 }
b6bf28ec 224
8d938dbd
PMF
225 result = ltt_trace_start(trace_name);
226 if(result < 0) {
227 ERR("ltt_trace_start failed");
228 return 1;
229 }
230
9c67dc50 231 start_consumer();
68c1021b 232 printf("Hello, World!\n");
59b161cd 233
9c67dc50 234 sleep(1);
98963de4 235 for(i=0; i<50; i++) {
98963de4 236 trace_mark(foo, bar, "%s", "FOOBAZ");
9c67dc50 237 usleep(100000);
8d938dbd 238 }
59b161cd 239
98963de4
PMF
240 ltt_trace_stop(trace_name);
241 ltt_trace_destroy(trace_name);
242
68c1021b
PMF
243 scanf("%*s");
244
245 return 0;
246}
c463904d
PMF
247
248MARKER_LIB
This page took 0.032245 seconds and 4 git commands to generate.