+/* Do a buffer switch. Don't switch if buffer is completely empty */
+static void flush_buffer(struct ltt_buf *ltt_buf)
+{
+
+
+}
+
+static inline int ltt_buffer_get(struct ltt_buf *ltt_buf,
+ unsigned int *offset)
+{
+ unsigned int consumed_old, consumed_idx;
+ consumed_old = atomic_read(<t_buf->consumed);
+ consumed_idx = SUBBUF_INDEX(consumed_old, ltt_buf);
+
+ if(atomic_read(<t_buf->commit_count[consumed_idx])
+ != atomic_read(<t_buf->reserve_count[consumed_idx])) {
+ return -EAGAIN;
+ }
+ if((SUBBUF_TRUNC(atomic_read(<t_buf->offset), ltt_buf)
+ -SUBBUF_TRUNC(consumed_old, ltt_buf)) == 0) {
+ return -EAGAIN;
+ }
+
+ *offset = consumed_old;
+
+ return 0;
+}
+
+static inline int ltt_buffer_put(struct ltt_buf *ltt_buf,
+ unsigned int offset)
+{
+ unsigned int consumed_old, consumed_new;
+ int ret;
+
+ consumed_old = offset;
+ consumed_new = SUBBUF_ALIGN(consumed_old, ltt_buf);
+ if(atomic_cmpxchg(<t_buf->consumed, consumed_old, consumed_new)
+ != consumed_old) {
+ /* We have been pushed by the writer : the last buffer read _is_
+ * corrupted!
+ * It can also happen if this is a buffer we never got. */
+ return -EIO;
+ } else {
+ if(atomic_read(<t_buf->full) == 1) {
+ /* tell the client that buffer is now unfull */
+ ret = futex(<t_buf->full, FUTEX_WAKE, 1, NULL, NULL, 0);
+ if(ret != 1) {
+ printf("LTT warning : race condition : writer not waiting or too many writers\n");
+ }
+ atomic_set(<t_buf->full, 0);
+ }
+ }
+}
+
+/* In the writer :
+ *
+ * if(buffer full condition) {
+ * put myself in the wait queue
+ * ltt_buf->full = 1;
+ * schedule
+ * }
+ *{
+ if(buffer_is_full) {
+ atomic_set(<t_buf->full, 1);
+ ret = futex(<t_buf->full, 1, NULL, NULL, 0);
+ }
+}
+
+ */
+
+static int read_subbuffer(struct ltt_buf *ltt_buf, int fd)
+{
+ int err;
+ printf("LTT read buffer\n");
+
+
+ err = ltt_buffer_get(&shared_trace_info->channel.cpu, &consumed_old);
+ if(err != -EAGAIN && err != 0) {
+ printf("LTT Reserving sub buffer failed\n");
+ goto get_error;
+ }
+
+ err = TEMP_FAILURE_RETRY(write(fd,
+ ltt_buf->start
+ + (consumed_old & ((ltt_buf->alloc_size)-1)),
+ ltt_buf->subbuf_size));
+
+ if(err < 0) {
+ perror("Error in writing to file");
+ goto write_error;
+ }
+#if 0
+ err = fsync(pair->trace);
+ if(err < 0) {
+ ret = errno;
+ perror("Error in writing to file");
+ goto write_error;
+ }
+#endif //0
+write_error:
+ err = ltt_buffer_put(&shared_trace_info->channel.cpu, consumed_old);
+
+ if(err != 0) {
+ if(err == -EIO) {
+ perror("Reader has been pushed by the writer, last subbuffer corrupted.");
+ /* FIXME : we may delete the last written buffer if we wish. */
+ }
+ goto get_error;
+ }
+
+get_error:
+ return err;
+}