Last active
October 31, 2025 10:02
-
-
Save DhruvaG2000/fe2e5267130a5ce908648ba327ff5b7d to your computer and use it in GitHub Desktop.
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| #include <string.h> | |
| #include <errno.h> | |
| #define QOS_DEV "/dev/system_wakeup_latency" | |
| static int fd = -1; | |
| static volatile int keep_running = 1; | |
| void signal_handler(int sig) { | |
| printf("\nCaught signal %d, cleaning up and exiting...\n", sig); | |
| keep_running = 0; | |
| } | |
| void setup_signal_handlers(void) { | |
| struct sigaction sa; | |
| memset(&sa, 0, sizeof(sa)); | |
| sa.sa_handler = signal_handler; | |
| sigaction(SIGINT, &sa, NULL); | |
| sigaction(SIGTERM, &sa, NULL); | |
| } | |
| int main(int argc, char *argv[]) { | |
| int latency_us; | |
| char buffer[32]; | |
| if (argc != 2) { | |
| fprintf(stderr, "Usage: %s <latency_in_us>\n", argv[0]); | |
| return 1; | |
| } | |
| latency_us = atoi(argv[1]); | |
| if (latency_us < 0) { | |
| fprintf(stderr, "Error: Latency value must be non-negative\n"); | |
| return 1; | |
| } | |
| printf("Setting system wakeup latency QoS to %d us\n", latency_us); | |
| /* Open the QoS file */ | |
| fd = open(QOS_DEV, O_RDWR); | |
| if (fd < 0) { | |
| fprintf(stderr, "Error opening %s: %s\n", QOS_DEV, strerror(errno)); | |
| return 1; | |
| } | |
| /* Write the latency value */ | |
| snprintf(buffer, sizeof(buffer), "%d", latency_us); | |
| if (write(fd, buffer, strlen(buffer)) < 0) { | |
| fprintf(stderr, "Error writing to %s: %s\n", QOS_DEV, strerror(errno)); | |
| close(fd); | |
| return 1; | |
| } | |
| setup_signal_handlers(); | |
| printf("QoS constraint set successfully. Process will keep running to maintain the constraint.\n"); | |
| printf("Press Ctrl+C to exit and release the constraint.\n"); | |
| /* Keep the program running to maintain the file descriptor open */ | |
| while (keep_running) { | |
| sleep(1); | |
| } | |
| /* Close the file descriptor before exiting */ | |
| if (fd >= 0) { | |
| close(fd); | |
| } | |
| printf("QoS constraint released.\n"); | |
| return 0; | |
| } |
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
| #include <stdio.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| #define QOS_DEV "/dev/system_wakeup_latency" | |
| #define LATENCY_VAL "0x64" /* 100 us in hex */ | |
| static volatile int keep_running = 1; | |
| void sig_handler(int sig) { | |
| keep_running = 0; | |
| } | |
| int main(void) { | |
| int fd; | |
| signal(SIGINT, sig_handler); | |
| signal(SIGTERM, sig_handler); | |
| fd = open(QOS_DEV, O_RDWR); | |
| if (fd < 0) { | |
| perror("open"); | |
| return 1; | |
| } | |
| if (write(fd, LATENCY_VAL, sizeof(LATENCY_VAL) - 1) < 0) { | |
| perror("write"); | |
| close(fd); | |
| return 1; | |
| } | |
| printf("QoS set to %s. Press Ctrl+C to exit.\n", LATENCY_VAL); | |
| while (keep_running) | |
| sleep(1); | |
| close(fd); | |
| printf("Released.\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment