2 * Copyright (C) 2009 Pierre-Marc Fournier
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
33 struct list_head buffers
= LIST_HEAD_INIT(buffers
);
46 int file_fd
; /* output file */
48 struct list_head list
;
53 /* return value: 0 = subbuffer is finished, it won't produce data anymore
54 * 1 = got subbuffer successfully
58 int get_subbuffer(struct buffer_info
*buf
)
66 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
67 result
= send_message(buf
->pid
, send_msg
, &received_msg
);
69 ERR("get_subbuffer: send_message failed");
74 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
75 if(result
!= 2 && result
!= 1) {
76 ERR("unable to parse response to get_subbuffer");
80 DBG("received msg is %s", received_msg
);
82 if(!strcmp(rep_code
, "OK")) {
83 DBG("got subbuffer %s", buf
->name
);
86 else if(nth_token_is(received_msg
, "END", 0) == 1) {
90 DBG("error getting subbuffer %s", buf
->name
);
94 /* FIMXE: free correctly the stuff */
100 int put_subbuffer(struct buffer_info
*buf
)
108 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
109 result
= send_message(buf
->pid
, send_msg
, &received_msg
);
111 ERR("put_subbuffer: send_message failed");
116 result
= sscanf(received_msg
, "%as", &rep_code
);
118 ERR("unable to parse response to put_subbuffer");
123 if(!strcmp(rep_code
, "OK")) {
124 DBG("subbuffer put %s", buf
->name
);
128 ERR("invalid response to put_subbuffer");
135 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
137 const char *bufc
= (const char *) buf
;
141 result
= write(fd
, bufc
, count
);
153 return bufc
-(const char *)buf
;
156 void *consumer_thread(void *arg
)
158 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
162 result
= get_subbuffer(buf
);
164 ERR("error getting subbuffer");
172 /* write data to file */
173 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
176 /* FIXME: maybe drop this trace */
179 result
= put_subbuffer(buf
);
181 ERR("error putting subbuffer");
186 DBG("thread for buffer %s is stopping", buf
->name
);
191 int add_buffer(pid_t pid
, char *bufname
)
193 struct buffer_info
*buf
;
201 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
203 ERR("add_buffer: insufficient memory");
211 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
212 send_message(pid
, send_msg
, &received_msg
);
214 DBG("got buffer name %s", buf
->name
);
216 result
= sscanf(received_msg
, "%d", &buf
->shmid
);
218 ERR("unable to parse response to get_shmid");
222 DBG("got shmid %d", buf
->shmid
);
225 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
226 send_message(pid
, send_msg
, &received_msg
);
229 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
231 ERR("unable to parse response to get_n_subbufs");
235 DBG("got n_subbufs %d", buf
->n_subbufs
);
237 /* get subbuf size */
238 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
239 send_message(pid
, send_msg
, &received_msg
);
242 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
244 ERR("unable to parse response to get_subbuf_size");
248 DBG("got subbuf_size %d", buf
->subbuf_size
);
251 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
252 if(buf
->mem
== (void *) 0) {
256 DBG("successfully attached memory");
258 /* open file for output */
259 asprintf(&tmp
, "/tmp/trace/%s_0", buf
->name
);
260 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
268 //list_add(&buf->list, &buffers);
270 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
275 int main(int argc
, char **argv
)
277 struct ustcomm_ustd ustd
;
280 result
= ustcomm_init_ustd(&ustd
);
282 ERR("failed to initialize socket");
290 /* check for requests on our public socket */
291 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
293 ERR("error in ustcomm_ustd_recv_message");
297 if(!strncmp(recvbuf
, "collect", 7)) {
302 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
304 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
307 result
= add_buffer(pid
, bufname
);
309 ERR("error in add_buffer");
This page took 0.038257 seconds and 4 git commands to generate.