-
Notifications
You must be signed in to change notification settings - Fork 620
apps/examples/power: Add cpu usage 100 test #7037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ewoodev
wants to merge
1
commit into
Samsung:master
Choose a base branch
from
ewoodev:WORK-251113_Add_cpu_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−3
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,8 @@ | |
| #include <unistd.h> | ||
| #include <pthread.h> | ||
| #include <sys/stat.h> | ||
| #include <sched.h> | ||
| #include <sys/prctl.h> | ||
|
|
||
| #include <tinyara/fs/ioctl.h> | ||
|
|
||
|
|
@@ -46,7 +48,41 @@ | |
| ****************************************************************************/ | ||
|
|
||
| static int is_running; | ||
| static volatile int cpu_stress_running; | ||
|
|
||
| static int cpu_stress_thread(void *arg) | ||
| { | ||
| int cpu_id = (int)arg; | ||
| cpu_set_t cpuset; | ||
| pthread_t this_thread = pthread_self(); | ||
| char thread_name[32]; | ||
| clock_t prev = clock(); | ||
| clock_t now; | ||
|
|
||
| #ifdef CONFIG_SMP | ||
| CPU_ZERO(&cpuset); | ||
| CPU_SET(cpu_id, &cpuset); | ||
|
|
||
| if (pthread_setaffinity_np(this_thread, sizeof(cpu_set_t), &cpuset) != 0) { | ||
|
Contributor
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. Is it possible to change assigned cpu at running by itself?? |
||
| printf("Failed to set CPU%d affinity for thread\n", cpu_id); | ||
| return -1; | ||
| } | ||
| #endif | ||
| snprintf(thread_name, sizeof(thread_name), "cpu_stress%d", cpu_id); | ||
| pthread_setname_np(this_thread, thread_name); | ||
| printf("CPU stress thread started on CPU%d\n", cpu_id); | ||
|
|
||
| while (cpu_stress_running) { | ||
| now = clock(); | ||
| if (TICK2SEC(now - prev) > 10) { | ||
| printf("CPU%d stress running... %ld\n", cpu_id, now); | ||
| prev = now; | ||
| } | ||
| } | ||
|
|
||
| printf("CPU%d stress thread stopped\n", cpu_id); | ||
| return 0; | ||
| } | ||
|
|
||
| static int pm_sleep_test(void *args) | ||
| { | ||
|
|
@@ -229,6 +265,59 @@ static int start_pm_test(int argc, char *argv[]) | |
| return 0; | ||
| } | ||
|
|
||
| static int start_cpu_stress_test(int argc, char *argv[]) | ||
| { | ||
| pthread_t cpu_threads[CONFIG_SMP_NCPUS]; | ||
| int cpu_num; | ||
|
|
||
| printf("argc %d\n", argc); | ||
| for (int i = 0; i < argc; i++) { | ||
| printf("argv %s\n", argv[i]); | ||
| } | ||
| printf("######################### CPU STRESS TEST START #########################\n"); | ||
| printf("Starting CPU stress test to achieve 100%% CPU usage\n"); | ||
|
|
||
| printf("System has %d CPUs available\n", CONFIG_SMP_NCPUS); | ||
|
|
||
| if (argc < 2) { | ||
| printf("Creating threads on all %d CPUs\n", CONFIG_SMP_NCPUS); | ||
|
|
||
| for (int i = 0; i < CONFIG_SMP_NCPUS; i++) { | ||
| if (pthread_create(&cpu_threads[i], NULL, (pthread_startroutine_t)cpu_stress_thread, (void*)i) < 0) { | ||
| printf("Failed to create CPU%d stress thread(errno %d)\n", i, get_errno()); | ||
|
|
||
| for (int j = 0; j < i; j++) { | ||
| pthread_cancel(cpu_threads[j]); | ||
| } | ||
| cpu_stress_running = false; | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < CONFIG_SMP_NCPUS; i++) { | ||
| pthread_join(cpu_threads[i], NULL); | ||
| } | ||
| } else { | ||
| cpu_num = atoi(argv[1]); | ||
| if (cpu_num < 0 || cpu_num >= CONFIG_SMP_NCPUS) { | ||
| printf("Invalid CPU number: %d. Use 0 to %d, or no argument for all CPUs\n", cpu_num, CONFIG_SMP_NCPUS - 1); | ||
| cpu_stress_running = false; | ||
| return -1; | ||
| } | ||
| printf("Creating thread on CPU%d only\n", cpu_num); | ||
|
|
||
| if (pthread_create(&cpu_threads[cpu_num], NULL, (pthread_startroutine_t)cpu_stress_thread, (void*)cpu_num) < 0) { | ||
| printf("Failed to create CPU%d stress thread(errno %d)\n", cpu_num, get_errno()); | ||
| cpu_stress_running = false; | ||
| return -1; | ||
| } | ||
| pthread_join(cpu_threads[cpu_num], NULL); | ||
| } | ||
|
|
||
| printf("######################### CPU STRESS TEST END #########################\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| static void help_func(void) | ||
| { | ||
| printf("usage: power <command> \n\n"); | ||
|
|
@@ -240,12 +329,14 @@ static void help_func(void) | |
| printf("and it is testing suspending of power managemenet operation and resuming for block ender chipset sleep mode.\n"); | ||
| printf(" start [options]\t\t Start power management test\n"); | ||
| printf(" stop \t\t stop power management test\n"); | ||
| printf(" cpu start [cpu_num]\t Start CPU stress test (100%% CPU usage)\n"); | ||
| printf(" cpu stop \t Stop CPU stress test\n"); | ||
| printf(" options: -l, --lock-test \t\t Test pm suspend/resume API\n"); | ||
| printf(" -t, --timed-wakeup [time(ms)]\t\t Test timed wakeup (default 100ms)\n"); | ||
| printf("\n"); | ||
| printf("start and stop are used to control the power management test.\n"); | ||
| printf(" suspend <name>\t\t Suspend power management test\n"); | ||
| printf(" resume <name>\t\t Start power management test\n"); | ||
| printf("suspend and resume are used to control the power management test.\n"); | ||
| printf(" suspend <name>\t\t suspend power management test\n"); | ||
| printf(" resume <name>\t\t resume power management test\n"); | ||
| printf("\n"); | ||
| } | ||
|
|
||
|
|
@@ -289,6 +380,37 @@ int power_main(int argc, char *argv[]) | |
|
|
||
| is_running = false; | ||
|
|
||
| } else if (strncmp(argv[1], "cpu", 8) == 0) { | ||
|
Contributor
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. It should be 4, not 8 |
||
| if (argc < 3) { | ||
| printf("Usage: power cpu {start|stop} [cpu_num]\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| if (strncmp(argv[2], "start", 6) == 0) { | ||
| if (cpu_stress_running) { | ||
| printf("CPU stress test is already running\n"); | ||
| return 0; | ||
| } | ||
| cpu_stress_running = true; | ||
|
|
||
| pid = task_create("cpu_stress_test", 100, 1024, start_cpu_stress_test, argv + 3); | ||
| if (pid < 0) { | ||
| printf("Fail to create cpu_stress_test task(errno %d)\n", get_errno()); | ||
| return -1; | ||
| } | ||
|
|
||
| } else if (strncmp(argv[2], "stop", 5) == 0) { | ||
| if (!cpu_stress_running) { | ||
| printf("CPU stress test is not running\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| cpu_stress_running = false; | ||
| } else { | ||
| printf("Invalid cpu subcommand. Use 'start' or 'stop'\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| } else if (strncmp(argv[1], "suspend", 8) == 0 && argc == 3) { | ||
| _pm_suspend(argv[2]); | ||
| printf("Done pm suspend domain: %s\n", argv[2]); | ||
|
|
||
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.
Is this config checking necessary?
If we execute the codes below with the config disabled, what happen?