blob: b1d1bbafe7ae1ea51d9662514450b0968fb1afd0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsafabf012382014-03-17 14:39:00 +01002#include "tests.h"
3#include "machine.h"
4#include "thread.h"
5#include "map.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +02006#include "debug.h"
Jiri Olsafabf012382014-03-17 14:39:00 +01007
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -03008int test__thread_mg_share(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsafabf012382014-03-17 14:39:00 +01009{
10 struct machines machines;
11 struct machine *machine;
12
13 /* thread group */
14 struct thread *leader;
15 struct thread *t1, *t2, *t3;
16 struct map_groups *mg;
17
18 /* other process */
19 struct thread *other, *other_leader;
20 struct map_groups *other_mg;
21
22 /*
23 * This test create 2 processes abstractions (struct thread)
24 * with several threads and checks they properly share and
25 * maintain map groups info (struct map_groups).
26 *
27 * thread group (pid: 0, tids: 0, 1, 2, 3)
28 * other group (pid: 4, tids: 4, 5)
29 */
30
31 machines__init(&machines);
32 machine = &machines.host;
33
34 /* create process with 4 threads */
35 leader = machine__findnew_thread(machine, 0, 0);
36 t1 = machine__findnew_thread(machine, 0, 1);
37 t2 = machine__findnew_thread(machine, 0, 2);
38 t3 = machine__findnew_thread(machine, 0, 3);
39
40 /* and create 1 separated process, without thread leader */
41 other = machine__findnew_thread(machine, 4, 5);
42
43 TEST_ASSERT_VAL("failed to create threads",
44 leader && t1 && t2 && t3 && other);
45
46 mg = leader->mg;
Elena Reshetovaead05e82017-02-21 17:35:00 +020047 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&mg->refcnt), 4);
Jiri Olsafabf012382014-03-17 14:39:00 +010048
49 /* test the map groups pointer is shared */
50 TEST_ASSERT_VAL("map groups don't match", mg == t1->mg);
51 TEST_ASSERT_VAL("map groups don't match", mg == t2->mg);
52 TEST_ASSERT_VAL("map groups don't match", mg == t3->mg);
53
54 /*
55 * Verify the other leader was created by previous call.
56 * It should have shared map groups with no change in
57 * refcnt.
58 */
59 other_leader = machine__find_thread(machine, 4, 4);
60 TEST_ASSERT_VAL("failed to find other leader", other_leader);
61
Arnaldo Carvalho de Melo8b00f4692015-05-11 18:13:14 -030062 /*
63 * Ok, now that all the rbtree related operations were done,
64 * lets remove all of them from there so that we can do the
65 * refcounting tests.
66 */
67 machine__remove_thread(machine, leader);
68 machine__remove_thread(machine, t1);
69 machine__remove_thread(machine, t2);
70 machine__remove_thread(machine, t3);
71 machine__remove_thread(machine, other);
72 machine__remove_thread(machine, other_leader);
73
Jiri Olsafabf012382014-03-17 14:39:00 +010074 other_mg = other->mg;
Elena Reshetovaead05e82017-02-21 17:35:00 +020075 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_mg->refcnt), 2);
Jiri Olsafabf012382014-03-17 14:39:00 +010076
77 TEST_ASSERT_VAL("map groups don't match", other_mg == other_leader->mg);
78
79 /* release thread group */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030080 thread__put(leader);
Elena Reshetovaead05e82017-02-21 17:35:00 +020081 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&mg->refcnt), 3);
Jiri Olsafabf012382014-03-17 14:39:00 +010082
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030083 thread__put(t1);
Elena Reshetovaead05e82017-02-21 17:35:00 +020084 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&mg->refcnt), 2);
Jiri Olsafabf012382014-03-17 14:39:00 +010085
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030086 thread__put(t2);
Elena Reshetovaead05e82017-02-21 17:35:00 +020087 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&mg->refcnt), 1);
Jiri Olsafabf012382014-03-17 14:39:00 +010088
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030089 thread__put(t3);
Jiri Olsafabf012382014-03-17 14:39:00 +010090
91 /* release other group */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030092 thread__put(other_leader);
Elena Reshetovaead05e82017-02-21 17:35:00 +020093 TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(&other_mg->refcnt), 1);
Jiri Olsafabf012382014-03-17 14:39:00 +010094
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030095 thread__put(other);
Jiri Olsafabf012382014-03-17 14:39:00 +010096
Jiri Olsafabf012382014-03-17 14:39:00 +010097 machines__exit(&machines);
98 return 0;
99}