d5884dfe040c8bed7667a03e06784e444023c0e1
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
);
38 struct ustcomm_connection conn
;
47 int file_fd
; /* output file */
49 struct list_head list
;
54 /* return value: 0 = subbuffer is finished, it won't produce data anymore
55 * 1 = got subbuffer successfully
59 int get_subbuffer(struct buffer_info
*buf
)
67 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
68 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
70 ERR("get_subbuffer: ustcomm_send_request failed");
75 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
76 if(result
!= 2 && result
!= 1) {
77 ERR("unable to parse response to get_subbuffer");
81 DBG("received msg is %s", received_msg
);
83 if(!strcmp(rep_code
, "OK")) {
84 DBG("got subbuffer %s", buf
->name
);
87 else if(nth_token_is(received_msg
, "END", 0) == 1) {
91 DBG("error getting subbuffer %s", buf
->name
);
95 /* FIMXE: free correctly the stuff */
101 int put_subbuffer(struct buffer_info
*buf
)
109 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
110 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
112 ERR("put_subbuffer: send_message failed");
117 result
= sscanf(received_msg
, "%as", &rep_code
);
119 ERR("unable to parse response to put_subbuffer");
124 if(!strcmp(rep_code
, "OK")) {
125 DBG("subbuffer put %s", buf
->name
);
129 ERR("invalid response to put_subbuffer");
136 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
138 const char *bufc
= (const char *) buf
;
142 result
= write(fd
, bufc
, count
);
154 return bufc
-(const char *)buf
;
157 void *consumer_thread(void *arg
)
159 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
163 result
= get_subbuffer(buf
);
165 ERR("error getting subbuffer");
173 /* write data to file */
174 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
177 /* FIXME: maybe drop this trace */
180 result
= put_subbuffer(buf
);
182 ERR("error putting subbuffer");
187 DBG("thread for buffer %s is stopping", buf
->name
);
192 int add_buffer(pid_t pid
, char *bufname
)
194 struct buffer_info
*buf
;
202 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
204 ERR("add_buffer: insufficient memory");
212 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
214 ERR("unable to connect to process");
219 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
220 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
222 DBG("got buffer name %s", buf
->name
);
224 result
= sscanf(received_msg
, "%d", &buf
->shmid
);
226 ERR("unable to parse response to get_shmid");
230 DBG("got shmid %d", buf
->shmid
);
233 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
234 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
237 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
239 ERR("unable to parse response to get_n_subbufs");
243 DBG("got n_subbufs %d", buf
->n_subbufs
);
245 /* get subbuf size */
246 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
247 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
250 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
252 ERR("unable to parse response to get_subbuf_size");
256 DBG("got subbuf_size %d", buf
->subbuf_size
);
259 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
260 if(buf
->mem
== (void *) 0) {
264 DBG("successfully attached memory");
266 /* open file for output */
267 asprintf(&tmp
, "/tmp/trace/%s_0", buf
->name
);
268 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
276 //list_add(&buf->list, &buffers);
278 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
283 int main(int argc
, char **argv
)
285 struct ustcomm_ustd ustd
;
288 result
= ustcomm_init_ustd(&ustd
);
290 ERR("failed to initialize socket");
298 /* check for requests on our public socket */
299 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
301 ERR("error in ustcomm_ustd_recv_message");
305 if(!strncmp(recvbuf
, "collect", 7)) {
310 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
312 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
315 result
= add_buffer(pid
, bufname
);
317 ERR("error in add_buffer");
This page took 0.035779 seconds and 3 git commands to generate.