1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include "membarrier.h"
20 
21 class ScopedErrnoCleaner {
22  public:
ScopedErrnoCleaner()23   ScopedErrnoCleaner() { errno = 0; }
~ScopedErrnoCleaner()24   ~ScopedErrnoCleaner() { errno = 0; }
25 };
26 
HasMembarrier(art::MembarrierCommand cmd)27 bool HasMembarrier(art::MembarrierCommand cmd) {
28   ScopedErrnoCleaner errno_cleaner;
29   int supported_cmds = art::membarrier(art::MembarrierCommand::kQuery);
30   return (supported_cmds > 0) && ((supported_cmds & static_cast<int>(cmd)) != 0);
31 }
32 
TEST(membarrier,query)33 TEST(membarrier, query) {
34   ScopedErrnoCleaner errno_cleaner;
35   int supported = art::membarrier(art::MembarrierCommand::kQuery);
36   if (errno == 0) {
37     ASSERT_LE(0, supported);
38   } else {
39     ASSERT_TRUE(errno == ENOSYS && supported == -1);
40   }
41 }
42 
TEST(membarrier,global_barrier)43 TEST(membarrier, global_barrier) {
44   if (!HasMembarrier(art::MembarrierCommand::kGlobal)) {
45     GTEST_LOG_(INFO) << "MembarrierCommand::kGlobal not supported, skipping test.";
46     return;
47   }
48   ASSERT_EQ(0, art::membarrier(art::MembarrierCommand::kGlobal));
49 }
50 
MembarrierCommandToName(art::MembarrierCommand cmd)51 static const char* MembarrierCommandToName(art::MembarrierCommand cmd) {
52 #define CASE_VALUE(x) case (x): return #x;
53   switch (cmd) {
54     CASE_VALUE(art::MembarrierCommand::kQuery);
55     CASE_VALUE(art::MembarrierCommand::kGlobal);
56     CASE_VALUE(art::MembarrierCommand::kGlobalExpedited);
57     CASE_VALUE(art::MembarrierCommand::kRegisterGlobalExpedited);
58     CASE_VALUE(art::MembarrierCommand::kPrivateExpedited);
59     CASE_VALUE(art::MembarrierCommand::kRegisterPrivateExpedited);
60     CASE_VALUE(art::MembarrierCommand::kPrivateExpeditedSyncCore);
61     CASE_VALUE(art::MembarrierCommand::kRegisterPrivateExpeditedSyncCore);
62   }
63 }
64 
TestRegisterAndBarrierCommands(art::MembarrierCommand membarrier_cmd_register,art::MembarrierCommand membarrier_cmd_barrier)65 static void TestRegisterAndBarrierCommands(art::MembarrierCommand membarrier_cmd_register,
66                                            art::MembarrierCommand membarrier_cmd_barrier) {
67   if (!HasMembarrier(membarrier_cmd_register)) {
68     GTEST_LOG_(INFO) << MembarrierCommandToName(membarrier_cmd_register)
69         << " not supported, skipping test.";
70     return;
71   }
72   if (!HasMembarrier(membarrier_cmd_barrier)) {
73     GTEST_LOG_(INFO) << MembarrierCommandToName(membarrier_cmd_barrier)
74         << " not supported, skipping test.";
75     return;
76   }
77 
78   ScopedErrnoCleaner errno_cleaner;
79 
80   // Check barrier use without prior registration.
81   if (membarrier_cmd_register == art::MembarrierCommand::kRegisterGlobalExpedited) {
82     // Global barrier use is always okay.
83     ASSERT_EQ(0, art::membarrier(membarrier_cmd_barrier));
84   } else {
85     // Private barrier should fail.
86     ASSERT_EQ(-1, art::membarrier(membarrier_cmd_barrier));
87     ASSERT_EQ(EPERM, errno);
88     errno = 0;
89   }
90 
91   // Check registration for barrier succeeds.
92   ASSERT_EQ(0, art::membarrier(membarrier_cmd_register));
93 
94   // Check barrier use after registration succeeds.
95   ASSERT_EQ(0, art::membarrier(membarrier_cmd_barrier));
96 }
97 
TEST(membarrier,global_expedited)98 TEST(membarrier, global_expedited) {
99   TestRegisterAndBarrierCommands(art::MembarrierCommand::kRegisterGlobalExpedited,
100                                  art::MembarrierCommand::kGlobalExpedited);
101 }
102 
TEST(membarrier,private_expedited)103 TEST(membarrier, private_expedited) {
104   TestRegisterAndBarrierCommands(art::MembarrierCommand::kRegisterPrivateExpedited,
105                                  art::MembarrierCommand::kPrivateExpedited);
106 }
107 
TEST(membarrier,private_expedited_sync_core)108 TEST(membarrier, private_expedited_sync_core) {
109   TestRegisterAndBarrierCommands(art::MembarrierCommand::kRegisterPrivateExpeditedSyncCore,
110                                  art::MembarrierCommand::kPrivateExpeditedSyncCore);
111 }
112