| Directory: | ./ |
|---|---|
| File: | src/process_manager.cpp |
| Date: | 2022-06-29 13:58:11 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 1 | 24 | 4.2% |
| Branches: | 2 | 20 | 10.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file process_manager.cpp | ||
| 3 | * @author Maximilien Naveau (maximilien.naveau@gmail.com) | ||
| 4 | * license License BSD-3-Clause | ||
| 5 | * @copyright Copyright (c) 2019, New York University and Max Planck | ||
| 6 | * Gesellschaft. | ||
| 7 | * @date 2019-05-22 | ||
| 8 | * | ||
| 9 | * @brief Allow us to fix the current process to a specific set of cpus. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "real_time_tools/process_manager.hpp" | ||
| 13 | #include <fcntl.h> | ||
| 14 | #include <stdint.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <sys/stat.h> | ||
| 18 | #include <sys/types.h> | ||
| 19 | #include <unistd.h> | ||
| 20 | #include <vector> | ||
| 21 | #include "real_time_tools/iostream.hpp" | ||
| 22 | |||
| 23 | namespace real_time_tools | ||
| 24 | { | ||
| 25 | ✗ | bool fix_current_process_to_cpu(std::vector<int>& cpu_affinities, int pid) | |
| 26 | { | ||
| 27 | #ifdef XENOMAI | ||
| 28 | return false; | ||
| 29 | #elif __APPLE__ | ||
| 30 | // Apple does not support pinning a process. | ||
| 31 | return false; | ||
| 32 | #elif defined(NON_REAL_TIME) || defined(RT_PREEMPT) | ||
| 33 | ✗ | if (cpu_affinities.size() > 0) | |
| 34 | { | ||
| 35 | cpu_set_t mask; | ||
| 36 | ✗ | CPU_ZERO(&mask); | |
| 37 | ✗ | for (unsigned i = 0; i < cpu_affinities.size(); ++i) | |
| 38 | { | ||
| 39 | ✗ | if (cpu_affinities[i] > -1) | |
| 40 | { | ||
| 41 | ✗ | CPU_SET(cpu_affinities[i], &mask); | |
| 42 | } | ||
| 43 | } | ||
| 44 | ✗ | pid_t process_pid = static_cast<pid_t>(pid); | |
| 45 | ✗ | int ret = sched_setaffinity(process_pid, sizeof(mask), &mask); | |
| 46 | ✗ | if (ret) | |
| 47 | { | ||
| 48 | ✗ | rt_printf("sched_setaffinity failed. Ret= %d\n", ret); | |
| 49 | ✗ | return false; | |
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | ✗ | return true; | |
| 54 | } | ||
| 55 | } | ||
| 56 | ✗ | rt_printf("fix_current_process_to_cpu: Nothing to be done.\n"); | |
| 57 | ✗ | return true; | |
| 58 | #endif | ||
| 59 | } | ||
| 60 | |||
| 61 | ✗ | bool set_cpu_dma_latency(int max_latency_us) | |
| 62 | { | ||
| 63 | #ifdef XENOMAI | ||
| 64 | return false; | ||
| 65 | #elif defined(NON_REAL_TIME) || defined(RT_PREEMPT) | ||
| 66 | int fd; | ||
| 67 | |||
| 68 | ✗ | fd = open("/dev/cpu_dma_latency", O_WRONLY); | |
| 69 | ✗ | if (fd < 0) | |
| 70 | { | ||
| 71 | ✗ | perror("open /dev/cpu_dma_latency"); | |
| 72 | ✗ | return false; | |
| 73 | } | ||
| 74 | ✗ | if (write(fd, &max_latency_us, sizeof(max_latency_us)) != | |
| 75 | sizeof(max_latency_us)) | ||
| 76 | { | ||
| 77 | ✗ | perror("write to /dev/cpu_dma_latency"); | |
| 78 | ✗ | return false; | |
| 79 | } | ||
| 80 | |||
| 81 | ✗ | return true; | |
| 82 | #endif | ||
| 83 | } | ||
| 84 | |||
| 85 |
2/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
|
42 | } // namespace real_time_tools |
| 86 |