2014-03-17 20:39:00 +07:00
|
|
|
#include "tests.h"
|
|
|
|
#include "machine.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "map.h"
|
2014-07-15 04:46:48 +07:00
|
|
|
#include "debug.h"
|
2014-03-17 20:39:00 +07:00
|
|
|
|
|
|
|
int test__thread_mg_share(void)
|
|
|
|
{
|
|
|
|
struct machines machines;
|
|
|
|
struct machine *machine;
|
|
|
|
|
|
|
|
/* thread group */
|
|
|
|
struct thread *leader;
|
|
|
|
struct thread *t1, *t2, *t3;
|
|
|
|
struct map_groups *mg;
|
|
|
|
|
|
|
|
/* other process */
|
|
|
|
struct thread *other, *other_leader;
|
|
|
|
struct map_groups *other_mg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test create 2 processes abstractions (struct thread)
|
|
|
|
* with several threads and checks they properly share and
|
|
|
|
* maintain map groups info (struct map_groups).
|
|
|
|
*
|
|
|
|
* thread group (pid: 0, tids: 0, 1, 2, 3)
|
|
|
|
* other group (pid: 4, tids: 4, 5)
|
|
|
|
*/
|
|
|
|
|
|
|
|
machines__init(&machines);
|
|
|
|
machine = &machines.host;
|
|
|
|
|
|
|
|
/* create process with 4 threads */
|
|
|
|
leader = machine__findnew_thread(machine, 0, 0);
|
|
|
|
t1 = machine__findnew_thread(machine, 0, 1);
|
|
|
|
t2 = machine__findnew_thread(machine, 0, 2);
|
|
|
|
t3 = machine__findnew_thread(machine, 0, 3);
|
|
|
|
|
|
|
|
/* and create 1 separated process, without thread leader */
|
|
|
|
other = machine__findnew_thread(machine, 4, 5);
|
|
|
|
|
|
|
|
TEST_ASSERT_VAL("failed to create threads",
|
|
|
|
leader && t1 && t2 && t3 && other);
|
|
|
|
|
|
|
|
mg = leader->mg;
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", mg->refcnt, 4);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
|
|
|
/* test the map groups pointer is shared */
|
|
|
|
TEST_ASSERT_VAL("map groups don't match", mg == t1->mg);
|
|
|
|
TEST_ASSERT_VAL("map groups don't match", mg == t2->mg);
|
|
|
|
TEST_ASSERT_VAL("map groups don't match", mg == t3->mg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the other leader was created by previous call.
|
|
|
|
* It should have shared map groups with no change in
|
|
|
|
* refcnt.
|
|
|
|
*/
|
|
|
|
other_leader = machine__find_thread(machine, 4, 4);
|
|
|
|
TEST_ASSERT_VAL("failed to find other leader", other_leader);
|
|
|
|
|
|
|
|
other_mg = other->mg;
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", other_mg->refcnt, 2);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
|
|
|
TEST_ASSERT_VAL("map groups don't match", other_mg == other_leader->mg);
|
|
|
|
|
|
|
|
/* release thread group */
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(leader);
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", mg->refcnt, 3);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(t1);
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", mg->refcnt, 2);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(t2);
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", mg->refcnt, 1);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(t3);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
|
|
|
/* release other group */
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(other_leader);
|
2015-05-12 02:30:20 +07:00
|
|
|
TEST_ASSERT_EQUAL("wrong refcnt", other_mg->refcnt, 1);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
thread__put(other);
|
2014-03-17 20:39:00 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cannot call machine__delete_threads(machine) now,
|
|
|
|
* because we've already released all the threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
machines__exit(&machines);
|
|
|
|
return 0;
|
|
|
|
}
|