This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Enforce consistent stack size for Flutter threads #49111
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/build_config.h" | ||
#include "flutter/fml/thread.h" | ||
|
||
#if defined(FML_OS_MACOSX) || defined(FML_OS_LINUX) || defined(FML_OS_ANDROID) | ||
|
@@ -15,6 +16,11 @@ | |
#else | ||
#endif | ||
|
||
#if defined(FML_OS_WIN) | ||
#include <windows.h> | ||
#endif | ||
|
||
#include <algorithm> | ||
#include <memory> | ||
#include "gtest/gtest.h" | ||
|
||
|
@@ -37,6 +43,35 @@ TEST(Thread, HasARunningMessageLoop) { | |
ASSERT_TRUE(done); | ||
} | ||
|
||
TEST(Thread, HasExpectedStackSize) { | ||
size_t stack_size = 0; | ||
fml::Thread thread; | ||
|
||
thread.GetTaskRunner()->PostTask([&stack_size]() { | ||
#if defined(FML_OS_WIN) | ||
ULONG_PTR low_limit; | ||
ULONG_PTR high_limit; | ||
GetCurrentThreadStackLimits(&low_limit, &high_limit); | ||
stack_size = high_limit - low_limit; | ||
#elif defined(FML_OS_MACOSX) | ||
stack_size = pthread_get_stacksize_np(pthread_self()); | ||
#else | ||
pthread_attr_t attr; | ||
pthread_getattr_np(pthread_self(), &attr); | ||
pthread_attr_getstacksize(&attr, &stack_size); | ||
pthread_attr_destroy(&attr); | ||
#endif | ||
}); | ||
thread.Join(); | ||
|
||
// Actual stack size will be aligned to page size, this assumes no supported | ||
// platform has a page size larger than 16k. On Linux reducing the default | ||
// stack size (8MB) does not seem to have any effect. | ||
const size_t kPageSize = 16384; | ||
ASSERT_TRUE(stack_size / kPageSize >= | ||
fml::Thread::GetDefaultStackSize() / kPageSize); | ||
} | ||
|
||
#if FLUTTER_PTHREAD_SUPPORTED | ||
TEST(Thread, ThreadNameCreatedWithConfig) { | ||
const std::string name = "Thread1"; | ||
|
@@ -45,15 +80,20 @@ TEST(Thread, ThreadNameCreatedWithConfig) { | |
bool done = false; | ||
thread.GetTaskRunner()->PostTask([&done, &name]() { | ||
done = true; | ||
char thread_name[8]; | ||
char thread_name[16]; | ||
pthread_t current_thread = pthread_self(); | ||
pthread_getname_np(current_thread, thread_name, 8); | ||
pthread_getname_np(current_thread, thread_name, 16); | ||
ASSERT_EQ(thread_name, name); | ||
}); | ||
thread.Join(); | ||
ASSERT_TRUE(done); | ||
} | ||
|
||
static int clamp_priority(int priority, int policy) { | ||
return std::clamp(priority, sched_get_priority_min(policy), | ||
sched_get_priority_max(policy)); | ||
} | ||
|
||
static void MockThreadConfigSetter(const fml::Thread::ThreadConfig& config) { | ||
// set thread name | ||
fml::Thread::SetCurrentThreadName(config); | ||
|
@@ -63,10 +103,10 @@ static void MockThreadConfigSetter(const fml::Thread::ThreadConfig& config) { | |
int policy = SCHED_OTHER; | ||
switch (config.priority) { | ||
case fml::Thread::ThreadPriority::kDisplay: | ||
param.sched_priority = 10; | ||
param.sched_priority = clamp_priority(10, policy); | ||
break; | ||
default: | ||
param.sched_priority = 1; | ||
param.sched_priority = clamp_priority(1, policy); | ||
} | ||
pthread_setschedparam(tid, policy, ¶m); | ||
} | ||
|
@@ -84,27 +124,27 @@ TEST(Thread, ThreadPriorityCreatedWithConfig) { | |
int policy; | ||
thread.GetTaskRunner()->PostTask([&]() { | ||
done = true; | ||
char thread_name[8]; | ||
char thread_name[16]; | ||
pthread_t current_thread = pthread_self(); | ||
pthread_getname_np(current_thread, thread_name, 8); | ||
pthread_getname_np(current_thread, thread_name, 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fails with error 34 when buffer length is less than 16 bytes. |
||
pthread_getschedparam(current_thread, &policy, ¶m); | ||
ASSERT_EQ(thread_name, thread1_name); | ||
ASSERT_EQ(policy, SCHED_OTHER); | ||
ASSERT_EQ(param.sched_priority, 1); | ||
ASSERT_EQ(param.sched_priority, clamp_priority(1, policy)); | ||
}); | ||
|
||
fml::Thread thread2(MockThreadConfigSetter, | ||
fml::Thread::ThreadConfig( | ||
thread2_name, fml::Thread::ThreadPriority::kDisplay)); | ||
thread2.GetTaskRunner()->PostTask([&]() { | ||
done = true; | ||
char thread_name[8]; | ||
char thread_name[16]; | ||
pthread_t current_thread = pthread_self(); | ||
pthread_getname_np(current_thread, thread_name, 8); | ||
pthread_getname_np(current_thread, thread_name, 16); | ||
pthread_getschedparam(current_thread, &policy, ¶m); | ||
ASSERT_EQ(thread_name, thread2_name); | ||
ASSERT_EQ(policy, SCHED_OTHER); | ||
ASSERT_EQ(param.sched_priority, 10); | ||
ASSERT_EQ(param.sched_priority, clamp_priority(10, policy)); | ||
}); | ||
thread.Join(); | ||
ASSERT_TRUE(done); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the value perhaps be
sched_get_priority_min
andsched_get_priority_max
? The values are platform specific (i.e. on macOS the allowed range is 15-47 for SCHED_OTHER, on Linux it is 0-0 for SCHED_OTHER and for other policies it requires root).I don't quite understand how these tests were passing at all on linux.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch, actually, I know how. pthread tests never actually ran at all- because of missing
build_config.h
includeFLUTTER_PTHREAD_SUPPORTED
was 0 all the time.