#define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
-#include <linux/inotify.h>
+#include <sys/inotify.h>
+#if 0 /* should now be provided by libc. */
/* From the inotify-tools 2.6 package */
static inline int inotify_init (void)
{
{
return syscall (__NR_inotify_rm_watch, fd, wd);
}
+#endif //0
#define HAS_INOTIFY
#else
static inline int inotify_init (void)
int num;
};
-
+static __thread int thread_pipe[2];
struct channel_trace_fd fd_pairs = { NULL, 0 };
int inotify_fd = -1;
volatile static int quit_program = 0; /* For signal handler */
static int dump_flight_only = 0;
static int dump_normal_only = 0;
+static int verbose_mode = 0;
+
+#define printf_verbose(fmt, args...) \
+ do { \
+ if (verbose_mode) \
+ printf(fmt, ##args); \
+ } while (0)
/* Args :
*
* -t directory Directory name of the trace to write to. Will be created.
* -c directory Root directory of the debugfs trace channels.
* -d Run in background (daemon).
- * -a Trace append mode.
- * -s Send SIGUSR1 to parent when ready for IO.
+ * -a Trace append mode.
+ * -s Send SIGUSR1 to parent when ready for IO.
*/
void show_arguments(void)
{
printf("-N Number of threads to start.\n");
printf("-f Dump only flight recorder channels.\n");
printf("-n Dump only normal channels.\n");
+ printf("-v Verbose mode.\n");
printf("\n");
}
case 'n':
dump_normal_only = 1;
break;
+ case 'v':
+ verbose_mode = 1;
+ break;
default:
printf("Invalid argument '%s'.\n", argv[argn]);
printf("\n");
if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) {
if(dump_flight_only) {
- printf("Skipping normal channel %s\n", path_channel);
+ printf_verbose("Skipping normal channel %s\n",
+ path_channel);
return 0;
}
} else {
if(dump_normal_only) {
- printf("Skipping flight channel %s\n", path_channel);
+ printf_verbose("Skipping flight channel %s\n",
+ path_channel);
return 0;
}
}
- printf("Opening file.\n");
+ printf_verbose("Opening file.\n");
fd_pairs->pair = realloc(fd_pairs->pair,
++fd_pairs->num_pairs * sizeof(struct fd_pair));
ret = stat(path_trace, &stat_buf);
if(ret == 0) {
if(append_mode) {
- printf("Appending to file %s as requested\n", path_trace);
+ printf_verbose("Appending to file %s as requested\n",
+ path_trace);
fd_pairs->pair[fd_pairs->num_pairs-1].trace =
open(path_trace, O_WRONLY|O_APPEND,
goto end;
}
- printf("Creating trace subdirectory %s\n", subtrace_name);
+ printf_verbose("Creating trace subdirectory %s\n", subtrace_name);
ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
if(ret == -1) {
if(errno != EEXIST) {
iwatch_array->elem = realloc(iwatch_array->elem,
++iwatch_array->num * sizeof(struct inotify_watch));
- printf("Adding inotify for channel %s\n", path_channel);
+ printf_verbose("Adding inotify for channel %s\n", path_channel);
iwatch_array->elem[iwatch_array->num-1].wd = inotify_add_watch(*inotify_fd, path_channel, IN_CREATE);
strcpy(iwatch_array->elem[iwatch_array->num-1].path_channel, path_channel);
strcpy(iwatch_array->elem[iwatch_array->num-1].path_trace, path_trace);
- printf("Added inotify for channel %s, wd %u\n", iwatch_array->elem[iwatch_array->num-1].path_channel,
+ printf_verbose("Added inotify for channel %s, wd %u\n",
+ iwatch_array->elem[iwatch_array->num-1].path_channel,
iwatch_array->elem[iwatch_array->num-1].wd);
#endif
continue;
}
- printf("Channel file : %s\n", path_channel);
+ printf_verbose("Channel file : %s\n", path_channel);
if(S_ISDIR(stat_buf.st_mode)) {
- printf("Entering channel subdirectory...\n");
+ printf_verbose("Entering channel subdirectory...\n");
ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs,
inotify_fd, iwatch_array);
if(ret < 0) continue;
int read_subbuffer(struct fd_pair *pair)
{
- unsigned int consumed_old;
- int err, ret=0;
+ unsigned int consumed_old;
+ int err;
+ long ret;
+ unsigned long len;
+ off_t offset;
- err = ioctl(pair->channel, RELAY_GET_SUBBUF,
- &consumed_old);
- printf("cookie : %u\n", consumed_old);
+ err = ioctl(pair->channel, RELAY_GET_SUBBUF, &consumed_old);
+ printf_verbose("cookie : %u\n", consumed_old);
if(err != 0) {
ret = errno;
perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
goto get_error;
}
-
+#if 0
err = TEMP_FAILURE_RETRY(write(pair->trace,
pair->mmap
+ (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
perror("Error in writing to file");
goto write_error;
}
+#endif //0
+ len = pair->subbuf_size;
+ offset = 0;
+ while (len > 0) {
+ printf_verbose("splice chan to pipe offset %lu\n",
+ (unsigned long)offset);
+ ret = splice(pair->channel, &offset, thread_pipe[1], NULL,
+ len, SPLICE_F_MOVE);
+ printf_verbose("splice chan to pipe ret %ld\n", ret);
+ if (ret < 0) {
+ perror("Error in relay splice");
+ goto write_error;
+ }
+ ret = splice(thread_pipe[0], NULL, pair->trace, NULL,
+ ret, SPLICE_F_MOVE);
+ printf_verbose("splice pipe to file %ld\n", ret);
+ if (ret < 0) {
+ perror("Error in file splice");
+ goto write_error;
+ }
+ len -= ret;
+ }
+
#if 0
err = fsync(pair->trace);
if(err < 0) {
}
#endif //0
write_error:
+ ret = 0;
err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old);
if(err != 0) {
ret = errno;
}
-
int map_channels(struct channel_trace_fd *fd_pairs,
int idx_begin, int idx_end)
{
}
}
+#if 0
/* Mmap each FD */
for(i=idx_begin;i<idx_end;i++) {
struct fd_pair *pair = &fd_pairs->pair[i];
ret |= err_ret;
}
+#endif //0
end:
return ret;
-
-
}
-
int unmap_channels(struct channel_trace_fd *fd_pairs)
{
int j;
struct fd_pair *pair = &fd_pairs->pair[j];
int err_ret;
+#if 0
err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
if(err_ret != 0) {
perror("Error in munmap");
}
ret |= err_ret;
+#endif //0
err_ret = pthread_mutex_destroy(&pair->mutex);
if(err_ret != 0) {
perror("Error in mutex destroy");
for(i=0; i<iwatch_array->num; i++) {
if(iwatch_array->elem[i].wd == ievent->wd &&
ievent->mask == IN_CREATE) {
- printf("inotify wd %u event mask : %u for %s%s\n",
+ printf_verbose(
+ "inotify wd %u event mask : %u for %s%s\n",
ievent->wd, ievent->mask,
- iwatch_array->elem[i].path_channel, ievent->name);
+ iwatch_array->elem[i].path_channel,
+ ievent->name);
old_num = fd_pairs->num_pairs;
strcpy(path_channel, iwatch_array->elem[i].path_channel);
strcat(path_channel, ievent->name);
goto free_fd;
}
- printf("Data received\n");
+ printf_verbose("Data received\n");
#ifdef HAS_INOTIFY
switch(pollfd[0].revents) {
case POLLERR:
- printf("Error returned in polling inotify fd %d.\n", pollfd[0].fd);
+ printf_verbose(
+ "Error returned in polling inotify fd %d.\n",
+ pollfd[0].fd);
break;
case POLLHUP:
- printf("Polling inotify fd %d tells it has hung up.\n", pollfd[0].fd);
+ printf_verbose(
+ "Polling inotify fd %d tells it has hung up.\n",
+ pollfd[0].fd);
break;
case POLLNVAL:
- printf("Polling inotify fd %d tells fd is not open.\n", pollfd[0].fd);
+ printf_verbose(
+ "Polling inotify fd %d tells fd is not open.\n",
+ pollfd[0].fd);
break;
case POLLPRI:
case POLLIN:
-
- printf("Polling inotify fd %d : data ready.\n", pollfd[0].fd);
+ printf_verbose(
+ "Polling inotify fd %d : data ready.\n",
+ pollfd[0].fd);
pthread_rwlock_wrlock(&fd_pairs_lock);
read_inotify(inotify_fd, fd_pairs, iwatch_array);
for(i=inotify_fds;i<num_pollfd;i++) {
switch(pollfd[i].revents) {
case POLLERR:
- printf("Error returned in polling fd %d.\n", pollfd[i].fd);
+ printf_verbose(
+ "Error returned in polling fd %d.\n",
+ pollfd[i].fd);
num_hup++;
break;
case POLLHUP:
- printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
+ printf_verbose(
+ "Polling fd %d tells it has hung up.\n",
+ pollfd[i].fd);
num_hup++;
break;
case POLLNVAL:
- printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
+ printf_verbose(
+ "Polling fd %d tells fd is not open.\n",
+ pollfd[i].fd);
num_hup++;
break;
case POLLPRI:
pthread_rwlock_rdlock(&fd_pairs_lock);
if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
- printf("Urgent read on fd %d\n", pollfd[i].fd);
+ printf_verbose(
+ "Urgent read on fd %d\n",
+ pollfd[i].fd);
/* Take care of high priority channels first. */
high_prio = 1;
/* it's ok to have an unavailable subbuffer */
pthread_rwlock_rdlock(&fd_pairs_lock);
if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
/* Take care of low priority channels. */
- printf("Normal read on fd %d\n", pollfd[i].fd);
+ printf_verbose(
+ "Normal read on fd %d\n",
+ pollfd[i].fd);
/* it's ok to have an unavailable subbuffer */
ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
if(ret == EAGAIN) ret = 0;
long ret;
unsigned long thread_num = (unsigned long)arg;
+ ret = pipe(thread_pipe);
+ if (ret < 0) {
+ perror("Error creating pipe");
+ return (void*)ret;
+ }
ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array);
-
+ close(thread_pipe[0]); /* close read end */
+ close(thread_pipe[1]); /* close write end */
return (void*)ret;
}
if(ret = map_channels(&fd_pairs, 0, fd_pairs.num_pairs))
goto close_channel;
-
return 0;
close_channel:
show_info();
- if(daemon_mode) {
- ret = daemon(0, 0);
-
- if(ret == -1) {
- perror("An error occured while daemonizing.");
- exit(-1);
- }
- }
-
/* Connect the signal handlers */
act.sa_handler = handler;
act.sa_flags = 0;
if(ret = channels_init())
return ret;
+ if(daemon_mode) {
+ ret = daemon(0, 0);
+
+ if(ret == -1) {
+ perror("An error occured while daemonizing.");
+ exit(-1);
+ }
+ }
+
tids = malloc(sizeof(pthread_t) * num_threads);
for(i=0; i<num_threads; i++) {
}
free(tids);
-
ret = unmap_channels(&fd_pairs);
close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array);
if(inotify_fd >= 0)