xref: /aosp_15_r20/system/media/audio_utils/tests/audio_thread_tests.cpp (revision b9df5ad1c9ac98a7fefaac271a55f7ae3db05414)
1 /*
2  * Copyright (C) 2023 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 <audio_utils/threads.h>
18 #include <gtest/gtest.h>
19 #include <thread>
20 
21 using namespace android;
22 using namespace android::audio_utils;
23 
TEST(audio_thread_tests,conversion)24 TEST(audio_thread_tests, conversion) {
25     EXPECT_EQ(120, kDefaultPrio);
26 
27     EXPECT_EQ(kMaxRtPrio, nice_to_unified_priority(kMinNice));
28     EXPECT_EQ(kMaxPrio - 1, nice_to_unified_priority(kMaxNice));
29 
30     EXPECT_EQ(kMinNice, unified_priority_to_nice(kMaxRtPrio));
31     EXPECT_EQ(kMaxNice, unified_priority_to_nice(kMaxPrio - 1));
32 
33     EXPECT_EQ(kMaxRtPrio - 1, unified_priority_to_rtprio(0));
34     EXPECT_EQ(kMinRtPrio, unified_priority_to_rtprio(98));
35 
36     EXPECT_EQ(0, rtprio_to_unified_priority(kMaxRtPrio - 1));
37     EXPECT_EQ(98, rtprio_to_unified_priority(kMinRtPrio));
38 
39     EXPECT_FALSE(is_cfs_priority(kMaxRtPrio-1));
40     EXPECT_TRUE(is_cfs_priority(kMaxRtPrio));  // note the bound is exclusive
41 
42     EXPECT_TRUE(is_realtime_priority(kMaxRtPrio-1));
43     EXPECT_FALSE(is_realtime_priority(kMaxRtPrio));  // the bound is exclusive.
44 }
45 
TEST(audio_thread_tests,priority)46 TEST(audio_thread_tests, priority) {
47     const auto tid = gettid_wrapper();
48     const int priority = get_thread_priority(tid);
49     ASSERT_GE(priority, 0);
50 
51     constexpr int kPriority110 = 110;
52     EXPECT_EQ(NO_ERROR, set_thread_priority(tid, kPriority110));
53     EXPECT_EQ(kPriority110, get_thread_priority(tid));
54 
55     constexpr int kPriority130 = 130;
56     EXPECT_EQ(NO_ERROR, set_thread_priority(tid, kPriority130));
57     EXPECT_EQ(kPriority130, get_thread_priority(tid));
58 
59     // Requires privilege to go RT.
60     // constexpr int kPriority98 = 98;
61     // EXPECT_EQ(NO_ERROR, set_thread_priority(tid, kPriority98));
62     // EXPECT_EQ(kPriority98, get_thread_priority(tid));
63 
64     EXPECT_EQ(NO_ERROR, set_thread_priority(tid, priority));
65 }
66 
TEST(audio_thread_tests,cpu_count)67 TEST(audio_thread_tests, cpu_count) {
68     const unsigned cpu_count = std::thread::hardware_concurrency();
69     ASSERT_EQ(cpu_count, get_number_cpus());
70 }
71 
TEST(audio_thread_tests,affinity)72 TEST(audio_thread_tests, affinity) {
73     constexpr pid_t self = 0;
74     const int limit = std::min(get_number_cpus(), sizeof(uint64_t) * CHAR_BIT);
75     for (int i = 0; i < limit; ++i) {
76         uint64_t mask = 1ULL << i;
77         const status_t result = set_thread_affinity(self, mask);
78         ASSERT_EQ(NO_ERROR, result);
79         EXPECT_EQ(mask, get_thread_affinity(self).to_ullong());
80     }
81 }
82 
TEST(audio_thread_tests,invalid_affinity)83 TEST(audio_thread_tests, invalid_affinity) {
84     constexpr pid_t self = 0;
85     const int cpu_count = get_number_cpus();
86     ASSERT_NE(NO_ERROR, set_thread_affinity(self, std::bitset<kMaxCpus>{}.set(cpu_count)));
87 }
88