!config/epoll.m4
!config/config_feature.m4
-lttng-sessiond/lttng-sessiond
-lttng/lttng
-lttng-kconsumerd/lttng-kconsumerd
-lttng-consumerd/lttng-consumerd
+src/bin/lttng-sessiond/lttng-sessiond
+src/bin/lttng/lttng
+src/bin/lttng-consumerd/lttng-consumerd
tests/test_sessions
tests/test_kernel_data_trace
ACLOCAL_AMFLAGS = -I config
-SUBDIRS = common \
- liblttng-sessiond-comm \
- libkernelctl \
- liblttng-ht \
- liblttng-kconsumer \
- liblttng-ustconsumer \
- liblttng-consumer \
- lttng-consumerd
-
-if ! BUILD_CONSUMERD_ONLY
-SUBDIRS += liblttngctl \
- lttng \
- lttng-sessiond
-endif
-
-SUBDIRS += tests \
- include \
- doc
+SUBDIRS = src \
+ tests \
+ include \
+ doc
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-AM_CFLAGS = -fno-strict-aliasing
-
-noinst_LTLIBRARIES = libcommon.la
-
-libcommon_la_SOURCES = runas.c runas.h
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/wait.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sched.h>
-#include <sys/mman.h>
-
-#include <lttngerr.h>
-
-#include "runas.h"
-
-#define RUNAS_CHILD_STACK_SIZE 10485760
-
-struct run_as_data {
- int (*cmd)(void *data);
- void *data;
- uid_t uid;
- gid_t gid;
- int retval_pipe;
-};
-
-struct run_as_mkdir_data {
- const char *path;
- mode_t mode;
-};
-
-struct run_as_open_data {
- const char *path;
- int flags;
- mode_t mode;
-};
-
-/*
- * Create recursively directory using the FULL path.
- */
-static
-int _mkdir_recursive(void *_data)
-{
- struct run_as_mkdir_data *data = _data;
- const char *path;
- char *p, tmp[PATH_MAX];
- struct stat statbuf;
- mode_t mode;
- size_t len;
- int ret;
-
- path = data->path;
- mode = data->mode;
-
- ret = snprintf(tmp, sizeof(tmp), "%s", path);
- if (ret < 0) {
- PERROR("snprintf mkdir");
- goto error;
- }
-
- len = ret;
- if (tmp[len - 1] == '/') {
- tmp[len - 1] = 0;
- }
-
- for (p = tmp + 1; *p; p++) {
- if (*p == '/') {
- *p = 0;
- ret = stat(tmp, &statbuf);
- if (ret < 0) {
- ret = mkdir(tmp, mode);
- if (ret < 0) {
- if (!(errno == EEXIST)) {
- PERROR("mkdir recursive");
- ret = -errno;
- goto error;
- }
- }
- }
- *p = '/';
- }
- }
-
- ret = mkdir(tmp, mode);
- if (ret < 0) {
- if (!(errno == EEXIST)) {
- PERROR("mkdir recursive last piece");
- ret = -errno;
- } else {
- ret = 0;
- }
- }
-
-error:
- return ret;
-}
-
-static
-int _mkdir(void *_data)
-{
- struct run_as_mkdir_data *data = _data;
- return mkdir(data->path, data->mode);
-}
-
-static
-int _open(void *_data)
-{
- struct run_as_open_data *data = _data;
- return open(data->path, data->flags, data->mode);
-}
-
-static
-int child_run_as(void *_data)
-{
- struct run_as_data *data = _data;
- size_t writelen, writeleft, index;
- union {
- int i;
- char c[sizeof(int)];
- } sendret;
- int ret;
-
- /*
- * Child: it is safe to drop egid and euid while sharing the
- * file descriptors with the parent process, since we do not
- * drop "uid": therefore, the user we are dropping egid/euid to
- * cannot attach to this process with, e.g. ptrace, nor map this
- * process memory.
- */
- if (data->gid != getegid()) {
- ret = setegid(data->gid);
- if (ret < 0) {
- perror("setegid");
- return EXIT_FAILURE;
- }
- }
- if (data->uid != geteuid()) {
- ret = seteuid(data->uid);
- if (ret < 0) {
- perror("seteuid");
- return EXIT_FAILURE;
- }
- }
- /*
- * Also set umask to 0 for mkdir executable bit.
- */
- umask(0);
- sendret.i = (*data->cmd)(data->data);
- /* send back return value */
- writeleft = sizeof(sendret);
- index = 0;
- do {
- writelen = write(data->retval_pipe, &sendret.c[index],
- writeleft);
- if (writelen < 0) {
- perror("write");
- return EXIT_FAILURE;
- }
- writeleft -= writelen;
- index += writelen;
- } while (writeleft > 0);
- return EXIT_SUCCESS;
-}
-
-static
-int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
-{
- struct run_as_data run_as_data;
- int ret = 0;
- int status;
- pid_t pid;
- int retval_pipe[2];
- ssize_t readlen, readleft, index;
- void *child_stack;
- union {
- int i;
- char c[sizeof(int)];
- } retval;
-
- /*
- * If we are non-root, we can only deal with our own uid.
- */
- if (geteuid() != 0) {
- if (uid != geteuid()) {
- ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
- uid, geteuid());
- return -EPERM;
- }
- }
-
- ret = pipe(retval_pipe);
- if (ret < 0) {
- perror("pipe");
- goto end;
- }
- run_as_data.data = data;
- run_as_data.cmd = cmd;
- run_as_data.uid = uid;
- run_as_data.gid = gid;
- run_as_data.retval_pipe = retval_pipe[1]; /* write end */
- child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
- PROT_WRITE | PROT_READ,
- MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | MAP_STACK,
- -1, 0);
- if (child_stack == MAP_FAILED) {
- perror("mmap");
- ret = -ENOMEM;
- goto close_pipe;
- }
- /*
- * Pointing to the middle of the stack to support architectures
- * where the stack grows up (HPPA).
- */
- pid = clone(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
- CLONE_FILES | SIGCHLD,
- &run_as_data, NULL);
- if (pid < 0) {
- perror("clone");
- ret = pid;
- goto unmap_stack;
- }
- /* receive return value */
- readleft = sizeof(retval);
- index = 0;
- do {
- readlen = read(retval_pipe[0], &retval.c[index], readleft);
- if (readlen < 0) {
- perror("read");
- ret = -1;
- break;
- }
- readleft -= readlen;
- index += readlen;
- } while (readleft > 0);
-
- /*
- * Parent: wait for child to return, in which case the
- * shared memory map will have been created.
- */
- pid = waitpid(pid, &status, 0);
- if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- perror("wait");
- ret = -1;
- }
-unmap_stack:
- ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
- if (ret < 0) {
- perror("munmap");
- }
-close_pipe:
- close(retval_pipe[0]);
- close(retval_pipe[1]);
-end:
- return retval.i;
-}
-
-int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
-{
- struct run_as_mkdir_data data;
-
- DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
- path, mode, uid, gid);
- data.path = path;
- data.mode = mode;
- return run_as(_mkdir_recursive, &data, uid, gid);
-}
-
-int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
-{
- struct run_as_mkdir_data data;
-
- DBG3("mkdir() %s with mode %d for uid %d and gid %d",
- path, mode, uid, gid);
- data.path = path;
- data.mode = mode;
- return run_as(_mkdir, &data, uid, gid);
-}
-
-/*
- * Note: open_run_as is currently not working. We'd need to pass the fd
- * opened in the child to the parent.
- */
-int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
-{
- struct run_as_open_data data;
-
- DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
- path, flags, mode, uid, gid);
- data.path = path;
- data.flags = flags;
- data.mode = mode;
- return run_as(_open, &data, uid, gid);
-}
+++ /dev/null
-#ifndef _RUNAS_H
-#define _RUNAS_H
-
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only verion 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#include <unistd.h>
-
-int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid);
-int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
-int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid);
-
-#endif /* _RUNAS_H */
CFLAGS="-Wall $CFLAGS -g -fno-strict-aliasing"
-DEFAULT_INCLUDES="-I\$(top_srcdir) -I\$(top_builddir)"
+DEFAULT_INCLUDES="-I\$(top_srcdir) -I\$(top_builddir) -I\$(top_builddir)/src -I\$(top_builddir)/include"
lttngincludedir="${includedir}/lttng"
AC_CONFIG_FILES([
Makefile
+ doc/Makefile
include/Makefile
- common/Makefile
- libkernelctl/Makefile
- liblttng-consumer/Makefile
- liblttng-kconsumer/Makefile
- liblttng-ustconsumer/Makefile
- liblttngctl/Makefile
- liblttng-sessiond-comm/Makefile
- liblttng-ht/Makefile
- lttng-consumerd/Makefile
- lttng-sessiond/Makefile
- lttng/Makefile
+ src/Makefile
+ src/common/Makefile
+ src/common/kernel-ctl/Makefile
+ src/common/kernel-consumer/Makefile
+ src/common/ust-consumer/Makefile
+ src/common/hashtable/Makefile
+ src/common/sessiond-comm/Makefile
+ src/lib/Makefile
+ src/lib/lttng-ctl/Makefile
+ src/bin/Makefile
+ src/bin/lttng-consumerd/Makefile
+ src/bin/lttng-sessiond/Makefile
+ src/bin/lttng/Makefile
tests/Makefile
tests/ust-nevents/Makefile
tests/ust-nprocesses/Makefile
- doc/Makefile
])
AC_OUTPUT
-lttnginclude_HEADERS = lttng/lttng.h lttng/lttng-kconsumer.h \
- lttng/lttng-ustconsumer.h lttng/lttng-consumer.h
-
-noinst_HEADERS = lttngerr.h lttng-kernel.h lttng-consumerd.h lttng-share.h \
- lttng-sessiond-comm.h lttng-kernel-ctl.h lttng-ht.h
+lttnginclude_HEADERS = lttng/lttng.h
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only verion 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_CONSUMERD_H
-#define _LTTNG_CONSUMERD_H
-
-#define CONSUMERD_RUNDIR "%s"
-
-/* Kernel consumer path */
-#define KCONSUMERD_PATH CONSUMERD_RUNDIR "/kconsumerd"
-#define KCONSUMERD_CMD_SOCK_PATH KCONSUMERD_PATH "/command"
-#define KCONSUMERD_ERR_SOCK_PATH KCONSUMERD_PATH "/error"
-
-/* UST 64-bit consumer path */
-#define USTCONSUMERD64_PATH CONSUMERD_RUNDIR "/ustconsumerd64"
-#define USTCONSUMERD64_CMD_SOCK_PATH USTCONSUMERD64_PATH "/command"
-#define USTCONSUMERD64_ERR_SOCK_PATH USTCONSUMERD64_PATH "/error"
-
-/* UST 32-bit consumer path */
-#define USTCONSUMERD32_PATH CONSUMERD_RUNDIR "/ustconsumerd32"
-#define USTCONSUMERD32_CMD_SOCK_PATH USTCONSUMERD32_PATH "/command"
-#define USTCONSUMERD32_ERR_SOCK_PATH USTCONSUMERD32_PATH "/error"
-
-#endif /* _LTTNG_CONSUMERD_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTT_HT_H
-#define _LTT_HT_H
-
-#include <urcu.h>
-#include "../liblttng-ht/rculfhash.h"
-#include "../liblttng-ht/rculfhash-internal.h"
-
-typedef unsigned long (*hash_fct)(void *_key, unsigned long seed);
-typedef cds_lfht_match_fct hash_match_fct;
-
-enum lttng_ht_type {
- LTTNG_HT_TYPE_STRING,
- LTTNG_HT_TYPE_ULONG,
-};
-
-struct lttng_ht {
- struct cds_lfht *ht;
- cds_lfht_match_fct match_fct;
- hash_fct hash_fct;
-};
-
-struct lttng_ht_iter {
- struct cds_lfht_iter iter;
-};
-
-struct lttng_ht_node_str {
- char *key;
- struct cds_lfht_node node;
- struct rcu_head head;
-};
-
-struct lttng_ht_node_ulong {
- unsigned long key;
- struct cds_lfht_node node;
- struct rcu_head head;
-};
-
-/* Hashtable new and destroy */
-extern struct lttng_ht *lttng_ht_new(unsigned long size, int type);
-extern void lttng_ht_destroy(struct lttng_ht *ht);
-
-/* Specialized node init and free functions */
-extern void lttng_ht_node_init_str(struct lttng_ht_node_str *node, char *key);
-extern void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong *node,
- unsigned long key);
-extern void lttng_ht_node_free_str(struct lttng_ht_node_str *node);
-extern void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong *node);
-
-extern void lttng_ht_lookup(struct lttng_ht *ht, void *key,
- struct lttng_ht_iter *iter);
-
-/* Specialized add unique functions */
-extern void lttng_ht_add_unique_str(struct lttng_ht *ht,
- struct lttng_ht_node_str *node);
-extern void lttng_ht_add_unique_ulong(struct lttng_ht *ht,
- struct lttng_ht_node_ulong *node);
-
-extern int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter);
-
-extern void lttng_ht_get_first(struct lttng_ht *ht,
- struct lttng_ht_iter *iter);
-extern void lttng_ht_get_next(struct lttng_ht *ht, struct lttng_ht_iter *iter);
-
-extern unsigned long lttng_ht_get_count(struct lttng_ht *ht);
-
-extern struct lttng_ht_node_str *lttng_ht_iter_get_node_str(
- struct lttng_ht_iter *iter);
-extern struct lttng_ht_node_ulong *lttng_ht_iter_get_node_ulong(
- struct lttng_ht_iter *iter);
-
-#endif /* _LTT_HT_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_KERNEL_CTL_H
-#define _LTTNG_KERNEL_CTL_H
-
-#include <lttng/lttng.h>
-#include <lttng-kernel.h>
-
-int kernctl_create_session(int fd);
-int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops);
-int kernctl_create_channel(int fd, struct lttng_channel_attr *chops);
-int kernctl_create_stream(int fd);
-int kernctl_create_event(int fd, struct lttng_kernel_event *ev);
-int kernctl_add_context(int fd, struct lttng_kernel_context *ctx);
-
-int kernctl_enable(int fd);
-int kernctl_disable(int fd);
-int kernctl_start_session(int fd);
-int kernctl_stop_session(int fd);
-
-int kernctl_tracepoint_list(int fd);
-int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v);
-int kernctl_wait_quiescent(int fd);
-int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate);
-
-
-/* Buffer operations */
-
-/* For mmap mode, readable without "get" operation */
-int kernctl_get_mmap_len(int fd, unsigned long *len);
-int kernctl_get_max_subbuf_size(int fd, unsigned long *len);
-
-/*
- * For mmap mode, operate on the current packet (between get/put or
- * get_next/put_next).
- */
-int kernctl_get_mmap_read_offset(int fd, unsigned long *len);
-int kernctl_get_subbuf_size(int fd, unsigned long *len);
-int kernctl_get_padded_subbuf_size(int fd, unsigned long *len);
-
-int kernctl_get_next_subbuf(int fd);
-int kernctl_put_next_subbuf(int fd);
-
-/* snapshot */
-int kernctl_snapshot(int fd);
-int kernctl_snapshot_get_consumed(int fd, unsigned long *pos);
-int kernctl_snapshot_get_produced(int fd, unsigned long *pos);
-int kernctl_get_subbuf(int fd, unsigned long *pos);
-int kernctl_put_subbuf(int fd);
-
-int kernctl_buffer_flush(int fd);
-
-#endif /* _LTTNG_KERNEL_CTL_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_KERNEL_H
-#define _LTTNG_KERNEL_H
-
-#include <stdint.h>
-
-#include <lttng-share.h>
-
-#define LTTNG_SYM_NAME_LEN 256
-
-/*
- * LTTng DebugFS ABI structures.
- *
- * This is the kernel ABI copied from lttng-modules tree.
- */
-
-enum lttng_kernel_instrumentation {
- LTTNG_KERNEL_ALL = -1, /* Used within lttng-tools */
- LTTNG_KERNEL_TRACEPOINT = 0,
- LTTNG_KERNEL_KPROBE = 1,
- LTTNG_KERNEL_FUNCTION = 2,
- LTTNG_KERNEL_KRETPROBE = 3,
- LTTNG_KERNEL_NOOP = 4, /* not hooked */
- LTTNG_KERNEL_SYSCALL = 5,
-};
-
-enum lttng_kernel_context_type {
- LTTNG_KERNEL_CONTEXT_PID = 0,
- LTTNG_KERNEL_CONTEXT_PERF_COUNTER = 1,
- LTTNG_KERNEL_CONTEXT_COMM = 2,
- LTTNG_KERNEL_CONTEXT_PRIO = 3,
- LTTNG_KERNEL_CONTEXT_NICE = 4,
- LTTNG_KERNEL_CONTEXT_VPID = 5,
- LTTNG_KERNEL_CONTEXT_TID = 6,
- LTTNG_KERNEL_CONTEXT_VTID = 7,
- LTTNG_KERNEL_CONTEXT_PPID = 8,
- LTTNG_KERNEL_CONTEXT_VPPID = 9,
-};
-
-/* Perf counter attributes */
-struct lttng_kernel_perf_counter_ctx {
- uint32_t type;
- uint64_t config;
- char name[LTTNG_SYM_NAME_LEN];
-};
-
-/* Event/Channel context */
-struct lttng_kernel_context {
- enum lttng_kernel_context_type ctx;
- union {
- struct lttng_kernel_perf_counter_ctx perf_counter;
- } u;
-};
-
-struct lttng_kernel_kretprobe {
- uint64_t addr;
-
- uint64_t offset;
- char symbol_name[LTTNG_SYM_NAME_LEN];
-};
-
-/*
- * Either addr is used, or symbol_name and offset.
- */
-struct lttng_kernel_kprobe {
- uint64_t addr;
-
- uint64_t offset;
- char symbol_name[LTTNG_SYM_NAME_LEN];
-};
-
-/* Function tracer */
-struct lttng_kernel_function {
- char symbol_name[LTTNG_SYM_NAME_LEN];
-};
-
-struct lttng_kernel_event {
- char name[LTTNG_SYM_NAME_LEN];
- enum lttng_kernel_instrumentation instrumentation;
- /* Per instrumentation type configuration */
- union {
- struct lttng_kernel_kretprobe kretprobe;
- struct lttng_kernel_kprobe kprobe;
- struct lttng_kernel_function ftrace;
- } u;
-};
-
-struct lttng_kernel_tracer_version {
- uint32_t version;
- uint32_t patchlevel;
- uint32_t sublevel;
-};
-
-enum lttng_kernel_calibrate_type {
- LTTNG_KERNEL_CALIBRATE_KRETPROBE,
-};
-
-struct lttng_kernel_calibrate {
- enum lttng_kernel_calibrate_type type; /* type (input) */
-};
-
-#endif /* _LTTNG_KERNEL_H */
+++ /dev/null
-#ifndef _LTTNG_SESSIOND_COMM_H
-#define _LTTNG_SESSIOND_COMM_H
-
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*
- * This header is meant for liblttng and libust internal use ONLY.
- * These declarations should NOT be considered stable API.
- */
-
-#define _GNU_SOURCE
-#include <limits.h>
-#include <lttng/lttng.h>
-#include <sys/socket.h>
-
-#define LTTNG_RUNDIR "/var/run/lttng"
-#define LTTNG_HOME_RUNDIR "%s/.lttng"
-
-/* Default unix socket path */
-#define DEFAULT_GLOBAL_CLIENT_UNIX_SOCK LTTNG_RUNDIR "/client-lttng-sessiond"
-#define DEFAULT_GLOBAL_APPS_UNIX_SOCK LTTNG_RUNDIR "/apps-lttng-sessiond"
-#define DEFAULT_HOME_APPS_UNIX_SOCK LTTNG_HOME_RUNDIR "/apps-lttng-sessiond"
-#define DEFAULT_HOME_CLIENT_UNIX_SOCK LTTNG_HOME_RUNDIR "/client-lttng-sessiond"
-
-/* Queue size of listen(2) */
-#define LTTNG_SESSIOND_COMM_MAX_LISTEN 64
-
-/*
- * Get the error code index from 0 since LTTCOMM_OK start at 1000
- */
-#define LTTCOMM_ERR_INDEX(code) (code - LTTCOMM_OK)
-
-enum lttcomm_sessiond_command {
- /* Tracer command */
- LTTNG_ADD_CONTEXT,
- LTTNG_CALIBRATE,
- LTTNG_DISABLE_CHANNEL,
- LTTNG_DISABLE_EVENT,
- LTTNG_DISABLE_ALL_EVENT,
- LTTNG_ENABLE_CHANNEL,
- LTTNG_ENABLE_EVENT,
- LTTNG_ENABLE_ALL_EVENT,
- /* Session daemon command */
- LTTNG_CREATE_SESSION,
- LTTNG_DESTROY_SESSION,
- LTTNG_LIST_CHANNELS,
- LTTNG_LIST_DOMAINS,
- LTTNG_LIST_EVENTS,
- LTTNG_LIST_SESSIONS,
- LTTNG_LIST_TRACEPOINTS,
- LTTNG_REGISTER_CONSUMER,
- LTTNG_START_TRACE,
- LTTNG_STOP_TRACE,
-};
-
-/*
- * lttcomm error code.
- */
-enum lttcomm_return_code {
- LTTCOMM_OK = 1000, /* Ok */
- LTTCOMM_ERR, /* Unknown Error */
- LTTCOMM_UND, /* Undefine command */
- LTTCOMM_NOT_IMPLEMENTED, /* Command not implemented */
- LTTCOMM_UNKNOWN_DOMAIN, /* Tracing domain not known */
- LTTCOMM_ALLOC_FAIL, /* Trace allocation fail */
- LTTCOMM_NO_SESSION, /* No session found */
- LTTCOMM_CREATE_FAIL, /* Create trace fail */
- LTTCOMM_SESSION_FAIL, /* Create session fail */
- LTTCOMM_START_FAIL, /* Start tracing fail */
- LTTCOMM_STOP_FAIL, /* Stop tracing fail */
- LTTCOMM_LIST_FAIL, /* Listing apps fail */
- LTTCOMM_NO_APPS, /* No traceable application */
- LTTCOMM_SESS_NOT_FOUND, /* Session name not found */
- LTTCOMM_NO_TRACE, /* No trace exist */
- LTTCOMM_FATAL, /* Session daemon had a fatal error */
- LTTCOMM_NO_TRACEABLE, /* Error for non traceable app */
- LTTCOMM_SELECT_SESS, /* Must select a session */
- LTTCOMM_EXIST_SESS, /* Session name already exist */
- LTTCOMM_NO_EVENT, /* No event found */
- LTTCOMM_CONNECT_FAIL, /* Unable to connect to unix socket */
- LTTCOMM_APP_NOT_FOUND, /* App not found in traceable app list */
- LTTCOMM_EPERM, /* Permission denied */
- LTTCOMM_KERN_NA, /* Kernel tracer unavalable */
- LTTCOMM_KERN_EVENT_EXIST, /* Kernel event already exists */
- LTTCOMM_KERN_SESS_FAIL, /* Kernel create session failed */
- LTTCOMM_KERN_CHAN_FAIL, /* Kernel create channel failed */
- LTTCOMM_KERN_CHAN_NOT_FOUND, /* Kernel channel not found */
- LTTCOMM_KERN_CHAN_DISABLE_FAIL, /* Kernel disable channel failed */
- LTTCOMM_KERN_CHAN_ENABLE_FAIL, /* Kernel enable channel failed */
- LTTCOMM_KERN_CONTEXT_FAIL, /* Kernel add context failed */
- LTTCOMM_KERN_ENABLE_FAIL, /* Kernel enable event failed */
- LTTCOMM_KERN_DISABLE_FAIL, /* Kernel disable event failed */
- LTTCOMM_KERN_META_FAIL, /* Kernel open metadata failed */
- LTTCOMM_KERN_START_FAIL, /* Kernel start trace failed */
- LTTCOMM_KERN_STOP_FAIL, /* Kernel stop trace failed */
- LTTCOMM_KERN_CONSUMER_FAIL, /* Kernel consumer start failed */
- LTTCOMM_KERN_STREAM_FAIL, /* Kernel create stream failed */
- LTTCOMM_KERN_DIR_FAIL, /* Kernel trace directory creation failed */
- LTTCOMM_KERN_DIR_EXIST, /* Kernel trace directory exist */
- LTTCOMM_KERN_NO_SESSION, /* No kernel session found */
- LTTCOMM_KERN_LIST_FAIL, /* Kernel listing events failed */
- LTTCOMM_UST_SESS_FAIL, /* UST create session failed */
- LTTCOMM_UST_CHAN_EXIST, /* UST channel already exist */
- LTTCOMM_UST_CHAN_FAIL, /* UST create channel failed */
- LTTCOMM_UST_CHAN_NOT_FOUND, /* UST channel not found */
- LTTCOMM_UST_CHAN_DISABLE_FAIL, /* UST disable channel failed */
- LTTCOMM_UST_CHAN_ENABLE_FAIL, /* UST enable channel failed */
- LTTCOMM_UST_CONTEXT_FAIL, /* UST add context failed */
- LTTCOMM_UST_ENABLE_FAIL, /* UST enable event failed */
- LTTCOMM_UST_DISABLE_FAIL, /* UST disable event failed */
- LTTCOMM_UST_META_FAIL, /* UST open metadata failed */
- LTTCOMM_UST_START_FAIL, /* UST start trace failed */
- LTTCOMM_UST_STOP_FAIL, /* UST stop trace failed */
- LTTCOMM_UST_CONSUMER64_FAIL, /* 64-bit UST consumer start failed */
- LTTCOMM_UST_CONSUMER32_FAIL, /* 32-bit UST consumer start failed */
- LTTCOMM_UST_STREAM_FAIL, /* UST create stream failed */
- LTTCOMM_UST_DIR_FAIL, /* UST trace directory creation failed */
- LTTCOMM_UST_DIR_EXIST, /* UST trace directory exist */
- LTTCOMM_UST_NO_SESSION, /* No UST session found */
- LTTCOMM_UST_LIST_FAIL, /* UST listing events failed */
- LTTCOMM_UST_EVENT_EXIST, /* UST event exist */
- LTTCOMM_UST_EVENT_NOT_FOUND, /* UST event not found */
- LTTCOMM_UST_CONTEXT_EXIST, /* UST context exist */
-
- CONSUMERD_COMMAND_SOCK_READY, /* when consumerd command socket ready */
- CONSUMERD_SUCCESS_RECV_FD, /* success on receiving fds */
- CONSUMERD_ERROR_RECV_FD, /* error on receiving fds */
- CONSUMERD_ERROR_RECV_CMD, /* error on receiving command */
- CONSUMERD_POLL_ERROR, /* Error in polling thread in kconsumerd */
- CONSUMERD_POLL_NVAL, /* Poll on closed fd */
- CONSUMERD_POLL_HUP, /* All fds have hungup */
- CONSUMERD_EXIT_SUCCESS, /* kconsumerd exiting normally */
- CONSUMERD_EXIT_FAILURE, /* kconsumerd exiting on error */
- CONSUMERD_OUTFD_ERROR, /* error opening the tracefile */
- CONSUMERD_SPLICE_EBADF, /* EBADF from splice(2) */
- CONSUMERD_SPLICE_EINVAL, /* EINVAL from splice(2) */
- CONSUMERD_SPLICE_ENOMEM, /* ENOMEM from splice(2) */
- CONSUMERD_SPLICE_ESPIPE, /* ESPIPE from splice(2) */
- /* MUST be last element */
- LTTCOMM_NR, /* Last element */
-};
-
-/*
- * Data structure received from lttng client to session daemon.
- */
-struct lttcomm_session_msg {
- uint32_t cmd_type; /* enum lttcomm_sessiond_command */
- struct lttng_session session;
- struct lttng_domain domain;
- union {
- struct {
- char channel_name[NAME_MAX];
- char name[NAME_MAX];
- } disable;
- /* Event data */
- struct {
- char channel_name[NAME_MAX];
- struct lttng_event event;
- } enable;
- /* Create channel */
- struct {
- struct lttng_channel chan;
- } channel;
- /* Context */
- struct {
- char channel_name[NAME_MAX];
- char event_name[NAME_MAX];
- struct lttng_event_context ctx;
- } context;
- /* Use by register_consumer */
- struct {
- char path[PATH_MAX];
- } reg;
- /* List */
- struct {
- char channel_name[NAME_MAX];
- } list;
- struct lttng_calibrate calibrate;
- } u;
-};
-
-/*
- * Data structure for the response from sessiond to the lttng client.
- */
-struct lttcomm_lttng_msg {
- uint32_t cmd_type; /* enum lttcomm_sessiond_command */
- uint32_t ret_code; /* enum lttcomm_return_code */
- uint32_t pid; /* pid_t */
- uint32_t data_size;
- /* Contains: trace_name + data */
- char payload[];
-};
-
-/*
- * lttcomm_consumer_msg is the message sent from sessiond to consumerd
- * to either add a channel, add a stream, update a stream, or stop
- * operation.
- */
-struct lttcomm_consumer_msg {
- uint32_t cmd_type; /* enum consumerd_command */
- union {
- struct {
- int channel_key;
- uint64_t max_sb_size; /* the subbuffer size for this channel */
- /* shm_fd and wait_fd are sent as ancillary data */
- uint64_t mmap_len;
- } channel;
- struct {
- int channel_key;
- int stream_key;
- /* shm_fd and wait_fd are sent as ancillary data */
- uint32_t state; /* enum lttcomm_consumer_fd_state */
- enum lttng_event_output output; /* use splice or mmap to consume this fd */
- uint64_t mmap_len;
- uid_t uid; /* User ID owning the session */
- gid_t gid; /* Group ID owning the session */
- char path_name[PATH_MAX];
- } stream;
- } u;
-};
-
-#ifdef HAVE_LIBLTTNG_UST_CTL
-
-#include <lttng/ust-abi.h>
-
-/*
- * Data structure for the commands sent from sessiond to UST.
- */
-struct lttcomm_ust_msg {
- uint32_t handle;
- uint32_t cmd;
- union {
- struct lttng_ust_channel channel;
- struct lttng_ust_stream stream;
- struct lttng_ust_event event;
- struct lttng_ust_context context;
- struct lttng_ust_tracer_version version;
- } u;
-};
-
-/*
- * Data structure for the response from UST to the session daemon.
- * cmd_type is sent back in the reply for validation.
- */
-struct lttcomm_ust_reply {
- uint32_t handle;
- uint32_t cmd;
- uint32_t ret_code; /* enum lttcomm_return_code */
- uint32_t ret_val; /* return value */
- union {
- struct {
- uint64_t memory_map_size;
- } channel;
- struct {
- uint64_t memory_map_size;
- } stream;
- struct lttng_ust_tracer_version version;
- } u;
-};
-
-#endif /* HAVE_LIBLTTNG_UST_CTL */
-
-extern int lttcomm_create_unix_sock(const char *pathname);
-extern int lttcomm_connect_unix_sock(const char *pathname);
-extern int lttcomm_accept_unix_sock(int sock);
-extern int lttcomm_listen_unix_sock(int sock);
-extern int lttcomm_close_unix_sock(int sock);
-
-#define LTTCOMM_MAX_SEND_FDS 4
-/* Send a message accompanied by fd(s) over a unix socket. */
-extern ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd);
-/* Recv a message accompanied by fd(s) from a unix socket */
-extern ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd);
-
-extern ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len);
-extern ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len);
-
-extern ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len);
-extern ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
- struct ucred *creds);
-
-extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code);
-extern int lttcomm_setsockopt_creds_unix_sock(int sock);
-
-#endif /* _LTTNG_SESSIOND_COMM_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_SHARE_H
-#define _LTTNG_SHARE_H
-
-#include <stdlib.h>
-
-#include <lttng/lttng.h>
-
-/* Default size of a hash table */
-#define DEFAULT_HT_SIZE 4
-
-/* Default channel attributes */
-#define DEFAULT_CHANNEL_NAME "channel0"
-#define DEFAULT_CHANNEL_OVERWRITE 0 /* usec */
-/* DEFAULT_CHANNEL_SUBBUF_SIZE must always be a power of 2 */
-#define DEFAULT_CHANNEL_SUBBUF_SIZE 4096 /* bytes */
-/* DEFAULT_CHANNEL_SUBBUF_NUM must always be a power of 2 */
-#define DEFAULT_CHANNEL_SUBBUF_NUM 8
-#define DEFAULT_CHANNEL_SWITCH_TIMER 0 /* usec */
-#define DEFAULT_CHANNEL_READ_TIMER 200 /* usec */
-#define DEFAULT_CHANNEL_OUTPUT LTTNG_EVENT_MMAP
-
-#define DEFAULT_METADATA_SUBBUF_SIZE 4096
-#define DEFAULT_METADATA_SUBBUF_NUM 2
-
-/* Kernel has different defaults */
-
-/* DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE must always be a power of 2 */
-#define DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE 262144 /* bytes */
-/* DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM must always be a power of 2 */
-#define DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM 4
-/* See lttng-kernel.h enum lttng_kernel_output for channel output */
-#define DEFAULT_KERNEL_CHANNEL_OUTPUT LTTNG_EVENT_SPLICE
-
-/* User space defaults */
-
-/* Must be a power of 2 */
-#define DEFAULT_UST_CHANNEL_SUBBUF_SIZE 4096 /* bytes */
-/* Must be a power of 2 */
-#define DEFAULT_UST_CHANNEL_SUBBUF_NUM 4
-/* See lttng-ust.h enum lttng_ust_output */
-#define DEFAULT_UST_CHANNEL_OUTPUT LTTNG_EVENT_MMAP
-
-/*
- * Default timeout value for the sem_timedwait() call. Blocking forever is not
- * wanted so a timeout is used to control the data flow and not freeze the
- * session daemon.
- */
-#define DEFAULT_SEM_WAIT_TIMEOUT 30 /* in seconds */
-
-/*
- * Takes a pointer x and transform it so we can use it to access members
- * without a function call. Here an example:
- *
- * #define GET_SIZE(x) LTTNG_REF(x)->size
- *
- * struct { int size; } s;
- *
- * printf("size : %d\n", GET_SIZE(&s));
- *
- * For this example we can't use something like this for compatibility purpose
- * since this will fail:
- *
- * #define GET_SIZE(x) x->size;
- *
- * This is mostly use for the compatibility layer of lttng-tools. See
- * poll/epoll for a good example. Since x can be on the stack or allocated
- * memory using malloc(), we must use generic accessors for compat in order to
- * *not* use a function to access members and not the variable name.
- */
-#define LTTNG_REF(x) ((typeof(*x) *)(x))
-
-/*
- * Memory allocation zeroed
- */
-#define zmalloc(x) calloc(1, x)
-
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
-#endif
-
-
-#endif /* _LTTNG_SHARE_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_CONSUMER_H
-#define _LTTNG_CONSUMER_H
-
-#include <limits.h>
-#include <poll.h>
-#include <unistd.h>
-
-#include <lttng/lttng.h>
-#include <lttng-ht.h>
-
-/*
- * When the receiving thread dies, we need to have a way to make the polling
- * thread exit eventually. If all FDs hang up (normal case when the
- * lttng-sessiond stops), we can exit cleanly, but if there is a problem and
- * for whatever reason some FDs remain open, the consumer should still exit
- * eventually.
- *
- * If the timeout is reached, it means that during this period no events
- * occurred on the FDs so we need to force an exit. This case should not happen
- * but it is a safety to ensure we won't block the consumer indefinitely.
- *
- * The value of 2 seconds is an arbitrary choice.
- */
-#define LTTNG_CONSUMER_POLL_TIMEOUT 2000
-
-/* Commands for consumer */
-enum lttng_consumer_command {
- LTTNG_CONSUMER_ADD_CHANNEL,
- LTTNG_CONSUMER_ADD_STREAM,
- /* pause, delete, active depending on fd state */
- LTTNG_CONSUMER_UPDATE_STREAM,
- /* inform the consumer to quit when all fd has hang up */
- LTTNG_CONSUMER_STOP,
-};
-
-/* State of each fd in consumer */
-enum lttng_consumer_stream_state {
- LTTNG_CONSUMER_ACTIVE_STREAM,
- LTTNG_CONSUMER_PAUSE_STREAM,
- LTTNG_CONSUMER_DELETE_STREAM,
-};
-
-enum lttng_consumer_type {
- LTTNG_CONSUMER_UNKNOWN = 0,
- LTTNG_CONSUMER_KERNEL,
- LTTNG_CONSUMER64_UST,
- LTTNG_CONSUMER32_UST,
-};
-
-struct lttng_consumer_channel {
- struct lttng_ht_node_ulong node;
- int key;
- uint64_t max_sb_size; /* the subbuffer size for this channel */
- int refcount; /* Number of streams referencing this channel */
- /* For UST */
- int shm_fd;
- int wait_fd;
- void *mmap_base;
- size_t mmap_len;
- struct lttng_ust_shm_handle *handle;
- int nr_streams;
- int wait_fd_is_copy;
- int cpucount;
-};
-
-/* Forward declaration for UST. */
-struct lttng_ust_lib_ring_buffer;
-
-/*
- * Internal representation of the streams, sessiond_key is used to identify
- * uniquely a stream.
- */
-struct lttng_consumer_stream {
- struct lttng_ht_node_ulong node;
- struct lttng_consumer_channel *chan; /* associated channel */
- /*
- * key is the key used by the session daemon to refer to the
- * object in the consumer daemon.
- */
- int key;
- int shm_fd;
- int wait_fd;
- int out_fd; /* output file to write the data */
- off_t out_fd_offset; /* write position in the output file descriptor */
- char path_name[PATH_MAX]; /* tracefile name */
- enum lttng_consumer_stream_state state;
- size_t shm_len;
- void *mmap_base;
- size_t mmap_len;
- enum lttng_event_output output; /* splice or mmap */
- int shm_fd_is_copy;
- int wait_fd_is_copy;
- /* For UST */
- struct lttng_ust_lib_ring_buffer *buf;
- int cpu;
- int hangup_flush_done;
- /* UID/GID of the user owning the session to which stream belongs */
- uid_t uid;
- gid_t gid;
-};
-
-/*
- * UST consumer local data to the program. One or more instance per
- * process.
- */
-struct lttng_consumer_local_data {
- /* function to call when data is available on a buffer */
- int (*on_buffer_ready)(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx);
- /*
- * function to call when we receive a new channel, it receives a
- * newly allocated channel, depending on the return code of this
- * function, the new channel will be handled by the application
- * or the library.
- *
- * Returns:
- * > 0 (success, FD is kept by application)
- * == 0 (success, FD is left to library)
- * < 0 (error)
- */
- int (*on_recv_channel)(struct lttng_consumer_channel *channel);
- /*
- * function to call when we receive a new stream, it receives a
- * newly allocated stream, depending on the return code of this
- * function, the new stream will be handled by the application
- * or the library.
- *
- * Returns:
- * > 0 (success, FD is kept by application)
- * == 0 (success, FD is left to library)
- * < 0 (error)
- */
- int (*on_recv_stream)(struct lttng_consumer_stream *stream);
- /*
- * function to call when a stream is getting updated by the session
- * daemon, this function receives the sessiond key and the new
- * state, depending on the return code of this function the
- * update of state for the stream is handled by the application
- * or the library.
- *
- * Returns:
- * > 0 (success, FD is kept by application)
- * == 0 (success, FD is left to library)
- * < 0 (error)
- */
- int (*on_update_stream)(int sessiond_key, uint32_t state);
- /* socket to communicate errors with sessiond */
- int consumer_error_socket;
- /* socket to exchange commands with sessiond */
- char *consumer_command_sock_path;
- /* communication with splice */
- int consumer_thread_pipe[2];
- /* pipe to wake the poll thread when necessary */
- int consumer_poll_pipe[2];
- /* to let the signal handler wake up the fd receiver thread */
- int consumer_should_quit[2];
-};
-
-/*
- * Library-level data. One instance per process.
- */
-struct lttng_consumer_global_data {
-
- /*
- * At this time, this lock is used to ensure coherence between the count
- * and number of element in the hash table. It's also a protection for
- * concurrent read/write between threads.
- *
- * XXX: We need to see if this lock is still needed with the lockless RCU
- * hash tables.
- */
- pthread_mutex_t lock;
-
- /*
- * Number of streams in the hash table. Protected by consumer_data.lock.
- */
- int stream_count;
- /*
- * Hash tables of streams and channels. Protected by consumer_data.lock.
- */
- struct lttng_ht *stream_ht;
- struct lttng_ht *channel_ht;
- /*
- * Flag specifying if the local array of FDs needs update in the
- * poll function. Protected by consumer_data.lock.
- */
- unsigned int need_update;
- enum lttng_consumer_type type;
-};
-
-/*
- * Init consumer data structures.
- */
-extern void lttng_consumer_init(void);
-
-/*
- * Set the error socket for communication with a session daemon.
- */
-extern void lttng_consumer_set_error_sock(
- struct lttng_consumer_local_data *ctx, int sock);
-
-/*
- * Set the command socket path for communication with a session daemon.
- */
-extern void lttng_consumer_set_command_sock_path(
- struct lttng_consumer_local_data *ctx, char *sock);
-
-/*
- * Send return code to session daemon.
- *
- * Returns the return code of sendmsg : the number of bytes transmitted or -1
- * on error.
- */
-extern int lttng_consumer_send_error(
- struct lttng_consumer_local_data *ctx, int cmd);
-
-/*
- * Called from signal handler to ensure a clean exit.
- */
-extern void lttng_consumer_should_exit(
- struct lttng_consumer_local_data *ctx);
-
-/*
- * Cleanup the daemon's socket on exit.
- */
-extern void lttng_consumer_cleanup(void);
-
-/*
- * Flush pending writes to trace output disk file.
- */
-extern void lttng_consumer_sync_trace_file(
- struct lttng_consumer_stream *stream, off_t orig_offset);
-
-/*
- * Poll on the should_quit pipe and the command socket return -1 on error and
- * should exit, 0 if data is available on the command socket
- */
-extern int lttng_consumer_poll_socket(struct pollfd *kconsumer_sockpoll);
-
-extern int consumer_update_poll_array(
- struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
- struct lttng_consumer_stream **local_consumer_streams);
-
-extern struct lttng_consumer_stream *consumer_allocate_stream(
- int channel_key, int stream_key,
- int shm_fd, int wait_fd,
- enum lttng_consumer_stream_state state,
- uint64_t mmap_len,
- enum lttng_event_output output,
- const char *path_name,
- uid_t uid,
- gid_t gid);
-extern int consumer_add_stream(struct lttng_consumer_stream *stream);
-extern void consumer_del_stream(struct lttng_consumer_stream *stream);
-extern void consumer_change_stream_state(int stream_key,
- enum lttng_consumer_stream_state state);
-extern void consumer_del_channel(struct lttng_consumer_channel *channel);
-extern struct lttng_consumer_channel *consumer_allocate_channel(
- int channel_key,
- int shm_fd, int wait_fd,
- uint64_t mmap_len,
- uint64_t max_sb_size);
-int consumer_add_channel(struct lttng_consumer_channel *channel);
-
-extern struct lttng_consumer_local_data *lttng_consumer_create(
- enum lttng_consumer_type type,
- int (*buffer_ready)(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx),
- int (*recv_channel)(struct lttng_consumer_channel *channel),
- int (*recv_stream)(struct lttng_consumer_stream *stream),
- int (*update_stream)(int sessiond_key, uint32_t state));
-extern void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx);
-extern int lttng_consumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-extern int lttng_consumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-extern int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream);
-extern int lttng_consumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos);
-extern void *lttng_consumer_thread_poll_fds(void *data);
-extern void *lttng_consumer_thread_receive_fds(void *data);
-extern int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll);
-
-int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx);
-int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream);
-
-#endif /* _LTTNG_CONSUMER_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_KCONSUMER_H
-#define _LTTNG_KCONSUMER_H
-
-#include <lttng/lttng-consumer.h>
-
-/*
- * Mmap the ring buffer, read it and write the data to the tracefile.
- *
- * Returns the number of bytes written.
- */
-extern int lttng_kconsumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-
-/*
- * Splice the data from the ring buffer to the tracefile.
- *
- * Returns the number of bytes spliced.
- */
-extern int lttng_kconsumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-
-/*
- * Take a snapshot for a specific fd
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream);
-
-/*
- * Get the produced position
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_kconsumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos);
-
-int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll);
-
-
-int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx);
-int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream);
-
-#endif /* _LTTNG_KCONSUMER_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNG_USTCONSUMER_H
-#define _LTTNG_USTCONSUMER_H
-
-#include <config.h>
-#include <lttng/lttng-consumer.h>
-#include <errno.h>
-
-#ifdef HAVE_LIBLTTNG_UST_CTL
-
-/*
- * Mmap the ring buffer, read it and write the data to the tracefile.
- *
- * Returns the number of bytes written.
- */
-extern int lttng_ustconsumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-
-/* Not implemented */
-extern int lttng_ustconsumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len);
-
-/*
- * Take a snapshot for a specific fd
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream);
-
-/*
- * Get the produced position
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_ustconsumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos);
-
-int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll);
-
-extern int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan);
-extern void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan);
-extern int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream);
-extern void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream);
-
-int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx);
-int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream);
-
-void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream);
-
-#else /* HAVE_LIBLTTNG_UST_CTL */
-
-static inline
-int lttng_ustconsumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *uststream, unsigned long len)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan)
-{
- return -ENOSYS;
-}
-
-static inline
-void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
-{
-}
-
-static inline
-int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream)
-{
- return -ENOSYS;
-}
-
-static inline
-void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
-{
-}
-
-static inline
-int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx)
-{
- return -ENOSYS;
-}
-
-static inline
-int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
-{
- return -ENOSYS;
-}
-
-static inline
-void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
-{
-}
-
-#endif /* HAVE_LIBLTTNG_UST_CTL */
-
-#endif /* _LTTNG_USTCONSUMER_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTTNGERR_H
-#define _LTTNGERR_H
-
-#include <errno.h>
-#include <stdio.h>
-
-/* Stringify the expansion of a define */
-#define XSTR(d) STR(d)
-#define STR(s) #s
-
-extern int opt_quiet;
-extern int opt_verbose;
-
-#define PRINT_ERR 0x1
-#define PRINT_WARN 0x2
-#define PRINT_BUG 0x3
-#define PRINT_MSG 0x4
-#define PRINT_DBG 0x10
-#define PRINT_DBG2 0x20
-#define PRINT_DBG3 0x30
-
-/*
- * Macro for printing message depending on command line option and verbosity.
- */
-#define __lttng_print(type, fmt, args...) \
- do { \
- if (opt_quiet == 0) { \
- if (type == PRINT_MSG) { \
- fprintf(stdout, fmt, ## args); \
- } else if (((type & PRINT_DBG) && opt_verbose == 1) || \
- ((type & (PRINT_DBG | PRINT_DBG2)) && \
- opt_verbose == 2) || \
- ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \
- opt_verbose == 3)) { \
- fprintf(stderr, fmt, ## args); \
- } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) { \
- fprintf(stderr, fmt, ## args); \
- } \
- } \
- } while (0);
-
-#define MSG(fmt, args...) \
- __lttng_print(PRINT_MSG, fmt "\n", ## args)
-#define ERR(fmt, args...) \
- __lttng_print(PRINT_ERR, "Error: " fmt "\n", ## args)
-#define WARN(fmt, args...) \
- __lttng_print(PRINT_WARN, "Warning: " fmt "\n", ## args)
-#define BUG(fmt, args...) \
- __lttng_print(PRINT_BUG, "BUG: " fmt "\n", ## args)
-
-/* Three level of debug. Use -v, -vv or -vvv for the levels */
-#define DBG(fmt, args...) __lttng_print(PRINT_DBG, "DEBUG1: " fmt \
- " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
-#define DBG2(fmt, args...) __lttng_print(PRINT_DBG2, "DEBUG2: " fmt \
- " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
-#define DBG3(fmt, args...) __lttng_print(PRINT_DBG3, "DEBUG3: " fmt \
- " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__)
-
-#define _PERROR(fmt, args...) \
- __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args)
-
-#define PERROR(call, args...) \
- do { \
- char *buf; \
- char tmp[200]; \
- buf = strerror_r(errno, tmp, sizeof(tmp)); \
- _PERROR(call ": %s", ## args, buf); \
- } while(0);
-
-#endif /* _LTTNGERR_H */
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-noinst_LTLIBRARIES = libkernelctl.la
-
-libkernelctl_la_SOURCES = kernelctl.c kernel-ioctl.h
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTT_KERNEL_IOCTL_H
-#define _LTT_KERNEL_IOCTL_H
-
-/* Get a snapshot of the current ring buffer producer and consumer positions */
-#define RING_BUFFER_SNAPSHOT _IO(0xF6, 0x00)
-/* Get the consumer position (iteration start) */
-#define RING_BUFFER_SNAPSHOT_GET_CONSUMED _IOR(0xF6, 0x01, unsigned long)
-/* Get the producer position (iteration end) */
-#define RING_BUFFER_SNAPSHOT_GET_PRODUCED _IOR(0xF6, 0x02, unsigned long)
-/* Get exclusive read access to the specified sub-buffer position */
-#define RING_BUFFER_GET_SUBBUF _IOW(0xF6, 0x03, unsigned long)
-/* Release exclusive sub-buffer access */
-#define RING_BUFFER_PUT_SUBBUF _IO(0xF6, 0x04)
-
-/* Get exclusive read access to the next sub-buffer that can be read. */
-#define RING_BUFFER_GET_NEXT_SUBBUF _IO(0xF6, 0x05)
-/* Release exclusive sub-buffer access, move consumer forward. */
-#define RING_BUFFER_PUT_NEXT_SUBBUF _IO(0xF6, 0x06)
-/* returns the size of the current sub-buffer, without padding (for mmap). */
-#define RING_BUFFER_GET_SUBBUF_SIZE _IOR(0xF6, 0x07, unsigned long)
-/* returns the size of the current sub-buffer, with padding (for splice). */
-#define RING_BUFFER_GET_PADDED_SUBBUF_SIZE _IOR(0xF6, 0x08, unsigned long)
-/* returns the maximum size for sub-buffers. */
-#define RING_BUFFER_GET_MAX_SUBBUF_SIZE _IOR(0xF6, 0x09, unsigned long)
-/* returns the length to mmap. */
-#define RING_BUFFER_GET_MMAP_LEN _IOR(0xF6, 0x0A, unsigned long)
-/* returns the offset of the subbuffer belonging to the mmap reader. */
-#define RING_BUFFER_GET_MMAP_READ_OFFSET _IOR(0xF6, 0x0B, unsigned long)
-/* flush the current sub-buffer */
-#define RING_BUFFER_FLUSH _IO(0xF6, 0x0C)
-
-/* LTTng file descriptor ioctl */
-#define LTTNG_KERNEL_SESSION _IO(0xF6, 0x40)
-#define LTTNG_KERNEL_TRACER_VERSION \
- _IOR(0xF6, 0x41, struct lttng_kernel_tracer_version)
-#define LTTNG_KERNEL_TRACEPOINT_LIST _IO(0xF6, 0x42)
-#define LTTNG_KERNEL_WAIT_QUIESCENT _IO(0xF6, 0x43)
-#define LTTNG_KERNEL_CALIBRATE \
- _IOWR(0xF6, 0x44, struct lttng_kernel_calibrate)
-
-/* Session FD ioctl */
-#define LTTNG_KERNEL_METADATA \
- _IOW(0xF6, 0x50, struct lttng_channel_attr)
-#define LTTNG_KERNEL_CHANNEL \
- _IOW(0xF6, 0x51, struct lttng_channel_attr)
-#define LTTNG_KERNEL_SESSION_START _IO(0xF6, 0x52)
-#define LTTNG_KERNEL_SESSION_STOP _IO(0xF6, 0x53)
-
-/* Channel FD ioctl */
-#define LTTNG_KERNEL_STREAM _IO(0xF6, 0x60)
-#define LTTNG_KERNEL_EVENT \
- _IOW(0xF6, 0x61, struct lttng_kernel_event)
-
-/* Event and Channel FD ioctl */
-#define LTTNG_KERNEL_CONTEXT \
- _IOW(0xF6, 0x70, struct lttng_kernel_context)
-
-/* Event, Channel and Session ioctl */
-#define LTTNG_KERNEL_ENABLE _IO(0xF6, 0x80)
-#define LTTNG_KERNEL_DISABLE _IO(0xF6, 0x81)
-
-#endif /* _LTT_KERNEL_IOCTL_H */
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#include <sys/ioctl.h>
-
-#include <lttng-kernel-ctl.h>
-
-#include "kernel-ioctl.h"
-
-int kernctl_create_session(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_SESSION);
-}
-
-/* open the metadata global channel */
-int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
-{
- return ioctl(fd, LTTNG_KERNEL_METADATA, chops);
-}
-
-int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
-{
- return ioctl(fd, LTTNG_KERNEL_CHANNEL, chops);
-}
-
-int kernctl_create_stream(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_STREAM);
-}
-
-int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
-{
- return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
-}
-
-int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
-{
- return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
-}
-
-
-/* Enable event, channel and session ioctl */
-int kernctl_enable(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_ENABLE);
-}
-
-/* Disable event, channel and session ioctl */
-int kernctl_disable(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_DISABLE);
-}
-
-int kernctl_start_session(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_SESSION_START);
-}
-
-int kernctl_stop_session(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_SESSION_STOP);
-}
-
-
-int kernctl_tracepoint_list(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_TRACEPOINT_LIST);
-}
-
-int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
-{
- return ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
-}
-
-int kernctl_wait_quiescent(int fd)
-{
- return ioctl(fd, LTTNG_KERNEL_WAIT_QUIESCENT);
-}
-
-int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
-{
- return ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
-}
-
-
-int kernctl_buffer_flush(int fd)
-{
- return ioctl(fd, RING_BUFFER_FLUSH);
-}
-
-
-/* Buffer operations */
-
-/* For mmap mode, readable without "get" operation */
-
-/* returns the length to mmap. */
-int kernctl_get_mmap_len(int fd, unsigned long *len)
-{
- return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
-}
-
-/* returns the maximum size for sub-buffers. */
-int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
-{
- return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
-}
-
-/*
- * For mmap mode, operate on the current packet (between get/put or
- * get_next/put_next).
- */
-
-/* returns the offset of the subbuffer belonging to the mmap reader. */
-int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
-{
- return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
-}
-
-/* returns the size of the current sub-buffer, without padding (for mmap). */
-int kernctl_get_subbuf_size(int fd, unsigned long *len)
-{
- return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
-}
-
-/* returns the size of the current sub-buffer, without padding (for mmap). */
-int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
-{
- return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
-}
-
-/* Get exclusive read access to the next sub-buffer that can be read. */
-int kernctl_get_next_subbuf(int fd)
-{
- return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
-}
-
-
-/* Release exclusive sub-buffer access, move consumer forward. */
-int kernctl_put_next_subbuf(int fd)
-{
- return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
-}
-
-/* snapshot */
-
-/* Get a snapshot of the current ring buffer producer and consumer positions */
-int kernctl_snapshot(int fd)
-{
- return ioctl(fd, RING_BUFFER_SNAPSHOT);
-}
-
-/* Get the consumer position (iteration start) */
-int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
-{
- return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
-}
-
-/* Get the producer position (iteration end) */
-int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
-{
- return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
-}
-
-/* Get exclusive read access to the specified sub-buffer position */
-int kernctl_get_subbuf(int fd, unsigned long *len)
-{
- return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
-}
-
-/* Release exclusive sub-buffer access */
-int kernctl_put_subbuf(int fd)
-{
- return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
-}
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-lib_LTLIBRARIES = liblttng-consumer.la
-
-liblttng_consumer_la_SOURCES = lttng-consumer.c
-
-liblttng_consumer_la_LIBADD = \
- $(top_builddir)/liblttng-sessiond-comm/liblttng-sessiond-comm.la \
- $(top_builddir)/liblttng-kconsumer/liblttng-kconsumer.la \
- $(top_builddir)/liblttng-ht/liblttng-ht.la
-
-if HAVE_LIBLTTNG_UST_CTL
-liblttng_consumer_la_LIBADD += \
- $(top_builddir)/liblttng-ustconsumer/liblttng-ustconsumer.la
-endif
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <assert.h>
-#include <fcntl.h>
-#include <poll.h>
-#include <pthread.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <lttng-kernel-ctl.h>
-#include <lttng-sessiond-comm.h>
-#include <lttng/lttng-consumer.h>
-#include <lttng/lttng-kconsumer.h>
-#include <lttng/lttng-ustconsumer.h>
-#include <lttngerr.h>
-
-struct lttng_consumer_global_data consumer_data = {
- .stream_count = 0,
- .need_update = 1,
- .type = LTTNG_CONSUMER_UNKNOWN,
-};
-
-/* timeout parameter, to control the polling thread grace period. */
-int consumer_poll_timeout = -1;
-
-/*
- * Flag to inform the polling thread to quit when all fd hung up. Updated by
- * the consumer_thread_receive_fds when it notices that all fds has hung up.
- * Also updated by the signal handler (consumer_should_exit()). Read by the
- * polling threads.
- */
-volatile int consumer_quit = 0;
-
-/*
- * Find a stream. The consumer_data.lock must be locked during this
- * call.
- */
-static struct lttng_consumer_stream *consumer_find_stream(int key)
-{
- struct lttng_ht_iter iter;
- struct lttng_ht_node_ulong *node;
- struct lttng_consumer_stream *stream = NULL;
-
- /* Negative keys are lookup failures */
- if (key < 0)
- return NULL;
-
- rcu_read_lock();
-
- lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
- &iter);
- node = lttng_ht_iter_get_node_ulong(&iter);
- if (node != NULL) {
- stream = caa_container_of(node, struct lttng_consumer_stream, node);
- }
-
- rcu_read_unlock();
-
- return stream;
-}
-
-static void consumer_steal_stream_key(int key)
-{
- struct lttng_consumer_stream *stream;
-
- stream = consumer_find_stream(key);
- if (stream)
- stream->key = -1;
-}
-
-static struct lttng_consumer_channel *consumer_find_channel(int key)
-{
- struct lttng_ht_iter iter;
- struct lttng_ht_node_ulong *node;
- struct lttng_consumer_channel *channel = NULL;
-
- /* Negative keys are lookup failures */
- if (key < 0)
- return NULL;
-
- rcu_read_lock();
-
- lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
- &iter);
- node = lttng_ht_iter_get_node_ulong(&iter);
- if (node != NULL) {
- channel = caa_container_of(node, struct lttng_consumer_channel, node);
- }
-
- rcu_read_unlock();
-
- return channel;
-}
-
-static void consumer_steal_channel_key(int key)
-{
- struct lttng_consumer_channel *channel;
-
- channel = consumer_find_channel(key);
- if (channel)
- channel->key = -1;
-}
-
-/*
- * Remove a stream from the global list protected by a mutex. This
- * function is also responsible for freeing its data structures.
- */
-void consumer_del_stream(struct lttng_consumer_stream *stream)
-{
- int ret;
- struct lttng_ht_iter iter;
- struct lttng_consumer_channel *free_chan = NULL;
-
- pthread_mutex_lock(&consumer_data.lock);
-
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- if (stream->mmap_base != NULL) {
- ret = munmap(stream->mmap_base, stream->mmap_len);
- if (ret != 0) {
- perror("munmap");
- }
- }
- break;
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- lttng_ustconsumer_del_stream(stream);
- break;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- goto end;
- }
-
- rcu_read_lock();
-
- /* Get stream node from hash table */
- lttng_ht_lookup(consumer_data.stream_ht,
- (void *)((unsigned long) stream->key), &iter);
- /* Remove stream node from hash table */
- ret = lttng_ht_del(consumer_data.stream_ht, &iter);
- assert(!ret);
-
- rcu_read_unlock();
-
- if (consumer_data.stream_count <= 0) {
- goto end;
- }
- consumer_data.stream_count--;
- if (!stream) {
- goto end;
- }
- if (stream->out_fd >= 0) {
- close(stream->out_fd);
- }
- if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
- close(stream->wait_fd);
- }
- if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
- close(stream->shm_fd);
- }
- if (!--stream->chan->refcount)
- free_chan = stream->chan;
- free(stream);
-end:
- consumer_data.need_update = 1;
- pthread_mutex_unlock(&consumer_data.lock);
-
- if (free_chan)
- consumer_del_channel(free_chan);
-}
-
-static void consumer_del_stream_rcu(struct rcu_head *head)
-{
- struct lttng_ht_node_ulong *node =
- caa_container_of(head, struct lttng_ht_node_ulong, head);
- struct lttng_consumer_stream *stream =
- caa_container_of(node, struct lttng_consumer_stream, node);
-
- consumer_del_stream(stream);
-}
-
-struct lttng_consumer_stream *consumer_allocate_stream(
- int channel_key, int stream_key,
- int shm_fd, int wait_fd,
- enum lttng_consumer_stream_state state,
- uint64_t mmap_len,
- enum lttng_event_output output,
- const char *path_name,
- uid_t uid,
- gid_t gid)
-{
- struct lttng_consumer_stream *stream;
- int ret;
-
- stream = zmalloc(sizeof(*stream));
- if (stream == NULL) {
- perror("malloc struct lttng_consumer_stream");
- goto end;
- }
- stream->chan = consumer_find_channel(channel_key);
- if (!stream->chan) {
- perror("Unable to find channel key");
- goto end;
- }
- stream->chan->refcount++;
- stream->key = stream_key;
- stream->shm_fd = shm_fd;
- stream->wait_fd = wait_fd;
- stream->out_fd = -1;
- stream->out_fd_offset = 0;
- stream->state = state;
- stream->mmap_len = mmap_len;
- stream->mmap_base = NULL;
- stream->output = output;
- stream->uid = uid;
- stream->gid = gid;
- strncpy(stream->path_name, path_name, PATH_MAX - 1);
- stream->path_name[PATH_MAX - 1] = '\0';
- lttng_ht_node_init_ulong(&stream->node, stream->key);
-
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- break;
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- stream->cpu = stream->chan->cpucount++;
- ret = lttng_ustconsumer_allocate_stream(stream);
- if (ret) {
- free(stream);
- return NULL;
- }
- break;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- goto end;
- }
- DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
- stream->path_name, stream->key,
- stream->shm_fd,
- stream->wait_fd,
- (unsigned long long) stream->mmap_len,
- stream->out_fd);
-end:
- return stream;
-}
-
-/*
- * Add a stream to the global list protected by a mutex.
- */
-int consumer_add_stream(struct lttng_consumer_stream *stream)
-{
- int ret = 0;
-
- pthread_mutex_lock(&consumer_data.lock);
- /* Steal stream identifier, for UST */
- consumer_steal_stream_key(stream->key);
- rcu_read_lock();
- lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
- rcu_read_unlock();
- consumer_data.stream_count++;
- consumer_data.need_update = 1;
-
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- break;
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- /* Streams are in CPU number order (we rely on this) */
- stream->cpu = stream->chan->nr_streams++;
- break;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- goto end;
- }
-
-end:
- pthread_mutex_unlock(&consumer_data.lock);
- return ret;
-}
-
-/*
- * Update a stream according to what we just received.
- */
-void consumer_change_stream_state(int stream_key,
- enum lttng_consumer_stream_state state)
-{
- struct lttng_consumer_stream *stream;
-
- pthread_mutex_lock(&consumer_data.lock);
- stream = consumer_find_stream(stream_key);
- if (stream) {
- stream->state = state;
- }
- consumer_data.need_update = 1;
- pthread_mutex_unlock(&consumer_data.lock);
-}
-
-/*
- * Remove a channel from the global list protected by a mutex. This
- * function is also responsible for freeing its data structures.
- */
-void consumer_del_channel(struct lttng_consumer_channel *channel)
-{
- int ret;
- struct lttng_ht_iter iter;
-
- pthread_mutex_lock(&consumer_data.lock);
-
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- break;
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- lttng_ustconsumer_del_channel(channel);
- break;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- goto end;
- }
-
- rcu_read_lock();
-
- lttng_ht_lookup(consumer_data.channel_ht,
- (void *)((unsigned long) channel->key), &iter);
- ret = lttng_ht_del(consumer_data.channel_ht, &iter);
- assert(!ret);
-
- rcu_read_unlock();
-
- if (channel->mmap_base != NULL) {
- ret = munmap(channel->mmap_base, channel->mmap_len);
- if (ret != 0) {
- perror("munmap");
- }
- }
- if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
- close(channel->wait_fd);
- }
- if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
- close(channel->shm_fd);
- }
- free(channel);
-end:
- pthread_mutex_unlock(&consumer_data.lock);
-}
-
-static void consumer_del_channel_rcu(struct rcu_head *head)
-{
- struct lttng_ht_node_ulong *node =
- caa_container_of(head, struct lttng_ht_node_ulong, head);
- struct lttng_consumer_channel *channel=
- caa_container_of(node, struct lttng_consumer_channel, node);
-
- consumer_del_channel(channel);
-}
-
-struct lttng_consumer_channel *consumer_allocate_channel(
- int channel_key,
- int shm_fd, int wait_fd,
- uint64_t mmap_len,
- uint64_t max_sb_size)
-{
- struct lttng_consumer_channel *channel;
- int ret;
-
- channel = zmalloc(sizeof(*channel));
- if (channel == NULL) {
- perror("malloc struct lttng_consumer_channel");
- goto end;
- }
- channel->key = channel_key;
- channel->shm_fd = shm_fd;
- channel->wait_fd = wait_fd;
- channel->mmap_len = mmap_len;
- channel->max_sb_size = max_sb_size;
- channel->refcount = 0;
- channel->nr_streams = 0;
- lttng_ht_node_init_ulong(&channel->node, channel->key);
-
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- channel->mmap_base = NULL;
- channel->mmap_len = 0;
- break;
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- ret = lttng_ustconsumer_allocate_channel(channel);
- if (ret) {
- free(channel);
- return NULL;
- }
- break;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- goto end;
- }
- DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
- channel->key,
- channel->shm_fd,
- channel->wait_fd,
- (unsigned long long) channel->mmap_len,
- (unsigned long long) channel->max_sb_size);
-end:
- return channel;
-}
-
-/*
- * Add a channel to the global list protected by a mutex.
- */
-int consumer_add_channel(struct lttng_consumer_channel *channel)
-{
- pthread_mutex_lock(&consumer_data.lock);
- /* Steal channel identifier, for UST */
- consumer_steal_channel_key(channel->key);
- rcu_read_lock();
- lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
- rcu_read_unlock();
- pthread_mutex_unlock(&consumer_data.lock);
- return 0;
-}
-
-/*
- * Allocate the pollfd structure and the local view of the out fds to avoid
- * doing a lookup in the linked list and concurrency issues when writing is
- * needed. Called with consumer_data.lock held.
- *
- * Returns the number of fds in the structures.
- */
-int consumer_update_poll_array(
- struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
- struct lttng_consumer_stream **local_stream)
-{
- int i = 0;
- struct lttng_ht_iter iter;
- struct lttng_consumer_stream *stream;
-
- DBG("Updating poll fd array");
- cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
- node.node) {
- if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
- continue;
- }
- DBG("Active FD %d", stream->wait_fd);
- (*pollfd)[i].fd = stream->wait_fd;
- (*pollfd)[i].events = POLLIN | POLLPRI;
- local_stream[i] = stream;
- i++;
- }
-
- /*
- * Insert the consumer_poll_pipe at the end of the array and don't
- * increment i so nb_fd is the number of real FD.
- */
- (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
- (*pollfd)[i].events = POLLIN;
- return i;
-}
-
-/*
- * Poll on the should_quit pipe and the command socket return -1 on error and
- * should exit, 0 if data is available on the command socket
- */
-int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
-{
- int num_rdy;
-
- num_rdy = poll(consumer_sockpoll, 2, -1);
- if (num_rdy == -1) {
- perror("Poll error");
- goto exit;
- }
- if (consumer_sockpoll[0].revents == POLLIN) {
- DBG("consumer_should_quit wake up");
- goto exit;
- }
- return 0;
-
-exit:
- return -1;
-}
-
-/*
- * Set the error socket.
- */
-void lttng_consumer_set_error_sock(
- struct lttng_consumer_local_data *ctx, int sock)
-{
- ctx->consumer_error_socket = sock;
-}
-
-/*
- * Set the command socket path.
- */
-
-void lttng_consumer_set_command_sock_path(
- struct lttng_consumer_local_data *ctx, char *sock)
-{
- ctx->consumer_command_sock_path = sock;
-}
-
-/*
- * Send return code to the session daemon.
- * If the socket is not defined, we return 0, it is not a fatal error
- */
-int lttng_consumer_send_error(
- struct lttng_consumer_local_data *ctx, int cmd)
-{
- if (ctx->consumer_error_socket > 0) {
- return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
- sizeof(enum lttcomm_sessiond_command));
- }
-
- return 0;
-}
-
-/*
- * Close all the tracefiles and stream fds, should be called when all instances
- * are destroyed.
- */
-void lttng_consumer_cleanup(void)
-{
- int ret;
- struct lttng_ht_iter iter;
- struct lttng_ht_node_ulong *node;
-
- rcu_read_lock();
-
- /*
- * close all outfd. Called when there are no more threads running (after
- * joining on the threads), no need to protect list iteration with mutex.
- */
- cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
- node) {
- ret = lttng_ht_del(consumer_data.stream_ht, &iter);
- assert(!ret);
- call_rcu(&node->head, consumer_del_stream_rcu);
- }
-
- cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
- node) {
- ret = lttng_ht_del(consumer_data.channel_ht, &iter);
- assert(!ret);
- call_rcu(&node->head, consumer_del_channel_rcu);
- }
-
- rcu_read_unlock();
-}
-
-/*
- * Called from signal handler.
- */
-void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
-{
- int ret;
- consumer_quit = 1;
- ret = write(ctx->consumer_should_quit[1], "4", 1);
- if (ret < 0) {
- perror("write consumer quit");
- }
-}
-
-void lttng_consumer_sync_trace_file(
- struct lttng_consumer_stream *stream, off_t orig_offset)
-{
- int outfd = stream->out_fd;
-
- /*
- * This does a blocking write-and-wait on any page that belongs to the
- * subbuffer prior to the one we just wrote.
- * Don't care about error values, as these are just hints and ways to
- * limit the amount of page cache used.
- */
- if (orig_offset < stream->chan->max_sb_size) {
- return;
- }
- sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
- stream->chan->max_sb_size,
- SYNC_FILE_RANGE_WAIT_BEFORE
- | SYNC_FILE_RANGE_WRITE
- | SYNC_FILE_RANGE_WAIT_AFTER);
- /*
- * Give hints to the kernel about how we access the file:
- * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
- * we write it.
- *
- * We need to call fadvise again after the file grows because the
- * kernel does not seem to apply fadvise to non-existing parts of the
- * file.
- *
- * Call fadvise _after_ having waited for the page writeback to
- * complete because the dirty page writeback semantic is not well
- * defined. So it can be expected to lead to lower throughput in
- * streaming.
- */
- posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
- stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
-}
-
-/*
- * Initialise the necessary environnement :
- * - create a new context
- * - create the poll_pipe
- * - create the should_quit pipe (for signal handler)
- * - create the thread pipe (for splice)
- *
- * Takes a function pointer as argument, this function is called when data is
- * available on a buffer. This function is responsible to do the
- * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
- * buffer configuration and then kernctl_put_next_subbuf at the end.
- *
- * Returns a pointer to the new context or NULL on error.
- */
-struct lttng_consumer_local_data *lttng_consumer_create(
- enum lttng_consumer_type type,
- int (*buffer_ready)(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx),
- int (*recv_channel)(struct lttng_consumer_channel *channel),
- int (*recv_stream)(struct lttng_consumer_stream *stream),
- int (*update_stream)(int stream_key, uint32_t state))
-{
- int ret, i;
- struct lttng_consumer_local_data *ctx;
-
- assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
- consumer_data.type == type);
- consumer_data.type = type;
-
- ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
- if (ctx == NULL) {
- perror("allocating context");
- goto error;
- }
-
- ctx->consumer_error_socket = -1;
- /* assign the callbacks */
- ctx->on_buffer_ready = buffer_ready;
- ctx->on_recv_channel = recv_channel;
- ctx->on_recv_stream = recv_stream;
- ctx->on_update_stream = update_stream;
-
- ret = pipe(ctx->consumer_poll_pipe);
- if (ret < 0) {
- perror("Error creating poll pipe");
- goto error_poll_pipe;
- }
-
- ret = pipe(ctx->consumer_should_quit);
- if (ret < 0) {
- perror("Error creating recv pipe");
- goto error_quit_pipe;
- }
-
- ret = pipe(ctx->consumer_thread_pipe);
- if (ret < 0) {
- perror("Error creating thread pipe");
- goto error_thread_pipe;
- }
-
- return ctx;
-
-
-error_thread_pipe:
- for (i = 0; i < 2; i++) {
- int err;
-
- err = close(ctx->consumer_should_quit[i]);
- assert(!err);
- }
-error_quit_pipe:
- for (i = 0; i < 2; i++) {
- int err;
-
- err = close(ctx->consumer_poll_pipe[i]);
- assert(!err);
- }
-error_poll_pipe:
- free(ctx);
-error:
- return NULL;
-}
-
-/*
- * Close all fds associated with the instance and free the context.
- */
-void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
-{
- close(ctx->consumer_error_socket);
- close(ctx->consumer_thread_pipe[0]);
- close(ctx->consumer_thread_pipe[1]);
- close(ctx->consumer_poll_pipe[0]);
- close(ctx->consumer_poll_pipe[1]);
- close(ctx->consumer_should_quit[0]);
- close(ctx->consumer_should_quit[1]);
- unlink(ctx->consumer_command_sock_path);
- free(ctx);
-}
-
-/*
- * Mmap the ring buffer, read it and write the data to the tracefile.
- *
- * Returns the number of bytes written
- */
-int lttng_consumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- }
-}
-
-/*
- * Splice the data from the ring buffer to the tracefile.
- *
- * Returns the number of bytes spliced.
- */
-int lttng_consumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return -ENOSYS;
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-
-}
-
-/*
- * Take a snapshot for a specific fd
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_take_snapshot(ctx, stream);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_take_snapshot(ctx, stream);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-
-}
-
-/*
- * Get the produced position
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_consumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-}
-
-int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-}
-
-/*
- * This thread polls the fds in the set to consume the data and write
- * it to tracefile if necessary.
- */
-void *lttng_consumer_thread_poll_fds(void *data)
-{
- int num_rdy, num_hup, high_prio, ret, i;
- struct pollfd *pollfd = NULL;
- /* local view of the streams */
- struct lttng_consumer_stream **local_stream = NULL;
- /* local view of consumer_data.fds_count */
- int nb_fd = 0;
- char tmp;
- int tmp2;
- struct lttng_consumer_local_data *ctx = data;
-
- rcu_register_thread();
-
- local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
-
- while (1) {
- high_prio = 0;
- num_hup = 0;
-
- /*
- * the fds set has been updated, we need to update our
- * local array as well
- */
- pthread_mutex_lock(&consumer_data.lock);
- if (consumer_data.need_update) {
- if (pollfd != NULL) {
- free(pollfd);
- pollfd = NULL;
- }
- if (local_stream != NULL) {
- free(local_stream);
- local_stream = NULL;
- }
-
- /* allocate for all fds + 1 for the consumer_poll_pipe */
- pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
- if (pollfd == NULL) {
- perror("pollfd malloc");
- pthread_mutex_unlock(&consumer_data.lock);
- goto end;
- }
-
- /* allocate for all fds + 1 for the consumer_poll_pipe */
- local_stream = zmalloc((consumer_data.stream_count + 1) *
- sizeof(struct lttng_consumer_stream));
- if (local_stream == NULL) {
- perror("local_stream malloc");
- pthread_mutex_unlock(&consumer_data.lock);
- goto end;
- }
- ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
- if (ret < 0) {
- ERR("Error in allocating pollfd or local_outfds");
- lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
- pthread_mutex_unlock(&consumer_data.lock);
- goto end;
- }
- nb_fd = ret;
- consumer_data.need_update = 0;
- }
- pthread_mutex_unlock(&consumer_data.lock);
-
- /* poll on the array of fds */
- DBG("polling on %d fd", nb_fd + 1);
- num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
- DBG("poll num_rdy : %d", num_rdy);
- if (num_rdy == -1) {
- perror("Poll error");
- lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
- goto end;
- } else if (num_rdy == 0) {
- DBG("Polling thread timed out");
- goto end;
- }
-
- /* No FDs and consumer_quit, consumer_cleanup the thread */
- if (nb_fd == 0 && consumer_quit == 1) {
- goto end;
- }
-
- /*
- * If the consumer_poll_pipe triggered poll go
- * directly to the beginning of the loop to update the
- * array. We want to prioritize array update over
- * low-priority reads.
- */
- if (pollfd[nb_fd].revents & POLLIN) {
- DBG("consumer_poll_pipe wake up");
- tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1);
- if (tmp2 < 0) {
- perror("read consumer poll");
- }
- continue;
- }
-
- /* Take care of high priority channels first. */
- for (i = 0; i < nb_fd; i++) {
- if (pollfd[i].revents & POLLPRI) {
- DBG("Urgent read on fd %d", pollfd[i].fd);
- high_prio = 1;
- ret = ctx->on_buffer_ready(local_stream[i], ctx);
- /* it's ok to have an unavailable sub-buffer */
- if (ret == EAGAIN) {
- ret = 0;
- }
- } else if (pollfd[i].revents & POLLERR) {
- ERR("Error returned in polling fd %d.", pollfd[i].fd);
- rcu_read_lock();
- consumer_del_stream_rcu(&local_stream[i]->node.head);
- rcu_read_unlock();
- num_hup++;
- } else if (pollfd[i].revents & POLLNVAL) {
- ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
- rcu_read_lock();
- consumer_del_stream_rcu(&local_stream[i]->node.head);
- rcu_read_unlock();
- num_hup++;
- } else if ((pollfd[i].revents & POLLHUP) &&
- !(pollfd[i].revents & POLLIN)) {
- if (consumer_data.type == LTTNG_CONSUMER32_UST
- || consumer_data.type == LTTNG_CONSUMER64_UST) {
- DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
- pollfd[i].fd);
- if (!local_stream[i]->hangup_flush_done) {
- lttng_ustconsumer_on_stream_hangup(local_stream[i]);
- /* read after flush */
- do {
- ret = ctx->on_buffer_ready(local_stream[i], ctx);
- } while (ret == EAGAIN);
- }
- } else {
- DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
- }
- rcu_read_lock();
- consumer_del_stream_rcu(&local_stream[i]->node.head);
- rcu_read_unlock();
- num_hup++;
- }
- }
-
- /* If every buffer FD has hung up, we end the read loop here */
- if (nb_fd > 0 && num_hup == nb_fd) {
- DBG("every buffer FD has hung up\n");
- if (consumer_quit == 1) {
- goto end;
- }
- continue;
- }
-
- /* Take care of low priority channels. */
- if (high_prio == 0) {
- for (i = 0; i < nb_fd; i++) {
- if (pollfd[i].revents & POLLIN) {
- DBG("Normal read on fd %d", pollfd[i].fd);
- ret = ctx->on_buffer_ready(local_stream[i], ctx);
- /* it's ok to have an unavailable subbuffer */
- if (ret == EAGAIN) {
- ret = 0;
- }
- }
- }
- }
- }
-end:
- DBG("polling thread exiting");
- if (pollfd != NULL) {
- free(pollfd);
- pollfd = NULL;
- }
- if (local_stream != NULL) {
- free(local_stream);
- local_stream = NULL;
- }
- rcu_unregister_thread();
- return NULL;
-}
-
-/*
- * This thread listens on the consumerd socket and receives the file
- * descriptors from the session daemon.
- */
-void *lttng_consumer_thread_receive_fds(void *data)
-{
- int sock, client_socket, ret;
- /*
- * structure to poll for incoming data on communication socket avoids
- * making blocking sockets.
- */
- struct pollfd consumer_sockpoll[2];
- struct lttng_consumer_local_data *ctx = data;
-
- rcu_register_thread();
-
- DBG("Creating command socket %s", ctx->consumer_command_sock_path);
- unlink(ctx->consumer_command_sock_path);
- client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
- if (client_socket < 0) {
- ERR("Cannot create command socket");
- goto end;
- }
-
- ret = lttcomm_listen_unix_sock(client_socket);
- if (ret < 0) {
- goto end;
- }
-
- DBG("Sending ready command to lttng-sessiond");
- ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
- /* return < 0 on error, but == 0 is not fatal */
- if (ret < 0) {
- ERR("Error sending ready command to lttng-sessiond");
- goto end;
- }
-
- ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
- if (ret < 0) {
- perror("fcntl O_NONBLOCK");
- goto end;
- }
-
- /* prepare the FDs to poll : to client socket and the should_quit pipe */
- consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
- consumer_sockpoll[0].events = POLLIN | POLLPRI;
- consumer_sockpoll[1].fd = client_socket;
- consumer_sockpoll[1].events = POLLIN | POLLPRI;
-
- if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
- goto end;
- }
- DBG("Connection on client_socket");
-
- /* Blocking call, waiting for transmission */
- sock = lttcomm_accept_unix_sock(client_socket);
- if (sock <= 0) {
- WARN("On accept");
- goto end;
- }
- ret = fcntl(sock, F_SETFL, O_NONBLOCK);
- if (ret < 0) {
- perror("fcntl O_NONBLOCK");
- goto end;
- }
-
- /* update the polling structure to poll on the established socket */
- consumer_sockpoll[1].fd = sock;
- consumer_sockpoll[1].events = POLLIN | POLLPRI;
-
- while (1) {
- if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
- goto end;
- }
- DBG("Incoming command on sock");
- ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
- if (ret == -ENOENT) {
- DBG("Received STOP command");
- goto end;
- }
- if (ret < 0) {
- ERR("Communication interrupted on command socket");
- goto end;
- }
- if (consumer_quit) {
- DBG("consumer_thread_receive_fds received quit from signal");
- goto end;
- }
- DBG("received fds on sock");
- }
-end:
- DBG("consumer_thread_receive_fds exiting");
-
- /*
- * when all fds have hung up, the polling thread
- * can exit cleanly
- */
- consumer_quit = 1;
-
- /*
- * 2s of grace period, if no polling events occur during
- * this period, the polling thread will exit even if there
- * are still open FDs (should not happen, but safety mechanism).
- */
- consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
-
- /* wake up the polling thread */
- ret = write(ctx->consumer_poll_pipe[1], "4", 1);
- if (ret < 0) {
- perror("poll pipe write");
- }
- rcu_unregister_thread();
- return NULL;
-}
-
-int lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_read_subbuffer(stream, ctx);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_read_subbuffer(stream, ctx);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-}
-
-int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
-{
- switch (consumer_data.type) {
- case LTTNG_CONSUMER_KERNEL:
- return lttng_kconsumer_on_recv_stream(stream);
- case LTTNG_CONSUMER32_UST:
- case LTTNG_CONSUMER64_UST:
- return lttng_ustconsumer_on_recv_stream(stream);
- default:
- ERR("Unknown consumer_data type");
- assert(0);
- return -ENOSYS;
- }
-}
-
-/*
- * Allocate and set consumer data hash tables.
- */
-void lttng_consumer_init(void)
-{
- consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
- consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
-}
-
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-noinst_LTLIBRARIES = liblttng-ht.la
-
-liblttng_ht_la_SOURCES = lttng-ht.c \
- utils.c utils.h \
- rculfhash-internal.h urcu-flavor.h \
- rculfhash.h rculfhash.c \
- rculfhash-mm-chunk.c \
- rculfhash-mm-mmap.c \
- rculfhash-mm-order.c
-
-liblttng_ht_la_LIBADD = -lurcu-common -lurcu
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <assert.h>
-#include <string.h>
-#include <urcu.h>
-#include <urcu/compiler.h>
-
-#include <lttng-ht.h>
-#include <lttng-share.h>
-#include <lttngerr.h>
-
-#include "utils.h"
-
-#define HASH_SEED 0x42UL /* The answer to life */
-
-static unsigned long min_hash_alloc_size = 1;
-static unsigned long max_hash_buckets_size = (1UL << 20);
-
-/*
- * Match function for string node.
- */
-static int match_str(struct cds_lfht_node *node, const void *key)
-{
- struct lttng_ht_node_str *match_node =
- caa_container_of(node, struct lttng_ht_node_str, node);
-
- return hash_match_key_str(match_node->key, (void *) key);
-}
-
-/*
- * Match function for ulong node.
- */
-static int match_ulong(struct cds_lfht_node *node, const void *key)
-{
- struct lttng_ht_node_ulong *match_node =
- caa_container_of(node, struct lttng_ht_node_ulong, node);
-
- return hash_match_key_ulong((void *) match_node->key, (void *) key);
-}
-
-/*
- * Return an allocated lttng hashtable.
- */
-struct lttng_ht *lttng_ht_new(unsigned long size, int type)
-{
- struct lttng_ht *ht;
-
- /* Test size */
- if (!size)
- size = DEFAULT_HT_SIZE;
-
- ht = zmalloc(sizeof(*ht));
- if (ht == NULL) {
- PERROR("zmalloc lttng_ht");
- goto error;
- }
-
- ht->ht = cds_lfht_new(size, min_hash_alloc_size, max_hash_buckets_size,
- CDS_LFHT_AUTO_RESIZE, NULL);
- /*
- * There is already an assert in the RCU hashtable code so if the ht is
- * NULL here there is a *huge* problem.
- */
- assert(ht->ht);
-
- switch (type) {
- case LTTNG_HT_TYPE_STRING:
- ht->match_fct = match_str;
- ht->hash_fct = hash_key_str;
- break;
- case LTTNG_HT_TYPE_ULONG:
- ht->match_fct = match_ulong;
- ht->hash_fct = hash_key_ulong;
- break;
- default:
- ERR("Unknown lttng hashtable type %d", type);
- goto error;
- }
-
- DBG3("Created hashtable size %lu at %p of type %d", size, ht->ht, type);
-
- return ht;
-
-error:
- return NULL;
-}
-
-/*
- * Free a lttng hashtable.
- */
-void lttng_ht_destroy(struct lttng_ht *ht)
-{
- int ret;
-
- ret = cds_lfht_destroy(ht->ht, NULL);
- assert(!ret);
-}
-
-/*
- * Init lttng ht node string.
- */
-void lttng_ht_node_init_str(struct lttng_ht_node_str *node, char *key)
-{
- assert(node);
-
- node->key = key;
- cds_lfht_node_init(&node->node);
-}
-
-/*
- * Init lttng ht node unsigned long.
- */
-void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong *node,
- unsigned long key)
-{
- assert(node);
-
- node->key = key;
- cds_lfht_node_init(&node->node);
-}
-
-/*
- * Free lttng ht node string.
- */
-void lttng_ht_node_free_str(struct lttng_ht_node_str *node)
-{
- assert(node);
- free(node);
-}
-
-/*
- * Free lttng ht node unsigned long.
- */
-void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong *node)
-{
- assert(node);
- free(node);
-}
-
-/*
- * Lookup function in hashtable.
- */
-void lttng_ht_lookup(struct lttng_ht *ht, void *key,
- struct lttng_ht_iter *iter)
-{
- assert(ht);
- assert(ht->ht);
-
- cds_lfht_lookup(ht->ht, ht->hash_fct(key, HASH_SEED),
- ht->match_fct, key, &iter->iter);
-}
-
-/*
- * Add unique string node to hashtable.
- */
-void lttng_ht_add_unique_str(struct lttng_ht *ht,
- struct lttng_ht_node_str *node)
-{
- struct cds_lfht_node *node_ptr;
- assert(ht);
- assert(ht->ht);
- assert(node);
-
- node_ptr = cds_lfht_add_unique(ht->ht, ht->hash_fct(node->key, HASH_SEED),
- ht->match_fct, node->key, &node->node);
- assert(node_ptr == &node->node);
-}
-
-/*
- * Add unique unsigned long node to hashtable.
- */
-void lttng_ht_add_unique_ulong(struct lttng_ht *ht,
- struct lttng_ht_node_ulong *node)
-{
- struct cds_lfht_node *node_ptr;
- assert(ht);
- assert(ht->ht);
- assert(node);
-
- node_ptr = cds_lfht_add_unique(ht->ht,
- ht->hash_fct((void *) node->key, HASH_SEED), ht->match_fct,
- (void *) node->key, &node->node);
- assert(node_ptr == &node->node);
-}
-
-/*
- * Delete node from hashtable.
- */
-int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter)
-{
- assert(ht);
- assert(ht->ht);
- assert(iter);
-
- return cds_lfht_del(ht->ht, iter->iter.node);
-}
-
-/*
- * Get first node in the hashtable.
- */
-void lttng_ht_get_first(struct lttng_ht *ht, struct lttng_ht_iter *iter)
-{
- assert(ht);
- assert(ht->ht);
- assert(iter);
-
- cds_lfht_first(ht->ht, &iter->iter);
-}
-
-/*
- * Get next node in the hashtable.
- */
-void lttng_ht_get_next(struct lttng_ht *ht, struct lttng_ht_iter *iter)
-{
- assert(ht);
- assert(ht->ht);
- assert(iter);
-
- cds_lfht_next(ht->ht, &iter->iter);
-}
-
-/*
- * Return the number of nodes in the hashtable.
- */
-unsigned long lttng_ht_get_count(struct lttng_ht *ht)
-{
- long scb, sca;
- unsigned long count;
-
- assert(ht);
- assert(ht->ht);
-
- cds_lfht_count_nodes(ht->ht, &scb, &count, &sca);
-
- return count;
-}
-
-/*
- * Return lttng ht string node from iterator.
- */
-struct lttng_ht_node_str *lttng_ht_iter_get_node_str(
- struct lttng_ht_iter *iter)
-{
- struct cds_lfht_node *node;
-
- assert(iter);
- node = cds_lfht_iter_get_node(&iter->iter);
- if (!node) {
- return NULL;
- }
- return caa_container_of(node, struct lttng_ht_node_str, node);
-}
-
-/*
- * Return lttng ht unsigned long node from iterator.
- */
-struct lttng_ht_node_ulong *lttng_ht_iter_get_node_ulong(
- struct lttng_ht_iter *iter)
-{
- struct cds_lfht_node *node;
-
- assert(iter);
- node = cds_lfht_iter_get_node(&iter->iter);
- if (!node) {
- return NULL;
- }
- return caa_container_of(node, struct lttng_ht_node_ulong, node);
-}
+++ /dev/null
-#ifndef _URCU_RCULFHASH_INTERNAL_H
-#define _URCU_RCULFHASH_INTERNAL_H
-
-/*
- * urcu/rculfhash-internal.h
- *
- * Internal header for Lock-Free RCU Hash Table
- *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "rculfhash.h"
-
-#ifdef DEBUG
-#define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
-#else
-#define dbg_printf(fmt, args...)
-#endif
-
-#if (CAA_BITS_PER_LONG == 32)
-#define MAX_TABLE_ORDER 32
-#else
-#define MAX_TABLE_ORDER 64
-#endif
-
-#define MAX_CHUNK_TABLE (1UL << 10)
-
-#ifndef min
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#endif
-
-#ifndef max
-#define max(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
-struct ht_items_count;
-
-/*
- * cds_lfht: Top-level data structure representing a lock-free hash
- * table. Defined in the implementation file to make it be an opaque
- * cookie to users.
- *
- * The fields used in fast-paths are placed near the end of the
- * structure, because we need to have a variable-sized union to contain
- * the mm plugin fields, which are used in the fast path.
- */
-struct cds_lfht {
- /* Initial configuration items */
- unsigned long max_nr_buckets;
- const struct cds_lfht_mm_type *mm; /* memory management plugin */
- const struct rcu_flavor_struct *flavor; /* RCU flavor */
-
- long count; /* global approximate item count */
-
- /*
- * We need to put the work threads offline (QSBR) when taking this
- * mutex, because we use synchronize_rcu within this mutex critical
- * section, which waits on read-side critical sections, and could
- * therefore cause grace-period deadlock if we hold off RCU G.P.
- * completion.
- */
- pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
- pthread_attr_t *resize_attr; /* Resize threads attributes */
- unsigned int in_progress_resize, in_progress_destroy;
- unsigned long resize_target;
- int resize_initiated;
-
- /*
- * Variables needed for add and remove fast-paths.
- */
- int flags;
- unsigned long min_alloc_buckets_order;
- unsigned long min_nr_alloc_buckets;
- struct ht_items_count *split_count; /* split item count */
-
- /*
- * Variables needed for the lookup, add and remove fast-paths.
- */
- unsigned long size; /* always a power of 2, shared (RCU) */
- /*
- * bucket_at pointer is kept here to skip the extra level of
- * dereference needed to get to "mm" (this is a fast-path).
- */
- struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht,
- unsigned long index);
- /*
- * Dynamic length "tbl_chunk" needs to be at the end of
- * cds_lfht.
- */
- union {
- /*
- * Contains the per order-index-level bucket node table.
- * The size of each bucket node table is half the number
- * of hashes contained in this order (except for order 0).
- * The minimum allocation buckets size parameter allows
- * combining the bucket node arrays of the lowermost
- * levels to improve cache locality for small index orders.
- */
- struct cds_lfht_node *tbl_order[MAX_TABLE_ORDER];
-
- /*
- * Contains the bucket node chunks. The size of each
- * bucket node chunk is ->min_alloc_size (we avoid to
- * allocate chunks with different size). Chunks improve
- * cache locality for small index orders, and are more
- * friendly with environments where allocation of large
- * contiguous memory areas is challenging due to memory
- * fragmentation concerns or inability to use virtual
- * memory addressing.
- */
- struct cds_lfht_node *tbl_chunk[0];
-
- /*
- * Memory mapping with room for all possible buckets.
- * Their memory is allocated when needed.
- */
- struct cds_lfht_node *tbl_mmap;
- };
- /*
- * End of variables needed for the lookup, add and remove
- * fast-paths.
- */
-};
-
-extern unsigned int cds_lfht_fls_ulong(unsigned long x);
-extern int cds_lfht_get_count_order_ulong(unsigned long x);
-
-#ifdef POISON_FREE
-#define poison_free(ptr) \
- do { \
- if (ptr) { \
- memset(ptr, 0x42, sizeof(*(ptr))); \
- free(ptr); \
- } \
- } while (0)
-#else
-#define poison_free(ptr) free(ptr)
-#endif
-
-static inline
-struct cds_lfht *__default_alloc_cds_lfht(
- const struct cds_lfht_mm_type *mm,
- unsigned long cds_lfht_size,
- unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets)
-{
- struct cds_lfht *ht;
-
- ht = calloc(1, cds_lfht_size);
- assert(ht);
-
- ht->mm = mm;
- ht->bucket_at = mm->bucket_at;
- ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
- ht->min_alloc_buckets_order =
- cds_lfht_get_count_order_ulong(min_nr_alloc_buckets);
- ht->max_nr_buckets = max_nr_buckets;
-
- return ht;
-}
-
-#endif /* _URCU_RCULFHASH_INTERNAL_H */
+++ /dev/null
-/*
- * rculfhash-mm-chunk.c
- *
- * Chunk based memory management for Lock-Free RCU Hash Table
- *
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <stddef.h>
-#include "rculfhash-internal.h"
-
-static
-void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0) {
- ht->tbl_chunk[0] = calloc(ht->min_nr_alloc_buckets,
- sizeof(struct cds_lfht_node));
- assert(ht->tbl_chunk[0]);
- } else if (order > ht->min_alloc_buckets_order) {
- unsigned long i, len = 1UL << (order - 1 - ht->min_alloc_buckets_order);
-
- for (i = len; i < 2 * len; i++) {
- ht->tbl_chunk[i] = calloc(ht->min_nr_alloc_buckets,
- sizeof(struct cds_lfht_node));
- assert(ht->tbl_chunk[i]);
- }
- }
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-/*
- * cds_lfht_free_bucket_table() should be called with decreasing order.
- * When cds_lfht_free_bucket_table(0) is called, it means the whole
- * lfht is destroyed.
- */
-static
-void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0)
- poison_free(ht->tbl_chunk[0]);
- else if (order > ht->min_alloc_buckets_order) {
- unsigned long i, len = 1UL << (order - 1 - ht->min_alloc_buckets_order);
-
- for (i = len; i < 2 * len; i++)
- poison_free(ht->tbl_chunk[i]);
- }
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-static
-struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
-{
- unsigned long chunk, offset;
-
- chunk = index >> ht->min_alloc_buckets_order;
- offset = index & (ht->min_nr_alloc_buckets - 1);
- return &ht->tbl_chunk[chunk][offset];
-}
-
-static
-struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets)
-{
- unsigned long nr_chunks, cds_lfht_size;
-
- min_nr_alloc_buckets = max(min_nr_alloc_buckets,
- max_nr_buckets / MAX_CHUNK_TABLE);
- nr_chunks = max_nr_buckets / min_nr_alloc_buckets;
- cds_lfht_size = offsetof(struct cds_lfht, tbl_chunk) +
- sizeof(struct cds_lfht_node *) * nr_chunks;
- cds_lfht_size = max(cds_lfht_size, sizeof(struct cds_lfht));
-
- return __default_alloc_cds_lfht(
- &cds_lfht_mm_chunk, cds_lfht_size,
- min_nr_alloc_buckets, max_nr_buckets);
-}
-
-const struct cds_lfht_mm_type cds_lfht_mm_chunk = {
- .alloc_cds_lfht = alloc_cds_lfht,
- .alloc_bucket_table = cds_lfht_alloc_bucket_table,
- .free_bucket_table = cds_lfht_free_bucket_table,
- .bucket_at = bucket_at,
-};
+++ /dev/null
-/*
- * rculfhash-mm-mmap.c
- *
- * mmap/reservation based memory management for Lock-Free RCU Hash Table
- *
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <unistd.h>
-#include <sys/mman.h>
-#include "rculfhash-internal.h"
-
-/* reserve inaccessible memory space without allocation any memory */
-static void *memory_map(size_t length)
-{
- void *ret = mmap(NULL, length, PROT_NONE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-
- assert(ret != MAP_FAILED);
- return ret;
-}
-
-static void memory_unmap(void *ptr, size_t length)
-{
- int ret __attribute__((unused));
-
- ret = munmap(ptr, length);
-
- assert(ret == 0);
-}
-
-static void memory_populate(void *ptr, size_t length)
-{
- void *ret __attribute__((unused));
-
- ret = mmap(ptr, length, PROT_READ | PROT_WRITE,
- MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-
- assert(ret == ptr);
-}
-
-/*
- * Discard garbage memory and avoid system save it when try to swap it out.
- * Make it still reserved, inaccessible.
- */
-static void memory_discard(void *ptr, size_t length)
-{
- void *ret __attribute__((unused));
-
- ret = mmap(ptr, length, PROT_NONE,
- MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-
- assert(ret == ptr);
-}
-
-static
-void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0) {
- if (ht->min_nr_alloc_buckets == ht->max_nr_buckets) {
- /* small table */
- ht->tbl_mmap = calloc(ht->max_nr_buckets,
- sizeof(*ht->tbl_mmap));
- assert(ht->tbl_mmap);
- return;
- }
- /* large table */
- ht->tbl_mmap = memory_map(ht->max_nr_buckets
- * sizeof(*ht->tbl_mmap));
- memory_populate(ht->tbl_mmap,
- ht->min_nr_alloc_buckets * sizeof(*ht->tbl_mmap));
- } else if (order > ht->min_alloc_buckets_order) {
- /* large table */
- unsigned long len = 1UL << (order - 1);
-
- assert(ht->min_nr_alloc_buckets < ht->max_nr_buckets);
- memory_populate(ht->tbl_mmap + len,
- len * sizeof(*ht->tbl_mmap));
- }
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-/*
- * cds_lfht_free_bucket_table() should be called with decreasing order.
- * When cds_lfht_free_bucket_table(0) is called, it means the whole
- * lfht is destroyed.
- */
-static
-void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0) {
- if (ht->min_nr_alloc_buckets == ht->max_nr_buckets) {
- /* small table */
- poison_free(ht->tbl_mmap);
- return;
- }
- /* large table */
- memory_unmap(ht->tbl_mmap,
- ht->max_nr_buckets * sizeof(*ht->tbl_mmap));
- } else if (order > ht->min_alloc_buckets_order) {
- /* large table */
- unsigned long len = 1UL << (order - 1);
-
- assert(ht->min_nr_alloc_buckets < ht->max_nr_buckets);
- memory_discard(ht->tbl_mmap + len, len * sizeof(*ht->tbl_mmap));
- }
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-static
-struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
-{
- return &ht->tbl_mmap[index];
-}
-
-static
-struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets)
-{
- unsigned long page_bucket_size;
-
- page_bucket_size = getpagesize() / sizeof(struct cds_lfht_node);
- if (max_nr_buckets <= page_bucket_size) {
- /* small table */
- min_nr_alloc_buckets = max_nr_buckets;
- } else {
- /* large table */
- min_nr_alloc_buckets = max(min_nr_alloc_buckets,
- page_bucket_size);
- }
-
- return __default_alloc_cds_lfht(
- &cds_lfht_mm_mmap, sizeof(struct cds_lfht),
- min_nr_alloc_buckets, max_nr_buckets);
-}
-
-const struct cds_lfht_mm_type cds_lfht_mm_mmap = {
- .alloc_cds_lfht = alloc_cds_lfht,
- .alloc_bucket_table = cds_lfht_alloc_bucket_table,
- .free_bucket_table = cds_lfht_free_bucket_table,
- .bucket_at = bucket_at,
-};
+++ /dev/null
-/*
- * rculfhash-mm-order.c
- *
- * Order based memory management for Lock-Free RCU Hash Table
- *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "rculfhash-internal.h"
-
-static
-void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0) {
- ht->tbl_order[0] = calloc(ht->min_nr_alloc_buckets,
- sizeof(struct cds_lfht_node));
- assert(ht->tbl_order[0]);
- } else if (order > ht->min_alloc_buckets_order) {
- ht->tbl_order[order] = calloc(1UL << (order -1),
- sizeof(struct cds_lfht_node));
- assert(ht->tbl_order[order]);
- }
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-/*
- * cds_lfht_free_bucket_table() should be called with decreasing order.
- * When cds_lfht_free_bucket_table(0) is called, it means the whole
- * lfht is destroyed.
- */
-static
-void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- if (order == 0)
- poison_free(ht->tbl_order[0]);
- else if (order > ht->min_alloc_buckets_order)
- poison_free(ht->tbl_order[order]);
- /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
-}
-
-static
-struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
-{
- unsigned long order;
-
- if (index < ht->min_nr_alloc_buckets) {
- dbg_printf("bucket index %lu order 0 aridx 0\n", index);
- return &ht->tbl_order[0][index];
- }
- /*
- * equivalent to cds_lfht_get_count_order_ulong(index + 1), but
- * optimizes away the non-existing 0 special-case for
- * cds_lfht_get_count_order_ulong.
- */
- order = cds_lfht_fls_ulong(index);
- dbg_printf("bucket index %lu order %lu aridx %lu\n",
- index, order, index & ((1UL << (order - 1)) - 1));
- return &ht->tbl_order[order][index & ((1UL << (order - 1)) - 1)];
-}
-
-static
-struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets)
-{
- return __default_alloc_cds_lfht(
- &cds_lfht_mm_order, sizeof(struct cds_lfht),
- min_nr_alloc_buckets, max_nr_buckets);
-}
-
-const struct cds_lfht_mm_type cds_lfht_mm_order = {
- .alloc_cds_lfht = alloc_cds_lfht,
- .alloc_bucket_table = cds_lfht_alloc_bucket_table,
- .free_bucket_table = cds_lfht_free_bucket_table,
- .bucket_at = bucket_at,
-};
+++ /dev/null
-/*
- * rculfhash.c
- *
- * Userspace RCU library - Lock-Free Resizable RCU Hash Table
- *
- * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Based on the following articles:
- * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
- * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
- * - Michael, M. M. High performance dynamic lock-free hash tables
- * and list-based sets. In Proceedings of the fourteenth annual ACM
- * symposium on Parallel algorithms and architectures, ACM Press,
- * (2002), 73-82.
- *
- * Some specificities of this Lock-Free Resizable RCU Hash Table
- * implementation:
- *
- * - RCU read-side critical section allows readers to perform hash
- * table lookups and use the returned objects safely by delaying
- * memory reclaim of a grace period.
- * - Add and remove operations are lock-free, and do not need to
- * allocate memory. They need to be executed within RCU read-side
- * critical section to ensure the objects they read are valid and to
- * deal with the cmpxchg ABA problem.
- * - add and add_unique operations are supported. add_unique checks if
- * the node key already exists in the hash table. It ensures no key
- * duplicata exists.
- * - The resize operation executes concurrently with add/remove/lookup.
- * - Hash table nodes are contained within a split-ordered list. This
- * list is ordered by incrementing reversed-bits-hash value.
- * - An index of bucket nodes is kept. These bucket nodes are the hash
- * table "buckets", and they are also chained together in the
- * split-ordered list, which allows recursive expansion.
- * - The resize operation for small tables only allows expanding the hash table.
- * It is triggered automatically by detecting long chains in the add
- * operation.
- * - The resize operation for larger tables (and available through an
- * API) allows both expanding and shrinking the hash table.
- * - Split-counters are used to keep track of the number of
- * nodes within the hash table for automatic resize triggering.
- * - Resize operation initiated by long chain detection is executed by a
- * call_rcu thread, which keeps lock-freedom of add and remove.
- * - Resize operations are protected by a mutex.
- * - The removal operation is split in two parts: first, a "removed"
- * flag is set in the next pointer within the node to remove. Then,
- * a "garbage collection" is performed in the bucket containing the
- * removed node (from the start of the bucket up to the removed node).
- * All encountered nodes with "removed" flag set in their next
- * pointers are removed from the linked-list. If the cmpxchg used for
- * removal fails (due to concurrent garbage-collection or concurrent
- * add), we retry from the beginning of the bucket. This ensures that
- * the node with "removed" flag set is removed from the hash table
- * (not visible to lookups anymore) before the RCU read-side critical
- * section held across removal ends. Furthermore, this ensures that
- * the node with "removed" flag set is removed from the linked-list
- * before its memory is reclaimed. Only the thread which removal
- * successfully set the "removed" flag (with a cmpxchg) into a node's
- * next pointer is considered to have succeeded its removal (and thus
- * owns the node to reclaim). Because we garbage-collect starting from
- * an invariant node (the start-of-bucket bucket node) up to the
- * "removed" node (or find a reverse-hash that is higher), we are sure
- * that a successful traversal of the chain leads to a chain that is
- * present in the linked-list (the start node is never removed) and
- * that is does not contain the "removed" node anymore, even if
- * concurrent delete/add operations are changing the structure of the
- * list concurrently.
- * - The add operation performs gargage collection of buckets if it
- * encounters nodes with removed flag set in the bucket where it wants
- * to add its new node. This ensures lock-freedom of add operation by
- * helping the remover unlink nodes from the list rather than to wait
- * for it do to so.
- * - A RCU "order table" indexed by log2(hash index) is copied and
- * expanded by the resize operation. This order table allows finding
- * the "bucket node" tables.
- * - There is one bucket node table per hash index order. The size of
- * each bucket node table is half the number of hashes contained in
- * this order (except for order 0).
- * - synchronzie_rcu is used to garbage-collect the old bucket node table.
- * - The per-order bucket node tables contain a compact version of the
- * hash table nodes. These tables are invariant after they are
- * populated into the hash table.
- *
- * Bucket node tables:
- *
- * hash table hash table the last all bucket node tables
- * order size bucket node 0 1 2 3 4 5 6(index)
- * table size
- * 0 1 1 1
- * 1 2 1 1 1
- * 2 4 2 1 1 2
- * 3 8 4 1 1 2 4
- * 4 16 8 1 1 2 4 8
- * 5 32 16 1 1 2 4 8 16
- * 6 64 32 1 1 2 4 8 16 32
- *
- * When growing/shrinking, we only focus on the last bucket node table
- * which size is (!order ? 1 : (1 << (order -1))).
- *
- * Example for growing/shrinking:
- * grow hash table from order 5 to 6: init the index=6 bucket node table
- * shrink hash table from order 6 to 5: fini the index=6 bucket node table
- *
- * A bit of ascii art explanation:
- *
- * Order index is the off-by-one compare to the actual power of 2 because
- * we use index 0 to deal with the 0 special-case.
- *
- * This shows the nodes for a small table ordered by reversed bits:
- *
- * bits reverse
- * 0 000 000
- * 4 100 001
- * 2 010 010
- * 6 110 011
- * 1 001 100
- * 5 101 101
- * 3 011 110
- * 7 111 111
- *
- * This shows the nodes in order of non-reversed bits, linked by
- * reversed-bit order.
- *
- * order bits reverse
- * 0 0 000 000
- * 1 | 1 001 100 <-
- * 2 | | 2 010 010 <- |
- * | | | 3 011 110 | <- |
- * 3 -> | | | 4 100 001 | |
- * -> | | 5 101 101 |
- * -> | 6 110 011
- * -> 7 111 111
- */
-
-#define _LGPL_SOURCE
-#include <stdlib.h>
-#include <errno.h>
-#include <assert.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-
-#include "config.h"
-#include <urcu.h>
-#include <urcu-call-rcu.h>
-#include <urcu/arch.h>
-#include <urcu/uatomic.h>
-#include <urcu/compiler.h>
-#include <stdio.h>
-#include <pthread.h>
-
-#include "rculfhash.h"
-#include "rculfhash-internal.h"
-#include "urcu-flavor.h"
-
-/*
- * Split-counters lazily update the global counter each 1024
- * addition/removal. It automatically keeps track of resize required.
- * We use the bucket length as indicator for need to expand for small
- * tables and machines lacking per-cpu data suppport.
- */
-#define COUNT_COMMIT_ORDER 10
-#define DEFAULT_SPLIT_COUNT_MASK 0xFUL
-#define CHAIN_LEN_TARGET 1
-#define CHAIN_LEN_RESIZE_THRESHOLD 3
-
-/*
- * Define the minimum table size.
- */
-#define MIN_TABLE_ORDER 0
-#define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
-
-/*
- * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
- */
-#define MIN_PARTITION_PER_THREAD_ORDER 12
-#define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
-
-/*
- * The removed flag needs to be updated atomically with the pointer.
- * It indicates that no node must attach to the node scheduled for
- * removal, and that node garbage collection must be performed.
- * The bucket flag does not require to be updated atomically with the
- * pointer, but it is added as a pointer low bit flag to save space.
- */
-#define REMOVED_FLAG (1UL << 0)
-#define BUCKET_FLAG (1UL << 1)
-#define REMOVAL_OWNER_FLAG (1UL << 2)
-#define FLAGS_MASK ((1UL << 3) - 1)
-
-/* Value of the end pointer. Should not interact with flags. */
-#define END_VALUE NULL
-
-DEFINE_RCU_FLAVOR(rcu_flavor);
-
-/*
- * ht_items_count: Split-counters counting the number of node addition
- * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
- * is set at hash table creation.
- *
- * These are free-running counters, never reset to zero. They count the
- * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
- * operations to update the global counter. We choose a power-of-2 value
- * for the trigger to deal with 32 or 64-bit overflow of the counter.
- */
-struct ht_items_count {
- unsigned long add, del;
-} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
-
-/*
- * rcu_resize_work: Contains arguments passed to RCU worker thread
- * responsible for performing lazy resize.
- */
-struct rcu_resize_work {
- struct rcu_head head;
- struct cds_lfht *ht;
-};
-
-/*
- * partition_resize_work: Contains arguments passed to worker threads
- * executing the hash table resize on partitions of the hash table
- * assigned to each processor's worker thread.
- */
-struct partition_resize_work {
- pthread_t thread_id;
- struct cds_lfht *ht;
- unsigned long i, start, len;
- void (*fct)(struct cds_lfht *ht, unsigned long i,
- unsigned long start, unsigned long len);
-};
-
-/*
- * Algorithm to reverse bits in a word by lookup table, extended to
- * 64-bit words.
- * Source:
- * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
- * Originally from Public Domain.
- */
-
-static const uint8_t BitReverseTable256[256] =
-{
-#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
-#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
-#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
- R6(0), R6(2), R6(1), R6(3)
-};
-#undef R2
-#undef R4
-#undef R6
-
-static
-uint8_t bit_reverse_u8(uint8_t v)
-{
- return BitReverseTable256[v];
-}
-
-static __attribute__((unused))
-uint32_t bit_reverse_u32(uint32_t v)
-{
- return ((uint32_t) bit_reverse_u8(v) << 24) |
- ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
- ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
- ((uint32_t) bit_reverse_u8(v >> 24));
-}
-
-static __attribute__((unused))
-uint64_t bit_reverse_u64(uint64_t v)
-{
- return ((uint64_t) bit_reverse_u8(v) << 56) |
- ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
- ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
- ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
- ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
- ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
- ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
- ((uint64_t) bit_reverse_u8(v >> 56));
-}
-
-static
-unsigned long bit_reverse_ulong(unsigned long v)
-{
-#if (CAA_BITS_PER_LONG == 32)
- return bit_reverse_u32(v);
-#else
- return bit_reverse_u64(v);
-#endif
-}
-
-/*
- * fls: returns the position of the most significant bit.
- * Returns 0 if no bit is set, else returns the position of the most
- * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
- */
-#if defined(__i386) || defined(__x86_64)
-static inline
-unsigned int fls_u32(uint32_t x)
-{
- int r;
-
- asm("bsrl %1,%0\n\t"
- "jnz 1f\n\t"
- "movl $-1,%0\n\t"
- "1:\n\t"
- : "=r" (r) : "rm" (x));
- return r + 1;
-}
-#define HAS_FLS_U32
-#endif
-
-#if defined(__x86_64)
-static inline
-unsigned int fls_u64(uint64_t x)
-{
- long r;
-
- asm("bsrq %1,%0\n\t"
- "jnz 1f\n\t"
- "movq $-1,%0\n\t"
- "1:\n\t"
- : "=r" (r) : "rm" (x));
- return r + 1;
-}
-#define HAS_FLS_U64
-#endif
-
-#ifndef HAS_FLS_U64
-static __attribute__((unused))
-unsigned int fls_u64(uint64_t x)
-{
- unsigned int r = 64;
-
- if (!x)
- return 0;
-
- if (!(x & 0xFFFFFFFF00000000ULL)) {
- x <<= 32;
- r -= 32;
- }
- if (!(x & 0xFFFF000000000000ULL)) {
- x <<= 16;
- r -= 16;
- }
- if (!(x & 0xFF00000000000000ULL)) {
- x <<= 8;
- r -= 8;
- }
- if (!(x & 0xF000000000000000ULL)) {
- x <<= 4;
- r -= 4;
- }
- if (!(x & 0xC000000000000000ULL)) {
- x <<= 2;
- r -= 2;
- }
- if (!(x & 0x8000000000000000ULL)) {
- x <<= 1;
- r -= 1;
- }
- return r;
-}
-#endif
-
-#ifndef HAS_FLS_U32
-static __attribute__((unused))
-unsigned int fls_u32(uint32_t x)
-{
- unsigned int r = 32;
-
- if (!x)
- return 0;
- if (!(x & 0xFFFF0000U)) {
- x <<= 16;
- r -= 16;
- }
- if (!(x & 0xFF000000U)) {
- x <<= 8;
- r -= 8;
- }
- if (!(x & 0xF0000000U)) {
- x <<= 4;
- r -= 4;
- }
- if (!(x & 0xC0000000U)) {
- x <<= 2;
- r -= 2;
- }
- if (!(x & 0x80000000U)) {
- x <<= 1;
- r -= 1;
- }
- return r;
-}
-#endif
-
-unsigned int cds_lfht_fls_ulong(unsigned long x)
-{
-#if (CAA_BITS_PER_LONG == 32)
- return fls_u32(x);
-#else
- return fls_u64(x);
-#endif
-}
-
-/*
- * Return the minimum order for which x <= (1UL << order).
- * Return -1 if x is 0.
- */
-int cds_lfht_get_count_order_u32(uint32_t x)
-{
- if (!x)
- return -1;
-
- return fls_u32(x - 1);
-}
-
-/*
- * Return the minimum order for which x <= (1UL << order).
- * Return -1 if x is 0.
- */
-int cds_lfht_get_count_order_ulong(unsigned long x)
-{
- if (!x)
- return -1;
-
- return cds_lfht_fls_ulong(x - 1);
-}
-
-static
-void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
-
-static
-void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
- unsigned long count);
-
-static long nr_cpus_mask = -1;
-static long split_count_mask = -1;
-
-#if defined(HAVE_SYSCONF)
-static void ht_init_nr_cpus_mask(void)
-{
- long maxcpus;
-
- maxcpus = sysconf(_SC_NPROCESSORS_CONF);
- if (maxcpus <= 0) {
- nr_cpus_mask = -2;
- return;
- }
- /*
- * round up number of CPUs to next power of two, so we
- * can use & for modulo.
- */
- maxcpus = 1UL << cds_lfht_get_count_order_ulong(maxcpus);
- nr_cpus_mask = maxcpus - 1;
-}
-#else /* #if defined(HAVE_SYSCONF) */
-static void ht_init_nr_cpus_mask(void)
-{
- nr_cpus_mask = -2;
-}
-#endif /* #else #if defined(HAVE_SYSCONF) */
-
-static
-void alloc_split_items_count(struct cds_lfht *ht)
-{
- struct ht_items_count *count;
-
- if (nr_cpus_mask == -1) {
- ht_init_nr_cpus_mask();
- if (nr_cpus_mask < 0)
- split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
- else
- split_count_mask = nr_cpus_mask;
- }
-
- assert(split_count_mask >= 0);
-
- if (ht->flags & CDS_LFHT_ACCOUNTING) {
- ht->split_count = calloc(split_count_mask + 1, sizeof(*count));
- assert(ht->split_count);
- } else {
- ht->split_count = NULL;
- }
-}
-
-static
-void free_split_items_count(struct cds_lfht *ht)
-{
- poison_free(ht->split_count);
-}
-
-#if defined(HAVE_SCHED_GETCPU)
-static
-int ht_get_split_count_index(unsigned long hash)
-{
- int cpu;
-
- assert(split_count_mask >= 0);
- cpu = sched_getcpu();
- if (caa_unlikely(cpu < 0))
- return hash & split_count_mask;
- else
- return cpu & split_count_mask;
-}
-#else /* #if defined(HAVE_SCHED_GETCPU) */
-static
-int ht_get_split_count_index(unsigned long hash)
-{
- return hash & split_count_mask;
-}
-#endif /* #else #if defined(HAVE_SCHED_GETCPU) */
-
-static
-void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
-{
- unsigned long split_count;
- int index;
- long count;
-
- if (caa_unlikely(!ht->split_count))
- return;
- index = ht_get_split_count_index(hash);
- split_count = uatomic_add_return(&ht->split_count[index].add, 1);
- if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
- return;
- /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
-
- dbg_printf("add split count %lu\n", split_count);
- count = uatomic_add_return(&ht->count,
- 1UL << COUNT_COMMIT_ORDER);
- if (caa_likely(count & (count - 1)))
- return;
- /* Only if global count is power of 2 */
-
- if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
- return;
- dbg_printf("add set global %ld\n", count);
- cds_lfht_resize_lazy_count(ht, size,
- count >> (CHAIN_LEN_TARGET - 1));
-}
-
-static
-void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
-{
- unsigned long split_count;
- int index;
- long count;
-
- if (caa_unlikely(!ht->split_count))
- return;
- index = ht_get_split_count_index(hash);
- split_count = uatomic_add_return(&ht->split_count[index].del, 1);
- if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
- return;
- /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
-
- dbg_printf("del split count %lu\n", split_count);
- count = uatomic_add_return(&ht->count,
- -(1UL << COUNT_COMMIT_ORDER));
- if (caa_likely(count & (count - 1)))
- return;
- /* Only if global count is power of 2 */
-
- if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
- return;
- dbg_printf("del set global %ld\n", count);
- /*
- * Don't shrink table if the number of nodes is below a
- * certain threshold.
- */
- if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
- return;
- cds_lfht_resize_lazy_count(ht, size,
- count >> (CHAIN_LEN_TARGET - 1));
-}
-
-static
-void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
-{
- unsigned long count;
-
- if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
- return;
- count = uatomic_read(&ht->count);
- /*
- * Use bucket-local length for small table expand and for
- * environments lacking per-cpu data support.
- */
- if (count >= (1UL << COUNT_COMMIT_ORDER))
- return;
- if (chain_len > 100)
- dbg_printf("WARNING: large chain length: %u.\n",
- chain_len);
- if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
- cds_lfht_resize_lazy_grow(ht, size,
- cds_lfht_get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
-}
-
-static
-struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
-{
- return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
-}
-
-static
-int is_removed(struct cds_lfht_node *node)
-{
- return ((unsigned long) node) & REMOVED_FLAG;
-}
-
-static
-struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
-{
- return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
-}
-
-static
-int is_bucket(struct cds_lfht_node *node)
-{
- return ((unsigned long) node) & BUCKET_FLAG;
-}
-
-static
-struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node)
-{
- return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG);
-}
-
-static
-int is_removal_owner(struct cds_lfht_node *node)
-{
- return ((unsigned long) node) & REMOVAL_OWNER_FLAG;
-}
-
-static
-struct cds_lfht_node *flag_removal_owner(struct cds_lfht_node *node)
-{
- return (struct cds_lfht_node *) (((unsigned long) node) | REMOVAL_OWNER_FLAG);
-}
-
-static
-struct cds_lfht_node *get_end(void)
-{
- return (struct cds_lfht_node *) END_VALUE;
-}
-
-static
-int is_end(struct cds_lfht_node *node)
-{
- return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
-}
-
-static
-unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
- unsigned long v)
-{
- unsigned long old1, old2;
-
- old1 = uatomic_read(ptr);
- do {
- old2 = old1;
- if (old2 >= v)
- return old2;
- } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
- return old2;
-}
-
-static
-void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- return ht->mm->alloc_bucket_table(ht, order);
-}
-
-/*
- * cds_lfht_free_bucket_table() should be called with decreasing order.
- * When cds_lfht_free_bucket_table(0) is called, it means the whole
- * lfht is destroyed.
- */
-static
-void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
-{
- return ht->mm->free_bucket_table(ht, order);
-}
-
-static inline
-struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
-{
- return ht->bucket_at(ht, index);
-}
-
-static inline
-struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
- unsigned long hash)
-{
- assert(size > 0);
- return bucket_at(ht, hash & (size - 1));
-}
-
-/*
- * Remove all logically deleted nodes from a bucket up to a certain node key.
- */
-static
-void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node)
-{
- struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
-
- assert(!is_bucket(bucket));
- assert(!is_removed(bucket));
- assert(!is_bucket(node));
- assert(!is_removed(node));
- for (;;) {
- iter_prev = bucket;
- /* We can always skip the bucket node initially */
- iter = rcu_dereference(iter_prev->next);
- assert(!is_removed(iter));
- assert(iter_prev->reverse_hash <= node->reverse_hash);
- /*
- * We should never be called with bucket (start of chain)
- * and logically removed node (end of path compression
- * marker) being the actual same node. This would be a
- * bug in the algorithm implementation.
- */
- assert(bucket != node);
- for (;;) {
- if (caa_unlikely(is_end(iter)))
- return;
- if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
- return;
- next = rcu_dereference(clear_flag(iter)->next);
- if (caa_likely(is_removed(next)))
- break;
- iter_prev = clear_flag(iter);
- iter = next;
- }
- assert(!is_removed(iter));
- if (is_bucket(iter))
- new_next = flag_bucket(clear_flag(next));
- else
- new_next = clear_flag(next);
- (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
- }
-}
-
-static
-int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
- struct cds_lfht_node *old_node,
- struct cds_lfht_node *old_next,
- struct cds_lfht_node *new_node)
-{
- struct cds_lfht_node *bucket, *ret_next;
-
- if (!old_node) /* Return -ENOENT if asked to replace NULL node */
- return -ENOENT;
-
- assert(!is_removed(old_node));
- assert(!is_bucket(old_node));
- assert(!is_removed(new_node));
- assert(!is_bucket(new_node));
- assert(new_node != old_node);
- for (;;) {
- /* Insert after node to be replaced */
- if (is_removed(old_next)) {
- /*
- * Too late, the old node has been removed under us
- * between lookup and replace. Fail.
- */
- return -ENOENT;
- }
- assert(old_next == clear_flag(old_next));
- assert(new_node != old_next);
- new_node->next = old_next;
- /*
- * Here is the whole trick for lock-free replace: we add
- * the replacement node _after_ the node we want to
- * replace by atomically setting its next pointer at the
- * same time we set its removal flag. Given that
- * the lookups/get next use an iterator aware of the
- * next pointer, they will either skip the old node due
- * to the removal flag and see the new node, or use
- * the old node, but will not see the new one.
- * This is a replacement of a node with another node
- * that has the same value: we are therefore not
- * removing a value from the hash table.
- */
- ret_next = uatomic_cmpxchg(&old_node->next,
- old_next, flag_removed(new_node));
- if (ret_next == old_next)
- break; /* We performed the replacement. */
- old_next = ret_next;
- }
-
- /*
- * Ensure that the old node is not visible to readers anymore:
- * lookup for the node, and remove it (along with any other
- * logically removed node) if found.
- */
- bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
- _cds_lfht_gc_bucket(bucket, new_node);
-
- assert(is_removed(rcu_dereference(old_node->next)));
- return 0;
-}
-
-/*
- * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
- * mode. A NULL unique_ret allows creation of duplicate keys.
- */
-static
-void _cds_lfht_add(struct cds_lfht *ht,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- unsigned long size,
- struct cds_lfht_node *node,
- struct cds_lfht_iter *unique_ret,
- int bucket_flag)
-{
- struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
- *return_node;
- struct cds_lfht_node *bucket;
-
- assert(!is_bucket(node));
- assert(!is_removed(node));
- bucket = lookup_bucket(ht, size, hash);
- for (;;) {
- uint32_t chain_len = 0;
-
- /*
- * iter_prev points to the non-removed node prior to the
- * insert location.
- */
- iter_prev = bucket;
- /* We can always skip the bucket node initially */
- iter = rcu_dereference(iter_prev->next);
- assert(iter_prev->reverse_hash <= node->reverse_hash);
- for (;;) {
- if (caa_unlikely(is_end(iter)))
- goto insert;
- if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
- goto insert;
-
- /* bucket node is the first node of the identical-hash-value chain */
- if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash)
- goto insert;
-
- next = rcu_dereference(clear_flag(iter)->next);
- if (caa_unlikely(is_removed(next)))
- goto gc_node;
-
- /* uniquely add */
- if (unique_ret
- && !is_bucket(next)
- && clear_flag(iter)->reverse_hash == node->reverse_hash) {
- struct cds_lfht_iter d_iter = { .node = node, .next = iter, };
-
- /*
- * uniquely adding inserts the node as the first
- * node of the identical-hash-value node chain.
- *
- * This semantic ensures no duplicated keys
- * should ever be observable in the table
- * (including observe one node by one node
- * by forward iterations)
- */
- cds_lfht_next_duplicate(ht, match, key, &d_iter);
- if (!d_iter.node)
- goto insert;
-
- *unique_ret = d_iter;
- return;
- }
-
- /* Only account for identical reverse hash once */
- if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
- && !is_bucket(next))
- check_resize(ht, size, ++chain_len);
- iter_prev = clear_flag(iter);
- iter = next;
- }
-
- insert:
- assert(node != clear_flag(iter));
- assert(!is_removed(iter_prev));
- assert(!is_removed(iter));
- assert(iter_prev != node);
- if (!bucket_flag)
- node->next = clear_flag(iter);
- else
- node->next = flag_bucket(clear_flag(iter));
- if (is_bucket(iter))
- new_node = flag_bucket(node);
- else
- new_node = node;
- if (uatomic_cmpxchg(&iter_prev->next, iter,
- new_node) != iter) {
- continue; /* retry */
- } else {
- return_node = node;
- goto end;
- }
-
- gc_node:
- assert(!is_removed(iter));
- if (is_bucket(iter))
- new_next = flag_bucket(clear_flag(next));
- else
- new_next = clear_flag(next);
- (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
- /* retry */
- }
-end:
- if (unique_ret) {
- unique_ret->node = return_node;
- /* unique_ret->next left unset, never used. */
- }
-}
-
-static
-int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
- struct cds_lfht_node *node)
-{
- struct cds_lfht_node *bucket, *next;
-
- if (!node) /* Return -ENOENT if asked to delete NULL node */
- return -ENOENT;
-
- /* logically delete the node */
- assert(!is_bucket(node));
- assert(!is_removed(node));
- assert(!is_removal_owner(node));
-
- /*
- * We are first checking if the node had previously been
- * logically removed (this check is not atomic with setting the
- * logical removal flag). Return -ENOENT if the node had
- * previously been removed.
- */
- next = rcu_dereference(node->next);
- if (caa_unlikely(is_removed(next)))
- return -ENOENT;
- assert(!is_bucket(next));
- /*
- * We set the REMOVED_FLAG unconditionally. Note that there may
- * be more than one concurrent thread setting this flag.
- * Knowing which wins the race will be known after the garbage
- * collection phase, stay tuned!
- */
- uatomic_or(&node->next, REMOVED_FLAG);
- /* We performed the (logical) deletion. */
-
- /*
- * Ensure that the node is not visible to readers anymore: lookup for
- * the node, and remove it (along with any other logically removed node)
- * if found.
- */
- bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
- _cds_lfht_gc_bucket(bucket, node);
-
- assert(is_removed(rcu_dereference(node->next)));
- /*
- * Last phase: atomically exchange node->next with a version
- * having "REMOVAL_OWNER_FLAG" set. If the returned node->next
- * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own
- * the node and win the removal race.
- * It is interesting to note that all "add" paths are forbidden
- * to change the next pointer starting from the point where the
- * REMOVED_FLAG is set, so here using a read, followed by a
- * xchg() suffice to guarantee that the xchg() will ever only
- * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag
- * was already set).
- */
- if (!is_removal_owner(uatomic_xchg(&node->next,
- flag_removal_owner(node->next))))
- return 0;
- else
- return -ENOENT;
-}
-
-static
-void *partition_resize_thread(void *arg)
-{
- struct partition_resize_work *work = arg;
-
- work->ht->flavor->register_thread();
- work->fct(work->ht, work->i, work->start, work->len);
- work->ht->flavor->unregister_thread();
- return NULL;
-}
-
-static
-void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
- unsigned long len,
- void (*fct)(struct cds_lfht *ht, unsigned long i,
- unsigned long start, unsigned long len))
-{
- unsigned long partition_len;
- struct partition_resize_work *work;
- int thread, ret;
- unsigned long nr_threads;
-
- /*
- * Note: nr_cpus_mask + 1 is always power of 2.
- * We spawn just the number of threads we need to satisfy the minimum
- * partition size, up to the number of CPUs in the system.
- */
- if (nr_cpus_mask > 0) {
- nr_threads = min(nr_cpus_mask + 1,
- len >> MIN_PARTITION_PER_THREAD_ORDER);
- } else {
- nr_threads = 1;
- }
- partition_len = len >> cds_lfht_get_count_order_ulong(nr_threads);
- work = calloc(nr_threads, sizeof(*work));
- assert(work);
- for (thread = 0; thread < nr_threads; thread++) {
- work[thread].ht = ht;
- work[thread].i = i;
- work[thread].len = partition_len;
- work[thread].start = thread * partition_len;
- work[thread].fct = fct;
- ret = pthread_create(&(work[thread].thread_id), ht->resize_attr,
- partition_resize_thread, &work[thread]);
- assert(!ret);
- }
- for (thread = 0; thread < nr_threads; thread++) {
- ret = pthread_join(work[thread].thread_id, NULL);
- assert(!ret);
- }
- free(work);
-}
-
-/*
- * Holding RCU read lock to protect _cds_lfht_add against memory
- * reclaim that could be performed by other call_rcu worker threads (ABA
- * problem).
- *
- * When we reach a certain length, we can split this population phase over
- * many worker threads, based on the number of CPUs available in the system.
- * This should therefore take care of not having the expand lagging behind too
- * many concurrent insertion threads by using the scheduler's ability to
- * schedule bucket node population fairly with insertions.
- */
-static
-void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
- unsigned long start, unsigned long len)
-{
- unsigned long j, size = 1UL << (i - 1);
-
- assert(i > MIN_TABLE_ORDER);
- ht->flavor->read_lock();
- for (j = size + start; j < size + start + len; j++) {
- struct cds_lfht_node *new_node = bucket_at(ht, j);
-
- assert(j >= size && j < (size << 1));
- dbg_printf("init populate: order %lu index %lu hash %lu\n",
- i, j, j);
- new_node->reverse_hash = bit_reverse_ulong(j);
- _cds_lfht_add(ht, j, NULL, NULL, size, new_node, NULL, 1);
- }
- ht->flavor->read_unlock();
-}
-
-static
-void init_table_populate(struct cds_lfht *ht, unsigned long i,
- unsigned long len)
-{
- assert(nr_cpus_mask != -1);
- if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
- ht->flavor->thread_online();
- init_table_populate_partition(ht, i, 0, len);
- ht->flavor->thread_offline();
- return;
- }
- partition_resize_helper(ht, i, len, init_table_populate_partition);
-}
-
-static
-void init_table(struct cds_lfht *ht,
- unsigned long first_order, unsigned long last_order)
-{
- unsigned long i;
-
- dbg_printf("init table: first_order %lu last_order %lu\n",
- first_order, last_order);
- assert(first_order > MIN_TABLE_ORDER);
- for (i = first_order; i <= last_order; i++) {
- unsigned long len;
-
- len = 1UL << (i - 1);
- dbg_printf("init order %lu len: %lu\n", i, len);
-
- /* Stop expand if the resize target changes under us */
- if (CMM_LOAD_SHARED(ht->resize_target) < (1UL << i))
- break;
-
- cds_lfht_alloc_bucket_table(ht, i);
-
- /*
- * Set all bucket nodes reverse hash values for a level and
- * link all bucket nodes into the table.
- */
- init_table_populate(ht, i, len);
-
- /*
- * Update table size.
- */
- cmm_smp_wmb(); /* populate data before RCU size */
- CMM_STORE_SHARED(ht->size, 1UL << i);
-
- dbg_printf("init new size: %lu\n", 1UL << i);
- if (CMM_LOAD_SHARED(ht->in_progress_destroy))
- break;
- }
-}
-
-/*
- * Holding RCU read lock to protect _cds_lfht_remove against memory
- * reclaim that could be performed by other call_rcu worker threads (ABA
- * problem).
- * For a single level, we logically remove and garbage collect each node.
- *
- * As a design choice, we perform logical removal and garbage collection on a
- * node-per-node basis to simplify this algorithm. We also assume keeping good
- * cache locality of the operation would overweight possible performance gain
- * that could be achieved by batching garbage collection for multiple levels.
- * However, this would have to be justified by benchmarks.
- *
- * Concurrent removal and add operations are helping us perform garbage
- * collection of logically removed nodes. We guarantee that all logically
- * removed nodes have been garbage-collected (unlinked) before call_rcu is
- * invoked to free a hole level of bucket nodes (after a grace period).
- *
- * Logical removal and garbage collection can therefore be done in batch or on a
- * node-per-node basis, as long as the guarantee above holds.
- *
- * When we reach a certain length, we can split this removal over many worker
- * threads, based on the number of CPUs available in the system. This should
- * take care of not letting resize process lag behind too many concurrent
- * updater threads actively inserting into the hash table.
- */
-static
-void remove_table_partition(struct cds_lfht *ht, unsigned long i,
- unsigned long start, unsigned long len)
-{
- unsigned long j, size = 1UL << (i - 1);
-
- assert(i > MIN_TABLE_ORDER);
- ht->flavor->read_lock();
- for (j = size + start; j < size + start + len; j++) {
- struct cds_lfht_node *fini_bucket = bucket_at(ht, j);
- struct cds_lfht_node *parent_bucket = bucket_at(ht, j - size);
-
- assert(j >= size && j < (size << 1));
- dbg_printf("remove entry: order %lu index %lu hash %lu\n",
- i, j, j);
- /* Set the REMOVED_FLAG to freeze the ->next for gc */
- uatomic_or(&fini_bucket->next, REMOVED_FLAG);
- _cds_lfht_gc_bucket(parent_bucket, fini_bucket);
- }
- ht->flavor->read_unlock();
-}
-
-static
-void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
-{
-
- assert(nr_cpus_mask != -1);
- if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
- ht->flavor->thread_online();
- remove_table_partition(ht, i, 0, len);
- ht->flavor->thread_offline();
- return;
- }
- partition_resize_helper(ht, i, len, remove_table_partition);
-}
-
-/*
- * fini_table() is never called for first_order == 0, which is why
- * free_by_rcu_order == 0 can be used as criterion to know if free must
- * be called.
- */
-static
-void fini_table(struct cds_lfht *ht,
- unsigned long first_order, unsigned long last_order)
-{
- long i;
- unsigned long free_by_rcu_order = 0;
-
- dbg_printf("fini table: first_order %lu last_order %lu\n",
- first_order, last_order);
- assert(first_order > MIN_TABLE_ORDER);
- for (i = last_order; i >= first_order; i--) {
- unsigned long len;
-
- len = 1UL << (i - 1);
- dbg_printf("fini order %lu len: %lu\n", i, len);
-
- /* Stop shrink if the resize target changes under us */
- if (CMM_LOAD_SHARED(ht->resize_target) > (1UL << (i - 1)))
- break;
-
- cmm_smp_wmb(); /* populate data before RCU size */
- CMM_STORE_SHARED(ht->size, 1UL << (i - 1));
-
- /*
- * We need to wait for all add operations to reach Q.S. (and
- * thus use the new table for lookups) before we can start
- * releasing the old bucket nodes. Otherwise their lookup will
- * return a logically removed node as insert position.
- */
- ht->flavor->update_synchronize_rcu();
- if (free_by_rcu_order)
- cds_lfht_free_bucket_table(ht, free_by_rcu_order);
-
- /*
- * Set "removed" flag in bucket nodes about to be removed.
- * Unlink all now-logically-removed bucket node pointers.
- * Concurrent add/remove operation are helping us doing
- * the gc.
- */
- remove_table(ht, i, len);
-
- free_by_rcu_order = i;
-
- dbg_printf("fini new size: %lu\n", 1UL << i);
- if (CMM_LOAD_SHARED(ht->in_progress_destroy))
- break;
- }
-
- if (free_by_rcu_order) {
- ht->flavor->update_synchronize_rcu();
- cds_lfht_free_bucket_table(ht, free_by_rcu_order);
- }
-}
-
-static
-void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size)
-{
- struct cds_lfht_node *prev, *node;
- unsigned long order, len, i;
-
- cds_lfht_alloc_bucket_table(ht, 0);
-
- dbg_printf("create bucket: order 0 index 0 hash 0\n");
- node = bucket_at(ht, 0);
- node->next = flag_bucket(get_end());
- node->reverse_hash = 0;
-
- for (order = 1; order < cds_lfht_get_count_order_ulong(size) + 1; order++) {
- len = 1UL << (order - 1);
- cds_lfht_alloc_bucket_table(ht, order);
-
- for (i = 0; i < len; i++) {
- /*
- * Now, we are trying to init the node with the
- * hash=(len+i) (which is also a bucket with the
- * index=(len+i)) and insert it into the hash table,
- * so this node has to be inserted after the bucket
- * with the index=(len+i)&(len-1)=i. And because there
- * is no other non-bucket node nor bucket node with
- * larger index/hash inserted, so the bucket node
- * being inserted should be inserted directly linked
- * after the bucket node with index=i.
- */
- prev = bucket_at(ht, i);
- node = bucket_at(ht, len + i);
-
- dbg_printf("create bucket: order %lu index %lu hash %lu\n",
- order, len + i, len + i);
- node->reverse_hash = bit_reverse_ulong(len + i);
-
- /* insert after prev */
- assert(is_bucket(prev->next));
- node->next = prev->next;
- prev->next = flag_bucket(node);
- }
- }
-}
-
-struct cds_lfht *_cds_lfht_new(unsigned long init_size,
- unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets,
- int flags,
- const struct cds_lfht_mm_type *mm,
- const struct rcu_flavor_struct *flavor,
- pthread_attr_t *attr)
-{
- struct cds_lfht *ht;
- unsigned long order;
-
- /* min_nr_alloc_buckets must be power of two */
- if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1)))
- return NULL;
-
- /* init_size must be power of two */
- if (!init_size || (init_size & (init_size - 1)))
- return NULL;
-
- /*
- * Memory management plugin default.
- */
- if (!mm) {
- if (CAA_BITS_PER_LONG > 32
- && max_nr_buckets
- && max_nr_buckets <= (1ULL << 32)) {
- /*
- * For 64-bit architectures, with max number of
- * buckets small enough not to use the entire
- * 64-bit memory mapping space (and allowing a
- * fair number of hash table instances), use the
- * mmap allocator, which is faster than the
- * order allocator.
- */
- mm = &cds_lfht_mm_mmap;
- } else {
- /*
- * The fallback is to use the order allocator.
- */
- mm = &cds_lfht_mm_order;
- }
- }
-
- /* max_nr_buckets == 0 for order based mm means infinite */
- if (mm == &cds_lfht_mm_order && !max_nr_buckets)
- max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1);
-
- /* max_nr_buckets must be power of two */
- if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1)))
- return NULL;
-
- min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE);
- init_size = max(init_size, MIN_TABLE_SIZE);
- max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets);
- init_size = min(init_size, max_nr_buckets);
-
- ht = mm->alloc_cds_lfht(min_nr_alloc_buckets, max_nr_buckets);
- assert(ht);
- assert(ht->mm == mm);
- assert(ht->bucket_at == mm->bucket_at);
-
- ht->flags = flags;
- ht->flavor = flavor;
- ht->resize_attr = attr;
- alloc_split_items_count(ht);
- /* this mutex should not nest in read-side C.S. */
- pthread_mutex_init(&ht->resize_mutex, NULL);
- order = cds_lfht_get_count_order_ulong(init_size);
- ht->resize_target = 1UL << order;
- cds_lfht_create_bucket(ht, 1UL << order);
- ht->size = 1UL << order;
- return ht;
-}
-
-void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
- cds_lfht_match_fct match, const void *key,
- struct cds_lfht_iter *iter)
-{
- struct cds_lfht_node *node, *next, *bucket;
- unsigned long reverse_hash, size;
-
- reverse_hash = bit_reverse_ulong(hash);
-
- size = rcu_dereference(ht->size);
- bucket = lookup_bucket(ht, size, hash);
- /* We can always skip the bucket node initially */
- node = rcu_dereference(bucket->next);
- node = clear_flag(node);
- for (;;) {
- if (caa_unlikely(is_end(node))) {
- node = next = NULL;
- break;
- }
- if (caa_unlikely(node->reverse_hash > reverse_hash)) {
- node = next = NULL;
- break;
- }
- next = rcu_dereference(node->next);
- assert(node == clear_flag(node));
- if (caa_likely(!is_removed(next))
- && !is_bucket(next)
- && node->reverse_hash == reverse_hash
- && caa_likely(match(node, key))) {
- break;
- }
- node = clear_flag(next);
- }
- assert(!node || !is_bucket(rcu_dereference(node->next)));
- iter->node = node;
- iter->next = next;
-}
-
-void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
- const void *key, struct cds_lfht_iter *iter)
-{
- struct cds_lfht_node *node, *next;
- unsigned long reverse_hash;
-
- node = iter->node;
- reverse_hash = node->reverse_hash;
- next = iter->next;
- node = clear_flag(next);
-
- for (;;) {
- if (caa_unlikely(is_end(node))) {
- node = next = NULL;
- break;
- }
- if (caa_unlikely(node->reverse_hash > reverse_hash)) {
- node = next = NULL;
- break;
- }
- next = rcu_dereference(node->next);
- if (caa_likely(!is_removed(next))
- && !is_bucket(next)
- && caa_likely(match(node, key))) {
- break;
- }
- node = clear_flag(next);
- }
- assert(!node || !is_bucket(rcu_dereference(node->next)));
- iter->node = node;
- iter->next = next;
-}
-
-void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
-{
- struct cds_lfht_node *node, *next;
-
- node = clear_flag(iter->next);
- for (;;) {
- if (caa_unlikely(is_end(node))) {
- node = next = NULL;
- break;
- }
- next = rcu_dereference(node->next);
- if (caa_likely(!is_removed(next))
- && !is_bucket(next)) {
- break;
- }
- node = clear_flag(next);
- }
- assert(!node || !is_bucket(rcu_dereference(node->next)));
- iter->node = node;
- iter->next = next;
-}
-
-void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
-{
- /*
- * Get next after first bucket node. The first bucket node is the
- * first node of the linked list.
- */
- iter->next = bucket_at(ht, 0)->next;
- cds_lfht_next(ht, iter);
-}
-
-void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
- struct cds_lfht_node *node)
-{
- unsigned long size;
-
- node->reverse_hash = bit_reverse_ulong(hash);
- size = rcu_dereference(ht->size);
- _cds_lfht_add(ht, hash, NULL, NULL, size, node, NULL, 0);
- ht_count_add(ht, size, hash);
-}
-
-struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *node)
-{
- unsigned long size;
- struct cds_lfht_iter iter;
-
- node->reverse_hash = bit_reverse_ulong(hash);
- size = rcu_dereference(ht->size);
- _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
- if (iter.node == node)
- ht_count_add(ht, size, hash);
- return iter.node;
-}
-
-struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *node)
-{
- unsigned long size;
- struct cds_lfht_iter iter;
-
- node->reverse_hash = bit_reverse_ulong(hash);
- size = rcu_dereference(ht->size);
- for (;;) {
- _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
- if (iter.node == node) {
- ht_count_add(ht, size, hash);
- return NULL;
- }
-
- if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
- return iter.node;
- }
-}
-
-int cds_lfht_replace(struct cds_lfht *ht,
- struct cds_lfht_iter *old_iter,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *new_node)
-{
- unsigned long size;
-
- new_node->reverse_hash = bit_reverse_ulong(hash);
- if (!old_iter->node)
- return -ENOENT;
- if (caa_unlikely(old_iter->node->reverse_hash != new_node->reverse_hash))
- return -EINVAL;
- if (caa_unlikely(!match(old_iter->node, key)))
- return -EINVAL;
- size = rcu_dereference(ht->size);
- return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
- new_node);
-}
-
-int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node)
-{
- unsigned long size, hash;
- int ret;
-
- size = rcu_dereference(ht->size);
- ret = _cds_lfht_del(ht, size, node);
- if (!ret) {
- hash = bit_reverse_ulong(node->reverse_hash);
- ht_count_del(ht, size, hash);
- }
- return ret;
-}
-
-static
-int cds_lfht_delete_bucket(struct cds_lfht *ht)
-{
- struct cds_lfht_node *node;
- unsigned long order, i, size;
-
- /* Check that the table is empty */
- node = bucket_at(ht, 0);
- do {
- node = clear_flag(node)->next;
- if (!is_bucket(node))
- return -EPERM;
- assert(!is_removed(node));
- } while (!is_end(node));
- /*
- * size accessed without rcu_dereference because hash table is
- * being destroyed.
- */
- size = ht->size;
- /* Internal sanity check: all nodes left should be bucket */
- for (i = 0; i < size; i++) {
- node = bucket_at(ht, i);
- dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
- i, i, bit_reverse_ulong(node->reverse_hash));
- assert(is_bucket(node->next));
- }
-
- for (order = cds_lfht_get_count_order_ulong(size); (long)order >= 0; order--)
- cds_lfht_free_bucket_table(ht, order);
-
- return 0;
-}
-
-/*
- * Should only be called when no more concurrent readers nor writers can
- * possibly access the table.
- */
-int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
-{
- int ret;
-
- /* Wait for in-flight resize operations to complete */
- _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
- cmm_smp_mb(); /* Store destroy before load resize */
- while (uatomic_read(&ht->in_progress_resize))
- poll(NULL, 0, 100); /* wait for 100ms */
- ret = cds_lfht_delete_bucket(ht);
- if (ret)
- return ret;
- free_split_items_count(ht);
- if (attr)
- *attr = ht->resize_attr;
- poison_free(ht);
- return ret;
-}
-
-void cds_lfht_count_nodes(struct cds_lfht *ht,
- long *approx_before,
- unsigned long *count,
- long *approx_after)
-{
- struct cds_lfht_node *node, *next;
- unsigned long nr_bucket = 0, nr_removed = 0;
-
- *approx_before = 0;
- if (ht->split_count) {
- int i;
-
- for (i = 0; i < split_count_mask + 1; i++) {
- *approx_before += uatomic_read(&ht->split_count[i].add);
- *approx_before -= uatomic_read(&ht->split_count[i].del);
- }
- }
-
- *count = 0;
-
- /* Count non-bucket nodes in the table */
- node = bucket_at(ht, 0);
- do {
- next = rcu_dereference(node->next);
- if (is_removed(next)) {
- if (!is_bucket(next))
- (nr_removed)++;
- else
- (nr_bucket)++;
- } else if (!is_bucket(next))
- (*count)++;
- else
- (nr_bucket)++;
- node = clear_flag(next);
- } while (!is_end(node));
- dbg_printf("number of logically removed nodes: %lu\n", nr_removed);
- dbg_printf("number of bucket nodes: %lu\n", nr_bucket);
- *approx_after = 0;
- if (ht->split_count) {
- int i;
-
- for (i = 0; i < split_count_mask + 1; i++) {
- *approx_after += uatomic_read(&ht->split_count[i].add);
- *approx_after -= uatomic_read(&ht->split_count[i].del);
- }
- }
-}
-
-/* called with resize mutex held */
-static
-void _do_cds_lfht_grow(struct cds_lfht *ht,
- unsigned long old_size, unsigned long new_size)
-{
- unsigned long old_order, new_order;
-
- old_order = cds_lfht_get_count_order_ulong(old_size);
- new_order = cds_lfht_get_count_order_ulong(new_size);
- dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
- old_size, old_order, new_size, new_order);
- assert(new_size > old_size);
- init_table(ht, old_order + 1, new_order);
-}
-
-/* called with resize mutex held */
-static
-void _do_cds_lfht_shrink(struct cds_lfht *ht,
- unsigned long old_size, unsigned long new_size)
-{
- unsigned long old_order, new_order;
-
- new_size = max(new_size, MIN_TABLE_SIZE);
- old_order = cds_lfht_get_count_order_ulong(old_size);
- new_order = cds_lfht_get_count_order_ulong(new_size);
- dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
- old_size, old_order, new_size, new_order);
- assert(new_size < old_size);
-
- /* Remove and unlink all bucket nodes to remove. */
- fini_table(ht, new_order + 1, old_order);
-}
-
-
-/* called with resize mutex held */
-static
-void _do_cds_lfht_resize(struct cds_lfht *ht)
-{
- unsigned long new_size, old_size;
-
- /*
- * Resize table, re-do if the target size has changed under us.
- */
- do {
- assert(uatomic_read(&ht->in_progress_resize));
- if (CMM_LOAD_SHARED(ht->in_progress_destroy))
- break;
- ht->resize_initiated = 1;
- old_size = ht->size;
- new_size = CMM_LOAD_SHARED(ht->resize_target);
- if (old_size < new_size)
- _do_cds_lfht_grow(ht, old_size, new_size);
- else if (old_size > new_size)
- _do_cds_lfht_shrink(ht, old_size, new_size);
- ht->resize_initiated = 0;
- /* write resize_initiated before read resize_target */
- cmm_smp_mb();
- } while (ht->size != CMM_LOAD_SHARED(ht->resize_target));
-}
-
-static
-unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
-{
- return _uatomic_xchg_monotonic_increase(&ht->resize_target, new_size);
-}
-
-static
-void resize_target_update_count(struct cds_lfht *ht,
- unsigned long count)
-{
- count = max(count, MIN_TABLE_SIZE);
- count = min(count, ht->max_nr_buckets);
- uatomic_set(&ht->resize_target, count);
-}
-
-void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
-{
- resize_target_update_count(ht, new_size);
- CMM_STORE_SHARED(ht->resize_initiated, 1);
- ht->flavor->thread_offline();
- pthread_mutex_lock(&ht->resize_mutex);
- _do_cds_lfht_resize(ht);
- pthread_mutex_unlock(&ht->resize_mutex);
- ht->flavor->thread_online();
-}
-
-static
-void do_resize_cb(struct rcu_head *head)
-{
- struct rcu_resize_work *work =
- caa_container_of(head, struct rcu_resize_work, head);
- struct cds_lfht *ht = work->ht;
-
- ht->flavor->thread_offline();
- pthread_mutex_lock(&ht->resize_mutex);
- _do_cds_lfht_resize(ht);
- pthread_mutex_unlock(&ht->resize_mutex);
- ht->flavor->thread_online();
- poison_free(work);
- cmm_smp_mb(); /* finish resize before decrement */
- uatomic_dec(&ht->in_progress_resize);
-}
-
-static
-void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
-{
- struct rcu_resize_work *work;
-
- /* Store resize_target before read resize_initiated */
- cmm_smp_mb();
- if (!CMM_LOAD_SHARED(ht->resize_initiated)) {
- uatomic_inc(&ht->in_progress_resize);
- cmm_smp_mb(); /* increment resize count before load destroy */
- if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
- uatomic_dec(&ht->in_progress_resize);
- return;
- }
- work = malloc(sizeof(*work));
- work->ht = ht;
- ht->flavor->update_call_rcu(&work->head, do_resize_cb);
- CMM_STORE_SHARED(ht->resize_initiated, 1);
- }
-}
-
-static
-void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
-{
- unsigned long target_size = size << growth;
-
- target_size = min(target_size, ht->max_nr_buckets);
- if (resize_target_grow(ht, target_size) >= target_size)
- return;
-
- __cds_lfht_resize_lazy_launch(ht);
-}
-
-/*
- * We favor grow operations over shrink. A shrink operation never occurs
- * if a grow operation is queued for lazy execution. A grow operation
- * cancels any pending shrink lazy execution.
- */
-static
-void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
- unsigned long count)
-{
- if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
- return;
- count = max(count, MIN_TABLE_SIZE);
- count = min(count, ht->max_nr_buckets);
- if (count == size)
- return; /* Already the right size, no resize needed */
- if (count > size) { /* lazy grow */
- if (resize_target_grow(ht, count) >= count)
- return;
- } else { /* lazy shrink */
- for (;;) {
- unsigned long s;
-
- s = uatomic_cmpxchg(&ht->resize_target, size, count);
- if (s == size)
- break; /* no resize needed */
- if (s > size)
- return; /* growing is/(was just) in progress */
- if (s <= count)
- return; /* some other thread do shrink */
- size = s;
- }
- }
- __cds_lfht_resize_lazy_launch(ht);
-}
+++ /dev/null
-#ifndef _URCU_RCULFHASH_H
-#define _URCU_RCULFHASH_H
-
-/*
- * urcu/rculfhash.h
- *
- * Userspace RCU library - Lock-Free RCU Hash Table
- *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * Include this file _after_ including your URCU flavor.
- */
-
-#include <stdint.h>
-#include <urcu/compiler.h>
-#include <urcu-call-rcu.h>
-
-#include "urcu-flavor.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * cds_lfht_node: Contains the next pointers and reverse-hash
- * value required for lookup and traversal of the hash table.
- *
- * struct cds_lfht_node should be aligned on 8-bytes boundaries because
- * the three lower bits are used as flags. It is worth noting that the
- * information contained within these three bits could be represented on
- * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
- * BUCKET_FLAG. This can be done if we ensure that no iterator nor
- * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
- * is set. Given the minimum size of struct cds_lfht_node is 8 bytes on
- * 32-bit architectures, we choose to go for simplicity and reserve
- * three bits.
- *
- * struct cds_lfht_node can be embedded into a structure (as a field).
- * caa_container_of() can be used to get the structure from the struct
- * cds_lfht_node after a lookup.
- *
- * The structure which embeds it typically holds the key (or key-value
- * pair) of the object. The caller code is responsible for calculation
- * of the hash value for cds_lfht APIs.
- */
-struct cds_lfht_node {
- struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
- unsigned long reverse_hash;
-} __attribute__((aligned(8)));
-
-/* cds_lfht_iter: Used to track state while traversing a hash chain. */
-struct cds_lfht_iter {
- struct cds_lfht_node *node, *next;
-};
-
-static inline
-struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
-{
- return iter->node;
-}
-
-struct cds_lfht;
-
-/*
- * Caution !
- * Ensure reader and writer threads are registered as urcu readers.
- */
-
-typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
-
-/*
- * cds_lfht_node_init - initialize a hash table node
- * @node: the node to initialize.
- *
- * This function is kept to be eventually used for debugging purposes
- * (detection of memory corruption).
- */
-static inline
-void cds_lfht_node_init(struct cds_lfht_node *node)
-{
-}
-
-/*
- * Hash table creation flags.
- */
-enum {
- CDS_LFHT_AUTO_RESIZE = (1U << 0),
- CDS_LFHT_ACCOUNTING = (1U << 1),
-};
-
-struct cds_lfht_mm_type {
- struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets);
- void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order);
- void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order);
- struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht,
- unsigned long index);
-};
-
-extern const struct cds_lfht_mm_type cds_lfht_mm_order;
-extern const struct cds_lfht_mm_type cds_lfht_mm_chunk;
-extern const struct cds_lfht_mm_type cds_lfht_mm_mmap;
-
-/*
- * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
- */
-struct cds_lfht *_cds_lfht_new(unsigned long init_size,
- unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets,
- int flags,
- const struct cds_lfht_mm_type *mm,
- const struct rcu_flavor_struct *flavor,
- pthread_attr_t *attr);
-
-/*
- * cds_lfht_new - allocate a hash table.
- * @init_size: number of buckets to allocate initially. Must be power of two.
- * @min_nr_alloc_buckets: the minimum number of allocated buckets.
- * (must be power of two)
- * @max_nr_buckets: the maximum number of hash table buckets allowed.
- * (must be power of two)
- * @flags: hash table creation flags (can be combined with bitwise or: '|').
- * 0: no flags.
- * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
- * CDS_LFHT_ACCOUNTING: count the number of node addition
- * and removal in the table
- * @attr: optional resize worker thread attributes. NULL for default.
- *
- * Return NULL on error.
- * Note: the RCU flavor must be already included before the hash table header.
- *
- * The programmer is responsible for ensuring that resize operation has a
- * priority equal to hash table updater threads. It should be performed by
- * specifying the appropriate priority in the pthread "attr" argument, and,
- * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
- * this priority level. Having lower priority for call_rcu and resize threads
- * does not pose any correctness issue, but the resize operations could be
- * starved by updates, thus leading to long hash table bucket chains.
- * Threads calling this API are NOT required to be registered RCU read-side
- * threads. It can be called very early.(before rcu is initialized ...etc.)
- */
-static inline
-struct cds_lfht *cds_lfht_new(unsigned long init_size,
- unsigned long min_nr_alloc_buckets,
- unsigned long max_nr_buckets,
- int flags,
- pthread_attr_t *attr)
-{
- return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets,
- flags, NULL, &rcu_flavor, attr);
-}
-
-/*
- * cds_lfht_destroy - destroy a hash table.
- * @ht: the hash table to destroy.
- * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
- * The caller will typically want to free this pointer if dynamically
- * allocated. The attr point can be NULL if the caller does not
- * need to be informed of the value passed to cds_lfht_new().
- *
- * Return 0 on success, negative error value on error.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
-
-/*
- * cds_lfht_count_nodes - count the number of nodes in the hash table.
- * @ht: the hash table.
- * @split_count_before: Sample the node count split-counter before traversal.
- * @count: Traverse the hash table, count the number of nodes observed.
- * @split_count_after: Sample the node count split-counter after traversal.
- *
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_count_nodes(struct cds_lfht *ht,
- long *split_count_before,
- unsigned long *count,
- long *split_count_after);
-
-/*
- * cds_lfht_lookup - lookup a node by key.
- * @ht: the hash table.
- * @hash: the key hash.
- * @match: the key match function.
- * @key: the current node key.
- * @iter: Node, if found (output). *iter->node set to NULL if not found.
- *
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
- cds_lfht_match_fct match, const void *key,
- struct cds_lfht_iter *iter);
-
-/*
- * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
- * @ht: the hash table.
- * @match: the key match function.
- * @key: the current node key.
- * @iter: Node, if found (output). *iter->node set to NULL if not found.
- *
- * Uses an iterator initialized by a lookup.
- * Sets *iter-node to the following node with same key.
- * Sets *iter->node to NULL if no following node exists with same key.
- * RCU read-side lock must be held across cds_lfht_lookup and
- * cds_lfht_next calls, and also between cds_lfht_next calls using the
- * node returned by a previous cds_lfht_next.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_next_duplicate(struct cds_lfht *ht,
- cds_lfht_match_fct match, const void *key,
- struct cds_lfht_iter *iter);
-
-/*
- * cds_lfht_first - get the first node in the table.
- * @ht: the hash table.
- * @iter: First node, if exists (output). *iter->node set to NULL if not found.
- *
- * Output in "*iter". *iter->node set to NULL if table is empty.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
-
-/*
- * cds_lfht_next - get the next node in the table.
- * @ht: the hash table.
- * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
- *
- * Input/Output in "*iter". *iter->node set to NULL if *iter was
- * pointing to the last table node.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
-
-/*
- * cds_lfht_add - add a node to the hash table.
- * @ht: the hash table.
- * @hash: the key hash.
- * @node: the node to add.
- *
- * This function supports adding redundant keys into the table.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
- struct cds_lfht_node *node);
-
-/*
- * cds_lfht_add_unique - add a node to hash table, if key is not present.
- * @ht: the hash table.
- * @hash: the node's hash.
- * @match: the key match function.
- * @key: the node's key.
- * @node: the node to try adding.
- *
- * Return the node added upon success.
- * Return the unique node already present upon failure. If
- * cds_lfht_add_unique fails, the node passed as parameter should be
- * freed by the caller.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- *
- * The semantic of this function is that if only this function is used
- * to add keys into the table, no duplicated keys should ever be
- * observable in the table. The same guarantee apply for combination of
- * add_unique and add_replace (see below).
- */
-struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *node);
-
-/*
- * cds_lfht_add_replace - replace or add a node within hash table.
- * @ht: the hash table.
- * @hash: the node's hash.
- * @match: the key match function.
- * @key: the node's key.
- * @node: the node to add.
- *
- * Return the node replaced upon success. If no node matching the key
- * was present, return NULL, which also means the operation succeeded.
- * This replacement operation should never fail.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- * After successful replacement, a grace period must be waited for before
- * freeing the memory reserved for the returned node.
- *
- * The semantic of replacement vs lookups is the following: if lookups
- * are performed between a key unique insertion and its removal, we
- * guarantee that the lookups and get next will always find exactly one
- * instance of the key if it is replaced concurrently with the lookups.
- *
- * Providing this semantic allows us to ensure that replacement-only
- * schemes will never generate duplicated keys. It also allows us to
- * guarantee that a combination of add_replace and add_unique updates
- * will never generate duplicated keys.
- */
-struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *node);
-
-/*
- * cds_lfht_replace - replace a node pointer to by iter within hash table.
- * @ht: the hash table.
- * @old_iter: the iterator position of the node to replace.
- * @hash: the node's hash.
- * @match: the key match function.
- * @key: the node's key.
- * @new_node: the new node to use as replacement.
- *
- * Return 0 if replacement is successful, negative value otherwise.
- * Replacing a NULL old node or an already removed node will fail with
- * -ENOENT.
- * If the hash or value of the node to replace and the new node differ,
- * this function returns -EINVAL without proceeding to the replacement.
- * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
- * RCU read-side lock must be held between lookup and replacement.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- * After successful replacement, a grace period must be waited for before
- * freeing the memory reserved for the old node (which can be accessed
- * with cds_lfht_iter_get_node).
- *
- * The semantic of replacement vs lookups is the following: if lookups
- * are performed between a key unique insertion and its removal, we
- * guarantee that the lookups and get next will always find exactly one
- * instance of the key if it is replaced concurrently with the lookups.
- *
- * Providing this semantic allows us to ensure that replacement-only
- * schemes will never generate duplicated keys. It also allows us to
- * guarantee that a combination of add_replace and add_unique updates
- * will never generate duplicated keys.
- */
-int cds_lfht_replace(struct cds_lfht *ht,
- struct cds_lfht_iter *old_iter,
- unsigned long hash,
- cds_lfht_match_fct match,
- const void *key,
- struct cds_lfht_node *new_node);
-
-/*
- * cds_lfht_del - remove node pointed to by iterator from hash table.
- * @ht: the hash table.
- * @node: the node to delete.
- *
- * Return 0 if the node is successfully removed, negative value
- * otherwise.
- * Deleting a NULL node or an already removed node will fail with a
- * negative value.
- * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
- * followed by use of cds_lfht_iter_get_node.
- * RCU read-side lock must be held between lookup and removal.
- * Call with rcu_read_lock held.
- * Threads calling this API need to be registered RCU read-side threads.
- * After successful removal, a grace period must be waited for before
- * freeing the memory reserved for old node (which can be accessed with
- * cds_lfht_iter_get_node).
- */
-int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node);
-
-/*
- * cds_lfht_resize - Force a hash table resize
- * @ht: the hash table.
- * @new_size: update to this hash table size.
- *
- * Threads calling this API need to be registered RCU read-side threads.
- */
-void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
-
-/*
- * Note: cds_lfht_for_each are safe for element removal during
- * iteration.
- */
-#define cds_lfht_for_each(ht, iter, node) \
- for (cds_lfht_first(ht, iter), \
- node = cds_lfht_iter_get_node(iter); \
- node != NULL; \
- cds_lfht_next(ht, iter), \
- node = cds_lfht_iter_get_node(iter))
-
-#define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
- for (cds_lfht_lookup(ht, hash, match, key, iter), \
- node = cds_lfht_iter_get_node(iter); \
- node != NULL; \
- cds_lfht_next_duplicate(ht, match, key, iter), \
- node = cds_lfht_iter_get_node(iter))
-
-#define cds_lfht_for_each_entry(ht, iter, pos, member) \
- for (cds_lfht_first(ht, iter), \
- pos = caa_container_of(cds_lfht_iter_get_node(iter), \
- typeof(*(pos)), member); \
- &(pos)->member != NULL; \
- cds_lfht_next(ht, iter), \
- pos = caa_container_of(cds_lfht_iter_get_node(iter), \
- typeof(*(pos)), member))
-
-#define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
- iter, pos, member) \
- for (cds_lfht_lookup(ht, hash, match, key, iter), \
- pos = caa_container_of(cds_lfht_iter_get_node(iter), \
- typeof(*(pos)), member); \
- &(pos)->member != NULL; \
- cds_lfht_next_duplicate(ht, match, key, iter), \
- pos = caa_container_of(cds_lfht_iter_get_node(iter), \
- typeof(*(pos)), member))
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _URCU_RCULFHASH_H */
+++ /dev/null
-#ifndef _URCU_FLAVOR_H
-#define _URCU_FLAVOR_H
-
-/*
- * urcu-flavor.h
- *
- * Userspace RCU header - rcu flavor declarations
- *
- * Copyright (c) 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-struct rcu_flavor_struct {
- void (*read_lock)(void);
- void (*read_unlock)(void);
- void (*read_quiescent_state)(void);
- void (*update_call_rcu)(struct rcu_head *head,
- void (*func)(struct rcu_head *head));
- void (*update_synchronize_rcu)(void);
- void (*update_defer_rcu)(void (*fct)(void *p), void *p);
-
- void (*thread_offline)(void);
- void (*thread_online)(void);
- void (*register_thread)(void);
- void (*unregister_thread)(void);
-};
-
-#define DEFINE_RCU_FLAVOR(x) \
-const struct rcu_flavor_struct x = { \
- .read_lock = rcu_read_lock, \
- .read_unlock = rcu_read_unlock, \
- .read_quiescent_state = rcu_quiescent_state, \
- .update_call_rcu = call_rcu, \
- .update_synchronize_rcu = synchronize_rcu, \
- .update_defer_rcu = defer_rcu, \
- .thread_offline = rcu_thread_offline, \
- .thread_online = rcu_thread_online, \
- .register_thread = rcu_register_thread, \
- .unregister_thread = rcu_unregister_thread,\
-}
-
-extern const struct rcu_flavor_struct rcu_flavor;
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _URCU_FLAVOR_H */
+++ /dev/null
-/*
- * Copyright (C) - Bob Jenkins, May 2006, Public Domain.
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * These are functions for producing 32-bit hashes for hash table lookup.
- * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() are
- * externally useful functions. Routines to test the hash are included if
- * SELF_TEST is defined. You can use this free for any purpose. It's in the
- * public domain. It has no warranty.
- *
- * You probably want to use hashlittle(). hashlittle() and hashbig() hash byte
- * arrays. hashlittle() is is faster than hashbig() on little-endian machines.
- * Intel and AMD are little-endian machines. On second thought, you probably
- * want hashlittle2(), which is identical to hashlittle() except it returns two
- * 32-bit hashes for the price of one. You could implement hashbig2() if you
- * wanted but I haven't bothered here.
- *
- * If you want to find a hash of, say, exactly 7 integers, do
- * a = i1; b = i2; c = i3;
- * mix(a,b,c);
- * a += i4; b += i5; c += i6;
- * mix(a,b,c);
- * a += i7;
- * final(a,b,c);
- * then use c as the hash value. If you have a variable length array of
- * 4-byte integers to hash, use hashword(). If you have a byte array (like
- * a character string), use hashlittle(). If you have several byte arrays, or
- * a mix of things, see the comments above hashlittle().
- *
- * Why is this so big? I read 12 bytes at a time into 3 4-byte integers, then
- * mix those integers. This is fast (you can do a lot more thorough mixing
- * with 12*3 instructions on 3 integers than you can with 3 instructions on 1
- * byte), but shoehorning those bytes into integers efficiently is messy.
- */
-
-#include <assert.h>
-#include <endian.h> /* attempt to define endianness */
-#include <stdint.h> /* defines uint32_t etc */
-#include <stdio.h> /* defines printf for tests */
-#include <string.h>
-#include <sys/param.h> /* attempt to define endianness */
-#include <time.h> /* defines time_t for timings in the test */
-#include <urcu/compiler.h>
-
-#include "utils.h"
-
-/*
- * My best guess at if you are big-endian or little-endian. This may
- * need adjustment.
- */
-#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
- __BYTE_ORDER == __LITTLE_ENDIAN) || \
- (defined(i386) || defined(__i386__) || defined(__i486__) || \
- defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
-# define HASH_LITTLE_ENDIAN 1
-# define HASH_BIG_ENDIAN 0
-#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
- __BYTE_ORDER == __BIG_ENDIAN) || \
- (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
-# define HASH_LITTLE_ENDIAN 0
-# define HASH_BIG_ENDIAN 1
-#else
-# define HASH_LITTLE_ENDIAN 0
-# define HASH_BIG_ENDIAN 0
-#endif
-
-#define hashsize(n) ((uint32_t)1<<(n))
-#define hashmask(n) (hashsize(n)-1)
-#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
-
-/*
- * mix -- mix 3 32-bit values reversibly.
- *
- * This is reversible, so any information in (a,b,c) before mix() is
- * still in (a,b,c) after mix().
- *
- * If four pairs of (a,b,c) inputs are run through mix(), or through
- * mix() in reverse, there are at least 32 bits of the output that
- * are sometimes the same for one pair and different for another pair.
- * This was tested for:
- * * pairs that differed by one bit, by two bits, in any combination
- * of top bits of (a,b,c), or in any combination of bottom bits of
- * (a,b,c).
- * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
- * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
- * is commonly produced by subtraction) look like a single 1-bit
- * difference.
- * * the base values were pseudorandom, all zero but one bit set, or
- * all zero plus a counter that starts at zero.
- *
- * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
- * satisfy this are
- * 4 6 8 16 19 4
- * 9 15 3 18 27 15
- * 14 9 3 7 17 3
- * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
- * for "differ" defined as + with a one-bit base and a two-bit delta. I
- * used http://burtleburtle.net/bob/hash/avalanche.html to choose
- * the operations, constants, and arrangements of the variables.
- *
- * This does not achieve avalanche. There are input bits of (a,b,c)
- * that fail to affect some output bits of (a,b,c), especially of a. The
- * most thoroughly mixed value is c, but it doesn't really even achieve
- * avalanche in c.
- *
- * This allows some parallelism. Read-after-writes are good at doubling
- * the number of bits affected, so the goal of mixing pulls in the opposite
- * direction as the goal of parallelism. I did what I could. Rotates
- * seem to cost as much as shifts on every machine I could lay my hands
- * on, and rotates are much kinder to the top and bottom bits, so I used
- * rotates.
- */
-#define mix(a,b,c) \
-{ \
- a -= c; a ^= rot(c, 4); c += b; \
- b -= a; b ^= rot(a, 6); a += c; \
- c -= b; c ^= rot(b, 8); b += a; \
- a -= c; a ^= rot(c,16); c += b; \
- b -= a; b ^= rot(a,19); a += c; \
- c -= b; c ^= rot(b, 4); b += a; \
-}
-
-/*
- * final -- final mixing of 3 32-bit values (a,b,c) into c
- *
- * Pairs of (a,b,c) values differing in only a few bits will usually
- * produce values of c that look totally different. This was tested for
- * * pairs that differed by one bit, by two bits, in any combination
- * of top bits of (a,b,c), or in any combination of bottom bits of
- * (a,b,c).
- * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
- * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
- * is commonly produced by subtraction) look like a single 1-bit
- * difference.
- * * the base values were pseudorandom, all zero but one bit set, or
- * all zero plus a counter that starts at zero.
- *
- * These constants passed:
- * 14 11 25 16 4 14 24
- * 12 14 25 16 4 14 24
- * and these came close:
- * 4 8 15 26 3 22 24
- * 10 8 15 26 3 22 24
- * 11 8 15 26 3 22 24
- */
-#define final(a,b,c) \
-{ \
- c ^= b; c -= rot(b,14); \
- a ^= c; a -= rot(c,11); \
- b ^= a; b -= rot(a,25); \
- c ^= b; c -= rot(b,16); \
- a ^= c; a -= rot(c,4); \
- b ^= a; b -= rot(a,14); \
- c ^= b; c -= rot(b,24); \
-}
-
-/*
- * k - the key, an array of uint32_t values
- * length - the length of the key, in uint32_ts
- * initval - the previous hash, or an arbitrary value
- */
-static uint32_t __attribute__((unused)) hashword(const uint32_t *k,
- size_t length, uint32_t initval)
-{
- uint32_t a, b, c;
-
- /* Set up the internal state */
- a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
-
- /*----------------------------------------- handle most of the key */
- while (length > 3) {
- a += k[0];
- b += k[1];
- c += k[2];
- mix(a, b, c);
- length -= 3;
- k += 3;
- }
-
- /*----------------------------------- handle the last 3 uint32_t's */
- switch (length) { /* all the case statements fall through */
- case 3: c += k[2];
- case 2: b += k[1];
- case 1: a += k[0];
- final(a, b, c);
- case 0: /* case 0: nothing left to add */
- break;
- }
- /*---------------------------------------------- report the result */
- return c;
-}
-
-
-/*
- * hashword2() -- same as hashword(), but take two seeds and return two 32-bit
- * values. pc and pb must both be nonnull, and *pc and *pb must both be
- * initialized with seeds. If you pass in (*pb)==0, the output (*pc) will be
- * the same as the return value from hashword().
- */
-static void __attribute__((unused)) hashword2(const uint32_t *k, size_t length,
- uint32_t *pc, uint32_t *pb)
-{
- uint32_t a, b, c;
-
- /* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
- c += *pb;
-
- while (length > 3) {
- a += k[0];
- b += k[1];
- c += k[2];
- mix(a, b, c);
- length -= 3;
- k += 3;
- }
-
- switch (length) {
- case 3 :
- c += k[2];
- case 2 :
- b += k[1];
- case 1 :
- a += k[0];
- final(a, b, c);
- case 0: /* case 0: nothing left to add */
- break;
- }
-
- *pc = c;
- *pb = b;
-}
-
-/*
- * hashlittle() -- hash a variable-length key into a 32-bit value
- * k : the key (the unaligned variable-length array of bytes)
- * length : the length of the key, counting by bytes
- * initval : can be any 4-byte value
- * Returns a 32-bit value. Every bit of the key affects every bit of
- * the return value. Two keys differing by one or two bits will have
- * totally different hash values.
- *
- * The best hash table sizes are powers of 2. There is no need to do
- * mod a prime (mod is sooo slow!). If you need less than 32 bits,
- * use a bitmask. For example, if you need only 10 bits, do
- * h = (h & hashmask(10));
- * In which case, the hash table should have hashsize(10) elements.
- *
- * If you are hashing n strings (uint8_t **)k, do it like this:
- * for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
- *
- * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
- * code any way you wish, private, educational, or commercial. It's free.
- *
- * Use for hash table lookup, or anything where one collision in 2^^32 is
- * acceptable. Do NOT use for cryptographic purposes.
- */
-static uint32_t __attribute__((unused)) hashlittle(const void *key,
- size_t length, uint32_t initval)
-{
- uint32_t a,b,c;
- union {
- const void *ptr;
- size_t i;
- } u; /* needed for Mac Powerbook G4 */
-
- /* Set up the internal state */
- a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
-
- u.ptr = key;
- if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
- const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
-
- /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
- while (length > 12) {
- a += k[0];
- b += k[1];
- c += k[2];
- mix(a,b,c);
- length -= 12;
- k += 3;
- }
-
- /*
- * "k[2]&0xffffff" actually reads beyond the end of the string, but
- * then masks off the part it's not allowed to read. Because the
- * string is aligned, the masked-off tail is in the same word as the
- * rest of the string. Every machine with memory protection I've seen
- * does it on word boundaries, so is OK with this. But VALGRIND will
- * still catch it and complain. The masking trick does make the hash
- * noticably faster for short strings (like English words).
- */
-#ifndef VALGRIND
-
- switch (length) {
- case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
- case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
- case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
- case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
- case 8 : b+=k[1]; a+=k[0]; break;
- case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
- case 6 : b+=k[1]&0xffff; a+=k[0]; break;
- case 5 : b+=k[1]&0xff; a+=k[0]; break;
- case 4 : a+=k[0]; break;
- case 3 : a+=k[0]&0xffffff; break;
- case 2 : a+=k[0]&0xffff; break;
- case 1 : a+=k[0]&0xff; break;
- case 0 : return c; /* zero length strings require no mixing */
- }
-#else /* make valgrind happy */
- const uint8_t *k8;
-
- k8 = (const uint8_t *)k;
- switch (length) {
- case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
- case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
- case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
- case 9 : c+=k8[8]; /* fall through */
- case 8 : b+=k[1]; a+=k[0]; break;
- case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
- case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
- case 5 : b+=k8[4]; /* fall through */
- case 4 : a+=k[0]; break;
- case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
- case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
- case 1 : a+=k8[0]; break;
- case 0 : return c;
- }
-#endif /* !valgrind */
- } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
- const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
- const uint8_t *k8;
-
- /*--------------- all but last block: aligned reads and different mixing */
- while (length > 12) {
- a += k[0] + (((uint32_t)k[1])<<16);
- b += k[2] + (((uint32_t)k[3])<<16);
- c += k[4] + (((uint32_t)k[5])<<16);
- mix(a,b,c);
- length -= 12;
- k += 6;
- }
-
- k8 = (const uint8_t *)k;
- switch (length) {
- case 12:
- c+=k[4]+(((uint32_t)k[5])<<16);
- b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 11:
- c+=((uint32_t)k8[10])<<16; /* fall through */
- case 10:
- c+=k[4];
- b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 9:
- c+=k8[8]; /* fall through */
- case 8:
- b+=k[2]+(((uint32_t)k[3])<<16);
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 7:
- b+=((uint32_t)k8[6])<<16; /* fall through */
- case 6:
- b+=k[2];
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 5:
- b+=k8[4]; /* fall through */
- case 4:
- a+=k[0]+(((uint32_t)k[1])<<16);
- break;
- case 3:
- a+=((uint32_t)k8[2])<<16; /* fall through */
- case 2:
- a+=k[0];
- break;
- case 1:
- a+=k8[0];
- break;
- case 0:
- return c; /* zero length requires no mixing */
- }
-
- } else { /* need to read the key one byte at a time */
- const uint8_t *k = (const uint8_t *)key;
-
- while (length > 12) {
- a += k[0];
- a += ((uint32_t)k[1])<<8;
- a += ((uint32_t)k[2])<<16;
- a += ((uint32_t)k[3])<<24;
- b += k[4];
- b += ((uint32_t)k[5])<<8;
- b += ((uint32_t)k[6])<<16;
- b += ((uint32_t)k[7])<<24;
- c += k[8];
- c += ((uint32_t)k[9])<<8;
- c += ((uint32_t)k[10])<<16;
- c += ((uint32_t)k[11])<<24;
- mix(a,b,c);
- length -= 12;
- k += 12;
- }
-
- switch(length) { /* all the case statements fall through */
- case 12: c+=((uint32_t)k[11])<<24;
- case 11: c+=((uint32_t)k[10])<<16;
- case 10: c+=((uint32_t)k[9])<<8;
- case 9: c+=k[8];
- case 8: b+=((uint32_t)k[7])<<24;
- case 7: b+=((uint32_t)k[6])<<16;
- case 6: b+=((uint32_t)k[5])<<8;
- case 5: b+=k[4];
- case 4: a+=((uint32_t)k[3])<<24;
- case 3: a+=((uint32_t)k[2])<<16;
- case 2: a+=((uint32_t)k[1])<<8;
- case 1:
- a+=k[0];
- break;
- case 0:
- return c;
- }
- }
-
- final(a,b,c);
- return c;
-}
-
-#if (CAA_BITS_PER_LONG == 64)
-/*
- * Hash function for number value.
- */
-unsigned long hash_key_ulong(void *_key, unsigned long seed)
-{
- union {
- uint64_t v64;
- uint32_t v32[2];
- } v;
- union {
- uint64_t v64;
- uint32_t v32[2];
- } key;
-
- v.v64 = (uint64_t) seed;
- key.v64 = (uint64_t) _key;
- hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
- return v.v64;
-}
-#else
-/*
- * Hash function for number value.
- */
-unsigned long hash_key_ulong(void *_key, unsigned long seed)
-{
- uint32_t key = (uint32_t) _key;
-
- return hashword(&key, 1, seed);
-}
-#endif /* CAA_BITS_PER_LONG */
-
-/*
- * Hash function for string.
- */
-unsigned long hash_key_str(void *key, unsigned long seed)
-{
- return hashlittle(key, strlen((char *) key), seed);
-}
-
-/*
- * Hash function compare for number value.
- */
-int hash_match_key_ulong(void *key1, void *key2)
-{
- if (key1 == key2) {
- return 1;
- }
-
- return 0;
-}
-
-/*
- * Hash compare function for string.
- */
-int hash_match_key_str(void *key1, void *key2)
-{
- if (strcmp(key1, key2) == 0) {
- return 1;
- }
-
- return 0;
-}
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#ifndef _LTT_HT_UTILS_H
-#define _LTT_HT_UTILS_H
-
-#include <stdint.h>
-
-unsigned long hash_key_ulong(void *_key, unsigned long seed);
-unsigned long hash_key_str(void *key, unsigned long seed);
-int hash_match_key_ulong(void *key1, void *key2);
-int hash_match_key_str(void *key1, void *key2);
-
-#endif /* _LTT_HT_UTILS_H */
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-noinst_LTLIBRARIES = liblttng-kconsumer.la
-
-liblttng_kconsumer_la_SOURCES = lttng-kconsumer.c
-
-liblttng_kconsumer_la_LIBADD = \
- $(top_builddir)/libkernelctl/libkernelctl.la
+++ /dev/null
-/*
- * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <assert.h>
-#include <fcntl.h>
-#include <poll.h>
-#include <pthread.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <lttng-kernel-ctl.h>
-#include <lttng-sessiond-comm.h>
-#include <lttng/lttng-kconsumer.h>
-#include <lttngerr.h>
-
-#include "common/runas.h"
-
-extern struct lttng_consumer_global_data consumer_data;
-extern int consumer_poll_timeout;
-extern volatile int consumer_quit;
-
-/*
- * Mmap the ring buffer, read it and write the data to the tracefile.
- *
- * Returns the number of bytes written
- */
-int lttng_kconsumer_on_read_subbuffer_mmap(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len)
-{
- unsigned long mmap_offset;
- long ret = 0;
- off_t orig_offset = stream->out_fd_offset;
- int fd = stream->wait_fd;
- int outfd = stream->out_fd;
-
- /* get the offset inside the fd to mmap */
- ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
- if (ret != 0) {
- ret = -errno;
- perror("kernctl_get_mmap_read_offset");
- goto end;
- }
-
- while (len > 0) {
- ret = write(outfd, stream->mmap_base + mmap_offset, len);
- if (ret >= len) {
- len = 0;
- } else if (ret < 0) {
- ret = -errno;
- perror("Error in file write");
- goto end;
- }
- /* This won't block, but will start writeout asynchronously */
- sync_file_range(outfd, stream->out_fd_offset, ret,
- SYNC_FILE_RANGE_WRITE);
- stream->out_fd_offset += ret;
- }
-
- lttng_consumer_sync_trace_file(stream, orig_offset);
-
- goto end;
-
-end:
- return ret;
-}
-
-/*
- * Splice the data from the ring buffer to the tracefile.
- *
- * Returns the number of bytes spliced.
- */
-int lttng_kconsumer_on_read_subbuffer_splice(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream, unsigned long len)
-{
- long ret = 0;
- loff_t offset = 0;
- off_t orig_offset = stream->out_fd_offset;
- int fd = stream->wait_fd;
- int outfd = stream->out_fd;
-
- while (len > 0) {
- DBG("splice chan to pipe offset %lu (fd : %d)",
- (unsigned long)offset, fd);
- ret = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
- SPLICE_F_MOVE | SPLICE_F_MORE);
- DBG("splice chan to pipe ret %ld", ret);
- if (ret < 0) {
- ret = errno;
- perror("Error in relay splice");
- goto splice_error;
- }
-
- ret = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL, ret,
- SPLICE_F_MOVE | SPLICE_F_MORE);
- DBG("splice pipe to file %ld", ret);
- if (ret < 0) {
- ret = errno;
- perror("Error in file splice");
- goto splice_error;
- }
- len -= ret;
- /* This won't block, but will start writeout asynchronously */
- sync_file_range(outfd, stream->out_fd_offset, ret,
- SYNC_FILE_RANGE_WRITE);
- stream->out_fd_offset += ret;
- }
- lttng_consumer_sync_trace_file(stream, orig_offset);
-
- goto end;
-
-splice_error:
- /* send the appropriate error description to sessiond */
- switch(ret) {
- case EBADF:
- lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EBADF);
- break;
- case EINVAL:
- lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EINVAL);
- break;
- case ENOMEM:
- lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ENOMEM);
- break;
- case ESPIPE:
- lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ESPIPE);
- break;
- }
-
-end:
- return ret;
-}
-
-/*
- * Take a snapshot for a specific fd
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream)
-{
- int ret = 0;
- int infd = stream->wait_fd;
-
- ret = kernctl_snapshot(infd);
- if (ret != 0) {
- ret = errno;
- perror("Getting sub-buffer snapshot.");
- }
-
- return ret;
-}
-
-/*
- * Get the produced position
- *
- * Returns 0 on success, < 0 on error
- */
-int lttng_kconsumer_get_produced_snapshot(
- struct lttng_consumer_local_data *ctx,
- struct lttng_consumer_stream *stream,
- unsigned long *pos)
-{
- int ret;
- int infd = stream->wait_fd;
-
- ret = kernctl_snapshot_get_produced(infd, pos);
- if (ret != 0) {
- ret = errno;
- perror("kernctl_snapshot_get_produced");
- }
-
- return ret;
-}
-
-int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
- int sock, struct pollfd *consumer_sockpoll)
-{
- ssize_t ret;
- struct lttcomm_consumer_msg msg;
-
- ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
- if (ret != sizeof(msg)) {
- lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_CMD);
- return ret;
- }
- if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
- return -ENOENT;
- }
-
- switch (msg.cmd_type) {
- case LTTNG_CONSUMER_ADD_CHANNEL:
- {
- struct lttng_consumer_channel *new_channel;
-
- DBG("consumer_add_channel %d", msg.u.channel.channel_key);
- new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
- -1, -1,
- msg.u.channel.mmap_len,
- msg.u.channel.max_sb_size);
- if (new_channel == NULL) {
- lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
- goto end_nosignal;
- }
- if (ctx->on_recv_channel != NULL) {
- ret = ctx->on_recv_channel(new_channel);
- if (ret == 0) {
- consumer_add_channel(new_channel);
- } else if (ret < 0) {
- goto end_nosignal;
- }
- } else {
- consumer_add_channel(new_channel);
- }
- goto end_nosignal;
- }
- case LTTNG_CONSUMER_ADD_STREAM:
- {
- struct lttng_consumer_stream *new_stream;
- int fd;
-
- /* block */
- if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
- return -EINTR;
- }
- ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
- if (ret != sizeof(fd)) {
- lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
- return ret;
- }
-
- DBG("consumer_add_stream %s (%d)", msg.u.stream.path_name,
- fd);
- new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
- msg.u.stream.stream_key,
- fd, fd,
- msg.u.stream.state,
- msg.u.stream.mmap_len,
- msg.u.stream.output,
- msg.u.stream.path_name,
- msg.u.stream.uid,
- msg.u.stream.gid);
- if (new_stream == NULL) {
- lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
- goto end;
- }
- if (ctx->on_recv_stream != NULL) {
- ret = ctx->on_recv_stream(new_stream);
- if (ret == 0) {
- consumer_add_stream(new_stream);
- } else if (ret < 0) {
- goto end;
- }
- } else {
- consumer_add_stream(new_stream);
- }
- break;
- }
- case LTTNG_CONSUMER_UPDATE_STREAM:
- {
- if (ctx->on_update_stream != NULL) {
- ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
- if (ret == 0) {
- consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
- } else if (ret < 0) {
- goto end;
- }
- } else {
- consumer_change_stream_state(msg.u.stream.stream_key,
- msg.u.stream.state);
- }
- break;
- }
- default:
- break;
- }
-end:
- /* signal the poll thread */
- ret = write(ctx->consumer_poll_pipe[1], "4", 1);
- if (ret < 0) {
- perror("write consumer poll");
- }
-end_nosignal:
- return 0;
-}
-
-/*
- * Consume data on a file descriptor and write it on a trace file.
- */
-int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
- struct lttng_consumer_local_data *ctx)
-{
- unsigned long len;
- int err;
- long ret = 0;
- int infd = stream->wait_fd;
-
- DBG("In read_subbuffer (infd : %d)", infd);
- /* Get the next subbuffer */
- err = kernctl_get_next_subbuf(infd);
- if (err != 0) {
- ret = errno;
- /*
- * This is a debug message even for single-threaded consumer,
- * because poll() have more relaxed criterions than get subbuf,
- * so get_subbuf may fail for short race windows where poll()
- * would issue wakeups.
- */
- DBG("Reserving sub buffer failed (everything is normal, "
- "it is due to concurrency)");
- goto end;
- }
-
- switch (stream->output) {
- case LTTNG_EVENT_SPLICE:
- /* read the whole subbuffer */
- err = kernctl_get_padded_subbuf_size(infd, &len);
- if (err != 0) {
- ret = errno;
- perror("Getting sub-buffer len failed.");
- goto end;
- }
-
- /* splice the subbuffer to the tracefile */
- ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
- if (ret < 0) {
- /*
- * display the error but continue processing to try
- * to release the subbuffer
- */
- ERR("Error splicing to tracefile");
- }
- break;
- case LTTNG_EVENT_MMAP:
- /* read the used subbuffer size */
- err = kernctl_get_padded_subbuf_size(infd, &len);
- if (err != 0) {
- ret = errno;
- perror("Getting sub-buffer len failed.");
- goto end;
- }
- /* write the subbuffer to the tracefile */
- ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
- if (ret < 0) {
- /*
- * display the error but continue processing to try
- * to release the subbuffer
- */
- ERR("Error writing to tracefile");
- }
- break;
- default:
- ERR("Unknown output method");
- ret = -1;
- }
-
- err = kernctl_put_next_subbuf(infd);
- if (err != 0) {
- ret = errno;
- if (errno == EFAULT) {
- perror("Error in unreserving sub buffer\n");
- } else if (errno == EIO) {
- /* Should never happen with newer LTTng versions */
- perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
- }
- goto end;
- }
-
-end:
- return ret;
-}
-
-int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
-{
- int ret;
-
- /* Opening the tracefile in write mode */
- if (stream->path_name != NULL) {
- ret = run_as_open(stream->path_name,
- O_WRONLY|O_CREAT|O_TRUNC,
- S_IRWXU|S_IRWXG|S_IRWXO,
- stream->uid, stream->gid);
- if (ret < 0) {
- ERR("Opening %s", stream->path_name);
- perror("open");
- goto error;
- }
- stream->out_fd = ret;
- }
-
- if (stream->output == LTTNG_EVENT_MMAP) {
- /* get the len of the mmap region */
- unsigned long mmap_len;
-
- ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
- if (ret != 0) {
- ret = errno;
- perror("kernctl_get_mmap_len");
- goto error_close_fd;
- }
- stream->mmap_len = (size_t) mmap_len;
-
- stream->mmap_base = mmap(NULL, stream->mmap_len,
- PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
- if (stream->mmap_base == MAP_FAILED) {
- perror("Error mmaping");
- ret = -1;
- goto error_close_fd;
- }
- }
-
- /* we return 0 to let the library handle the FD internally */
- return 0;
-
-error_close_fd:
- {
- int err;
-
- err = close(stream->out_fd);
- assert(!err);
- }
-error:
- return ret;
-}
-
+++ /dev/null
-AM_CPPFLAGS = -I$(top_srcdir)/include
-
-noinst_LTLIBRARIES = liblttng-sessiond-comm.la
-
-liblttng_sessiond_comm_la_SOURCES = lttng-sessiond-comm.c
+++ /dev/null
-/*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
- * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#define _GNU_SOURCE
-#include <assert.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/un.h>
-#include <unistd.h>
-#include <errno.h>
-
-#include <lttng-sessiond-comm.h>
-
-/*
- * Human readable error message.
- */
-static const char *lttcomm_readable_code[] = {
- [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed",
- [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed",
- &n