[ltt-dev] [UST PATCH 2/3] Remove kref from kcompat's headers and code

Mathieu Desnoyers mathieu.desnoyers at polymtl.ca
Sat Nov 27 14:08:28 EST 2010


* David Goulet (david.goulet at polymtl.ca) wrote:
> It contains the exact same code of urcu_ref.h. The kref
> struct and methods are replaced by those from liburcu.

Merged, thanks!

> 
> Signed-off-by: David Goulet <david.goulet at polymtl.ca>
> ---
>  include/Makefile.am           |    1 -
>  include/ust/kcompat/kcompat.h |    2 -
>  include/ust/kcompat/kref.h    |   46 ---------------------------------------
>  libust/buffers.c              |   46 +++++++++++++++++++-------------------
>  libust/buffers.h              |    2 +-
>  libust/channels.c             |   48 ++++++++++++++++++++--------------------
>  libust/channels.h             |    9 +++++--
>  libust/tracer.c               |   18 +++++++-------
>  libust/tracer.h               |    8 +++---
>  9 files changed, 67 insertions(+), 113 deletions(-)
>  delete mode 100644 include/ust/kcompat/kref.h
> 
> diff --git a/include/Makefile.am b/include/Makefile.am
> index dd000ba..5697e43 100644
> --- a/include/Makefile.am
> +++ b/include/Makefile.am
> @@ -15,7 +15,6 @@ nobase_include_HEADERS = \
>  	ust/kcompat/compiler.h \
>  	ust/kcompat/disable.h \
>  	ust/kcompat/jhash.h \
> -	ust/kcompat/kref.h \
>  	ust/kcompat/simple.h \
>  	ust/kcompat/types.h \
>  	ust/kcompat/stringify.h \
> diff --git a/include/ust/kcompat/kcompat.h b/include/ust/kcompat/kcompat.h
> index b506afa..895144b 100644
> --- a/include/ust/kcompat/kcompat.h
> +++ b/include/ust/kcompat/kcompat.h
> @@ -63,6 +63,4 @@
>  
>  #include <ust/kcompat/disable.h>
>  
> -#include <ust/kcompat/kref.h>
> -
>  #endif /* KCOMPAT_H */
> diff --git a/include/ust/kcompat/kref.h b/include/ust/kcompat/kref.h
> deleted file mode 100644
> index f0604ef..0000000
> --- a/include/ust/kcompat/kref.h
> +++ /dev/null
> @@ -1,46 +0,0 @@
> -#ifndef _KCOMPAT_KREF_H
> -#define _KCOMPAT_KREF_H
> -
> -/*
> - * Kernel sourcecode compatible reference counting implementation
> - *
> - * Copyright (C) 2009 Novell Inc.
> - *
> - * Author: Jan Blunck <jblunck at suse.de>
> - *
> - * This program is free software; you can redistribute it and/or modify it
> - * under the terms of the GNU Lesser General Public License version 2.1 as
> - * published by the Free  Software Foundation.
> - */
> -
> -#include <assert.h>
> -#include <urcu/uatomic_arch.h>
> -
> -struct kref {
> -	long refcount; /* ATOMIC */
> -};
> -
> -static inline void kref_set(struct kref *ref, int val)
> -{
> -	uatomic_set(&ref->refcount, val);
> -}
> -
> -static inline void kref_init(struct kref *ref)
> -{
> -	kref_set(ref, 1);
> -}
> -
> -static inline void kref_get(struct kref *ref)
> -{
> -	long result = uatomic_add_return(&ref->refcount, 1);
> -	assert(result != 0);
> -}
> -
> -static inline void kref_put(struct kref *ref, void (*release)(struct kref *))
> -{
> -	long res = uatomic_sub_return(&ref->refcount, 1);
> -	if (res == 0)
> -		release(ref);
> -}
> -
> -#endif /* _KCOMPAT_KREF_H */
> diff --git a/libust/buffers.c b/libust/buffers.c
> index 2e4bb66..3c8b943 100644
> --- a/libust/buffers.c
> +++ b/libust/buffers.c
> @@ -197,13 +197,13 @@ int ust_buffers_create_buf(struct ust_channel *channel, int cpu)
>  		return -1;
>  
>  	buf->chan = channel;
> -	kref_get(&channel->kref);
> +	urcu_ref_get(&channel->urcu_ref);
>  	return 0;
>  }
>  
> -static void ust_buffers_destroy_channel(struct kref *kref)
> +static void ust_buffers_destroy_channel(struct urcu_ref *urcu_ref)
>  {
> -	struct ust_channel *chan = _ust_container_of(kref, struct ust_channel, kref);
> +	struct ust_channel *chan = _ust_container_of(urcu_ref, struct ust_channel, urcu_ref);
>  	free(chan);
>  }
>  
> @@ -219,13 +219,13 @@ static void ust_buffers_destroy_buf(struct ust_buffer *buf)
>  
>  //ust//	chan->buf[buf->cpu] = NULL;
>  	free(buf);
> -	kref_put(&chan->kref, ust_buffers_destroy_channel);
> +	urcu_ref_put(&chan->urcu_ref, ust_buffers_destroy_channel);
>  }
>  
> -/* called from kref_put */
> -static void ust_buffers_remove_buf(struct kref *kref)
> +/* called from urcu_ref_put */
> +static void ust_buffers_remove_buf(struct urcu_ref *urcu_ref)
>  {
> -	struct ust_buffer *buf = _ust_container_of(kref, struct ust_buffer, kref);
> +	struct ust_buffer *buf = _ust_container_of(urcu_ref, struct ust_buffer, urcu_ref);
>  	ust_buffers_destroy_buf(buf);
>  }
>  
> @@ -237,7 +237,7 @@ int ust_buffers_open_buf(struct ust_channel *chan, int cpu)
>  	if (result == -1)
>  		return -1;
>  
> -	kref_init(&chan->buf[cpu]->kref);
> +	urcu_ref_init(&chan->buf[cpu]->urcu_ref);
>  
>  	result = ust_buffers_init_buffer(chan->trace, chan, chan->buf[cpu], chan->subbuf_cnt);
>  	if(result == -1)
> @@ -254,7 +254,7 @@ int ust_buffers_open_buf(struct ust_channel *chan, int cpu)
>   */
>  static void ust_buffers_close_buf(struct ust_buffer *buf)
>  {
> -	kref_put(&buf->kref, ust_buffers_remove_buf);
> +	urcu_ref_put(&buf->urcu_ref, ust_buffers_remove_buf);
>  }
>  
>  int ust_buffers_channel_open(struct ust_channel *chan, size_t subbuf_size, size_t subbuf_cnt)
> @@ -280,7 +280,7 @@ int ust_buffers_channel_open(struct ust_channel *chan, size_t subbuf_size, size_
>  	chan->subbuf_size_order = get_count_order(subbuf_size);
>  	chan->alloc_size = subbuf_size * subbuf_cnt;
>  
> -	kref_init(&chan->kref);
> +	urcu_ref_init(&chan->urcu_ref);
>  
>  	pthread_mutex_lock(&ust_buffers_channels_mutex);
>  	for(i=0; i<chan->n_cpus; i++) {
> @@ -301,7 +301,7 @@ error:
>  		do {} while(0);
>  	}
>  
> -	kref_put(&chan->kref, ust_buffers_destroy_channel);
> +	urcu_ref_put(&chan->urcu_ref, ust_buffers_destroy_channel);
>  	pthread_mutex_unlock(&ust_buffers_channels_mutex);
>  	return -1;
>  }
> @@ -321,7 +321,7 @@ void ust_buffers_channel_close(struct ust_channel *chan)
>  	}
>  
>  	cds_list_del(&chan->list);
> -	kref_put(&chan->kref, ust_buffers_destroy_channel);
> +	urcu_ref_put(&chan->urcu_ref, ust_buffers_destroy_channel);
>  	pthread_mutex_unlock(&ust_buffers_channels_mutex);
>  }
>  
> @@ -590,10 +590,10 @@ static void ltt_relay_print_buffer_errors(struct ust_channel *channel, int cpu)
>  	ltt_relay_print_errors(trace, channel, cpu);
>  }
>  
> -static void ltt_relay_release_channel(struct kref *kref)
> +static void ltt_relay_release_channel(struct urcu_ref *urcu_ref)
>  {
> -	struct ust_channel *ltt_chan = _ust_container_of(kref,
> -			struct ust_channel, kref);
> +	struct ust_channel *ltt_chan = _ust_container_of(urcu_ref,
> +			struct ust_channel, urcu_ref);
>  	free(ltt_chan->buf);
>  }
>  
> @@ -648,9 +648,9 @@ static int ust_buffers_init_buffer(struct ust_trace *trace,
>  		zmalloc(sizeof(*buf->commit_count) * n_subbufs);
>  	if (!buf->commit_count)
>  		return -ENOMEM;
> -	kref_get(&trace->kref);
> -	kref_get(&trace->ltt_transport_kref);
> -	kref_get(&ltt_chan->kref);
> +	urcu_ref_get(&trace->urcu_ref);
> +	urcu_ref_get(&trace->ltt_transport_urcu_ref);
> +	urcu_ref_get(&ltt_chan->urcu_ref);
>  	uatomic_set(&buf->offset, ltt_subbuffer_header_size());
>  	uatomic_set(&buf->consumed, 0);
>  	uatomic_set(&buf->active_readers, 0);
> @@ -694,14 +694,14 @@ static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan, int cpu)
>  	struct ust_trace *trace = ltt_chan->trace;
>  	struct ust_buffer *ltt_buf = ltt_chan->buf[cpu];
>  
> -	kref_put(&ltt_chan->trace->ltt_transport_kref,
> +	urcu_ref_put(&ltt_chan->trace->ltt_transport_urcu_ref,
>  		ltt_release_transport);
>  	ltt_relay_print_buffer_errors(ltt_chan, cpu);
>  //ust//	free(ltt_buf->commit_seq);
>  	free(ltt_buf->commit_count);
>  	ltt_buf->commit_count = NULL;
> -	kref_put(&ltt_chan->kref, ltt_relay_release_channel);
> -	kref_put(&trace->kref, ltt_release_trace);
> +	urcu_ref_put(&ltt_chan->urcu_ref, ltt_relay_release_channel);
> +	urcu_ref_put(&trace->urcu_ref, ltt_release_trace);
>  //ust//	wake_up_interruptible(&trace->kref_wq);
>  }
>  
> @@ -769,7 +769,7 @@ static int ust_buffers_create_channel(const char *trace_name, struct ust_trace *
>  {
>  	int result;
>  
> -	kref_init(&ltt_chan->kref);
> +	urcu_ref_init(&ltt_chan->urcu_ref);
>  
>  	ltt_chan->trace = trace;
>  	ltt_chan->overwrite = overwrite;
> @@ -859,7 +859,7 @@ static void ltt_relay_finish_channel(struct ust_channel *channel)
>  static void ltt_relay_remove_channel(struct ust_channel *channel)
>  {
>  	ust_buffers_channel_close(channel);
> -	kref_put(&channel->kref, ltt_relay_release_channel);
> +	urcu_ref_put(&channel->urcu_ref, ltt_relay_release_channel);
>  }
>  
>  /*
> diff --git a/libust/buffers.h b/libust/buffers.h
> index e7630bb..eb75c8b 100644
> --- a/libust/buffers.h
> +++ b/libust/buffers.h
> @@ -94,7 +94,7 @@ struct ust_buffer {
>  
>  	struct ust_channel *chan;
>  
> -	struct kref kref;
> +	struct urcu_ref urcu_ref;
>  	void *buf_data;
>  	size_t buf_size;
>  	int shmid;
> diff --git a/libust/channels.c b/libust/channels.c
> index 6716b5d..8930705 100644
> --- a/libust/channels.c
> +++ b/libust/channels.c
> @@ -38,11 +38,11 @@ static CDS_LIST_HEAD(ltt_channels);
>   * Index of next channel in array. Makes sure that as long as a trace channel is
>   * allocated, no array index will be re-used when a channel is freed and then
>   * another channel is allocated. This index is cleared and the array indexeds
> - * get reassigned when the index_kref goes back to 0, which indicates that no
> + * get reassigned when the index_urcu_ref goes back to 0, which indicates that no
>   * more trace channels are allocated.
>   */
>  static unsigned int free_index;
> -static struct kref index_kref;	/* Keeps track of allocated trace channels */
> +static struct urcu_ref index_urcu_ref;	/* Keeps track of allocated trace channels */
>  
>  int ust_channels_overwrite_by_default = 0;
>  int ust_channels_request_collection_by_default = 1;
> @@ -64,14 +64,14 @@ static struct ltt_channel_setting *lookup_channel(const char *name)
>   *
>   * Called with lock_markers() and channels mutex held.
>   */
> -static void release_channel_setting(struct kref *kref)
> +static void release_channel_setting(struct urcu_ref *urcu_ref)
>  {
> -	struct ltt_channel_setting *setting = _ust_container_of(kref,
> -		struct ltt_channel_setting, kref);
> +	struct ltt_channel_setting *setting = _ust_container_of(urcu_ref,
> +		struct ltt_channel_setting, urcu_ref);
>  	struct ltt_channel_setting *iter;
>  
> -	if (uatomic_read(&index_kref.refcount) == 0
> -	    && uatomic_read(&setting->kref.refcount) == 0) {
> +	if (uatomic_read(&index_urcu_ref.refcount) == 0
> +	    && uatomic_read(&setting->urcu_ref.refcount) == 0) {
>  		cds_list_del(&setting->list);
>  		free(setting);
>  
> @@ -90,12 +90,12 @@ static void release_channel_setting(struct kref *kref)
>   *
>   * Called with lock_markers() and channels mutex held.
>   */
> -static void release_trace_channel(struct kref *kref)
> +static void release_trace_channel(struct urcu_ref *urcu_ref)
>  {
>  	struct ltt_channel_setting *iter, *n;
>  
>  	cds_list_for_each_entry_safe(iter, n, &ltt_channels, list)
> -		release_channel_setting(&iter->kref);
> +		release_channel_setting(&iter->urcu_ref);
>  }
>  
>  /**
> @@ -112,10 +112,10 @@ int ltt_channels_register(const char *name)
>  	pthread_mutex_lock(&ltt_channel_mutex);
>  	setting = lookup_channel(name);
>  	if (setting) {
> -		if (uatomic_read(&setting->kref.refcount) == 0)
> -			goto init_kref;
> +		if (uatomic_read(&setting->urcu_ref.refcount) == 0)
> +			goto init_urcu_ref;
>  		else {
> -			kref_get(&setting->kref);
> +			urcu_ref_get(&setting->urcu_ref);
>  			goto end;
>  		}
>  	}
> @@ -127,8 +127,8 @@ int ltt_channels_register(const char *name)
>  	cds_list_add(&setting->list, &ltt_channels);
>  	strncpy(setting->name, name, PATH_MAX-1);
>  	setting->index = free_index++;
> -init_kref:
> -	kref_init(&setting->kref);
> +init_urcu_ref:
> +	urcu_ref_init(&setting->urcu_ref);
>  end:
>  	pthread_mutex_unlock(&ltt_channel_mutex);
>  	return ret;
> @@ -148,11 +148,11 @@ int ltt_channels_unregister(const char *name)
>  
>  	pthread_mutex_lock(&ltt_channel_mutex);
>  	setting = lookup_channel(name);
> -	if (!setting || uatomic_read(&setting->kref.refcount) == 0) {
> +	if (!setting || uatomic_read(&setting->urcu_ref.refcount) == 0) {
>  		ret = -ENOENT;
>  		goto end;
>  	}
> -	kref_put(&setting->kref, release_channel_setting);
> +	urcu_ref_put(&setting->urcu_ref, release_channel_setting);
>  end:
>  	pthread_mutex_unlock(&ltt_channel_mutex);
>  	return ret;
> @@ -174,7 +174,7 @@ int ltt_channels_set_default(const char *name,
>  
>  	pthread_mutex_lock(&ltt_channel_mutex);
>  	setting = lookup_channel(name);
> -	if (!setting || uatomic_read(&setting->kref.refcount) == 0) {
> +	if (!setting || uatomic_read(&setting->urcu_ref.refcount) == 0) {
>  		ret = -ENOENT;
>  		goto end;
>  	}
> @@ -198,7 +198,7 @@ const char *ltt_channels_get_name_from_index(unsigned int index)
>  	struct ltt_channel_setting *iter;
>  
>  	cds_list_for_each_entry(iter, &ltt_channels, list)
> -		if (iter->index == index && uatomic_read(&iter->kref.refcount))
> +		if (iter->index == index && uatomic_read(&iter->urcu_ref.refcount))
>  			return iter->name;
>  	return NULL;
>  }
> @@ -211,7 +211,7 @@ ltt_channels_get_setting_from_name(const char *name)
>  
>  	cds_list_for_each_entry(iter, &ltt_channels, list)
>  		if (!strcmp(iter->name, name)
> -		    && uatomic_read(&iter->kref.refcount))
> +		    && uatomic_read(&iter->urcu_ref.refcount))
>  			return iter;
>  	return NULL;
>  }
> @@ -259,10 +259,10 @@ struct ust_channel *ltt_channels_trace_alloc(unsigned int *nr_channels,
>  		WARN("ltt_channels_trace_alloc: no free_index; are there any probes connected?");
>  		goto end;
>  	}
> -	if (!uatomic_read(&index_kref.refcount))
> -		kref_init(&index_kref);
> +	if (!uatomic_read(&index_urcu_ref.refcount))
> +		urcu_ref_init(&index_urcu_ref);
>  	else
> -		kref_get(&index_kref);
> +		urcu_ref_get(&index_urcu_ref);
>  	*nr_channels = free_index;
>  	channel = zmalloc(sizeof(struct ust_channel) * free_index);
>  	if (!channel) {
> @@ -270,7 +270,7 @@ struct ust_channel *ltt_channels_trace_alloc(unsigned int *nr_channels,
>  		goto end;
>  	}
>  	cds_list_for_each_entry(iter, &ltt_channels, list) {
> -		if (!uatomic_read(&iter->kref.refcount))
> +		if (!uatomic_read(&iter->urcu_ref.refcount))
>  			continue;
>  		channel[iter->index].subbuf_size = iter->subbuf_size;
>  		channel[iter->index].subbuf_cnt = iter->subbuf_cnt;
> @@ -297,7 +297,7 @@ void ltt_channels_trace_free(struct ust_channel *channels)
>  	lock_markers();
>  	pthread_mutex_lock(&ltt_channel_mutex);
>  	free(channels);
> -	kref_put(&index_kref, release_trace_channel);
> +	urcu_ref_put(&index_urcu_ref, release_trace_channel);
>  	pthread_mutex_unlock(&ltt_channel_mutex);
>  	unlock_markers();
>  }
> diff --git a/libust/channels.h b/libust/channels.h
> index 6db8e63..e7bc59f 100644
> --- a/libust/channels.h
> +++ b/libust/channels.h
> @@ -24,9 +24,12 @@
>  #include <linux/limits.h>
>  #include <errno.h>
>  #include <ust/kcompat/kcompat.h>
> -#include <urcu/list.h>
>  #include <ust/core.h>
>  
> +#define _LGPL_SOURCE
> +#include <urcu/list.h>
> +#include <urcu/urcu_ref.h>
> +
>  #define EVENTS_PER_CHANNEL	65536
>  #define MAX_CPUS		32
>  
> @@ -52,7 +55,7 @@ struct ust_channel {
>  						 */
>  	/* End of first 32 bytes cacheline */
>  
> -	struct kref kref;	/* Channel transport reference count */
> +	struct urcu_ref urcu_ref;	/* Channel transport reference count */
>  	size_t subbuf_size;
>  	int subbuf_size_order;
>  	unsigned int subbuf_cnt;
> @@ -67,7 +70,7 @@ struct ust_channel {
>  struct ltt_channel_setting {
>  	unsigned int subbuf_size;
>  	unsigned int subbuf_cnt;
> -	struct kref kref;	/* Number of references to structure content */
> +	struct urcu_ref urcu_ref;	/* Number of references to structure content */
>  	struct cds_list_head list;
>  	unsigned int index;	/* index of channel in trace channel array */
>  	u16 free_event_id;	/* Next event ID to allocate */
> diff --git a/libust/tracer.c b/libust/tracer.c
> index ecf403a..eb44f54 100644
> --- a/libust/tracer.c
> +++ b/libust/tracer.c
> @@ -324,7 +324,7 @@ struct ust_trace *_ltt_trace_find_setup(const char *trace_name)
>   * ltt_release_transport - Release an LTT transport
>   * @kref : reference count on the transport
>   */
> -void ltt_release_transport(struct kref *kref)
> +void ltt_release_transport(struct urcu_ref *urcu_ref)
>  {
>  //ust// 	struct ust_trace *trace = container_of(kref,
>  //ust// 			struct ust_trace, ltt_transport_kref);
> @@ -335,10 +335,10 @@ void ltt_release_transport(struct kref *kref)
>   * ltt_release_trace - Release a LTT trace
>   * @kref : reference count on the trace
>   */
> -void ltt_release_trace(struct kref *kref)
> +void ltt_release_trace(struct urcu_ref *urcu_ref)
>  {
> -	struct ust_trace *trace = _ust_container_of(kref,
> -			struct ust_trace, kref);
> +	struct ust_trace *trace = _ust_container_of(urcu_ref,
> +			struct ust_trace, urcu_ref);
>  	ltt_channels_trace_free(trace->channels);
>  	free(trace);
>  }
> @@ -640,9 +640,9 @@ int ltt_trace_alloc(const char *trace_name)
>  		goto traces_error;
>  	}
>  
> -	kref_init(&trace->kref);
> -	kref_init(&trace->ltt_transport_kref);
> -//ust//	init_waitqueue_head(&trace->kref_wq);
> +	urcu_ref_init(&trace->urcu_ref);
> +	urcu_ref_init(&trace->ltt_transport_urcu_ref);
> +//ust//	init_waitqueue_head(&trace->urcu_ref_wq);
>  	trace->active = 0;
>  //ust//	get_trace_clock();
>  	trace->freq_scale = trace_clock_freq_scale();
> @@ -811,7 +811,7 @@ static void __ltt_trace_destroy(struct ust_trace *trace, int drop)
>  			trace->ops->remove_channel(chan);
>  	}
>  
> -	kref_put(&trace->ltt_transport_kref, ltt_release_transport);
> +	urcu_ref_put(&trace->ltt_transport_urcu_ref, ltt_release_transport);
>  
>  //ust//	module_put(trace->transport->owner);
>  
> @@ -824,7 +824,7 @@ static void __ltt_trace_destroy(struct ust_trace *trace, int drop)
>  //ust//		__wait_event_interruptible(trace->kref_wq,
>  //ust//			(atomic_read(&trace->kref.refcount) == 1), ret);
>  //ust//	}
> -	kref_put(&trace->kref, ltt_release_trace);
> +	urcu_ref_put(&trace->urcu_ref, ltt_release_trace);
>  }
>  
>  int ltt_trace_destroy(const char *trace_name, int drop)
> diff --git a/libust/tracer.h b/libust/tracer.h
> index c316c9a..64996b2 100644
> --- a/libust/tracer.h
> +++ b/libust/tracer.h
> @@ -185,9 +185,9 @@ struct ust_trace {
>  	struct {
>  		struct dentry			*trace_root;
>  	} dentry;
> -	struct kref kref; /* Each channel has a kref of the trace struct */
> +	struct urcu_ref urcu_ref; /* Each channel has a urcu_ref of the trace struct */
>  	struct ltt_transport *transport;
> -	struct kref ltt_transport_kref;
> +	struct urcu_ref ltt_transport_urcu_ref;
>  	char trace_name[NAME_MAX];
>  } ____cacheline_aligned;
>  
> @@ -427,8 +427,8 @@ extern void ltt_core_register(int (*function)(u8, void *));
>  
>  extern void ltt_core_unregister(void);
>  
> -extern void ltt_release_trace(struct kref *kref);
> -extern void ltt_release_transport(struct kref *kref);
> +extern void ltt_release_trace(struct urcu_ref *urcu_ref);
> +extern void ltt_release_transport(struct urcu_ref *urcu_ref);
>  
>  extern void ltt_dump_marker_state(struct ust_trace *trace);
>  
> -- 
> 1.7.3.2
> 

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com




More information about the lttng-dev mailing list