xref: /OK3568_Linux_fs/kernel/tools/perf/tests/thread-maps-share.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include "tests.h"
3*4882a593Smuzhiyun #include "machine.h"
4*4882a593Smuzhiyun #include "thread.h"
5*4882a593Smuzhiyun #include "debug.h"
6*4882a593Smuzhiyun 
test__thread_maps_share(struct test * test __maybe_unused,int subtest __maybe_unused)7*4882a593Smuzhiyun int test__thread_maps_share(struct test *test __maybe_unused, int subtest __maybe_unused)
8*4882a593Smuzhiyun {
9*4882a593Smuzhiyun 	struct machines machines;
10*4882a593Smuzhiyun 	struct machine *machine;
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun 	/* thread group */
13*4882a593Smuzhiyun 	struct thread *leader;
14*4882a593Smuzhiyun 	struct thread *t1, *t2, *t3;
15*4882a593Smuzhiyun 	struct maps *maps;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 	/* other process */
18*4882a593Smuzhiyun 	struct thread *other, *other_leader;
19*4882a593Smuzhiyun 	struct maps *other_maps;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 	/*
22*4882a593Smuzhiyun 	 * This test create 2 processes abstractions (struct thread)
23*4882a593Smuzhiyun 	 * with several threads and checks they properly share and
24*4882a593Smuzhiyun 	 * maintain maps info (struct maps).
25*4882a593Smuzhiyun 	 *
26*4882a593Smuzhiyun 	 * thread group (pid: 0, tids: 0, 1, 2, 3)
27*4882a593Smuzhiyun 	 * other  group (pid: 4, tids: 4, 5)
28*4882a593Smuzhiyun 	*/
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	machines__init(&machines);
31*4882a593Smuzhiyun 	machine = &machines.host;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	/* create process with 4 threads */
34*4882a593Smuzhiyun 	leader = machine__findnew_thread(machine, 0, 0);
35*4882a593Smuzhiyun 	t1     = machine__findnew_thread(machine, 0, 1);
36*4882a593Smuzhiyun 	t2     = machine__findnew_thread(machine, 0, 2);
37*4882a593Smuzhiyun 	t3     = machine__findnew_thread(machine, 0, 3);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/* and create 1 separated process, without thread leader */
40*4882a593Smuzhiyun 	other  = machine__findnew_thread(machine, 4, 5);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	TEST_ASSERT_VAL("failed to create threads",
43*4882a593Smuzhiyun 			leader && t1 && t2 && t3 && other);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	maps = leader->maps;
46*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 4);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	/* test the maps pointer is shared */
49*4882a593Smuzhiyun 	TEST_ASSERT_VAL("maps don't match", maps == t1->maps);
50*4882a593Smuzhiyun 	TEST_ASSERT_VAL("maps don't match", maps == t2->maps);
51*4882a593Smuzhiyun 	TEST_ASSERT_VAL("maps don't match", maps == t3->maps);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/*
54*4882a593Smuzhiyun 	 * Verify the other leader was created by previous call.
55*4882a593Smuzhiyun 	 * It should have shared maps with no change in
56*4882a593Smuzhiyun 	 * refcnt.
57*4882a593Smuzhiyun 	 */
58*4882a593Smuzhiyun 	other_leader = machine__find_thread(machine, 4, 4);
59*4882a593Smuzhiyun 	TEST_ASSERT_VAL("failed to find other leader", other_leader);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	/*
62*4882a593Smuzhiyun 	 * Ok, now that all the rbtree related operations were done,
63*4882a593Smuzhiyun 	 * lets remove all of them from there so that we can do the
64*4882a593Smuzhiyun 	 * refcounting tests.
65*4882a593Smuzhiyun 	 */
66*4882a593Smuzhiyun 	machine__remove_thread(machine, leader);
67*4882a593Smuzhiyun 	machine__remove_thread(machine, t1);
68*4882a593Smuzhiyun 	machine__remove_thread(machine, t2);
69*4882a593Smuzhiyun 	machine__remove_thread(machine, t3);
70*4882a593Smuzhiyun 	machine__remove_thread(machine, other);
71*4882a593Smuzhiyun 	machine__remove_thread(machine, other_leader);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	other_maps = other->maps;
74*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 2);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	TEST_ASSERT_VAL("maps don't match", other_maps == other_leader->maps);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/* release thread group */
79*4882a593Smuzhiyun 	thread__put(leader);
80*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 3);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	thread__put(t1);
83*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 2);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	thread__put(t2);
86*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&maps->refcnt), 1);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	thread__put(t3);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* release other group  */
91*4882a593Smuzhiyun 	thread__put(other_leader);
92*4882a593Smuzhiyun 	TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_maps->refcnt), 1);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	thread__put(other);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	machines__exit(&machines);
97*4882a593Smuzhiyun 	return 0;
98*4882a593Smuzhiyun }
99