Debian Codegraph @

src/utils/misc.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <fcntl.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/uio.h>
9 #include <xcb/randr.h>
10 #include <xcb/render.h>
11
12 #include "compiler.h"
13 #include "misc.h"
14 #include "rtkit.h"
15 #include "str.h"
16
17 /// Report allocation failure without allocating memory
18 void report_allocation_failure(const char *func, const char *file, unsigned int line) {
19 // Since memory allocation failed, we try to print this error message without any
20 // memory allocation. Since logging framework allocates memory (and might even
21 // have not been initialized yet), so we can't use it.
22 char buf[11];
23 int llen = uitostr(line, buf);
24 const char msg1[] = " has failed to allocate memory, ";
25 const char msg2[] = ". Aborting...\n";
26 const struct iovec v[] = {
27 {.iov_base = (void *)func, .iov_len = strlen(func)},
28 {.iov_base = "()", .iov_len = 2},
29 {.iov_base = (void *)msg1, .iov_len = sizeof(msg1) - 1},
30 {.iov_base = "at ", .iov_len = 3},
31 {.iov_base = (void *)file, .iov_len = strlen(file)},
32 {.iov_base = ":", .iov_len = 1},
33 {.iov_base = buf, .iov_len = (size_t)llen},
34 {.iov_base = (void *)msg2, .iov_len = sizeof(msg2) - 1},
35 };
36
37 ssize_t _ attr_unused = writev(STDERR_FILENO, v, ARR_SIZE(v));
38 abort();
39
40 unreachable();
41 }
42
43 ///
44 /// Calculates next closest power of two of 32bit integer n
45 /// ref: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
46 ///
47 int next_power_of_two(int n) {
48 n--;
49 n |= n >> 1;
50 n |= n >> 2;
51 n |= n >> 4;
52 n |= n >> 8;
53 n |= n >> 16;
54 n++;
55 return n;
56 }
57 // Find the k-th smallest element in an array.
58 int quickselect(int *elems, int nelem, int k) {
59 int l = 0, r = nelem; // [l, r) is the range of candidates
60 while (l != r) {
61 int pivot = elems[l];
62 int i = l, j = r;
63 while (i < j) {
64 while (i < j && elems[--j] >= pivot) {
65 }
66 elems[i] = elems[j];
67 while (i < j && elems[++i] <= pivot) {
68 }
69 elems[j] = elems[i];
70 }
71 elems[i] = pivot;
72
73 if (i == k) {
74 break;
75 }
76
77 if (i < k) {
78 l = i + 1;
79 } else {
80 r = i;
81 }
82 }
83 return elems[k];
84 }
85
86 /// Switch to real-time scheduling policy (SCHED_RR) if possible
87 ///
88 /// Make picom realtime to reduce latency, and make rendering times more predictable to
89 /// help pacing.
90 ///
91 /// This requires the user to set up permissions for the real-time scheduling. e.g. by
92 /// setting `ulimit -r`, or giving us the CAP_SYS_NICE capability.
93 void set_rr_scheduling(void) {
94 static thread_local bool already_tried = false;
95 if (already_tried) {
96 return;
97 }
98 already_tried = true;
99
100 int priority = sched_get_priority_min(SCHED_RR);
101
102 if (rtkit_make_realtime(0, priority)) {
103 log_info("Set realtime priority to %d with rtkit.", priority);
104 return;
105 }
106
107 // Fallback to use pthread_setschedparam
108 struct sched_param param;
109 int old_policy;
110 int ret = pthread_getschedparam(pthread_self(), &old_policy, &param);
111 if (ret != 0) {
112 log_info("Couldn't get old scheduling priority.");
113 return;
114 }
115
116 param.sched_priority = priority;
117
118 ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
119 if (ret != 0) {
120 log_info("Couldn't set real-time scheduling priority to %d.", priority);
121 return;
122 }
123
124 log_info("Set real-time scheduling priority to %d.", priority);
125 }
126
127 // vim: set noet sw=8 ts=8 :