Skip to content

Commit cab2539

Browse files
committed
init
0 parents  commit cab2539

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/*

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# dmald
2+
3+
Daemon to control CPU DMA Latency
4+

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mkdir -p build/
2+
gcc main.c -o build/dmald

main.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include <signal.h>
7+
#include <sys/file.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
11+
#define LOCK_FILE "/tmp/dmald.lock"
12+
#define DATA_FILE "/tmp/dmald.data"
13+
14+
static int fd_lock = -1;
15+
static int fd_latency = -1;
16+
static volatile sig_atomic_t running = 1;
17+
static volatile sig_atomic_t need_update = 0;
18+
19+
static pid_t read_daemon_pid() {
20+
char pid_str[32] = {0};
21+
int fd = open(LOCK_FILE, O_RDONLY);
22+
if (fd == -1) return -1;
23+
24+
ssize_t len = read(fd, pid_str, sizeof(pid_str)-1);
25+
close(fd);
26+
return (len > 0) ? atoi(pid_str) : -1;
27+
}
28+
29+
static int set_latency(int32_t target) {
30+
if (fd_latency == -1) {
31+
fd_latency = open("/dev/cpu_dma_latency", O_WRONLY);
32+
if (fd_latency == -1) {
33+
perror("open /dev/cpu_dma_latency");
34+
return -1;
35+
}
36+
}
37+
return write(fd_latency, &target, sizeof(target));
38+
}
39+
40+
static void handle_sigterm(int sig) { running = 0; }
41+
static void handle_sigusr1(int sig) { need_update = 1; }
42+
43+
int main(int argc, char *argv[]) {
44+
if (argc != 2) {
45+
fprintf(stderr, "Usage: %s <latency_value> (or -1 to terminate daemon)\n", argv[0]);
46+
return 1;
47+
}
48+
49+
int32_t target = atoi(argv[1]);
50+
51+
fd_lock = open(LOCK_FILE, O_CREAT | O_RDWR, 0644);
52+
if (fd_lock == -1) {
53+
perror("open lock file");
54+
return 1;
55+
}
56+
57+
if (flock(fd_lock, LOCK_EX | LOCK_NB) == -1) {
58+
if (errno == EWOULDBLOCK) {
59+
pid_t daemon_pid = read_daemon_pid();
60+
if (daemon_pid <= 0) {
61+
fprintf(stderr, "Error: Can't read daemon PID\n");
62+
return 1;
63+
}
64+
65+
if (target == -1) {
66+
kill(daemon_pid, SIGTERM);
67+
printf("Terminated daemon (PID %d)\n", daemon_pid);
68+
} else {
69+
// Set target and signal to daemon
70+
int fd = open(DATA_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
71+
if (fd != -1) {
72+
write(fd, &target, sizeof(target));
73+
close(fd);
74+
}
75+
kill(daemon_pid, SIGUSR1);
76+
printf("Updated target to %d (PID %d)\n", target, daemon_pid);
77+
}
78+
return 0;
79+
}
80+
perror("flock");
81+
return 1;
82+
}
83+
if (target < 0) return 1;
84+
85+
// --- Deamon ----
86+
printf("Daemon started (PID %d)\n", getpid());
87+
88+
// PID to Lock
89+
char pid_str[32];
90+
snprintf(pid_str, sizeof(pid_str), "%d", getpid());
91+
ftruncate(fd_lock, 0);
92+
write(fd_lock, pid_str, strlen(pid_str));
93+
94+
// SIGNAL
95+
signal(SIGTERM, handle_sigterm);
96+
signal(SIGUSR1, handle_sigusr1);
97+
98+
// Set DMA
99+
if (set_latency(target) == -1) {
100+
flock(fd_lock, LOCK_UN);
101+
close(fd_lock);
102+
return 1;
103+
}
104+
105+
// Main Loop
106+
while (running) {
107+
if (need_update) {
108+
int fd = open(DATA_FILE, O_RDONLY);
109+
if (fd != -1) {
110+
int32_t new_target;
111+
read(fd, &new_target, sizeof(new_target));
112+
close(fd);
113+
set_latency(new_target);
114+
printf("Updated latency to %d\n", new_target);
115+
}
116+
need_update = 0;
117+
}
118+
pause();
119+
}
120+
121+
// Clean
122+
flock(fd_lock, LOCK_UN);
123+
close(fd_lock);
124+
printf("Daemon stopped\n");
125+
return 0;
126+
}

0 commit comments

Comments
 (0)