Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3a7c489 authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo
Browse files

perf tools: Reference count struct dso

This has a different model than the 'thread' and 'map' struct lifetimes:
there is not a definitive "don't use this DSO anymore" event, i.e. we may
get many 'struct map' holding references to the '/usr/lib64/libc-2.20.so'
DSO but then at some point some DSO may have no references but we still
don't want to straight away release its resources, because "soon" we may
get a new 'struct map' that needs it and we want to reuse its symtab or
other resources.

So we need some way to garbage collect it when crossing some memory
usage threshold, which is left for anoter patch, for now it is
sufficient to release it when calling dsos__exit(), i.e. when deleting
the whole list as part of deleting the 'struct machine' containing it,
which will leave only referenced objects being used.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: http://lkml.kernel.org/n/tip-majzgz07cm90t2tejrjy4clf@git.kernel.org


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent e8807844
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -166,7 +166,7 @@ int test__dso_data(void)
		free(buf);
		free(buf);
	}
	}


	dso__delete(dso);
	dso__put(dso);
	unlink(file);
	unlink(file);
	return 0;
	return 0;
}
}
@@ -226,7 +226,7 @@ static void dsos__delete(int cnt)
		struct dso *dso = dsos[i];
		struct dso *dso = dsos[i];


		unlink(dso->name);
		unlink(dso->name);
		dso__delete(dso);
		dso__put(dso);
	}
	}


	free(dsos);
	free(dsos);
+5 −1
Original line number Original line Diff line number Diff line
@@ -134,11 +134,15 @@ struct machine *setup_fake_machine(struct machines *machines)


			sym = symbol__new(fsym->start, fsym->length,
			sym = symbol__new(fsym->start, fsym->length,
					  STB_GLOBAL, fsym->name);
					  STB_GLOBAL, fsym->name);
			if (sym == NULL)
			if (sym == NULL) {
				dso__put(dso);
				goto out;
				goto out;
			}


			symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
			symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
		}
		}

		dso__put(dso);
	}
	}


	return machine;
	return machine;
+36 −1
Original line number Original line Diff line number Diff line
@@ -1049,6 +1049,7 @@ struct dso *dso__new(const char *name)
		INIT_LIST_HEAD(&dso->node);
		INIT_LIST_HEAD(&dso->node);
		INIT_LIST_HEAD(&dso->data.open_entry);
		INIT_LIST_HEAD(&dso->data.open_entry);
		pthread_mutex_init(&dso->lock, NULL);
		pthread_mutex_init(&dso->lock, NULL);
		atomic_set(&dso->refcnt, 1);
	}
	}


	return dso;
	return dso;
@@ -1083,6 +1084,19 @@ void dso__delete(struct dso *dso)
	free(dso);
	free(dso);
}
}


struct dso *dso__get(struct dso *dso)
{
	if (dso)
		atomic_inc(&dso->refcnt);
	return dso;
}

void dso__put(struct dso *dso)
{
	if (dso && atomic_dec_and_test(&dso->refcnt))
		dso__delete(dso);
}

void dso__set_build_id(struct dso *dso, void *build_id)
void dso__set_build_id(struct dso *dso, void *build_id)
{
{
	memcpy(dso->build_id, build_id, sizeof(dso->build_id));
	memcpy(dso->build_id, build_id, sizeof(dso->build_id));
@@ -1153,6 +1167,27 @@ void __dsos__add(struct dsos *dsos, struct dso *dso)
{
{
	list_add_tail(&dso->node, &dsos->head);
	list_add_tail(&dso->node, &dsos->head);
	__dso__findlink_by_longname(&dsos->root, dso, NULL);
	__dso__findlink_by_longname(&dsos->root, dso, NULL);
	/*
	 * It is now in the linked list, grab a reference, then garbage collect
	 * this when needing memory, by looking at LRU dso instances in the
	 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
	 * anywhere besides the one for the list, do, under a lock for the
	 * list: remove it from the list, then a dso__put(), that probably will
	 * be the last and will then call dso__delete(), end of life.
	 *
	 * That, or at the end of the 'struct machine' lifetime, when all
	 * 'struct dso' instances will be removed from the list, in
	 * dsos__exit(), if they have no other reference from some other data
	 * structure.
	 *
	 * E.g.: after processing a 'perf.data' file and storing references
	 * to objects instantiated while processing events, we will have
	 * references to the 'thread', 'map', 'dso' structs all from 'struct
	 * hist_entry' instances, but we may not need anything not referenced,
	 * so we might as well call machines__exit()/machines__delete() and
	 * garbage collect it.
	 */
	dso__get(dso);
}
}


void dsos__add(struct dsos *dsos, struct dso *dso)
void dsos__add(struct dsos *dsos, struct dso *dso)
@@ -1206,7 +1241,7 @@ struct dso *dsos__findnew(struct dsos *dsos, const char *name)
{
{
	struct dso *dso;
	struct dso *dso;
	pthread_rwlock_wrlock(&dsos->lock);
	pthread_rwlock_wrlock(&dsos->lock);
	dso = __dsos__findnew(dsos, name);
	dso = dso__get(__dsos__findnew(dsos, name));
	pthread_rwlock_unlock(&dsos->lock);
	pthread_rwlock_unlock(&dsos->lock);
	return dso;
	return dso;
}
}
+13 −1
Original line number Original line Diff line number Diff line
#ifndef __PERF_DSO
#ifndef __PERF_DSO
#define __PERF_DSO
#define __PERF_DSO


#include <linux/atomic.h>
#include <linux/types.h>
#include <linux/types.h>
#include <linux/rbtree.h>
#include <linux/rbtree.h>
#include <stdbool.h>
#include <stdbool.h>
@@ -179,7 +180,7 @@ struct dso {
		void	 *priv;
		void	 *priv;
		u64	 db_id;
		u64	 db_id;
	};
	};

	atomic_t	 refcnt;
	char		 name[0];
	char		 name[0];
};
};


@@ -206,6 +207,17 @@ void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);


int dso__name_len(const struct dso *dso);
int dso__name_len(const struct dso *dso);


struct dso *dso__get(struct dso *dso);
void dso__put(struct dso *dso);

static inline void __dso__zput(struct dso **dso)
{
	dso__put(*dso);
	*dso = NULL;
}

#define dso__zput(dso) __dso__zput(&dso)

bool dso__loaded(const struct dso *dso, enum map_type type);
bool dso__loaded(const struct dso *dso, enum map_type type);


bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
+1 −0
Original line number Original line Diff line number Diff line
@@ -1277,6 +1277,7 @@ static int __event_process_build_id(struct build_id_event *bev,
				  sbuild_id);
				  sbuild_id);
		pr_debug("build id event received for %s: %s\n",
		pr_debug("build id event received for %s: %s\n",
			 dso->long_name, sbuild_id);
			 dso->long_name, sbuild_id);
		dso__put(dso);
	}
	}


	err = 0;
	err = 0;
Loading