This repository was archived by the owner on Apr 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdrop_cache.c
More file actions
162 lines (150 loc) · 4.52 KB
/
drop_cache.c
File metadata and controls
162 lines (150 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright 2020 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "drop_cache.h"
#include "io.h"
#include "log.h"
#include "util.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
struct drop_cache_thread {
pthread_t pthread;
pthread_mutex_t lock;
pthread_cond_t cond;
char *path;
int should_run;
int period;
};
int drop_cache(const char *path)
{
int ret, fd;
char buf[] = { '1' };
fd = open(path, O_WRONLY | O_CREAT, 0666);
if (fd < 0) {
return -errno;
}
ret = safe_write(fd, buf, sizeof(buf));
close(fd);
return ret;
}
static void *drop_cache_thread_run(void *arg)
{
struct drop_cache_thread *thread = (struct drop_cache_thread *)arg;
struct timespec deadline;
int ret;
INFO("drop_cache_thread: starting with period %d.\n", thread->period);
while (1) {
pthread_mutex_lock(&thread->lock);
if (!thread->should_run) {
pthread_mutex_unlock(&thread->lock);
break;
}
ret = clock_gettime(CLOCK_MONOTONIC, &deadline);
if (ret) {
abort();
}
deadline.tv_sec += thread->period;
ret = pthread_cond_timedwait(&thread->cond, &thread->lock, &deadline);
INFO("ret = %d\n", ret);
pthread_mutex_unlock(&thread->lock);
ret = drop_cache(thread->path);
if (ret) {
INFO("drop_cache_thread: failed to drop cache: %s (%d)\n",
safe_strerror(ret), ret);
} else {
DEBUG("drop_cache_thread: dropped cache.\n");
}
}
INFO("drop_cache_thread: exiting.\n");
return NULL;
}
struct drop_cache_thread *drop_cache_thread_start(const char *path, int period)
{
struct drop_cache_thread *thread = NULL;
pthread_condattr_t attr;
int ret;
thread = calloc(sizeof(struct drop_cache_thread), 1);
if (!thread) {
INFO("drop_cache_thread_start: OOM\n");
goto error;
}
thread->should_run = 1;
thread->period = period;
thread->path = strdup(path);
if (!thread->path) {
INFO("drop_cache_thread_start: OOM\n");
goto error;
}
ret = pthread_mutex_init(&thread->lock, NULL);
if (ret) {
INFO("drop_cache_thread_start: failed to create lock: %s (%d)\n",
safe_strerror(ret), ret);
goto error_free_path;
}
ret = pthread_condattr_init(&attr);
if (ret) {
INFO("drop_cache_thread_start: failed to create condattr: %s (%d)\n",
safe_strerror(ret), ret);
goto error_mutex_destroy;
}
ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
if (ret) {
INFO("drop_cache_thread_start: failed to set cond clock: %s (%d)\n",
safe_strerror(ret), ret);
goto error_condattr_destroy;
}
ret = pthread_cond_init(&thread->cond, &attr);
if (ret) {
INFO("drop_cache_thread_start: failed to create cond: %s (%d)\n",
safe_strerror(ret), ret);
goto error_condattr_destroy;
}
ret = pthread_create(&thread->pthread, NULL, drop_cache_thread_run, thread);
if (ret) {
INFO("drop_cache_thread_start: failed to create thread: %s (%d)\n",
safe_strerror(ret), ret);
goto error_cond_destroy;
}
pthread_condattr_destroy(&attr);
return thread;
error_cond_destroy:
pthread_cond_destroy(&thread->cond);
error_condattr_destroy:
pthread_condattr_destroy(&attr);
error_mutex_destroy:
pthread_mutex_destroy(&thread->lock);
error_free_path:
free(thread->path);
error:
free(thread);
return NULL;
}
void drop_cache_thread_join(struct drop_cache_thread *thread)
{
pthread_mutex_lock(&thread->lock);
thread->should_run = 0;
pthread_cond_signal(&thread->cond);
pthread_mutex_unlock(&thread->lock);
pthread_join(thread->pthread, NULL);
pthread_cond_destroy(&thread->cond);
pthread_mutex_destroy(&thread->lock);
free(thread->path);
free(thread);
}
// vim: ts=4:sw=4:tw=99:et